ui: add typed editor field foundations

This commit is contained in:
2026-04-08 02:52:28 +08:00
parent 805e07bf90
commit 0a392e1311
69 changed files with 11676 additions and 1169 deletions

View File

@@ -25,6 +25,15 @@ endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/number_field_basic/CMakeLists.txt")
add_subdirectory(number_field_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/text_field_basic/CMakeLists.txt")
add_subdirectory(text_field_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vector2_field_basic/CMakeLists.txt")
add_subdirectory(vector2_field_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vector3_field_basic/CMakeLists.txt")
add_subdirectory(vector3_field_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/enum_field_basic/CMakeLists.txt")
add_subdirectory(enum_field_basic)
endif()

View File

@@ -3,6 +3,7 @@ add_executable(editor_ui_bool_field_basic_validation WIN32
)
target_include_directories(editor_ui_bool_field_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/tests/UI/Editor/integration/shared/src
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include

View File

@@ -3,7 +3,9 @@
#endif
#include <XCEditor/Core/UIEditorBoolFieldInteraction.h>
#include <XCEditor/Core/UIEditorTheme.h>
#include <XCEditor/Widgets/UIEditorBoolField.h>
#include "EditorValidationTheme.h"
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
@@ -45,19 +47,11 @@ using XCEngine::UI::Editor::Widgets::HitTestUIEditorBoolField;
using XCEngine::UI::Editor::Widgets::UIEditorBoolFieldHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorBoolFieldHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorBoolFieldSpec;
namespace Style = XCEngine::UI::Style;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorBoolFieldBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | BoolField Basic";
constexpr UIColor kWindowBg(0.13f, 0.13f, 0.13f, 1.0f);
constexpr UIColor kCardBg(0.18f, 0.18f, 0.18f, 1.0f);
constexpr UIColor kCardBorder(0.29f, 0.29f, 0.29f, 1.0f);
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
constexpr UIColor kTextMuted(0.72f, 0.72f, 0.72f, 1.0f);
constexpr UIColor kTextWeak(0.56f, 0.56f, 0.56f, 1.0f);
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
constexpr UIColor kButtonHoverBg(0.32f, 0.32f, 0.32f, 1.0f);
enum class ActionId : unsigned char {
Reset = 0,
Capture
@@ -83,10 +77,14 @@ std::filesystem::path ResolveRepoRootPath() {
if (root.size() >= 2u && root.front() == '"' && root.back() == '"') {
root = root.substr(1u, root.size() - 2u);
}
return std::filesystem::path(root).lexically_normal();
}
std::filesystem::path ResolveValidationThemePath() {
return (ResolveRepoRootPath() / "tests/UI/Editor/integration/shared/themes/editor_validation.xctheme")
.lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
@@ -105,10 +103,13 @@ std::int32_t MapBoolFieldKey(UINT keyCode) {
}
}
ScenarioLayout BuildScenarioLayout(float width, float height) {
constexpr float margin = 20.0f;
ScenarioLayout BuildScenarioLayout(
float width,
float height,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics) {
const float margin = shellMetrics.margin;
constexpr float leftWidth = 430.0f;
constexpr float gap = 16.0f;
const float gap = shellMetrics.gap;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 214.0f);
@@ -141,33 +142,48 @@ ScenarioLayout BuildScenarioLayout(float width, float height) {
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, kCardBg, 10.0f);
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 10.0f);
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 14.0f), std::string(title), kTextPrimary, 17.0f);
drawList.AddFilledRect(rect, shellPalette.cardBackground, shellMetrics.cardRadius);
drawList.AddRectOutline(rect, shellPalette.cardBorder, 1.0f, shellMetrics.cardRadius);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 14.0f),
std::string(title),
shellPalette.textPrimary,
shellMetrics.titleFontSize);
if (!subtitle.empty()) {
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 40.0f), std::string(subtitle), kTextMuted, 12.0f);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 40.0f),
std::string(subtitle),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
bool hovered) {
drawList.AddFilledRect(button.rect, hovered ? kButtonHoverBg : kButtonBg, 8.0f);
drawList.AddRectOutline(button.rect, kCardBorder, 1.0f, 8.0f);
drawList.AddFilledRect(
button.rect,
hovered ? shellPalette.buttonHoverBackground : shellPalette.buttonBackground,
shellMetrics.buttonRadius);
drawList.AddRectOutline(button.rect, shellPalette.cardBorder, 1.0f, shellMetrics.buttonRadius);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
}
std::string DescribeHitTarget(const UIEditorBoolFieldHitTarget& hitTarget) {
switch (hitTarget.kind) {
case UIEditorBoolFieldHitTargetKind::Toggle:
return "toggle";
case UIEditorBoolFieldHitTargetKind::Checkbox:
return "checkbox";
case UIEditorBoolFieldHitTargetKind::Row:
return "row";
case UIEditorBoolFieldHitTargetKind::None:
@@ -355,6 +371,14 @@ private:
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/bool_field_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
const auto themeLoad =
XCEngine::Tests::EditorUI::LoadEditorValidationTheme(ResolveValidationThemePath());
if (themeLoad.succeeded) {
m_theme = themeLoad.theme;
m_themeStatus = "loaded";
} else {
m_themeStatus = themeLoad.error.empty() ? "fallback" : themeLoad.error;
}
ResetScenario();
return true;
@@ -380,7 +404,10 @@ private:
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(width, height);
return BuildScenarioLayout(
width,
height,
XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme));
}
void ResetScenario() {
@@ -403,12 +430,14 @@ private:
}
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorBoolFieldMetrics(m_theme);
m_frame = UpdateUIEditorBoolFieldInteraction(
m_interactionState,
m_value,
layout.fieldRect,
m_spec,
{});
{},
metrics);
}
void OnResize(UINT width, UINT height) {
@@ -502,18 +531,20 @@ private:
UIEditorBoolFieldInteractionResult PumpEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorBoolFieldMetrics(m_theme);
m_frame = UpdateUIEditorBoolFieldInteraction(
m_interactionState,
m_value,
layout.fieldRect,
m_spec,
std::move(events));
std::move(events),
metrics);
return m_frame.result;
}
void UpdateResultText(const UIEditorBoolFieldInteractionResult& result) {
if (result.valueChanged) {
m_lastResult = std::string("值已切换到 ") + (m_value ? "true" : "false");
m_lastResult = std::string("值已切换到: ") + (m_value ? "true" : "false");
return;
}
@@ -547,76 +578,90 @@ private:
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const ScenarioLayout layout = BuildScenarioLayout(width, height);
const auto shellMetrics = XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme);
const auto shellPalette = XCEngine::Tests::EditorUI::ResolveEditorValidationShellPalette(m_theme);
const ScenarioLayout layout = BuildScenarioLayout(width, height, shellMetrics);
RefreshFrame();
const UIEditorBoolFieldHitTarget currentHit =
HitTestUIEditorBoolField(m_frame.layout, m_mousePosition);
const auto boolMetrics = XCEngine::UI::Editor::ResolveUIEditorBoolFieldMetrics(m_theme);
const auto boolPalette = XCEngine::UI::Editor::ResolveUIEditorBoolFieldPalette(m_theme);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorBoolFieldBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), shellPalette.windowBackground);
DrawCard(
drawList,
layout.introRect,
shellPalette,
shellMetrics,
"这个测试在验证什么功能",
"只验证 Editor BoolField 的基础交互契约,不涉及 PropertyGrid 或任何业务 Inspector。");
"只验证 Editor BoolField 的基础交互契约,不涉及 PropertyGrid 或业务 Inspector。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 row 或 toggle,检查 true / false 是否稳定切换。",
kTextPrimary,
12.0f);
"1. 点击 row 或 checkbox,检查 true / false 是否稳定切换。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 控件获得 focus 后按 Space / Enter也必须能切换值。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 检查 Hover / Focus / Value / Result 是否同步更新。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 按 F12 或点击截图按钮,确认自动截图路径正确。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.controlRect, "操作");
DrawCard(drawList, layout.controlRect, shellPalette, shellMetrics, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
shellPalette,
shellMetrics,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(drawList, layout.stateRect, "状态摘要", "重点检查 hit / focus / value / result。");
DrawCard(
drawList,
layout.stateRect,
shellPalette,
shellMetrics,
"状态摘要",
"重点检查 hit / focus / value / result。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.fieldState.focused ? "" : ""),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
std::string("Active: ") + (m_interactionState.fieldState.active ? "" : ""),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
std::string("Value: ") + (m_value ? "true" : "false"),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Result: " + m_lastResult,
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
@@ -627,17 +672,30 @@ private:
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
captureSummary,
kTextWeak,
12.0f);
shellPalette.textWeak,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
"Theme: " + m_themeStatus,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.previewRect, "BoolField 预览", "这里只放一个 BoolField。");
DrawCard(
drawList,
layout.previewRect,
shellPalette,
shellMetrics,
"BoolField 预览",
"这里只放一个 Unity 风格 BoolField。");
UIEditorBoolFieldSpec previewSpec = m_spec;
previewSpec.value = m_value;
AppendUIEditorBoolField(
drawList,
layout.fieldRect,
previewSpec,
m_interactionState.fieldState);
m_interactionState.fieldState,
boolPalette,
boolMetrics);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
@@ -657,10 +715,12 @@ private:
bool m_value = false;
UIEditorBoolFieldInteractionState m_interactionState = {};
UIEditorBoolFieldInteractionFrame m_frame = {};
Style::UITheme m_theme = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
std::string m_themeStatus = "fallback";
};
} // namespace

View File

