Refactor editor rendering contracts

This commit is contained in:
2026-04-28 02:57:49 +08:00
parent 3bc0cfcf08
commit b1ae6c462d
47 changed files with 798 additions and 377 deletions

View File

@@ -1,7 +1,5 @@
#include "Scene/SceneViewportToolOverlay.h"
#include "UiTextureHost.h"
#include <XCEditor/Widgets/UIEditorTextLayout.h>
#include <algorithm>
@@ -39,16 +37,16 @@ constexpr float kToggleMinWidth = 60.0f;
struct ToolButtonSpec {
SceneToolMode mode = SceneToolMode::View;
const char* label = "";
const char* inactiveFile = "";
const char* activeFile = "";
BuiltInIconKind inactiveIcon = BuiltInIconKind::ViewMoveTool;
BuiltInIconKind activeIcon = BuiltInIconKind::ViewMoveToolActive;
};
constexpr std::array<ToolButtonSpec, 5> kToolButtonSpecs = {{
{ SceneToolMode::View, "View", "view_move_tool.png", "view_move_tool_on.png" },
{ SceneToolMode::Translate, "Move", "move_tool.png", "move_tool_on.png" },
{ SceneToolMode::Rotate, "Rotate", "rotate_tool.png", "rotate_tool_on.png" },
{ SceneToolMode::Scale, "Scale", "scale_tool.png", "scale_tool_on.png" },
{ SceneToolMode::Transform, "Transform", "transform_tool.png", "transform_tool_on.png" }
{ SceneToolMode::View, "View", BuiltInIconKind::ViewMoveTool, BuiltInIconKind::ViewMoveToolActive },
{ SceneToolMode::Translate, "Move", BuiltInIconKind::MoveTool, BuiltInIconKind::MoveToolActive },
{ SceneToolMode::Rotate, "Rotate", BuiltInIconKind::RotateTool, BuiltInIconKind::RotateToolActive },
{ SceneToolMode::Scale, "Scale", BuiltInIconKind::ScaleTool, BuiltInIconKind::ScaleToolActive },
{ SceneToolMode::Transform, "Transform", BuiltInIconKind::TransformTool, BuiltInIconKind::TransformToolActive }
}};
bool ContainsPoint(const UIRect& rect, const UIPoint& point) {
@@ -198,47 +196,10 @@ UIRect BuildToggleButtonRect(
return kToggleIdle;
}
} // namespace
bool SceneViewportToolOverlay::Initialize(
const std::filesystem::path& repoRoot,
Rendering::Host::UiTextureHost& renderer) {
Shutdown(renderer);
const std::filesystem::path iconRoot =
(repoRoot / "editor" / "resources" / "Icons").lexically_normal();
bool loadedAnyTexture = false;
for (std::size_t index = 0; index < kToolButtonSpecs.size(); ++index) {
const ToolButtonSpec& path = kToolButtonSpecs[index];
ToolTextureSet& textureSet = m_toolTextures[index];
textureSet = {};
textureSet.mode = path.mode;
textureSet.label = path.label;
std::string error = {};
loadedAnyTexture =
renderer.LoadTextureFromFile(
iconRoot / path.inactiveFile,
textureSet.inactiveTexture,
error) || loadedAnyTexture;
error.clear();
loadedAnyTexture =
renderer.LoadTextureFromFile(
iconRoot / path.activeFile,
textureSet.activeTexture,
error) || loadedAnyTexture;
}
return loadedAnyTexture;
}
void SceneViewportToolOverlay::Shutdown(Rendering::Host::UiTextureHost& renderer) {
for (ToolTextureSet& textureSet : m_toolTextures) {
renderer.ReleaseTexture(textureSet.inactiveTexture);
renderer.ReleaseTexture(textureSet.activeTexture);
textureSet = {};
}
void SceneViewportToolOverlay::SetIconService(const EditorIconService* iconService) {
m_iconService = iconService;
ResetFrame();
}
@@ -305,7 +266,6 @@ void SceneViewportToolOverlay::BuildFrame(
for (std::size_t index = 0; index < m_frame.buttons.size(); ++index) {
const ToolButtonSpec& spec = kToolButtonSpecs[index];
const ToolTextureSet& textureSet = m_toolTextures[index];
SceneViewportToolOverlayButtonFrame& button = m_frame.buttons[index];
button = {};
button.mode = spec.mode;
@@ -314,9 +274,10 @@ void SceneViewportToolOverlay::BuildFrame(
button.active = spec.mode == activeMode;
button.hovered = index == hoveredIndex;
button.pressed = index == pressedIndex;
button.texture = spec.mode == activeMode
? textureSet.activeTexture
: textureSet.inactiveTexture;
if (m_iconService != nullptr) {
button.texture = m_iconService->Resolve(
spec.mode == activeMode ? spec.activeIcon : spec.inactiveIcon);
}
}
}