载入中...
搜索中...
未找到
UiRuntime.cpp
浏览该文件的文档.
2
3#include "ui/UIHost.h"
4#include "ui/Widget.h"
5
6#include <map>
7
8namespace eve::ui_editing {
9namespace {
10
11Result<ui::NodeType> nodeType(const std::string& type) {
12 static const std::map<std::string, ui::NodeType> types{{"progress", ui::NodeType::Progress},
13 {"filled-image", ui::NodeType::FilledImage},
14 {"radial-fill", ui::NodeType::RadialFill},
15 {"window", ui::NodeType::Window},
16 {"text", ui::NodeType::Text},
17 {"button", ui::NodeType::Button},
18 {"group", ui::NodeType::Group},
19 {"checkbox", ui::NodeType::Checkbox},
20 {"slider", ui::NodeType::Slider},
21 {"input", ui::NodeType::InputText},
22 {"flex", ui::NodeType::Flex},
23 {"image", ui::NodeType::Image},
24 {"image-button", ui::NodeType::ImageButton},
25 {"viewport", ui::NodeType::Viewport},
26 {"card", ui::NodeType::Card},
27 {"toolbar", ui::NodeType::Toolbar},
28 {"sidebar", ui::NodeType::Sidebar},
29 {"split-pane", ui::NodeType::SplitPane},
30 {"nine-patch", ui::NodeType::NinePatchPanel}};
31 if (type == "row" || type == "column") return eve::editing::applied<ui::NodeType>(ui::NodeType::Flex);
32 const auto found = types.find(type);
33 if (found == types.end())
34 return eve::editing::failed<ui::NodeType>(EditorStatus::Unsupported,
35 RuleId("editor.ui.unsupported-runtime-widget"),
36 "Runtime UI does not support widget type: " + type);
37 return eve::editing::applied<ui::NodeType>(found->second);
38}
39
40Result<ui::WidgetDesc> buildWidget(const UiDocumentTarget& document, const ObjectId& id,
41 const UiTextureResolver& textures, std::map<std::string, std::uint64_t>& cache) {
42 auto value = document.widget(id);
43 if (!value.ok())
44 return eve::editing::failed<ui::WidgetDesc>(EditorStatus::NotFound, RuleId("editor.ui.widget-not-found"),
45 "UI widget does not exist: " + id.value());
46 auto type = nodeType(value.value().type);
47 if (!type.ok()) return Result<ui::WidgetDesc>::failure(type.status());
48 const UiWidgetSnapshot& widget = value.value();
49 if (!widget.content.fontAsset.empty() || widget.content.imageFit != "stretch")
50 return eve::editing::failed<ui::WidgetDesc>(
51 EditorStatus::Unsupported, RuleId("editor.ui.runtime-content-unsupported"),
52 "Runtime document publishing requires the default font and stretch fit: " + id.value());
53 ui::WidgetDesc result;
54 result.type = type.value();
55 result.withId(widget.id.value())
56 .withKey(widget.id.value())
57 .withText(widget.text)
58 .withVisible(widget.visible)
59 .withEnabled(widget.enabled)
60 .withSize(static_cast<float>(widget.layout.width), static_cast<float>(widget.layout.height))
61 .withAbsolute(static_cast<float>(widget.layout.anchorX), static_cast<float>(widget.layout.anchorY),
62 static_cast<float>(widget.layout.x - widget.layout.width * widget.layout.pivotX),
63 static_cast<float>(widget.layout.y - widget.layout.height * widget.layout.pivotY))
64 .withMargin(static_cast<float>(widget.style.marginLeft), static_cast<float>(widget.style.marginTop),
65 static_cast<float>(widget.style.marginRight), static_cast<float>(widget.style.marginBottom))
66 .withPadding(static_cast<float>(widget.style.paddingLeft), static_cast<float>(widget.style.paddingTop),
67 static_cast<float>(widget.style.paddingRight), static_cast<float>(widget.style.paddingBottom))
68 .withTint(static_cast<float>(widget.style.tintR), static_cast<float>(widget.style.tintG),
69 static_cast<float>(widget.style.tintB), static_cast<float>(widget.style.tintA))
70 .withCornerRadius(static_cast<float>(widget.style.cornerRadius))
71 .withGap(static_cast<float>(widget.style.gap))
72 .withFlexGrow(static_cast<float>(widget.style.flexGrow));
73 if (!widget.content.textureAsset.empty()) {
74 const bool supportsTexture = result.type == ui::NodeType::Image || result.type == ui::NodeType::ImageButton ||
77 if (!supportsTexture || !textures)
78 return eve::editing::failed<ui::WidgetDesc>(
79 EditorStatus::Unsupported, RuleId("editor.ui.runtime-texture-resolver-required"),
80 "Texture content requires a supported image widget and a texture resolver: " + id.value());
81 auto found = cache.find(widget.content.textureAsset);
82 if (found == cache.end()) {
83 auto resolved = textures(widget.content.textureAsset);
84 if (!resolved.ok()) return Result<ui::WidgetDesc>::failure(resolved.status());
85 if (resolved.value() == 0)
86 return eve::editing::failed<ui::WidgetDesc>(EditorStatus::Rejected,
87 RuleId("editor.ui.runtime-texture-invalid"),
88 "Texture resolver returned an empty registration");
89 found = cache.emplace(widget.content.textureAsset, resolved.value()).first;
90 }
91 result.textureId = found->second;
92 }
93 result.value = static_cast<float>(widget.content.value);
94 result.fontSize = static_cast<float>(widget.content.fontSize);
95 result.textR = static_cast<float>(widget.content.textR);
96 result.textG = static_cast<float>(widget.content.textG);
97 result.textB = static_cast<float>(widget.content.textB);
98 result.textA = static_cast<float>(widget.content.textA);
99 const auto alignment = [](const std::string& value) {
100 return value == "center" ? .5f : (value == "end" ? 1.f : 0.f);
101 };
102 result.textAlignX = alignment(widget.content.horizontalAlign);
103 result.textAlignY = alignment(widget.content.verticalAlign);
104 result.fillDirection =
105 widget.content.fillDirection == "left"
107 : (widget.content.fillDirection == "down"
109 : (widget.content.fillDirection == "up" ? ui::FillDirection::Up : ui::FillDirection::Right));
110 result.fillStretch = widget.content.fillStretch;
111 result.fillStartDegrees = static_cast<float>(widget.content.fillStartDegrees);
112 result.fillClockwise = widget.content.fillClockwise;
113 result.fillInnerRadius = static_cast<float>(widget.content.fillInnerRadius);
115 result.mouseFilter = ui::MouseFilter::Ignore;
116 result.focusMode = ui::FocusMode::None;
117 }
118 result.flexDirection = widget.type == "column" || (widget.type != "row" && widget.style.direction == "column")
120 : ui::FlexDirection::Row;
121 const std::map<std::string, ui::FlexAlign> aligns{{"start", ui::FlexAlign::Start},
122 {"center", ui::FlexAlign::Center},
123 {"end", ui::FlexAlign::End},
124 {"stretch", ui::FlexAlign::Stretch}};
125 const std::map<std::string, ui::FlexJustify> justifies{{"start", ui::FlexJustify::Start},
126 {"center", ui::FlexJustify::Center},
127 {"end", ui::FlexJustify::End},
128 {"space-between", ui::FlexJustify::SpaceBetween},
129 {"space-around", ui::FlexJustify::SpaceAround}};
130 result.alignItems = aligns.at(widget.style.align);
131 result.justifyContent = justifies.at(widget.style.justify);
132 for (const ObjectId& child : document.children(id)) {
133 auto built = buildWidget(document, child, textures, cache);
134 if (!built.ok()) return Result<ui::WidgetDesc>::failure(built.status());
135 result.children.push_back(std::move(built).value());
136 }
137 return eve::editing::applied<ui::WidgetDesc>(std::move(result));
138}
139
140} // namespace
141
143 const UiTextureResolver& textures) const {
144 if (!host)
145 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.ui.runtime-host-required"),
146 "Runtime UI publishing requires a live UIHost");
147 std::map<std::string, std::uint64_t> cache;
148 auto built = buildWidget(document, root, textures, cache);
149 if (!built.ok()) return Result<void>::failure(built.status());
150 host->setTreeReconcile(std::move(built).value());
151 return eve::editing::applied<void>();
152}
153
154} // namespace eve::ui_editing
int root
Definition AnimSmr.cpp:119
std::string id
Poco::JSON::Object::Ptr result
Object::Ptr value
RoadNetwork resolved
bool found
int children
Definition TreeMesh.cpp:432
UIHostHandle host
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
ECS mount point for one UI panel/screen. Subclass to attach UI to game entities, e....
Definition UIHost.h:284
Result< void > publish(const UiDocumentTarget &document, const ObjectId &root, ui::UIHost *host, const UiTextureResolver &textures={}) const
Reconciles a document subtree atomically; unsupported content leaves the host unchanged....
Serializable UI authoring document with hierarchy and inspector capabilities.
Definition UiDocument.h:157
std::unordered_map< std::string, Value > Row
Definition Database.h:28
ui_editing::UiWidgetSnapshot UiWidgetSnapshot
std::function< Result< std::uint64_t >(const std::string &)> UiTextureResolver
Resolves authored texture asset keys to existing UI backend registrations. @ownership Caller owns tex...
Definition UiDocument.h:279
FlexDirection
Main-axis direction for Flex containers.
Definition UIHost.h:80
std::string widget