@@ -3,6 +3,7 @@ add_executable(editor_ui_enum_field_basic_validation WIN32
)
target_include_directories(editor_ui_enum_field_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/tests/UI/Editor/integration/shared/src
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include

View File

@@ -3,7 +3,10 @@
#endif
#include <XCEditor/Core/UIEditorEnumFieldInteraction.h>
#include <XCEditor/Core/UIEditorTheme.h>
#include <XCEditor/Widgets/UIEditorEnumField.h>
#include <XCEditor/Widgets/UIEditorMenuPopup.h>
#include "EditorValidationTheme.h"
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
@@ -41,24 +44,18 @@ using XCEngine::UI::Editor::UIEditorEnumFieldInteractionResult;
using XCEngine::UI::Editor::UIEditorEnumFieldInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorEnumFieldInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorEnumField;
using XCEngine::UI::Editor::Widgets::AppendUIEditorMenuPopup;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorEnumField;
using XCEngine::UI::Editor::Widgets::ResolveUIEditorEnumFieldValueText;
using XCEngine::UI::Editor::Widgets::UIEditorEnumFieldHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorEnumFieldHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorEnumFieldSpec;
using XCEngine::UI::Editor::Widgets::UIEditorMenuPopupInvalidIndex;
namespace Style = XCEngine::UI::Style;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorEnumFieldBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | EnumField Basic";
constexpr UIColor kWindowBg(0.13f, 0.13f, 0.13f, 1.0f);
constexpr UIColor kCardBg(0.18f, 0.18f, 0.18f, 1.0f);
constexpr UIColor kCardBorder(0.29f, 0.29f, 0.29f, 1.0f);
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
constexpr UIColor kTextMuted(0.72f, 0.72f, 0.72f, 1.0f);
constexpr UIColor kTextWeak(0.56f, 0.56f, 0.56f, 1.0f);
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
constexpr UIColor kButtonHoverBg(0.32f, 0.32f, 0.32f, 1.0f);
enum class ActionId : unsigned char {
Reset = 0,
Capture
@@ -87,6 +84,11 @@ std::filesystem::path ResolveRepoRootPath() {
return std::filesystem::path(root).lexically_normal();
}
std::filesystem::path ResolveValidationThemePath() {
return (ResolveRepoRootPath() / "tests/UI/Editor/integration/shared/themes/editor_validation.xctheme")
.lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
@@ -96,34 +98,41 @@ bool ContainsPoint(const UIRect& rect, float x, float y) {
std::int32_t MapEnumFieldKey(UINT keyCode) {
switch (keyCode) {
case VK_LEFT:
return static_cast<std::int32_t>(KeyCode::Left);
case VK_RIGHT:
return static_cast<std::int32_t>(KeyCode::Right);
case VK_UP:
return static_cast<std::int32_t>(KeyCode::Up);
case VK_DOWN:
return static_cast<std::int32_t>(KeyCode::Down);
case VK_HOME:
return static_cast<std::int32_t>(KeyCode::Home);
case VK_END:
return static_cast<std::int32_t>(KeyCode::End);
case VK_RETURN:
return static_cast<std::int32_t>(KeyCode::Enter);
case VK_SPACE:
return static_cast<std::int32_t>(KeyCode::Space);
case VK_ESCAPE:
return static_cast<std::int32_t>(KeyCode::Escape);
default:
return static_cast<std::int32_t>(KeyCode::None);
}
}
ScenarioLayout BuildScenarioLayout(float width, float height) {
constexpr float margin = 20.0f;
constexpr float leftWidth = 430.0f;
constexpr float gap = 16.0f;
ScenarioLayout BuildScenarioLayout(
float width,
float height,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics) {
const float margin = shellMetrics.margin;
constexpr float leftWidth = 440.0f;
const float gap = shellMetrics.gap;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 220.0f);
layout.introRect = UIRect(margin, margin, leftWidth, 240.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 84.0f);
layout.stateRect = UIRect(
margin,
layout.controlRect.y + layout.controlRect.height + gap,
leftWidth,
(std::max)(220.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
(std::max)(250.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
layout.previewRect = UIRect(
leftWidth + margin * 2.0f,
margin,
@@ -132,7 +141,7 @@ ScenarioLayout BuildScenarioLayout(float width, float height) {
layout.fieldRect = UIRect(
layout.previewRect.x + 24.0f,
layout.previewRect.y + 82.0f,
320.0f,
340.0f,
32.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
@@ -147,31 +156,48 @@ ScenarioLayout BuildScenarioLayout(float width, float height) {
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, kCardBg, 10.0f);
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 10.0f);
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 14.0f), std::string(title), kTextPrimary, 17.0f);
drawList.AddFilledRect(rect, shellPalette.cardBackground, shellMetrics.cardRadius);
drawList.AddRectOutline(rect, shellPalette.cardBorder, 1.0f, shellMetrics.cardRadius);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 14.0f),
std::string(title),
shellPalette.textPrimary,
shellMetrics.titleFontSize);
if (!subtitle.empty()) {
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 40.0f), std::string(subtitle), kTextMuted, 12.0f);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 40.0f),
std::string(subtitle),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
bool hovered) {
drawList.AddFilledRect(button.rect, hovered ? kButtonHoverBg : kButtonBg, 8.0f);
drawList.AddRectOutline(button.rect, kCardBorder, 1.0f, 8.0f);
drawList.AddText(UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f), button.label, kTextPrimary, 12.0f);
drawList.AddFilledRect(
button.rect,
hovered ? shellPalette.buttonHoverBackground : shellPalette.buttonBackground,
shellMetrics.buttonRadius);
drawList.AddRectOutline(button.rect, shellPalette.cardBorder, 1.0f, shellMetrics.buttonRadius);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
}
std::string DescribeHitTarget(const UIEditorEnumFieldHitTarget& hitTarget) {
switch (hitTarget.kind) {
case UIEditorEnumFieldHitTargetKind::PreviousButton:
return "previous";
case UIEditorEnumFieldHitTargetKind::NextButton:
return "next";
case UIEditorEnumFieldHitTargetKind::DropdownArrow:
return "dropdown_arrow";
case UIEditorEnumFieldHitTargetKind::ValueBox:
return "value_box";
case UIEditorEnumFieldHitTargetKind::Row:
@@ -361,6 +387,14 @@ private:
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/enum_field_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
const auto themeLoad =
XCEngine::Tests::EditorUI::LoadEditorValidationTheme(ResolveValidationThemePath());
if (themeLoad.succeeded) {
m_theme = themeLoad.theme;
m_themeStatus = "loaded";
} else {
m_themeStatus = themeLoad.error.empty() ? "fallback" : themeLoad.error;
}
ResetScenario();
return true;
@@ -386,7 +420,20 @@ private:
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(width, height);
return BuildScenarioLayout(
width,
height,
XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme));
}
UIRect GetViewportRect() const {
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
return UIRect(
0.0f,
0.0f,
static_cast<float>((std::max)(1L, clientRect.right - clientRect.left)),
static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top)));
}
void ResetScenario() {
@@ -410,14 +457,19 @@ private:
return;
}
m_spec.selectedIndex = m_selectedIndex;
const ScenarioLayout layout = GetLayout();
const auto fieldMetrics = XCEngine::UI::Editor::ResolveUIEditorEnumFieldMetrics(m_theme);
const auto popupMetrics = XCEngine::UI::Editor::ResolveUIEditorMenuPopupMetrics(m_theme);
m_spec.selectedIndex = m_selectedIndex;
m_frame = UpdateUIEditorEnumFieldInteraction(
m_interactionState,
m_selectedIndex,
layout.fieldRect,
m_spec,
{});
{},
fieldMetrics,
popupMetrics,
GetViewportRect());
m_spec.selectedIndex = m_selectedIndex;
}
@@ -511,13 +563,18 @@ private:
UIEditorEnumFieldInteractionResult PumpEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
const auto fieldMetrics = XCEngine::UI::Editor::ResolveUIEditorEnumFieldMetrics(m_theme);
const auto popupMetrics = XCEngine::UI::Editor::ResolveUIEditorMenuPopupMetrics(m_theme);
m_spec.selectedIndex = m_selectedIndex;
m_frame = UpdateUIEditorEnumFieldInteraction(
m_interactionState,
m_selectedIndex,
layout.fieldRect,
m_spec,
std::move(events));
std::move(events),
fieldMetrics,
popupMetrics,
GetViewportRect());
m_spec.selectedIndex = m_selectedIndex;
return m_frame.result;
}
@@ -527,6 +584,14 @@ private:
m_lastResult = std::string("已切换到: ") + m_spec.options[m_selectedIndex];
return;
}
if (result.popupOpened) {
m_lastResult = "下拉菜单已展开";
return;
}
if (result.popupClosed) {
m_lastResult = "下拉菜单已关闭";
return;
}
if (result.consumed) {
m_lastResult = "控件已消费输入";
return;
@@ -534,6 +599,16 @@ private:
m_lastResult = "等待交互";
}
std::string ResolveHighlightedText() const {
if (!m_frame.popupOpen ||
m_frame.popupState.hoveredIndex == UIEditorMenuPopupInvalidIndex ||
m_frame.popupState.hoveredIndex >= m_spec.options.size()) {
return "(none)";
}
return m_spec.options[m_frame.popupState.hoveredIndex];
}
void ExecuteAction(ActionId action) {
switch (action) {
case ActionId::Reset:
@@ -551,77 +626,103 @@ private:
return;
}
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const ScenarioLayout layout = BuildScenarioLayout(width, height);
const UIRect viewportRect = GetViewportRect();
const auto shellMetrics = XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme);
const auto shellPalette = XCEngine::Tests::EditorUI::ResolveEditorValidationShellPalette(m_theme);
const ScenarioLayout layout = BuildScenarioLayout(viewportRect.width, viewportRect.height, shellMetrics);
RefreshFrame();
const UIEditorEnumFieldHitTarget currentHit =
HitTestUIEditorEnumField(m_frame.layout, m_mousePosition);
const auto enumMetrics = XCEngine::UI::Editor::ResolveUIEditorEnumFieldMetrics(m_theme);
const auto enumPalette = XCEngine::UI::Editor::ResolveUIEditorEnumFieldPalette(m_theme);
const auto popupMetrics = XCEngine::UI::Editor::ResolveUIEditorMenuPopupMetrics(m_theme);
const auto popupPalette = XCEngine::UI::Editor::ResolveUIEditorMenuPopupPalette(m_theme);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorEnumFieldBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
drawList.AddFilledRect(viewportRect, shellPalette.windowBackground);
DrawCard(
drawList,
layout.introRect,
shellPalette,
shellMetrics,
"这个测试在验证什么功能",
"只验证 Editor EnumField 基础控件,不涉及 PropertyGrid 或业务 Inspector。");
"只验证 Editor EnumField 基础交互契约,不涉及 PropertyGrid 或业务 Inspector。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 < / > 按钮,检查枚举选项是否稳定切换",
kTextPrimary,
12.0f);
"1. 点击 value box 或 dropdown arrow检查下拉菜单是否展开/收起",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 控件获得 focus 后按 Left / Right / Home / End检查键盘切换",
kTextPrimary,
12.0f);
"2. 展开后 hover 列表项,检查高亮是否稳定跟随",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 检查 Hover / Focus / Selected / Result 是否同步更新",
kTextPrimary,
12.0f);
"3. 获得 focus 后按 Up / Down / Home / End再按 Enter / Space 选中Esc 关闭",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 这个场景只覆盖基础交互契约,不提前承载任何业务面板",
kTextPrimary,
12.0f);
"4. 检查 Hover / Popup / Highlight / Selected / Result 是否同步更新",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 按 F12 或点击截图按钮,确认自动截图路径正确。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.controlRect, "操作");
DrawCard(drawList, layout.controlRect, shellPalette, shellMetrics, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(drawList, button, m_hasHoveredAction && m_hoveredAction == button.action);
DrawButton(
drawList,
button,
shellPalette,
shellMetrics,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(drawList, layout.stateRect, "状态摘要");
DrawCard(
drawList,
layout.stateRect,
shellPalette,
shellMetrics,
"状态摘要",
"重点检查 hit / popup / highlight / selected / result。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.fieldState.focused ? "" : ""),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
std::string("Active: ") + (m_interactionState.fieldState.active ? "" : ""),
kTextPrimary,
12.0f);
std::string("Popup: ") + (m_frame.popupOpen ? "open" : "closed"),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
"Selected: " + ResolveUIEditorEnumFieldValueText(m_spec),
kTextPrimary,
12.0f);
"Highlight: " + ResolveHighlightedText(),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Selected: " + ResolveUIEditorEnumFieldValueText(m_spec),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Result: " + m_lastResult,
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
@@ -630,24 +731,46 @@ private:
? std::string("F12 -> tests/UI/Editor/integration/shell/enum_field_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
captureSummary,
kTextWeak,
12.0f);
shellPalette.textWeak,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
"Theme: " + m_themeStatus,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.previewRect, "EnumField 预览", "这里只放一个 EnumField。");
DrawCard(
drawList,
layout.previewRect,
shellPalette,
shellMetrics,
"EnumField 预览",
"这里只放一个 Unity 风格 EnumField。");
AppendUIEditorEnumField(
drawList,
layout.fieldRect,
m_spec,
m_interactionState.fieldState);
m_interactionState.fieldState,
enumPalette,
enumMetrics);
if (m_frame.popupOpen) {
AppendUIEditorMenuPopup(
drawList,
m_frame.popupLayout.popupRect,
m_frame.popupItems,
m_frame.popupState,
popupPalette,
popupMetrics);
}
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
m_renderer,
drawData,
static_cast<unsigned int>(width),
static_cast<unsigned int>(height),
static_cast<unsigned int>(viewportRect.width),
static_cast<unsigned int>(viewportRect.height),
framePresented);
}
@@ -660,10 +783,12 @@ private:
std::size_t m_selectedIndex = 0u;
UIEditorEnumFieldInteractionState m_interactionState = {};
UIEditorEnumFieldInteractionFrame m_frame = {};
Style::UITheme m_theme = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
std::string m_themeStatus = "fallback";
};
} // namespace

View File

@@ -3,6 +3,7 @@ add_executable(editor_ui_number_field_basic_validation WIN32
)
target_include_directories(editor_ui_number_field_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/tests/UI/Editor/integration/shared/src
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include

View File

@@ -3,7 +3,9 @@
#endif
#include <XCEditor/Core/UIEditorNumberFieldInteraction.h>
#include <XCEditor/Core/UIEditorTheme.h>
#include <XCEditor/Widgets/UIEditorNumberField.h>
#include "EditorValidationTheme.h"
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
@@ -46,19 +48,11 @@ using XCEngine::UI::Editor::Widgets::HitTestUIEditorNumberField;
using XCEngine::UI::Editor::Widgets::UIEditorNumberFieldHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorNumberFieldHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorNumberFieldSpec;
namespace Style = XCEngine::UI::Style;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorNumberFieldBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | NumberField Basic";
constexpr UIColor kWindowBg(0.13f, 0.13f, 0.13f, 1.0f);
constexpr UIColor kCardBg(0.18f, 0.18f, 0.18f, 1.0f);
constexpr UIColor kCardBorder(0.29f, 0.29f, 0.29f, 1.0f);
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
constexpr UIColor kTextMuted(0.72f, 0.72f, 0.72f, 1.0f);
constexpr UIColor kTextWeak(0.56f, 0.56f, 0.56f, 1.0f);
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
constexpr UIColor kButtonHoverBg(0.32f, 0.32f, 0.32f, 1.0f);
enum class ActionId : unsigned char {
Reset = 0,
Capture
@@ -75,6 +69,9 @@ struct ScenarioLayout {
UIRect controlRect = {};
UIRect stateRect = {};
UIRect previewRect = {};
UIRect inspectorRect = {};
UIRect inspectorHeaderRect = {};
UIRect sectionRect = {};
UIRect fieldRect = {};
std::vector<ButtonLayout> buttons = {};
};
@@ -87,6 +84,11 @@ std::filesystem::path ResolveRepoRootPath() {
return std::filesystem::path(root).lexically_normal();
}
std::filesystem::path ResolveValidationThemePath() {
return (ResolveRepoRootPath() / "tests/UI/Editor/integration/shared/themes/editor_validation.xctheme")
.lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
@@ -117,13 +119,16 @@ std::int32_t MapNumberFieldKey(UINT keyCode) {
}
}
ScenarioLayout BuildScenarioLayout(float width, float height) {
constexpr float margin = 20.0f;
constexpr float leftWidth = 430.0f;
constexpr float gap = 16.0f;
ScenarioLayout BuildScenarioLayout(
float width,
float height,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics) {
const float margin = shellMetrics.margin;
constexpr float leftWidth = 460.0f;
const float gap = shellMetrics.gap;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 232.0f);
layout.introRect = UIRect(margin, margin, leftWidth, 250.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 84.0f);
layout.stateRect = UIRect(
margin,
@@ -135,11 +140,26 @@ ScenarioLayout BuildScenarioLayout(float width, float height) {
margin,
(std::max)(420.0f, width - leftWidth - margin * 3.0f),
height - margin * 2.0f);
layout.inspectorRect = UIRect(
layout.previewRect.x + 18.0f,
layout.previewRect.y + 54.0f,
(std::min)(392.0f, layout.previewRect.width - 36.0f),
150.0f);
layout.inspectorHeaderRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y,
layout.inspectorRect.width,
24.0f);
layout.sectionRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y + layout.inspectorHeaderRect.height,
layout.inspectorRect.width,
24.0f);
layout.fieldRect = UIRect(
layout.previewRect.x + 24.0f,
layout.previewRect.y + 82.0f,
320.0f,
32.0f);
layout.inspectorRect.x,
layout.sectionRect.y + layout.sectionRect.height + 2.0f,
layout.inspectorRect.width,
22.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
const float buttonY = layout.controlRect.y + 32.0f;
@@ -150,34 +170,63 @@ ScenarioLayout BuildScenarioLayout(float width, float height) {
return layout;
}
XCEngine::UI::Editor::Widgets::UIEditorNumberFieldMetrics ResolveHostedNumberFieldMetrics(
const Style::UITheme& theme) {
const auto propertyMetrics = XCEngine::UI::Editor::ResolveUIEditorPropertyGridMetrics(theme);
const auto numberMetrics = XCEngine::UI::Editor::ResolveUIEditorNumberFieldMetrics(theme);
return XCEngine::UI::Editor::BuildUIEditorHostedNumberFieldMetrics(propertyMetrics, numberMetrics);
}
XCEngine::UI::Editor::Widgets::UIEditorNumberFieldPalette ResolveHostedNumberFieldPalette(
const Style::UITheme& theme) {
const auto propertyPalette = XCEngine::UI::Editor::ResolveUIEditorPropertyGridPalette(theme);
const auto numberPalette = XCEngine::UI::Editor::ResolveUIEditorNumberFieldPalette(theme);
return XCEngine::UI::Editor::BuildUIEditorHostedNumberFieldPalette(propertyPalette, numberPalette);
}
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, kCardBg, 10.0f);
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 10.0f);
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 14.0f), std::string(title), kTextPrimary, 17.0f);
drawList.AddFilledRect(rect, shellPalette.cardBackground, shellMetrics.cardRadius);
drawList.AddRectOutline(rect, shellPalette.cardBorder, 1.0f, shellMetrics.cardRadius);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 14.0f),
std::string(title),
shellPalette.textPrimary,
shellMetrics.titleFontSize);
if (!subtitle.empty()) {
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 40.0f), std::string(subtitle), kTextMuted, 12.0f);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 40.0f),
std::string(subtitle),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
bool hovered) {
drawList.AddFilledRect(button.rect, hovered ? kButtonHoverBg : kButtonBg, 8.0f);
drawList.AddRectOutline(button.rect, kCardBorder, 1.0f, 8.0f);
drawList.AddText(UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f), button.label, kTextPrimary, 12.0f);
drawList.AddFilledRect(
button.rect,
hovered ? shellPalette.buttonHoverBackground : shellPalette.buttonBackground,
shellMetrics.buttonRadius);
drawList.AddRectOutline(button.rect, shellPalette.cardBorder, 1.0f, shellMetrics.buttonRadius);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
}
std::string DescribeHitTarget(const UIEditorNumberFieldHitTarget& hitTarget) {
switch (hitTarget.kind) {
case UIEditorNumberFieldHitTargetKind::DecrementButton:
return "decrement";
case UIEditorNumberFieldHitTargetKind::IncrementButton:
return "increment";
case UIEditorNumberFieldHitTargetKind::ValueBox:
return "value_box";
case UIEditorNumberFieldHitTargetKind::Row:
@@ -381,6 +430,14 @@ private:
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/number_field_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
const auto themeLoad =
XCEngine::Tests::EditorUI::LoadEditorValidationTheme(ResolveValidationThemePath());
if (themeLoad.succeeded) {
m_theme = themeLoad.theme;
m_themeStatus = "loaded";
} else {
m_themeStatus = themeLoad.error.empty() ? "fallback" : themeLoad.error;
}
ResetScenario();
return true;
@@ -406,7 +463,10 @@ private:
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(width, height);
return BuildScenarioLayout(
width,
height,
XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme));
}
void ResetScenario() {
@@ -434,11 +494,13 @@ private:
}
const ScenarioLayout layout = GetLayout();
const auto metrics = ResolveHostedNumberFieldMetrics(m_theme);
m_frame = UpdateUIEditorNumberFieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
{});
{},
metrics);
}
void OnResize(UINT width, UINT height) {
@@ -542,17 +604,19 @@ private:
UIEditorNumberFieldInteractionResult PumpEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
const auto metrics = ResolveHostedNumberFieldMetrics(m_theme);
m_frame = UpdateUIEditorNumberFieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
std::move(events));
std::move(events),
metrics);
return m_frame.result;
}
void UpdateResultText(const UIEditorNumberFieldInteractionResult& result) {
if (result.editCommitRejected) {
m_lastResult = "提交失败,仍保在编辑态";
m_lastResult = "提交失败,仍保在编辑态";
return;
}
if (result.editCommitted) {
@@ -600,83 +664,101 @@ private:
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const ScenarioLayout layout = BuildScenarioLayout(width, height);
const auto shellMetrics = XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme);
const auto shellPalette = XCEngine::Tests::EditorUI::ResolveEditorValidationShellPalette(m_theme);
const ScenarioLayout layout = BuildScenarioLayout(width, height, shellMetrics);
RefreshFrame();
const UIEditorNumberFieldHitTarget currentHit =
HitTestUIEditorNumberField(m_frame.layout, m_mousePosition);
const auto numberMetrics = ResolveHostedNumberFieldMetrics(m_theme);
const auto numberPalette = ResolveHostedNumberFieldPalette(m_theme);
const auto propertyPalette = XCEngine::UI::Editor::ResolveUIEditorPropertyGridPalette(m_theme);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorNumberFieldBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), shellPalette.windowBackground);
DrawCard(
drawList,
layout.introRect,
shellPalette,
shellMetrics,
"这个测试在验证什么功能",
"验证 Editor NumberField 的基础交互契约,不涉及 PropertyGrid 或任何业务 Inspector");
"验证 Inspector 宿主中的 NumberField 交互契约和默认宿主风格");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 +/- 按钮,检查步进和上下界钳制是否稳定",
kTextPrimary,
12.0f);
"1. 点击 value box检查是否进入编辑态外观应是 Unity 风格单输入框",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 控件获得 focus 后按 Left / Right / Up / Down / Home / End检查键盘步进。",
kTextPrimary,
12.0f);
"2. 获得 focus 后按 Left / Right / Up / Down / Home / End检查键盘步进。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 按 Enter 进入编辑态直接输入字符Enter commitEsc cancel。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 检查 Hover / Focus / Editing / Value / Result 是否同步更新。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 按 F12 或点击截图按钮,确认自动截图路径正确。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.controlRect, "操作");
DrawCard(drawList, layout.controlRect, shellPalette, shellMetrics, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(drawList, button, m_hasHoveredAction && m_hoveredAction == button.action);
DrawButton(
drawList,
button,
shellPalette,
shellMetrics,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(drawList, layout.stateRect, "状态摘要", "重点检查 hit / focus / editing / value / result。");
DrawCard(
drawList,
layout.stateRect,
shellPalette,
shellMetrics,
"状态摘要",
"重点检查 hit / focus / editing / value / result。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.numberFieldState.focused ? "" : ""),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
std::string("Editing: ") + (m_interactionState.numberFieldState.editing ? "" : ""),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
"Value: " + FormatUIEditorNumberFieldValue(m_spec),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Display: " + m_interactionState.numberFieldState.displayText,
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Result: " + m_lastResult,
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
@@ -687,15 +769,44 @@ private:
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
captureSummary,
kTextWeak,
12.0f);
shellPalette.textWeak,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
"Theme: " + m_themeStatus,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.previewRect, "NumberField 预览", "这里只放一个 NumberField。");
DrawCard(
drawList,
layout.previewRect,
shellPalette,
shellMetrics,
"NumberField 预览",
"这里仅预览 Inspector 宿主中的 Unity 风格 Number 字段。");
drawList.AddFilledRect(layout.inspectorRect, propertyPalette.surfaceColor);
drawList.AddRectOutline(layout.inspectorRect, propertyPalette.borderColor, 1.0f);
drawList.AddFilledRect(layout.inspectorHeaderRect, shellPalette.cardBackground);
drawList.AddRectOutline(layout.inspectorHeaderRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.inspectorHeaderRect.x + 10.0f, layout.inspectorHeaderRect.y + 5.0f),
"Inspector",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddFilledRect(layout.sectionRect, propertyPalette.sectionHeaderColor);
drawList.AddRectOutline(layout.sectionRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.sectionRect.x + 10.0f, layout.sectionRect.y + 5.0f),
"v Transform",
propertyPalette.sectionTextColor,
shellMetrics.bodyFontSize);
AppendUIEditorNumberField(
drawList,
layout.fieldRect,
m_spec,
m_interactionState.numberFieldState);
m_interactionState.numberFieldState,
numberPalette,
numberMetrics);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
@@ -714,10 +825,12 @@ private:
UIEditorNumberFieldSpec m_spec = {};
UIEditorNumberFieldInteractionState m_interactionState = {};
UIEditorNumberFieldInteractionFrame m_frame = {};
Style::UITheme m_theme = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
std::string m_themeStatus = "fallback";
};
} // namespace

View File

@@ -3,6 +3,7 @@ add_executable(editor_ui_property_grid_basic_validation WIN32
)
target_include_directories(editor_ui_property_grid_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/tests/UI/Editor/integration/shared/src
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include

View File

@@ -3,7 +3,9 @@
#endif
#include <XCEditor/Core/UIEditorPropertyGridInteraction.h>
#include <XCEditor/Core/UIEditorTheme.h>
#include <XCEditor/Widgets/UIEditorPropertyGrid.h>
#include "EditorValidationTheme.h"
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
@@ -48,24 +50,17 @@ using XCEngine::UI::Editor::UpdateUIEditorPropertyGridInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorPropertyGridBackground;
using XCEngine::UI::Editor::Widgets::AppendUIEditorPropertyGridForeground;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorPropertyGrid;
using XCEngine::UI::Editor::Widgets::ResolveUIEditorPropertyGridFieldValueText;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridField;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridFieldKind;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridSection;
namespace Style = XCEngine::UI::Style;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorPropertyGridBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | PropertyGrid Basic";
constexpr UIColor kWindowBg(0.13f, 0.13f, 0.13f, 1.0f);
constexpr UIColor kCardBg(0.18f, 0.18f, 0.18f, 1.0f);
constexpr UIColor kCardBorder(0.29f, 0.29f, 0.29f, 1.0f);
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
constexpr UIColor kTextMuted(0.72f, 0.72f, 0.72f, 1.0f);
constexpr UIColor kTextWeak(0.56f, 0.56f, 0.56f, 1.0f);
constexpr UIColor kTextSuccess(0.63f, 0.76f, 0.63f, 1.0f);
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
constexpr UIColor kButtonHoverBg(0.32f, 0.32f, 0.32f, 1.0f);
enum class ActionId : unsigned char {
Reset = 0,
Capture
@@ -95,6 +90,11 @@ std::filesystem::path ResolveRepoRootPath() {
return std::filesystem::path(root).lexically_normal();
}
std::filesystem::path ResolveValidationThemePath() {
return (ResolveRepoRootPath() / "tests/UI/Editor/integration/shared/themes/editor_validation.xctheme")
.lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
@@ -129,10 +129,13 @@ std::int32_t MapEditorKey(UINT keyCode) {
}
}
ScenarioLayout BuildScenarioLayout(float width, float height) {
constexpr float margin = 20.0f;
ScenarioLayout BuildScenarioLayout(
float width,
float height,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics) {
const float margin = shellMetrics.margin;
constexpr float leftWidth = 430.0f;
constexpr float gap = 16.0f;
const float gap = shellMetrics.gap;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 214.0f);
@@ -166,48 +169,109 @@ ScenarioLayout BuildScenarioLayout(float width, float height) {
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, kCardBg, 10.0f);
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 10.0f);
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 14.0f), std::string(title), kTextPrimary, 17.0f);
drawList.AddFilledRect(rect, shellPalette.cardBackground, shellMetrics.cardRadius);
drawList.AddRectOutline(rect, shellPalette.cardBorder, 1.0f, shellMetrics.cardRadius);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 14.0f),
std::string(title),
shellPalette.textPrimary,
shellMetrics.titleFontSize);
if (!subtitle.empty()) {
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 40.0f), std::string(subtitle), kTextMuted, 12.0f);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 40.0f),
std::string(subtitle),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
bool hovered) {
drawList.AddFilledRect(button.rect, hovered ? kButtonHoverBg : kButtonBg, 8.0f);
drawList.AddRectOutline(button.rect, kCardBorder, 1.0f, 8.0f);
drawList.AddFilledRect(
button.rect,
hovered ? shellPalette.buttonHoverBackground : shellPalette.buttonBackground,
shellMetrics.buttonRadius);
drawList.AddRectOutline(button.rect, shellPalette.cardBorder, 1.0f, shellMetrics.buttonRadius);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
}
UIEditorPropertyGridField MakeTextField(
std::string id,
std::string label,
std::string value,
bool readOnly = false) {
UIEditorPropertyGridField field = {};
field.fieldId = std::move(id);
field.label = std::move(label);
field.valueText = std::move(value);
field.readOnly = readOnly;
return field;
}
UIEditorPropertyGridField MakeBoolField(
std::string id,
std::string label,
bool value) {
UIEditorPropertyGridField field = {};
field.fieldId = std::move(id);
field.label = std::move(label);
field.kind = UIEditorPropertyGridFieldKind::Bool;
field.boolValue = value;
return field;
}
UIEditorPropertyGridField MakeNumberField(
std::string id,
std::string label,
double value) {
UIEditorPropertyGridField field = {};
field.fieldId = std::move(id);
field.label = std::move(label);
field.kind = UIEditorPropertyGridFieldKind::Number;
field.numberValue.value = value;
field.numberValue.step = 1.0;
field.numberValue.minValue = 0.0;
field.numberValue.maxValue = 5000.0;
field.numberValue.integerMode = true;
return field;
}
UIEditorPropertyGridField MakeEnumField(
std::string id,
std::string label,
std::vector<std::string> options,
std::size_t selectedIndex) {
UIEditorPropertyGridField field = {};
field.fieldId = std::move(id);
field.label = std::move(label);
field.kind = UIEditorPropertyGridFieldKind::Enum;
field.enumValue.options = std::move(options);
field.enumValue.selectedIndex = selectedIndex;
return field;
}
std::vector<UIEditorPropertyGridSection> BuildSections() {
return {
{
"transform",
"Transform",
"inspector",
"Inspector",
{
{ "position", "Position", "0, 0, 0", false, 0.0f },
{ "rotation", "Rotation", "0, 45, 0", false, 0.0f },
{ "scale", "Scale", "1, 1, 1", false, 0.0f }
},
0.0f
},
{
"material",
"Material",
{
{ "shader", "Shader", "Standard/Lit", false, 0.0f },
{ "queue", "Render Queue", "2000", false, 0.0f },
{ "guid", "GUID", "asset-guid-001", true, 0.0f }
MakeBoolField("enabled", "Enabled", true),
MakeNumberField("render_queue", "Render Queue", 2000.0),
MakeEnumField("render_mode", "Render Mode", { "Opaque", "Cutout", "Fade" }, 0u),
MakeTextField("tag", "Tag", "Player")
},
0.0f
},
@@ -215,8 +279,7 @@ std::vector<UIEditorPropertyGridSection> BuildSections() {
"metadata",
"Metadata",
{
{ "tag", "Tag", "", false, 0.0f },
{ "layer", "Layer", "Default", false, 0.0f }
MakeTextField("guid", "GUID", "asset-guid-001", true)
},
0.0f
}
@@ -246,8 +309,7 @@ std::string DescribeHitTarget(
std::string BuildExpandedSummary(const UIExpansionModel& expansionModel) {
std::ostringstream stream = {};
stream << (expansionModel.IsExpanded("transform") ? "Transform" : "-");
stream << " / " << (expansionModel.IsExpanded("material") ? "Material" : "-");
stream << (expansionModel.IsExpanded("inspector") ? "Inspector" : "-");
stream << " / " << (expansionModel.IsExpanded("metadata") ? "Metadata" : "-");
return stream.str();
}
@@ -467,6 +529,14 @@ private:
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/property_grid_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
const auto themeLoad =
XCEngine::Tests::EditorUI::LoadEditorValidationTheme(ResolveValidationThemePath());
if (themeLoad.succeeded) {
m_theme = themeLoad.theme;
m_themeStatus = "loaded";
} else {
m_themeStatus = themeLoad.error.empty() ? "fallback" : themeLoad.error;
}
ResetScenario();
return true;
@@ -492,16 +562,18 @@ private:
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(width, height);
return BuildScenarioLayout(
width,
height,
XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme));
}
void ResetScenario() {
m_sections = BuildSections();
m_selectionModel = {};
m_selectionModel.SetSelection("rotation");
m_selectionModel.SetSelection("render_queue");
m_expansionModel = {};
m_expansionModel.Expand("transform");
m_expansionModel.Expand("material");
m_expansionModel.Expand("inspector");
m_expansionModel.Expand("metadata");
m_propertyEditModel = {};
m_interactionState = {};
@@ -520,6 +592,7 @@ private:
}
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorPropertyGridMetrics(m_theme);
m_gridFrame =
UpdateUIEditorPropertyGridInteraction(
m_interactionState,
@@ -528,23 +601,16 @@ private:
m_propertyEditModel,
layout.gridRect,
m_sections,
{});
{},
metrics);
}
void ApplyCommittedValue(const UIEditorPropertyGridInteractionResult& result) {
if (!result.editCommitted || result.committedFieldId.empty()) {
void UpdateLastCommit(const UIEditorPropertyGridInteractionResult& result) {
if (!result.fieldValueChanged || result.changedFieldId.empty()) {
return;
}
for (UIEditorPropertyGridSection& section : m_sections) {
for (UIEditorPropertyGridField& field : section.fields) {
if (field.fieldId == result.committedFieldId) {
field.valueText = result.committedValue;
m_lastCommit = result.committedFieldId + " = " + result.committedValue;
return;
}
}
}
m_lastCommit = result.changedFieldId + " = " + result.changedValue;
}
void OnResize(UINT width, UINT height) {
@@ -661,6 +727,7 @@ private:
UIEditorPropertyGridInteractionResult PumpGridEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorPropertyGridMetrics(m_theme);
m_gridFrame =
UpdateUIEditorPropertyGridInteraction(
m_interactionState,
@@ -669,8 +736,9 @@ private:
m_propertyEditModel,
layout.gridRect,
m_sections,
std::move(events));
ApplyCommittedValue(m_gridFrame.result);
std::move(events),
metrics);
UpdateLastCommit(m_gridFrame.result);
return m_gridFrame.result;
}
@@ -678,6 +746,26 @@ private:
const UIEditorPropertyGridInteractionResult& result,
bool wasFocused,
bool insideGrid) {
if (result.popupOpened && !m_interactionState.propertyGridState.popupFieldId.empty()) {
m_lastResult = "打开枚举下拉: " + m_interactionState.propertyGridState.popupFieldId;
return;
}
if (result.popupClosed && !result.fieldValueChanged) {
m_lastResult = "关闭枚举下拉";
return;
}
if (result.fieldValueChanged) {
m_lastResult = "字段已更新: " + result.changedFieldId + " = " + result.changedValue;
return;
}
if (result.editCommitRejected && !result.activeFieldId.empty()) {
m_lastResult = "提交被拒绝: " + result.activeFieldId;
return;
}
if (result.editCommitted) {
m_lastResult = "提交字段: " + result.committedFieldId + " = " + result.committedValue;
return;
@@ -753,99 +841,124 @@ private:
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const ScenarioLayout layout = BuildScenarioLayout(width, height);
const auto shellMetrics = XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme);
const auto shellPalette = XCEngine::Tests::EditorUI::ResolveEditorValidationShellPalette(m_theme);
const ScenarioLayout layout = BuildScenarioLayout(width, height, shellMetrics);
RefreshGridFrame();
const UIEditorPropertyGridHitTarget currentHit =
HitTestUIEditorPropertyGrid(m_gridFrame.layout, m_mousePosition);
const auto propertyMetrics = XCEngine::UI::Editor::ResolveUIEditorPropertyGridMetrics(m_theme);
const auto propertyPalette = XCEngine::UI::Editor::ResolveUIEditorPropertyGridPalette(m_theme);
const auto popupMetrics = XCEngine::UI::Editor::ResolveUIEditorMenuPopupMetrics(m_theme);
const auto popupPalette = XCEngine::UI::Editor::ResolveUIEditorMenuPopupPalette(m_theme);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorPropertyGridBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), shellPalette.windowBackground);
DrawCard(
drawList,
layout.introRect,
shellPalette,
shellMetrics,
"这个测试在验证什么功能",
"只验证 Editor PropertyGrid 基础控件,不涉及任何 Inspector 业务逻辑。");
"只验证 Editor PropertyGrid 作为 typed 属性宿主的基础契约,不涉及任何 Inspector 业务逻辑。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 section header检查展开/折叠是否稳定,字段布局不能歪。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 点击 field row 只切换 selection点击 value box 才进入编辑态",
kTextPrimary,
12.0f);
"2. 点击 value hostBool toggle、Number/Text edit、Enum popup",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 编辑态输入字符,按 Enter commit,按 Esc cancelread-only 字段不能进编辑",
kTextPrimary,
12.0f);
"3. Number / Text 编辑后按 Enter commitEsc cancelGUID 只读",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. Grid 获得 focus 后按 Up / Down / Home / End检查字段导航和 selection 同步。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 按 F12 手动截图;设置 XCUI_AUTO_CAPTURE_ON_STARTUP=1 可自动截图。",
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.controlRect, "操作");
DrawCard(drawList, layout.controlRect, shellPalette, shellMetrics, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
shellPalette,
shellMetrics,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(drawList, layout.stateRect, "状态摘要", "重点检查 hit / focus / selection / edit / commit。");
DrawCard(
drawList,
layout.stateRect,
shellPalette,
shellMetrics,
"状态摘要",
"重点检查 hit / focus / selection / edit / popup / commit。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit, m_sections),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.propertyGridState.focused ? "" : ""),
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
"Selected: " +
(m_selectionModel.HasSelection() ? m_selectionModel.GetSelectedId() : std::string("(none)")),
kTextSuccess,
12.0f);
shellPalette.textSuccess,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
"Active Edit: " +
(m_propertyEditModel.HasActiveEdit() ? m_propertyEditModel.GetActiveFieldId() : std::string("(none)")),
kTextMuted,
12.0f);
shellPalette.textMuted,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Staged: " +
(m_propertyEditModel.HasActiveEdit() ? m_propertyEditModel.GetStagedValue() : std::string("(none)")),
kTextMuted,
12.0f);
shellPalette.textMuted,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Expanded: " + BuildExpandedSummary(m_expansionModel),
kTextMuted,
12.0f);
"Popup: " +
(m_interactionState.propertyGridState.popupFieldId.empty()
? std::string("(none)")
: (m_interactionState.propertyGridState.popupFieldId + " / index " +
std::to_string(m_interactionState.propertyGridState.popupHighlightedIndex))),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
"Last Commit: " + m_lastCommit,
kTextPrimary,
12.0f);
"Expanded: " + BuildExpandedSummary(m_expansionModel),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
"Last Commit: " + m_lastCommit,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 262.0f),
"Result: " + m_lastResult,
kTextPrimary,
12.0f);
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
@@ -854,24 +967,42 @@ private:
? std::string("F12 -> tests/UI/Editor/integration/shell/property_grid_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 262.0f),
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 286.0f),
captureSummary,
kTextWeak,
12.0f);
shellPalette.textWeak,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 310.0f),
"Theme: " + m_themeStatus,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.previewRect, "PropertyGrid 预览", "这里只放一个 PropertyGrid不混入任何业务面板。");
DrawCard(
drawList,
layout.previewRect,
shellPalette,
shellMetrics,
"PropertyGrid 预览",
"这里只放一个 PropertyGrid用来验证 typed 属性宿主。");
AppendUIEditorPropertyGridBackground(
drawList,
m_gridFrame.layout,
m_sections,
m_selectionModel,
m_propertyEditModel,
m_interactionState.propertyGridState);
m_interactionState.propertyGridState,
propertyPalette,
propertyMetrics);
AppendUIEditorPropertyGridForeground(
drawList,
m_gridFrame.layout,
m_sections,
m_propertyEditModel);
m_interactionState.propertyGridState,
m_propertyEditModel,
propertyPalette,
propertyMetrics,
popupPalette,
popupMetrics);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
@@ -893,11 +1024,13 @@ private:
UIPropertyEditModel m_propertyEditModel = {};
UIEditorPropertyGridInteractionState m_interactionState = {};
UIEditorPropertyGridInteractionFrame m_gridFrame = {};
Style::UITheme m_theme = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
std::string m_lastCommit = {};
std::string m_themeStatus = "fallback";
};
} // namespace

View File

@@ -0,0 +1,31 @@
add_executable(editor_ui_text_field_basic_validation WIN32
main.cpp
)
target_include_directories(editor_ui_text_field_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/tests/UI/Editor/integration/shared/src
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include
)
target_compile_definitions(editor_ui_text_field_basic_validation PRIVATE
UNICODE
_UNICODE
XCENGINE_EDITOR_UI_TESTS_REPO_ROOT="${XCENGINE_EDITOR_UI_TESTS_REPO_ROOT_PATH}"
)
if(MSVC)
target_compile_options(editor_ui_text_field_basic_validation PRIVATE /utf-8 /FS)
set_property(TARGET editor_ui_text_field_basic_validation PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
target_link_libraries(editor_ui_text_field_basic_validation PRIVATE
XCUIEditorLib
XCUIEditorHost
)
set_target_properties(editor_ui_text_field_basic_validation PROPERTIES
OUTPUT_NAME "XCUIEditorTextFieldBasicValidation"
)

View File

@@ -0,0 +1,842 @@
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <XCEditor/Core/UIEditorTextFieldInteraction.h>
#include <XCEditor/Core/UIEditorTheme.h>
#include <XCEditor/Widgets/UIEditorTextField.h>
#include "EditorValidationTheme.h"
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
#include <XCEngine/Input/InputTypes.h>
#include <XCEngine/UI/DrawData.h>
#include <windows.h>
#include <windowsx.h>
#include <algorithm>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>
#ifndef XCENGINE_EDITOR_UI_TESTS_REPO_ROOT
#define XCENGINE_EDITOR_UI_TESTS_REPO_ROOT "."
#endif
namespace {
using XCEngine::Input::KeyCode;
using XCEngine::UI::UIDrawData;
using XCEngine::UI::UIDrawList;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Editor::Host::AutoScreenshotController;
using XCEngine::UI::Editor::Host::NativeRenderer;
using XCEngine::UI::Editor::UIEditorTextFieldInteractionFrame;
using XCEngine::UI::Editor::UIEditorTextFieldInteractionResult;
using XCEngine::UI::Editor::UIEditorTextFieldInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorTextFieldInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorTextField;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorTextField;
using XCEngine::UI::Editor::Widgets::UIEditorTextFieldHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorTextFieldHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorTextFieldSpec;
namespace Style = XCEngine::UI::Style;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorTextFieldBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | TextField Basic";
enum class ActionId : unsigned char {
Reset = 0,
Capture
};
struct ButtonLayout {
ActionId action = ActionId::Reset;
const char* label = "";
UIRect rect = {};
};
struct ScenarioLayout {
UIRect introRect = {};
UIRect controlRect = {};
UIRect stateRect = {};
UIRect previewRect = {};
UIRect inspectorRect = {};
UIRect inspectorHeaderRect = {};
UIRect sectionRect = {};
UIRect fieldRect = {};
std::vector<ButtonLayout> buttons = {};
};
std::filesystem::path ResolveRepoRootPath() {
std::string root = XCENGINE_EDITOR_UI_TESTS_REPO_ROOT;
if (root.size() >= 2u && root.front() == '"' && root.back() == '"') {
root = root.substr(1u, root.size() - 2u);
}
return std::filesystem::path(root).lexically_normal();
}
std::filesystem::path ResolveValidationThemePath() {
return (ResolveRepoRootPath() / "tests/UI/Editor/integration/shared/themes/editor_validation.xctheme")
.lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
y >= rect.y &&
y <= rect.y + rect.height;
}
std::int32_t MapTextFieldKey(UINT keyCode) {
switch (keyCode) {
case VK_RETURN:
return static_cast<std::int32_t>(KeyCode::Enter);
case VK_ESCAPE:
return static_cast<std::int32_t>(KeyCode::Escape);
default:
return static_cast<std::int32_t>(KeyCode::None);
}
}
ScenarioLayout BuildScenarioLayout(
float width,
float height,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics) {
const float margin = shellMetrics.margin;
constexpr float leftWidth = 460.0f;
const float gap = shellMetrics.gap;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 252.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 84.0f);
layout.stateRect = UIRect(
margin,
layout.controlRect.y + layout.controlRect.height + gap,
leftWidth,
(std::max)(244.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
layout.previewRect = UIRect(
leftWidth + margin * 2.0f,
margin,
(std::max)(420.0f, width - leftWidth - margin * 3.0f),
height - margin * 2.0f);
layout.inspectorRect = UIRect(
layout.previewRect.x + 18.0f,
layout.previewRect.y + 54.0f,
(std::min)(392.0f, layout.previewRect.width - 36.0f),
150.0f);
layout.inspectorHeaderRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y,
layout.inspectorRect.width,
24.0f);
layout.sectionRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y + layout.inspectorHeaderRect.height,
layout.inspectorRect.width,
24.0f);
layout.fieldRect = UIRect(
layout.inspectorRect.x,
layout.sectionRect.y + layout.sectionRect.height + 2.0f,
layout.inspectorRect.width,
22.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
const float buttonY = layout.controlRect.y + 32.0f;
layout.buttons = {
{ ActionId::Reset, "重置", UIRect(layout.controlRect.x + 14.0f, buttonY, buttonWidth, 36.0f) },
{ ActionId::Capture, "截图(F12)", UIRect(layout.controlRect.x + 26.0f + buttonWidth, buttonY, buttonWidth, 36.0f) }
};
return layout;
}
XCEngine::UI::Editor::Widgets::UIEditorTextFieldMetrics ResolveHostedTextFieldMetrics(
const Style::UITheme& theme) {
const auto propertyMetrics = XCEngine::UI::Editor::ResolveUIEditorPropertyGridMetrics(theme);
const auto textMetrics = XCEngine::UI::Editor::ResolveUIEditorTextFieldMetrics(theme);
return XCEngine::UI::Editor::BuildUIEditorHostedTextFieldMetrics(propertyMetrics, textMetrics);
}
XCEngine::UI::Editor::Widgets::UIEditorTextFieldPalette ResolveHostedTextFieldPalette(
const Style::UITheme& theme) {
const auto propertyPalette = XCEngine::UI::Editor::ResolveUIEditorPropertyGridPalette(theme);
const auto textPalette = XCEngine::UI::Editor::ResolveUIEditorTextFieldPalette(theme);
return XCEngine::UI::Editor::BuildUIEditorHostedTextFieldPalette(propertyPalette, textPalette);
}
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, shellPalette.cardBackground, shellMetrics.cardRadius);
drawList.AddRectOutline(rect, shellPalette.cardBorder, 1.0f, shellMetrics.cardRadius);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 14.0f),
std::string(title),
shellPalette.textPrimary,
shellMetrics.titleFontSize);
if (!subtitle.empty()) {
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 40.0f),
std::string(subtitle),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
bool hovered) {
drawList.AddFilledRect(
button.rect,
hovered ? shellPalette.buttonHoverBackground : shellPalette.buttonBackground,
shellMetrics.buttonRadius);
drawList.AddRectOutline(button.rect, shellPalette.cardBorder, 1.0f, shellMetrics.buttonRadius);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
}
std::string DescribeHitTarget(const UIEditorTextFieldHitTarget& hitTarget) {
switch (hitTarget.kind) {
case UIEditorTextFieldHitTargetKind::ValueBox:
return "value_box";
case UIEditorTextFieldHitTargetKind::Row:
return "row";
case UIEditorTextFieldHitTargetKind::None:
default:
return "none";
}
}
UIInputEvent MakePointerEvent(
UIInputEventType type,
const UIPoint& position,
UIPointerButton button = UIPointerButton::None) {
UIInputEvent event = {};
event.type = type;
event.position = position;
event.pointerButton = button;
return event;
}
UIInputEvent MakeKeyEvent(std::int32_t keyCode) {
UIInputEvent event = {};
event.type = UIInputEventType::KeyDown;
event.keyCode = keyCode;
return event;
}
UIInputEvent MakeCharacterEvent(wchar_t character) {
UIInputEvent event = {};
event.type = UIInputEventType::Character;
event.character = static_cast<std::uint32_t>(character);
return event;
}
UIInputEvent MakeFocusEvent(UIInputEventType type) {
UIInputEvent event = {};
event.type = type;
return event;
}
class ScenarioApp {
public:
int Run(HINSTANCE hInstance, int nCmdShow) {
if (!Initialize(hInstance, nCmdShow)) {
Shutdown();
return 1;
}
MSG message = {};
while (message.message != WM_QUIT) {
if (PeekMessageW(&message, nullptr, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessageW(&message);
continue;
}
RenderFrame();
Sleep(8);
}
Shutdown();
return static_cast<int>(message.wParam);
}
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_NCCREATE) {
const auto* createStruct = reinterpret_cast<CREATESTRUCTW*>(lParam);
auto* app = reinterpret_cast<ScenarioApp*>(createStruct->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(app));
return TRUE;
}
auto* app = reinterpret_cast<ScenarioApp*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (message) {
case WM_SIZE:
if (app != nullptr && wParam != SIZE_MINIMIZED) {
app->OnResize(static_cast<UINT>(LOWORD(lParam)), static_cast<UINT>(HIWORD(lParam)));
}
return 0;
case WM_MOUSEMOVE:
if (app != nullptr) {
app->HandleMouseMove(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_MOUSELEAVE:
if (app != nullptr) {
app->HandleMouseLeave();
return 0;
}
break;
case WM_LBUTTONDOWN:
if (app != nullptr) {
app->HandleLeftButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_LBUTTONUP:
if (app != nullptr) {
app->HandleLeftButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (app != nullptr) {
if (wParam == VK_F12) {
app->m_autoScreenshot.RequestCapture("manual_f12");
app->m_lastResult = "已请求截图,输出到 captures/latest.png";
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
}
if (wParam == VK_F6) {
app->HandleFocusLost();
return 0;
}
const std::int32_t keyCode = MapTextFieldKey(static_cast<UINT>(wParam));
if (keyCode != static_cast<std::int32_t>(KeyCode::None)) {
app->HandleKeyDown(keyCode);
return 0;
}
}
break;
case WM_CHAR:
if (app != nullptr) {
app->HandleCharacter(static_cast<wchar_t>(wParam));
return 0;
}
break;
case WM_PAINT:
if (app != nullptr) {
PAINTSTRUCT paintStruct = {};
BeginPaint(hwnd, &paintStruct);
app->RenderFrame();
EndPaint(hwnd, &paintStruct);
return 0;
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
if (app != nullptr) {
app->m_hwnd = nullptr;
}
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
bool Initialize(HINSTANCE hInstance, int nCmdShow) {
WNDCLASSEXW windowClass = {};
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &ScenarioApp::WndProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.lpszClassName = kWindowClassName;
m_windowClassAtom = RegisterClassExW(&windowClass);
if (m_windowClassAtom == 0) {
return false;
}
m_hwnd = CreateWindowExW(
0,
kWindowClassName,
kWindowTitle,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
1480,
920,
nullptr,
nullptr,
hInstance,
this);
if (m_hwnd == nullptr) {
return false;
}
ShowWindow(m_hwnd, nCmdShow);
UpdateWindow(m_hwnd);
if (!m_renderer.Initialize(m_hwnd)) {
return false;
}
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/text_field_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
const auto themeLoad =
XCEngine::Tests::EditorUI::LoadEditorValidationTheme(ResolveValidationThemePath());
if (themeLoad.succeeded) {
m_theme = themeLoad.theme;
m_themeStatus = "loaded";
} else {
m_themeStatus = themeLoad.error.empty() ? "fallback" : themeLoad.error;
}
ResetScenario();
return true;
}
void Shutdown() {
m_autoScreenshot.Shutdown();
m_renderer.Shutdown();
if (m_hwnd != nullptr && IsWindow(m_hwnd)) {
DestroyWindow(m_hwnd);
}
m_hwnd = nullptr;
if (m_windowClassAtom != 0) {
UnregisterClassW(kWindowClassName, GetModuleHandleW(nullptr));
m_windowClassAtom = 0;
}
}
ScenarioLayout GetLayout() const {
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(
width,
height,
XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme));
}
void ResetScenario() {
m_spec = {};
m_spec.fieldId = "game_object_name";
m_spec.label = "Name";
m_spec.value = "Player";
m_spec.readOnly = false;
m_interactionState = {};
m_interactionState.textFieldState.focused = true;
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hoveredAction = ActionId::Reset;
m_hasHoveredAction = false;
m_lastResult = "已重置到默认 TextField 状态";
RefreshFrame();
}
void RefreshFrame() {
if (m_hwnd == nullptr) {
return;
}
const ScenarioLayout layout = GetLayout();
const auto metrics = ResolveHostedTextFieldMetrics(m_theme);
m_frame = UpdateUIEditorTextFieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
{},
metrics);
}
void OnResize(UINT width, UINT height) {
if (width == 0u || height == 0u) {
return;
}
m_renderer.Resize(width, height);
RefreshFrame();
}
void HandleMouseMove(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
UpdateHoveredAction(layout, x, y);
TRACKMOUSEEVENT trackEvent = {};
trackEvent.cbSize = sizeof(trackEvent);
trackEvent.dwFlags = TME_LEAVE;
trackEvent.hwndTrack = m_hwnd;
TrackMouseEvent(&trackEvent);
PumpEvents({ MakePointerEvent(UIInputEventType::PointerMove, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleMouseLeave() {
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hasHoveredAction = false;
PumpEvents({ MakePointerEvent(UIInputEventType::PointerLeave, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
if (HitTestAction(layout, x, y) != nullptr) {
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorTextFieldInteractionResult result =
PumpEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button != nullptr) {
ExecuteAction(button->action);
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorTextFieldInteractionResult result =
PumpEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleKeyDown(std::int32_t keyCode) {
const UIEditorTextFieldInteractionResult result = PumpEvents({ MakeKeyEvent(keyCode) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleCharacter(wchar_t character) {
if (character < 32) {
return;
}
const UIEditorTextFieldInteractionResult result = PumpEvents({ MakeCharacterEvent(character) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleFocusLost() {
const UIEditorTextFieldInteractionResult result =
PumpEvents({ MakeFocusEvent(UIInputEventType::FocusLost) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void UpdateHoveredAction(const ScenarioLayout& layout, float x, float y) {
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button == nullptr) {
m_hasHoveredAction = false;
return;
}
m_hoveredAction = button->action;
m_hasHoveredAction = true;
}
const ButtonLayout* HitTestAction(const ScenarioLayout& layout, float x, float y) const {
for (const ButtonLayout& button : layout.buttons) {
if (ContainsPoint(button.rect, x, y)) {
return &button;
}
}
return nullptr;
}
UIEditorTextFieldInteractionResult PumpEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
const auto metrics = ResolveHostedTextFieldMetrics(m_theme);
m_frame = UpdateUIEditorTextFieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
std::move(events),
metrics);
return m_frame.result;
}
void UpdateResultText(const UIEditorTextFieldInteractionResult& result) {
if (result.editCommitted) {
m_lastResult = std::string("已提交文本: ") + result.committedText;
return;
}
if (result.editCanceled) {
m_lastResult = "已取消编辑";
return;
}
if (result.editStarted) {
m_lastResult = "已进入编辑态";
return;
}
if (result.focusChanged) {
m_lastResult = std::string("焦点变化: ") + (m_interactionState.textFieldState.focused ? "focused" : "lost");
return;
}
if (result.consumed) {
m_lastResult = "控件已消费输入";
return;
}
m_lastResult = "等待交互";
}
void ExecuteAction(ActionId action) {
switch (action) {
case ActionId::Reset:
ResetScenario();
break;
case ActionId::Capture:
m_autoScreenshot.RequestCapture("manual_button");
m_lastResult = "已请求截图,输出到 captures/latest.png";
break;
}
}
void RenderFrame() {
if (m_hwnd == nullptr) {
return;
}
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const auto shellMetrics = XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme);
const auto shellPalette = XCEngine::Tests::EditorUI::ResolveEditorValidationShellPalette(m_theme);
const ScenarioLayout layout = BuildScenarioLayout(width, height, shellMetrics);
RefreshFrame();
const UIEditorTextFieldHitTarget currentHit =
HitTestUIEditorTextField(m_frame.layout, m_mousePosition);
const auto textMetrics = ResolveHostedTextFieldMetrics(m_theme);
const auto textPalette = ResolveHostedTextFieldPalette(m_theme);
const auto propertyPalette = XCEngine::UI::Editor::ResolveUIEditorPropertyGridPalette(m_theme);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorTextFieldBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), shellPalette.windowBackground);
DrawCard(
drawList,
layout.introRect,
shellPalette,
shellMetrics,
"这个测试验证什么功能",
"验证 UIEditorTextField 的基础编辑交互契约,不涉及 PropertyGrid 或业务 Inspector。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 value box检查是否进入编辑态。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 获得 focus 后按 Enter 开始编辑;直接输入字符也应开始编辑。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 编辑态下按 Enter commit按 Escape cancel。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 编辑态下按 F6 模拟 FocusLost应提交暂存文本并退出编辑态。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 检查 Hover / Focus / Editing / Value / Result 是否同步更新。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 182.0f),
"6. 按 F12 或点击截图按钮,确认自动截图路径正确。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.controlRect, shellPalette, shellMetrics, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
shellPalette,
shellMetrics,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(
drawList,
layout.stateRect,
shellPalette,
shellMetrics,
"状态摘要",
"重点检查 hit / focus / editing / value / display / result。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.textFieldState.focused ? "" : ""),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
std::string("Editing: ") + (m_interactionState.textFieldState.editing ? "" : ""),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
"Value: " + m_spec.value,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Display: " + m_interactionState.textFieldState.displayText,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Result: " + m_lastResult,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
? "截图排队中..."
: (m_autoScreenshot.GetLastCaptureSummary().empty()
? std::string("F12 -> tests/UI/Editor/integration/shell/text_field_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
captureSummary,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
"Theme: " + m_themeStatus,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
DrawCard(
drawList,
layout.previewRect,
shellPalette,
shellMetrics,
"TextField 预览",
"这里只放一个 Editor TextField用来验证基础字段行为。");
drawList.AddFilledRect(layout.inspectorRect, propertyPalette.surfaceColor);
drawList.AddRectOutline(layout.inspectorRect, propertyPalette.borderColor, 1.0f);
drawList.AddFilledRect(layout.inspectorHeaderRect, shellPalette.cardBackground);
drawList.AddRectOutline(layout.inspectorHeaderRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.inspectorHeaderRect.x + 10.0f, layout.inspectorHeaderRect.y + 5.0f),
"Inspector",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddFilledRect(layout.sectionRect, propertyPalette.sectionHeaderColor);
drawList.AddRectOutline(layout.sectionRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.sectionRect.x + 10.0f, layout.sectionRect.y + 5.0f),
"v Transform",
propertyPalette.sectionTextColor,
shellMetrics.bodyFontSize);
AppendUIEditorTextField(
drawList,
layout.fieldRect,
m_spec,
m_interactionState.textFieldState,
textPalette,
textMetrics);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
m_renderer,
drawData,
static_cast<unsigned int>(width),
static_cast<unsigned int>(height),
framePresented);
}
HWND m_hwnd = nullptr;
ATOM m_windowClassAtom = 0;
NativeRenderer m_renderer = {};
AutoScreenshotController m_autoScreenshot = {};
std::filesystem::path m_captureRoot = {};
UIEditorTextFieldSpec m_spec = {};
UIEditorTextFieldInteractionState m_interactionState = {};
UIEditorTextFieldInteractionFrame m_frame = {};
Style::UITheme m_theme = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
std::string m_themeStatus = "fallback";
};
} // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow) {
return ScenarioApp().Run(hInstance, nCmdShow);
}

View File

@@ -0,0 +1,31 @@
add_executable(editor_ui_vector2_field_basic_validation WIN32
main.cpp
)
target_include_directories(editor_ui_vector2_field_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/tests/UI/Editor/integration/shared/src
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include
)
target_compile_definitions(editor_ui_vector2_field_basic_validation PRIVATE
UNICODE
_UNICODE
XCENGINE_EDITOR_UI_TESTS_REPO_ROOT="${XCENGINE_EDITOR_UI_TESTS_REPO_ROOT_PATH}"
)
if(MSVC)
target_compile_options(editor_ui_vector2_field_basic_validation PRIVATE /utf-8 /FS)
set_property(TARGET editor_ui_vector2_field_basic_validation PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
target_link_libraries(editor_ui_vector2_field_basic_validation PRIVATE
XCUIEditorLib
XCUIEditorHost
)
set_target_properties(editor_ui_vector2_field_basic_validation PROPERTIES
OUTPUT_NAME "XCUIEditorVector2FieldBasicValidation"
)

View File

@@ -0,0 +1,896 @@
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <XCEditor/Core/UIEditorTheme.h>
#include <XCEditor/Core/UIEditorVector2FieldInteraction.h>
#include <XCEditor/Widgets/UIEditorVector2Field.h>
#include "EditorValidationTheme.h"
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
#include <XCEngine/Input/InputTypes.h>
#include <XCEngine/UI/DrawData.h>
#include <windows.h>
#include <windowsx.h>
#include <algorithm>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>
#ifndef XCENGINE_EDITOR_UI_TESTS_REPO_ROOT
#define XCENGINE_EDITOR_UI_TESTS_REPO_ROOT "."
#endif
namespace {
using XCEngine::Input::KeyCode;
using XCEngine::UI::UIDrawData;
using XCEngine::UI::UIDrawList;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Editor::Host::AutoScreenshotController;
using XCEngine::UI::Editor::Host::NativeRenderer;
using XCEngine::UI::Editor::UIEditorVector2FieldInteractionFrame;
using XCEngine::UI::Editor::UIEditorVector2FieldInteractionResult;
using XCEngine::UI::Editor::UIEditorVector2FieldInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorVector2FieldInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorVector2Field;
using XCEngine::UI::Editor::Widgets::FormatUIEditorVector2FieldComponentValue;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorVector2Field;
using XCEngine::UI::Editor::Widgets::UIEditorVector2FieldHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorVector2FieldHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorVector2FieldInvalidComponentIndex;
using XCEngine::UI::Editor::Widgets::UIEditorVector2FieldSpec;
namespace Style = XCEngine::UI::Style;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorVector2FieldBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | Vector2Field Basic";
enum class ActionId : unsigned char {
Reset = 0,
Capture
};
struct ButtonLayout {
ActionId action = ActionId::Reset;
const char* label = "";
UIRect rect = {};
};
struct ScenarioLayout {
UIRect introRect = {};
UIRect controlRect = {};
UIRect stateRect = {};
UIRect previewRect = {};
UIRect inspectorRect = {};
UIRect inspectorHeaderRect = {};
UIRect sectionRect = {};
UIRect fieldRect = {};
std::vector<ButtonLayout> buttons = {};
};
std::filesystem::path ResolveRepoRootPath() {
std::string root = XCENGINE_EDITOR_UI_TESTS_REPO_ROOT;
if (root.size() >= 2u && root.front() == '"' && root.back() == '"') {
root = root.substr(1u, root.size() - 2u);
}
return std::filesystem::path(root).lexically_normal();
}
std::filesystem::path ResolveValidationThemePath() {
return (ResolveRepoRootPath() / "tests/UI/Editor/integration/shared/themes/editor_validation.xctheme")
.lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
y >= rect.y &&
y <= rect.y + rect.height;
}
std::int32_t MapVector2FieldKey(UINT keyCode) {
switch (keyCode) {
case VK_LEFT:
return static_cast<std::int32_t>(KeyCode::Left);
case VK_RIGHT:
return static_cast<std::int32_t>(KeyCode::Right);
case VK_UP:
return static_cast<std::int32_t>(KeyCode::Up);
case VK_DOWN:
return static_cast<std::int32_t>(KeyCode::Down);
case VK_HOME:
return static_cast<std::int32_t>(KeyCode::Home);
case VK_END:
return static_cast<std::int32_t>(KeyCode::End);
case VK_TAB:
return static_cast<std::int32_t>(KeyCode::Tab);
case VK_RETURN:
return static_cast<std::int32_t>(KeyCode::Enter);
case VK_ESCAPE:
return static_cast<std::int32_t>(KeyCode::Escape);
default:
return static_cast<std::int32_t>(KeyCode::None);
}
}
ScenarioLayout BuildScenarioLayout(
float width,
float height,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics) {
const float margin = shellMetrics.margin;
constexpr float leftWidth = 470.0f;
const float gap = shellMetrics.gap;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 272.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 84.0f);
layout.stateRect = UIRect(
margin,
layout.controlRect.y + layout.controlRect.height + gap,
leftWidth,
(std::max)(240.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
layout.previewRect = UIRect(
leftWidth + margin * 2.0f,
margin,
(std::max)(420.0f, width - leftWidth - margin * 3.0f),
height - margin * 2.0f);
layout.inspectorRect = UIRect(
layout.previewRect.x + 18.0f,
layout.previewRect.y + 54.0f,
(std::min)(392.0f, layout.previewRect.width - 36.0f),
172.0f);
layout.inspectorHeaderRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y,
layout.inspectorRect.width,
24.0f);
layout.sectionRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y + layout.inspectorHeaderRect.height,
layout.inspectorRect.width,
24.0f);
layout.fieldRect = UIRect(
layout.inspectorRect.x,
layout.sectionRect.y + layout.sectionRect.height + 2.0f,
layout.inspectorRect.width,
22.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
const float buttonY = layout.controlRect.y + 32.0f;
layout.buttons = {
{ ActionId::Reset, "重置", UIRect(layout.controlRect.x + 14.0f, buttonY, buttonWidth, 36.0f) },
{ ActionId::Capture, "截图(F12)", UIRect(layout.controlRect.x + 26.0f + buttonWidth, buttonY, buttonWidth, 36.0f) }
};
return layout;
}
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, shellPalette.cardBackground, shellMetrics.cardRadius);
drawList.AddRectOutline(rect, shellPalette.cardBorder, 1.0f, shellMetrics.cardRadius);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 14.0f),
std::string(title),
shellPalette.textPrimary,
shellMetrics.titleFontSize);
if (!subtitle.empty()) {
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 40.0f),
std::string(subtitle),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
bool hovered) {
drawList.AddFilledRect(
button.rect,
hovered ? shellPalette.buttonHoverBackground : shellPalette.buttonBackground,
shellMetrics.buttonRadius);
drawList.AddRectOutline(button.rect, shellPalette.cardBorder, 1.0f, shellMetrics.buttonRadius);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
}
std::string DescribeHitTarget(const UIEditorVector2FieldHitTarget& hitTarget) {
switch (hitTarget.kind) {
case UIEditorVector2FieldHitTargetKind::Component:
return std::string("component_") + std::to_string(hitTarget.componentIndex);
case UIEditorVector2FieldHitTargetKind::Row:
return "row";
case UIEditorVector2FieldHitTargetKind::None:
default:
return "none";
}
}
std::string DescribeSelectedComponent(std::size_t componentIndex) {
if (componentIndex == UIEditorVector2FieldInvalidComponentIndex) {
return "none";
}
return componentIndex == 0u ? "X" : "Y";
}
UIInputEvent MakePointerEvent(
UIInputEventType type,
const UIPoint& position,
UIPointerButton button = UIPointerButton::None) {
UIInputEvent event = {};
event.type = type;
event.position = position;
event.pointerButton = button;
return event;
}
UIInputEvent MakeKeyEvent(std::int32_t keyCode, bool shift = false) {
UIInputEvent event = {};
event.type = UIInputEventType::KeyDown;
event.keyCode = keyCode;
event.modifiers.shift = shift;
return event;
}
UIInputEvent MakeCharacterEvent(wchar_t character) {
UIInputEvent event = {};
event.type = UIInputEventType::Character;
event.character = static_cast<std::uint32_t>(character);
return event;
}
UIInputEvent MakeFocusEvent(UIInputEventType type) {
UIInputEvent event = {};
event.type = type;
return event;
}
class ScenarioApp {
public:
int Run(HINSTANCE hInstance, int nCmdShow) {
if (!Initialize(hInstance, nCmdShow)) {
Shutdown();
return 1;
}
MSG message = {};
while (message.message != WM_QUIT) {
if (PeekMessageW(&message, nullptr, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessageW(&message);
continue;
}
RenderFrame();
Sleep(8);
}
Shutdown();
return static_cast<int>(message.wParam);
}
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_NCCREATE) {
const auto* createStruct = reinterpret_cast<CREATESTRUCTW*>(lParam);
auto* app = reinterpret_cast<ScenarioApp*>(createStruct->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(app));
return TRUE;
}
auto* app = reinterpret_cast<ScenarioApp*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (message) {
case WM_SIZE:
if (app != nullptr && wParam != SIZE_MINIMIZED) {
app->OnResize(static_cast<UINT>(LOWORD(lParam)), static_cast<UINT>(HIWORD(lParam)));
}
return 0;
case WM_MOUSEMOVE:
if (app != nullptr) {
app->HandleMouseMove(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_MOUSELEAVE:
if (app != nullptr) {
app->HandleMouseLeave();
return 0;
}
break;
case WM_LBUTTONDOWN:
if (app != nullptr) {
app->HandleLeftButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_LBUTTONUP:
if (app != nullptr) {
app->HandleLeftButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (app != nullptr) {
if (wParam == VK_F12) {
app->m_autoScreenshot.RequestCapture("manual_f12");
app->m_lastResult = "已请求截图,输出到 captures/latest.png";
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
}
if (wParam == VK_F6) {
app->HandleFocusLost();
return 0;
}
const std::int32_t keyCode = MapVector2FieldKey(static_cast<UINT>(wParam));
if (keyCode != static_cast<std::int32_t>(KeyCode::None)) {
app->HandleKeyDown(keyCode, (GetKeyState(VK_SHIFT) & 0x8000) != 0);
return 0;
}
}
break;
case WM_CHAR:
if (app != nullptr) {
app->HandleCharacter(static_cast<wchar_t>(wParam));
return 0;
}
break;
case WM_PAINT:
if (app != nullptr) {
PAINTSTRUCT paintStruct = {};
BeginPaint(hwnd, &paintStruct);
app->RenderFrame();
EndPaint(hwnd, &paintStruct);
return 0;
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
if (app != nullptr) {
app->m_hwnd = nullptr;
}
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
bool Initialize(HINSTANCE hInstance, int nCmdShow) {
WNDCLASSEXW windowClass = {};
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &ScenarioApp::WndProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.lpszClassName = kWindowClassName;
m_windowClassAtom = RegisterClassExW(&windowClass);
if (m_windowClassAtom == 0) {
return false;
}
m_hwnd = CreateWindowExW(
0,
kWindowClassName,
kWindowTitle,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
1520,
920,
nullptr,
nullptr,
hInstance,
this);
if (m_hwnd == nullptr) {
return false;
}
ShowWindow(m_hwnd, nCmdShow);
UpdateWindow(m_hwnd);
if (!m_renderer.Initialize(m_hwnd)) {
return false;
}
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/vector2_field_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
const auto themeLoad =
XCEngine::Tests::EditorUI::LoadEditorValidationTheme(ResolveValidationThemePath());
if (themeLoad.succeeded) {
m_theme = themeLoad.theme;
m_themeStatus = "loaded";
} else {
m_themeStatus = themeLoad.error.empty() ? "fallback" : themeLoad.error;
}
ResetScenario();
return true;
}
void Shutdown() {
m_autoScreenshot.Shutdown();
m_renderer.Shutdown();
if (m_hwnd != nullptr && IsWindow(m_hwnd)) {
DestroyWindow(m_hwnd);
}
m_hwnd = nullptr;
if (m_windowClassAtom != 0) {
UnregisterClassW(kWindowClassName, GetModuleHandleW(nullptr));
m_windowClassAtom = 0;
}
}
ScenarioLayout GetLayout() const {
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(
width,
height,
XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme));
}
void ResetScenario() {
m_spec = {};
m_spec.fieldId = "position";
m_spec.label = "Position";
m_spec.values = { 1.25, -2.5 };
m_spec.componentLabels = { std::string("X"), std::string("Y") };
m_spec.step = 0.25;
m_spec.minValue = -10.0;
m_spec.maxValue = 10.0;
m_spec.integerMode = false;
m_spec.readOnly = false;
m_interactionState = {};
m_interactionState.vector2FieldState.focused = true;
m_interactionState.vector2FieldState.selectedComponentIndex = 0u;
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hoveredAction = ActionId::Reset;
m_hasHoveredAction = false;
m_lastResult = "已重置到默认 Vector2Field 状态";
RefreshFrame();
}
void RefreshFrame() {
if (m_hwnd == nullptr) {
return;
}
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorVector2FieldMetrics(m_theme);
m_frame = UpdateUIEditorVector2FieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
{},
metrics);
}
void OnResize(UINT width, UINT height) {
if (width == 0u || height == 0u) {
return;
}
m_renderer.Resize(width, height);
RefreshFrame();
}
void HandleMouseMove(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
UpdateHoveredAction(layout, x, y);
TRACKMOUSEEVENT trackEvent = {};
trackEvent.cbSize = sizeof(trackEvent);
trackEvent.dwFlags = TME_LEAVE;
trackEvent.hwndTrack = m_hwnd;
TrackMouseEvent(&trackEvent);
PumpEvents({ MakePointerEvent(UIInputEventType::PointerMove, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleMouseLeave() {
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hasHoveredAction = false;
PumpEvents({ MakePointerEvent(UIInputEventType::PointerLeave, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
if (HitTestAction(layout, x, y) != nullptr) {
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorVector2FieldInteractionResult result =
PumpEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button != nullptr) {
ExecuteAction(button->action);
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorVector2FieldInteractionResult result =
PumpEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleKeyDown(std::int32_t keyCode, bool shift) {
const UIEditorVector2FieldInteractionResult result =
PumpEvents({ MakeKeyEvent(keyCode, shift) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleCharacter(wchar_t character) {
if (character < 32) {
return;
}
const UIEditorVector2FieldInteractionResult result =
PumpEvents({ MakeCharacterEvent(character) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleFocusLost() {
const UIEditorVector2FieldInteractionResult result =
PumpEvents({ MakeFocusEvent(UIInputEventType::FocusLost) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void UpdateHoveredAction(const ScenarioLayout& layout, float x, float y) {
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button == nullptr) {
m_hasHoveredAction = false;
return;
}
m_hoveredAction = button->action;
m_hasHoveredAction = true;
}
const ButtonLayout* HitTestAction(const ScenarioLayout& layout, float x, float y) const {
for (const ButtonLayout& button : layout.buttons) {
if (ContainsPoint(button.rect, x, y)) {
return &button;
}
}
return nullptr;
}
UIEditorVector2FieldInteractionResult PumpEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorVector2FieldMetrics(m_theme);
m_frame = UpdateUIEditorVector2FieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
std::move(events),
metrics);
return m_frame.result;
}
void UpdateResultText(const UIEditorVector2FieldInteractionResult& result) {
if (result.editCommitRejected) {
m_lastResult = "提交失败,当前文本不是合法数字";
return;
}
if (result.editCommitted) {
m_lastResult =
std::string("已提交 ") +
DescribeSelectedComponent(result.changedComponentIndex) +
" = " + result.committedText;
return;
}
if (result.editCanceled) {
m_lastResult = "已取消编辑";
return;
}
if (result.editStarted) {
m_lastResult =
std::string("开始编辑 component ") +
DescribeSelectedComponent(result.selectedComponentIndex);
return;
}
if (result.stepApplied || result.valueChanged) {
m_lastResult =
std::string("数值已更新,当前 component = ") +
DescribeSelectedComponent(result.changedComponentIndex);
return;
}
if (result.selectionChanged) {
m_lastResult =
std::string("已切换选中 component: ") +
DescribeSelectedComponent(result.selectedComponentIndex);
return;
}
if (result.focusChanged) {
m_lastResult =
std::string("焦点变化: ") +
(m_interactionState.vector2FieldState.focused ? "focused" : "lost");
return;
}
if (result.consumed) {
m_lastResult = "控件已消费输入";
return;
}
m_lastResult = "等待交互";
}
void ExecuteAction(ActionId action) {
switch (action) {
case ActionId::Reset:
ResetScenario();
break;
case ActionId::Capture:
m_autoScreenshot.RequestCapture("manual_button");
m_lastResult = "已请求截图,输出到 captures/latest.png";
break;
}
}
void RenderFrame() {
if (m_hwnd == nullptr) {
return;
}
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const auto shellMetrics = XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme);
const auto shellPalette = XCEngine::Tests::EditorUI::ResolveEditorValidationShellPalette(m_theme);
const ScenarioLayout layout = BuildScenarioLayout(width, height, shellMetrics);
RefreshFrame();
const UIEditorVector2FieldHitTarget currentHit =
HitTestUIEditorVector2Field(m_frame.layout, m_mousePosition);
const auto vectorMetrics = XCEngine::UI::Editor::ResolveUIEditorVector2FieldMetrics(m_theme);
const auto vectorPalette = XCEngine::UI::Editor::ResolveUIEditorVector2FieldPalette(m_theme);
const auto propertyPalette = XCEngine::UI::Editor::ResolveUIEditorPropertyGridPalette(m_theme);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorVector2FieldBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), shellPalette.windowBackground);
DrawCard(
drawList,
layout.introRect,
shellPalette,
shellMetrics,
"这个测试验证什么功能",
"验证 UIEditorVector2Field 的双通道数值编辑契约,不涉及 PropertyGrid 或业务 Inspector。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 X / Y 对应的 value box检查 selected component 是否切换,并且应进入编辑态。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 获得 focus 后按 Tab检查 selected component 在 X / Y 之间切换。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 非编辑态按 Up / Down / Home / End检查当前 component 的 step / 边界行为。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 按 Enter 开始编辑直接输入字符也应开始编辑Enter commitEscape cancel。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 编辑态按 F6 模拟 FocusLost应提交暂存文本并退出编辑态。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 182.0f),
"6. 检查 Hover / Selected / Editing / Values / Result 是否同步更新。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 204.0f),
"7. 按 F12 或点击截图按钮,确认自动截图路径正确。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.controlRect, shellPalette, shellMetrics, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
shellPalette,
shellMetrics,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(
drawList,
layout.stateRect,
shellPalette,
shellMetrics,
"状态摘要",
"重点检查 hit / selected / editing / values / display / result。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
"Selected: " + DescribeSelectedComponent(m_interactionState.vector2FieldState.selectedComponentIndex),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
std::string("Focused: ") + (m_interactionState.vector2FieldState.focused ? "" : ""),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
std::string("Editing: ") + (m_interactionState.vector2FieldState.editing ? "" : ""),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Values: X=" + FormatUIEditorVector2FieldComponentValue(m_spec, 0u) +
" Y=" + FormatUIEditorVector2FieldComponentValue(m_spec, 1u),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Display: X=" + m_interactionState.vector2FieldState.displayTexts[0] +
" Y=" + m_interactionState.vector2FieldState.displayTexts[1],
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
"Result: " + m_lastResult,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
? "截图排队中..."
: (m_autoScreenshot.GetLastCaptureSummary().empty()
? std::string("F12 -> tests/UI/Editor/integration/shell/vector2_field_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
captureSummary,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 262.0f),
"Theme: " + m_themeStatus,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
DrawCard(
drawList,
layout.previewRect,
shellPalette,
shellMetrics,
"Vector2Field 预览",
"这里只放一个 Unity 风格的双通道 Vector2 字段。");
drawList.AddFilledRect(layout.inspectorRect, propertyPalette.surfaceColor);
drawList.AddRectOutline(layout.inspectorRect, propertyPalette.borderColor, 1.0f);
drawList.AddFilledRect(layout.inspectorHeaderRect, shellPalette.cardBackground);
drawList.AddRectOutline(layout.inspectorHeaderRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.inspectorHeaderRect.x + 10.0f, layout.inspectorHeaderRect.y + 5.0f),
"Inspector",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddFilledRect(layout.sectionRect, propertyPalette.sectionHeaderColor);
drawList.AddRectOutline(layout.sectionRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.sectionRect.x + 10.0f, layout.sectionRect.y + 5.0f),
"v Transform",
propertyPalette.sectionTextColor,
shellMetrics.bodyFontSize);
AppendUIEditorVector2Field(
drawList,
layout.fieldRect,
m_spec,
m_interactionState.vector2FieldState,
vectorPalette,
vectorMetrics);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
m_renderer,
drawData,
static_cast<unsigned int>(width),
static_cast<unsigned int>(height),
framePresented);
}
HWND m_hwnd = nullptr;
ATOM m_windowClassAtom = 0;
NativeRenderer m_renderer = {};
AutoScreenshotController m_autoScreenshot = {};
std::filesystem::path m_captureRoot = {};
UIEditorVector2FieldSpec m_spec = {};
UIEditorVector2FieldInteractionState m_interactionState = {};
UIEditorVector2FieldInteractionFrame m_frame = {};
Style::UITheme m_theme = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = "等待交互";
std::string m_themeStatus = "fallback";
};
} // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
ScenarioApp app = {};
return app.Run(hInstance, nCmdShow);
}

View File

@@ -0,0 +1,31 @@
add_executable(editor_ui_vector3_field_basic_validation WIN32
main.cpp
)
target_include_directories(editor_ui_vector3_field_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/tests/UI/Editor/integration/shared/src
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include
)
target_compile_definitions(editor_ui_vector3_field_basic_validation PRIVATE
UNICODE
_UNICODE
XCENGINE_EDITOR_UI_TESTS_REPO_ROOT="${XCENGINE_EDITOR_UI_TESTS_REPO_ROOT_PATH}"
)
if(MSVC)
target_compile_options(editor_ui_vector3_field_basic_validation PRIVATE /utf-8 /FS)
set_property(TARGET editor_ui_vector3_field_basic_validation PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
target_link_libraries(editor_ui_vector3_field_basic_validation PRIVATE
XCUIEditorLib
XCUIEditorHost
)
set_target_properties(editor_ui_vector3_field_basic_validation PROPERTIES
OUTPUT_NAME "XCUIEditorVector3FieldBasicValidation"
)

View File

@@ -0,0 +1,904 @@
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <XCEditor/Core/UIEditorTheme.h>
#include <XCEditor/Core/UIEditorVector3FieldInteraction.h>
#include <XCEditor/Widgets/UIEditorVector3Field.h>
#include "EditorValidationTheme.h"
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
#include <XCEngine/Input/InputTypes.h>
#include <XCEngine/UI/DrawData.h>
#include <windows.h>
#include <windowsx.h>
#include <algorithm>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>
#ifndef XCENGINE_EDITOR_UI_TESTS_REPO_ROOT
#define XCENGINE_EDITOR_UI_TESTS_REPO_ROOT "."
#endif
namespace {
using XCEngine::Input::KeyCode;
using XCEngine::UI::UIDrawData;
using XCEngine::UI::UIDrawList;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Editor::Host::AutoScreenshotController;
using XCEngine::UI::Editor::Host::NativeRenderer;
using XCEngine::UI::Editor::UIEditorVector3FieldInteractionFrame;
using XCEngine::UI::Editor::UIEditorVector3FieldInteractionResult;
using XCEngine::UI::Editor::UIEditorVector3FieldInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorVector3FieldInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorVector3Field;
using XCEngine::UI::Editor::Widgets::FormatUIEditorVector3FieldComponentValue;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorVector3Field;
using XCEngine::UI::Editor::Widgets::UIEditorVector3FieldHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorVector3FieldHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorVector3FieldInvalidComponentIndex;
using XCEngine::UI::Editor::Widgets::UIEditorVector3FieldSpec;
namespace Style = XCEngine::UI::Style;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorVector3FieldBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | Vector3Field Basic";
enum class ActionId : unsigned char {
Reset = 0,
Capture
};
struct ButtonLayout {
ActionId action = ActionId::Reset;
const char* label = "";
UIRect rect = {};
};
struct ScenarioLayout {
UIRect introRect = {};
UIRect controlRect = {};
UIRect stateRect = {};
UIRect previewRect = {};
UIRect inspectorRect = {};
UIRect inspectorHeaderRect = {};
UIRect sectionRect = {};
UIRect fieldRect = {};
std::vector<ButtonLayout> buttons = {};
};
std::filesystem::path ResolveRepoRootPath() {
std::string root = XCENGINE_EDITOR_UI_TESTS_REPO_ROOT;
if (root.size() >= 2u && root.front() == '"' && root.back() == '"') {
root = root.substr(1u, root.size() - 2u);
}
return std::filesystem::path(root).lexically_normal();
}
std::filesystem::path ResolveValidationThemePath() {
return (ResolveRepoRootPath() / "tests/UI/Editor/integration/shared/themes/editor_validation.xctheme")
.lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
y >= rect.y &&
y <= rect.y + rect.height;
}
std::int32_t MapVector3FieldKey(UINT keyCode) {
switch (keyCode) {
case VK_LEFT:
return static_cast<std::int32_t>(KeyCode::Left);
case VK_RIGHT:
return static_cast<std::int32_t>(KeyCode::Right);
case VK_UP:
return static_cast<std::int32_t>(KeyCode::Up);
case VK_DOWN:
return static_cast<std::int32_t>(KeyCode::Down);
case VK_HOME:
return static_cast<std::int32_t>(KeyCode::Home);
case VK_END:
return static_cast<std::int32_t>(KeyCode::End);
case VK_TAB:
return static_cast<std::int32_t>(KeyCode::Tab);
case VK_RETURN:
return static_cast<std::int32_t>(KeyCode::Enter);
case VK_ESCAPE:
return static_cast<std::int32_t>(KeyCode::Escape);
default:
return static_cast<std::int32_t>(KeyCode::None);
}
}
ScenarioLayout BuildScenarioLayout(
float width,
float height,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics) {
const float margin = shellMetrics.margin;
constexpr float leftWidth = 470.0f;
const float gap = shellMetrics.gap;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 272.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 84.0f);
layout.stateRect = UIRect(
margin,
layout.controlRect.y + layout.controlRect.height + gap,
leftWidth,
(std::max)(240.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
layout.previewRect = UIRect(
leftWidth + margin * 2.0f,
margin,
(std::max)(420.0f, width - leftWidth - margin * 3.0f),
height - margin * 2.0f);
layout.inspectorRect = UIRect(
layout.previewRect.x + 18.0f,
layout.previewRect.y + 54.0f,
(std::min)(392.0f, layout.previewRect.width - 36.0f),
172.0f);
layout.inspectorHeaderRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y,
layout.inspectorRect.width,
24.0f);
layout.sectionRect = UIRect(
layout.inspectorRect.x,
layout.inspectorRect.y + layout.inspectorHeaderRect.height,
layout.inspectorRect.width,
24.0f);
layout.fieldRect = UIRect(
layout.inspectorRect.x,
layout.sectionRect.y + layout.sectionRect.height + 2.0f,
layout.inspectorRect.width,
22.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
const float buttonY = layout.controlRect.y + 32.0f;
layout.buttons = {
{ ActionId::Reset, "重置", UIRect(layout.controlRect.x + 14.0f, buttonY, buttonWidth, 36.0f) },
{ ActionId::Capture, "截图(F12)", UIRect(layout.controlRect.x + 26.0f + buttonWidth, buttonY, buttonWidth, 36.0f) }
};
return layout;
}
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, shellPalette.cardBackground, shellMetrics.cardRadius);
drawList.AddRectOutline(rect, shellPalette.cardBorder, 1.0f, shellMetrics.cardRadius);
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 14.0f),
std::string(title),
shellPalette.textPrimary,
shellMetrics.titleFontSize);
if (!subtitle.empty()) {
drawList.AddText(
UIPoint(rect.x + 16.0f, rect.y + 40.0f),
std::string(subtitle),
shellPalette.textMuted,
shellMetrics.bodyFontSize);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
const XCEngine::Tests::EditorUI::EditorValidationShellPalette& shellPalette,
const XCEngine::Tests::EditorUI::EditorValidationShellMetrics& shellMetrics,
bool hovered) {
drawList.AddFilledRect(
button.rect,
hovered ? shellPalette.buttonHoverBackground : shellPalette.buttonBackground,
shellMetrics.buttonRadius);
drawList.AddRectOutline(button.rect, shellPalette.cardBorder, 1.0f, shellMetrics.buttonRadius);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
}
std::string DescribeHitTarget(const UIEditorVector3FieldHitTarget& hitTarget) {
switch (hitTarget.kind) {
case UIEditorVector3FieldHitTargetKind::Component:
return std::string("component_") + std::to_string(hitTarget.componentIndex);
case UIEditorVector3FieldHitTargetKind::Row:
return "row";
case UIEditorVector3FieldHitTargetKind::None:
default:
return "none";
}
}
std::string DescribeSelectedComponent(std::size_t componentIndex) {
switch (componentIndex) {
case 0u:
return "X";
case 1u:
return "Y";
case 2u:
return "Z";
default:
return "none";
}
}
UIInputEvent MakePointerEvent(
UIInputEventType type,
const UIPoint& position,
UIPointerButton button = UIPointerButton::None) {
UIInputEvent event = {};
event.type = type;
event.position = position;
event.pointerButton = button;
return event;
}
UIInputEvent MakeKeyEvent(std::int32_t keyCode, bool shift = false) {
UIInputEvent event = {};
event.type = UIInputEventType::KeyDown;
event.keyCode = keyCode;
event.modifiers.shift = shift;
return event;
}
UIInputEvent MakeCharacterEvent(wchar_t character) {
UIInputEvent event = {};
event.type = UIInputEventType::Character;
event.character = static_cast<std::uint32_t>(character);
return event;
}
UIInputEvent MakeFocusEvent(UIInputEventType type) {
UIInputEvent event = {};
event.type = type;
return event;
}
class ScenarioApp {
public:
int Run(HINSTANCE hInstance, int nCmdShow) {
if (!Initialize(hInstance, nCmdShow)) {
Shutdown();
return 1;
}
MSG message = {};
while (message.message != WM_QUIT) {
if (PeekMessageW(&message, nullptr, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessageW(&message);
continue;
}
RenderFrame();
Sleep(8);
}
Shutdown();
return static_cast<int>(message.wParam);
}
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_NCCREATE) {
const auto* createStruct = reinterpret_cast<CREATESTRUCTW*>(lParam);
auto* app = reinterpret_cast<ScenarioApp*>(createStruct->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(app));
return TRUE;
}
auto* app = reinterpret_cast<ScenarioApp*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (message) {
case WM_SIZE:
if (app != nullptr && wParam != SIZE_MINIMIZED) {
app->OnResize(static_cast<UINT>(LOWORD(lParam)), static_cast<UINT>(HIWORD(lParam)));
}
return 0;
case WM_MOUSEMOVE:
if (app != nullptr) {
app->HandleMouseMove(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_MOUSELEAVE:
if (app != nullptr) {
app->HandleMouseLeave();
return 0;
}
break;
case WM_LBUTTONDOWN:
if (app != nullptr) {
app->HandleLeftButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_LBUTTONUP:
if (app != nullptr) {
app->HandleLeftButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (app != nullptr) {
if (wParam == VK_F12) {
app->m_autoScreenshot.RequestCapture("manual_f12");
app->m_lastResult = "已请求截图,输出到 captures/latest.png";
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
}
if (wParam == VK_F6) {
app->HandleFocusLost();
return 0;
}
const std::int32_t keyCode = MapVector3FieldKey(static_cast<UINT>(wParam));
if (keyCode != static_cast<std::int32_t>(KeyCode::None)) {
app->HandleKeyDown(keyCode, (GetKeyState(VK_SHIFT) & 0x8000) != 0);
return 0;
}
}
break;
case WM_CHAR:
if (app != nullptr) {
app->HandleCharacter(static_cast<wchar_t>(wParam));
return 0;
}
break;
case WM_PAINT:
if (app != nullptr) {
PAINTSTRUCT paintStruct = {};
BeginPaint(hwnd, &paintStruct);
app->RenderFrame();
EndPaint(hwnd, &paintStruct);
return 0;
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
if (app != nullptr) {
app->m_hwnd = nullptr;
}
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
bool Initialize(HINSTANCE hInstance, int nCmdShow) {
WNDCLASSEXW windowClass = {};
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &ScenarioApp::WndProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.lpszClassName = kWindowClassName;
m_windowClassAtom = RegisterClassExW(&windowClass);
if (m_windowClassAtom == 0) {
return false;
}
m_hwnd = CreateWindowExW(
0,
kWindowClassName,
kWindowTitle,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
1520,
920,
nullptr,
nullptr,
hInstance,
this);
if (m_hwnd == nullptr) {
return false;
}
ShowWindow(m_hwnd, nCmdShow);
UpdateWindow(m_hwnd);
if (!m_renderer.Initialize(m_hwnd)) {
return false;
}
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/vector3_field_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
const auto themeLoad =
XCEngine::Tests::EditorUI::LoadEditorValidationTheme(ResolveValidationThemePath());
if (themeLoad.succeeded) {
m_theme = themeLoad.theme;
m_themeStatus = "loaded";
} else {
m_themeStatus = themeLoad.error.empty() ? "fallback" : themeLoad.error;
}
ResetScenario();
return true;
}
void Shutdown() {
m_autoScreenshot.Shutdown();
m_renderer.Shutdown();
if (m_hwnd != nullptr && IsWindow(m_hwnd)) {
DestroyWindow(m_hwnd);
}
m_hwnd = nullptr;
if (m_windowClassAtom != 0) {
UnregisterClassW(kWindowClassName, GetModuleHandleW(nullptr));
m_windowClassAtom = 0;
}
}
ScenarioLayout GetLayout() const {
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(
width,
height,
XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme));
}
void ResetScenario() {
m_spec = {};
m_spec.fieldId = "position";
m_spec.label = "Position";
m_spec.values = { 1.25, -2.5, 4.75 };
m_spec.componentLabels = { std::string("X"), std::string("Y"), std::string("Z") };
m_spec.step = 0.25;
m_spec.minValue = -10.0;
m_spec.maxValue = 10.0;
m_spec.integerMode = false;
m_spec.readOnly = false;
m_interactionState = {};
m_interactionState.vector3FieldState.focused = true;
m_interactionState.vector3FieldState.selectedComponentIndex = 0u;
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hoveredAction = ActionId::Reset;
m_hasHoveredAction = false;
m_lastResult = "已重置到默认 Vector3Field 状态";
RefreshFrame();
}
void RefreshFrame() {
if (m_hwnd == nullptr) {
return;
}
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorVector3FieldMetrics(m_theme);
m_frame = UpdateUIEditorVector3FieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
{},
metrics);
}
void OnResize(UINT width, UINT height) {
if (width == 0u || height == 0u) {
return;
}
m_renderer.Resize(width, height);
RefreshFrame();
}
void HandleMouseMove(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
UpdateHoveredAction(layout, x, y);
TRACKMOUSEEVENT trackEvent = {};
trackEvent.cbSize = sizeof(trackEvent);
trackEvent.dwFlags = TME_LEAVE;
trackEvent.hwndTrack = m_hwnd;
TrackMouseEvent(&trackEvent);
PumpEvents({ MakePointerEvent(UIInputEventType::PointerMove, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleMouseLeave() {
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hasHoveredAction = false;
PumpEvents({ MakePointerEvent(UIInputEventType::PointerLeave, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
if (HitTestAction(layout, x, y) != nullptr) {
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorVector3FieldInteractionResult result =
PumpEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button != nullptr) {
ExecuteAction(button->action);
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorVector3FieldInteractionResult result =
PumpEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleKeyDown(std::int32_t keyCode, bool shift) {
const UIEditorVector3FieldInteractionResult result =
PumpEvents({ MakeKeyEvent(keyCode, shift) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleCharacter(wchar_t character) {
if (character < 32) {
return;
}
const UIEditorVector3FieldInteractionResult result =
PumpEvents({ MakeCharacterEvent(character) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleFocusLost() {
const UIEditorVector3FieldInteractionResult result =
PumpEvents({ MakeFocusEvent(UIInputEventType::FocusLost) });
UpdateResultText(result);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void UpdateHoveredAction(const ScenarioLayout& layout, float x, float y) {
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button == nullptr) {
m_hasHoveredAction = false;
return;
}
m_hoveredAction = button->action;
m_hasHoveredAction = true;
}
const ButtonLayout* HitTestAction(const ScenarioLayout& layout, float x, float y) const {
for (const ButtonLayout& button : layout.buttons) {
if (ContainsPoint(button.rect, x, y)) {
return &button;
}
}
return nullptr;
}
UIEditorVector3FieldInteractionResult PumpEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
const auto metrics = XCEngine::UI::Editor::ResolveUIEditorVector3FieldMetrics(m_theme);
m_frame = UpdateUIEditorVector3FieldInteraction(
m_interactionState,
m_spec,
layout.fieldRect,
std::move(events),
metrics);
return m_frame.result;
}
void UpdateResultText(const UIEditorVector3FieldInteractionResult& result) {
if (result.editCommitRejected) {
m_lastResult = "提交失败,当前文本不是合法数字";
return;
}
if (result.editCommitted) {
m_lastResult =
std::string("已提交 ") +
DescribeSelectedComponent(result.changedComponentIndex) +
" = " + result.committedText;
return;
}
if (result.editCanceled) {
m_lastResult = "已取消编辑";
return;
}
if (result.editStarted) {
m_lastResult =
std::string("开始编辑 component ") +
DescribeSelectedComponent(result.selectedComponentIndex);
return;
}
if (result.stepApplied || result.valueChanged) {
m_lastResult =
std::string("数值已更新,当前 component = ") +
DescribeSelectedComponent(result.changedComponentIndex);
return;
}
if (result.selectionChanged) {
m_lastResult =
std::string("已切换选中 component: ") +
DescribeSelectedComponent(result.selectedComponentIndex);
return;
}
if (result.focusChanged) {
m_lastResult =
std::string("焦点变化: ") +
(m_interactionState.vector3FieldState.focused ? "focused" : "lost");
return;
}
if (result.consumed) {
m_lastResult = "控件已消费输入";
return;
}
m_lastResult = "等待交互";
}
void ExecuteAction(ActionId action) {
switch (action) {
case ActionId::Reset:
ResetScenario();
break;
case ActionId::Capture:
m_autoScreenshot.RequestCapture("manual_button");
m_lastResult = "已请求截图,输出到 captures/latest.png";
break;
}
}
void RenderFrame() {
if (m_hwnd == nullptr) {
return;
}
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const auto shellMetrics = XCEngine::Tests::EditorUI::ResolveEditorValidationShellMetrics(m_theme);
const auto shellPalette = XCEngine::Tests::EditorUI::ResolveEditorValidationShellPalette(m_theme);
const ScenarioLayout layout = BuildScenarioLayout(width, height, shellMetrics);
RefreshFrame();
const UIEditorVector3FieldHitTarget currentHit =
HitTestUIEditorVector3Field(m_frame.layout, m_mousePosition);
const auto vectorMetrics = XCEngine::UI::Editor::ResolveUIEditorVector3FieldMetrics(m_theme);
const auto vectorPalette = XCEngine::UI::Editor::ResolveUIEditorVector3FieldPalette(m_theme);
const auto propertyPalette = XCEngine::UI::Editor::ResolveUIEditorPropertyGridPalette(m_theme);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorVector3FieldBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), shellPalette.windowBackground);
DrawCard(
drawList,
layout.introRect,
shellPalette,
shellMetrics,
"这个测试验证什么功能",
"验证 UIEditorVector3Field 的三通道数值编辑契约,不涉及 PropertyGrid 或业务 Inspector。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 X / Y / Z 对应的 value box检查 selected component 是否切换,并应进入编辑态。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 获得 focus 后按 Tab检查 selected component 在 X / Y / Z 之间切换。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 非编辑态按 Up / Down / Home / End检查当前 component 的 step / 边界行为。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 按 Enter 开始编辑直接输入字符也应开始编辑Enter commitEscape cancel。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 编辑态按 F6 模拟 FocusLost应提交暂存文本并退出编辑态。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 182.0f),
"6. 检查 Hover / Selected / Editing / Values / Result 是否同步更新。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 204.0f),
"7. 按 F12 或点击截图按钮,确认自动截图路径正确。",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
DrawCard(drawList, layout.controlRect, shellPalette, shellMetrics, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
shellPalette,
shellMetrics,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(
drawList,
layout.stateRect,
shellPalette,
shellMetrics,
"状态摘要",
"重点检查 hit / selected / editing / values / display / result。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
"Selected: " + DescribeSelectedComponent(m_interactionState.vector3FieldState.selectedComponentIndex),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
std::string("Focused: ") + (m_interactionState.vector3FieldState.focused ? "" : ""),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
std::string("Editing: ") + (m_interactionState.vector3FieldState.editing ? "" : ""),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Values: X=" + FormatUIEditorVector3FieldComponentValue(m_spec, 0u) +
" Y=" + FormatUIEditorVector3FieldComponentValue(m_spec, 1u) +
" Z=" + FormatUIEditorVector3FieldComponentValue(m_spec, 2u),
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Display: X=" + m_interactionState.vector3FieldState.displayTexts[0] +
" Y=" + m_interactionState.vector3FieldState.displayTexts[1] +
" Z=" + m_interactionState.vector3FieldState.displayTexts[2],
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
"Result: " + m_lastResult,
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
? "截图排队中..."
: (m_autoScreenshot.GetLastCaptureSummary().empty()
? std::string("F12 -> tests/UI/Editor/integration/shell/vector3_field_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
captureSummary,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 262.0f),
"Theme: " + m_themeStatus,
shellPalette.textWeak,
shellMetrics.bodyFontSize);
DrawCard(
drawList,
layout.previewRect,
shellPalette,
shellMetrics,
"Vector3Field 预览",
"这里只放一个 Unity 风格的三通道 Vector3 字段。");
drawList.AddFilledRect(layout.inspectorRect, propertyPalette.surfaceColor);
drawList.AddRectOutline(layout.inspectorRect, propertyPalette.borderColor, 1.0f);
drawList.AddFilledRect(layout.inspectorHeaderRect, shellPalette.cardBackground);
drawList.AddRectOutline(layout.inspectorHeaderRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.inspectorHeaderRect.x + 10.0f, layout.inspectorHeaderRect.y + 5.0f),
"Inspector",
shellPalette.textPrimary,
shellMetrics.bodyFontSize);
drawList.AddFilledRect(layout.sectionRect, propertyPalette.sectionHeaderColor);
drawList.AddRectOutline(layout.sectionRect, propertyPalette.borderColor, 1.0f);
drawList.AddText(
UIPoint(layout.sectionRect.x + 10.0f, layout.sectionRect.y + 5.0f),
"v Transform",
propertyPalette.sectionTextColor,
shellMetrics.bodyFontSize);
AppendUIEditorVector3Field(
drawList,
layout.fieldRect,
m_spec,
m_interactionState.vector3FieldState,
vectorPalette,
vectorMetrics);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
m_renderer,
drawData,
static_cast<unsigned int>(width),
static_cast<unsigned int>(height),
framePresented);
}
HWND m_hwnd = nullptr;
ATOM m_windowClassAtom = 0;
NativeRenderer m_renderer = {};
AutoScreenshotController m_autoScreenshot = {};
std::filesystem::path m_captureRoot = {};
UIEditorVector3FieldSpec m_spec = {};
UIEditorVector3FieldInteractionState m_interactionState = {};
UIEditorVector3FieldInteractionFrame m_frame = {};
Style::UITheme m_theme = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = "等待交互";
std::string m_themeStatus = "fallback";
};
} // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
ScenarioApp app = {};
return app.Run(hInstance, nCmdShow);
}