载入中...
搜索中...
未找到
UiDocumentSerialization.cpp
浏览该文件的文档.
2
3#include <cmath>
4#include <set>
5#include <utility>
6
7namespace eve::ui_editing {
8namespace {
9
10const EditorValue* field(const EditorValue& value, const char* key) {
11 const auto* object = value.getIf<EditorValue::Object>();
12 if (!object) return nullptr;
13 const auto found = object->find(key);
14 return found == object->end() ? nullptr : &found->second;
15}
16
17const double* numberField(const EditorValue& value, const char* key) {
18 const EditorValue* entry = field(value, key);
19 return entry ? entry->getIf<double>() : nullptr;
20}
21
22} // namespace
23
24EditorValue UiDocumentTarget::layoutValue(const UiLayoutValue& layout) {
26 value["x"] = layout.x;
27 value["y"] = layout.y;
28 value["width"] = layout.width;
29 value["height"] = layout.height;
30 value["anchorX"] = layout.anchorX;
31 value["anchorY"] = layout.anchorY;
32 value["pivotX"] = layout.pivotX;
33 value["pivotY"] = layout.pivotY;
34 return EditorValue(std::move(value));
35}
36
37Result<UiLayoutValue> UiDocumentTarget::parseLayout(const EditorValue& value) {
38 const double* x = numberField(value, "x");
39 const double* y = numberField(value, "y");
40 const double* width = numberField(value, "width");
41 const double* height = numberField(value, "height");
42 const double* anchorX = numberField(value, "anchorX");
43 const double* anchorY = numberField(value, "anchorY");
44 const double* pivotX = numberField(value, "pivotX");
45 const double* pivotY = numberField(value, "pivotY");
46 if (!x || !y || !width || !height || !anchorX || !anchorY || !pivotX || !pivotY ||
47 *width < 0.0 || *height < 0.0 || *anchorX < 0.0 || *anchorX > 1.0 ||
48 *anchorY < 0.0 || *anchorY > 1.0 || *pivotX < 0.0 || *pivotX > 1.0 ||
49 *pivotY < 0.0 || *pivotY > 1.0)
50 return eve::editing::failed<UiLayoutValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-layout"),
51 "UI layout fields are missing or out of range");
52 return eve::editing::applied<UiLayoutValue>(
53 {*x, *y, *width, *height, *anchorX, *anchorY, *pivotX, *pivotY});
54}
55
56EditorValue UiDocumentTarget::styleValue(const UiStyleValue& style) {
57 return EditorValue::Object{{"marginLeft", style.marginLeft}, {"marginTop", style.marginTop},
58 {"marginRight", style.marginRight}, {"marginBottom", style.marginBottom},
59 {"paddingLeft", style.paddingLeft}, {"paddingTop", style.paddingTop},
60 {"paddingRight", style.paddingRight}, {"paddingBottom", style.paddingBottom},
61 {"tintR", style.tintR}, {"tintG", style.tintG}, {"tintB", style.tintB},
62 {"tintA", style.tintA}, {"cornerRadius", style.cornerRadius}, {"gap", style.gap},
63 {"flexGrow", style.flexGrow}, {"direction", style.direction}, {"align", style.align},
64 {"justify", style.justify}};
65}
66
67Result<UiStyleValue> UiDocumentTarget::parseStyle(const EditorValue& value) {
68 UiStyleValue style;
69 double* outputs[]{&style.marginLeft, &style.marginTop, &style.marginRight, &style.marginBottom,
70 &style.paddingLeft, &style.paddingTop, &style.paddingRight, &style.paddingBottom,
71 &style.tintR, &style.tintG, &style.tintB, &style.tintA, &style.cornerRadius,
72 &style.gap, &style.flexGrow};
73 const char* keys[]{"marginLeft", "marginTop", "marginRight", "marginBottom", "paddingLeft",
74 "paddingTop", "paddingRight", "paddingBottom", "tintR", "tintG", "tintB",
75 "tintA", "cornerRadius", "gap", "flexGrow"};
76 for (std::size_t i = 0; i < std::size(outputs); ++i) {
77 const double* number = numberField(value, keys[i]);
78 if (!number || !std::isfinite(*number))
79 return eve::editing::failed<UiStyleValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-style"),
80 "UI style numeric fields are missing or invalid");
81 *outputs[i] = *number;
82 }
83 const EditorValue* directionValue = field(value, "direction");
84 const EditorValue* alignValue = field(value, "align");
85 const EditorValue* justifyValue = field(value, "justify");
86 const auto* direction = directionValue ? directionValue->getIf<std::string>() : nullptr;
87 const auto* align = alignValue ? alignValue->getIf<std::string>() : nullptr;
88 const auto* justify = justifyValue ? justifyValue->getIf<std::string>() : nullptr;
89 if (!direction || !align || !justify)
90 return eve::editing::failed<UiStyleValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-style"),
91 "UI flex style fields are missing");
92 style.direction = *direction; style.align = *align; style.justify = *justify;
93 const std::set<std::string> directions{"row", "column"};
94 const std::set<std::string> aligns{"start", "center", "end", "stretch"};
95 const std::set<std::string> justifies{"start", "center", "end", "space-between", "space-around"};
96 for (std::size_t i = 0; i < std::size(outputs); ++i)
97 if (*outputs[i] < 0.0 || (i >= 8 && i <= 11 && *outputs[i] > 1.0))
98 return eve::editing::failed<UiStyleValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-style-range"),
99 "UI box values must be non-negative and tint normalized");
100 if (!directions.contains(style.direction) || !aligns.contains(style.align) ||
101 !justifies.contains(style.justify))
102 return eve::editing::failed<UiStyleValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-flex-style"),
103 "UI flex direction, alignment or justification is invalid");
104 return eve::editing::applied<UiStyleValue>(std::move(style));
105}
106
107EditorValue UiDocumentTarget::contentValue(const UiContentValue& content) {
108 return EditorValue::Object{{"fontAsset", content.fontAsset},
109 {"fontSize", content.fontSize},
110 {"textR", content.textR},
111 {"textG", content.textG},
112 {"textB", content.textB},
113 {"textA", content.textA},
114 {"horizontalAlign", content.horizontalAlign},
115 {"verticalAlign", content.verticalAlign},
116 {"textureAsset", content.textureAsset},
117 {"imageFit", content.imageFit},
118 {"clip", content.clip},
119 {"value", content.value},
120 {"fillDirection", content.fillDirection},
121 {"fillStretch", content.fillStretch},
122 {"fillStartDegrees", content.fillStartDegrees},
123 {"fillClockwise", content.fillClockwise},
124 {"fillInnerRadius", content.fillInnerRadius}};
125}
126
127Result<UiContentValue> UiDocumentTarget::parseContent(const EditorValue& value) {
128 UiContentValue content;
129 const EditorValue* fontAssetValue = field(value, "fontAsset");
130 const EditorValue* horizontalValue = field(value, "horizontalAlign");
131 const EditorValue* verticalValue = field(value, "verticalAlign");
132 const EditorValue* textureAssetValue = field(value, "textureAsset");
133 const EditorValue* fitValue = field(value, "imageFit");
134 const EditorValue* clipValue = field(value, "clip");
135 const auto* fontAsset = fontAssetValue ? fontAssetValue->getIf<std::string>() : nullptr;
136 const auto* horizontal = horizontalValue ? horizontalValue->getIf<std::string>() : nullptr;
137 const auto* vertical = verticalValue ? verticalValue->getIf<std::string>() : nullptr;
138 const auto* textureAsset = textureAssetValue ? textureAssetValue->getIf<std::string>() : nullptr;
139 const auto* fit = fitValue ? fitValue->getIf<std::string>() : nullptr;
140 const auto* clip = clipValue ? clipValue->getIf<bool>() : nullptr;
141 const double* fontSize = numberField(value, "fontSize");
142 const double* textR = numberField(value, "textR");
143 const double* textG = numberField(value, "textG");
144 const double* textB = numberField(value, "textB");
145 const double* textA = numberField(value, "textA");
146 if (!fontAsset || !horizontal || !vertical || !textureAsset || !fit || !clip || !fontSize ||
147 !textR || !textG || !textB || !textA)
148 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-content"),
149 "UI content fields are missing");
150 content = {*fontAsset, *fontSize, *textR, *textG, *textB, *textA, *horizontal, *vertical,
151 *textureAsset, *fit, *clip};
152 // Versions 1/2 omit fill properties and migrate to the default empty fill.
153 if (const auto* entry = field(value, "value")) {
154 const auto* number = entry->getIf<double>();
155 if (!number || !std::isfinite(*number) || *number < 0.0 || *number > 1.0)
156 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-fill"),
157 "Fill value must be in [0,1]");
158 content.value = *number;
159 }
160 if (const auto* entry = field(value, "fillDirection")) {
161 const auto* direction = entry->getIf<std::string>();
162 if (!direction || (*direction != "right" && *direction != "left" && *direction != "down" && *direction != "up"))
163 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-fill"),
164 "Invalid fill direction");
165 content.fillDirection = *direction;
166 }
167 for (auto [key, output] :
168 {std::pair{"fillStretch", &content.fillStretch}, std::pair{"fillClockwise", &content.fillClockwise}}) {
169 if (const auto* entry = field(value, key)) {
170 const auto* flag = entry->getIf<bool>();
171 if (!flag)
172 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-fill"),
173 "Invalid fill flag");
174 *output = *flag;
175 }
176 }
177 if (const auto* entry = field(value, "fillStartDegrees")) {
178 const auto* number = entry->getIf<double>();
179 if (!number || !std::isfinite(*number) || std::abs(*number) > 360000.0)
180 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-fill"),
181 "Invalid fill angle");
182 content.fillStartDegrees = *number;
183 }
184 if (const auto* entry = field(value, "fillInnerRadius")) {
185 const auto* number = entry->getIf<double>();
186 if (!number || !std::isfinite(*number) || *number < 0.0 || *number > 1.0)
187 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-fill"),
188 "Invalid inner radius");
189 content.fillInnerRadius = *number;
190 }
191 const std::set<std::string> aligns{"start", "center", "end"};
192 const std::set<std::string> fits{"stretch", "contain", "cover"};
193 if (!std::isfinite(content.fontSize) || content.fontSize <= 0.0 || content.fontSize > 512.0 ||
194 !std::isfinite(content.textR) || !std::isfinite(content.textG) ||
195 !std::isfinite(content.textB) || !std::isfinite(content.textA) || content.textR < 0.0 ||
196 content.textR > 1.0 || content.textG < 0.0 || content.textG > 1.0 ||
197 content.textB < 0.0 || content.textB > 1.0 || content.textA < 0.0 ||
198 content.textA > 1.0 || !aligns.contains(content.horizontalAlign) ||
199 !aligns.contains(content.verticalAlign) || !fits.contains(content.imageFit))
200 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected, RuleId("editor.ui.invalid-content-range"),
201 "UI content size, color, alignment or image fit is invalid");
202 return eve::editing::applied<UiContentValue>(std::move(content));
203}
204
205EditorValue UiDocumentTarget::widgetValue(const UiWidgetSnapshot& widget) {
207 value["id"] = widget.id.value();
208 value["parent"] = widget.parent.value();
209 value["type"] = widget.type;
210 value["name"] = widget.name;
211 value["text"] = widget.text;
212 value["visible"] = widget.visible;
213 value["enabled"] = widget.enabled;
214 value["layout"] = layoutValue(widget.layout);
215 value["style"] = styleValue(widget.style);
216 value["content"] = contentValue(widget.content);
217 return EditorValue(std::move(value));
218}
219
220Result<UiWidgetSnapshot> UiDocumentTarget::parseWidget(const EditorValue& value) {
221 const EditorValue* idValue = field(value, "id");
222 const EditorValue* parentValue = field(value, "parent");
223 const EditorValue* typeValue = field(value, "type");
224 const EditorValue* nameValue = field(value, "name");
225 const EditorValue* textValue = field(value, "text");
226 const EditorValue* visibleValue = field(value, "visible");
227 const EditorValue* enabledValue = field(value, "enabled");
228 const EditorValue* layout = field(value, "layout");
229 const EditorValue* styleValueEntry = field(value, "style");
230 const EditorValue* contentValueEntry = field(value, "content");
231 const auto* id = idValue ? idValue->getIf<std::string>() : nullptr;
232 const auto* parent = parentValue ? parentValue->getIf<std::string>() : nullptr;
233 const auto* type = typeValue ? typeValue->getIf<std::string>() : nullptr;
234 const auto* name = nameValue ? nameValue->getIf<std::string>() : nullptr;
235 const auto* text = textValue ? textValue->getIf<std::string>() : nullptr;
236 const auto* visible = visibleValue ? visibleValue->getIf<bool>() : nullptr;
237 const auto* enabled = enabledValue ? enabledValue->getIf<bool>() : nullptr;
238 if (!id || id->empty() || !parent || !type || type->empty() || !name || name->empty() ||
239 !text || !visible || !enabled || !layout)
240 return eve::editing::failed<UiWidgetSnapshot>(EditorStatus::Rejected, RuleId("editor.ui.invalid-widget"),
241 "UI widget fields are missing or invalid");
242 auto parsedLayout = parseLayout(*layout);
243 if (!parsedLayout.ok())
244 return eve::editing::failed<UiWidgetSnapshot>(EditorStatus::Rejected, RuleId("editor.ui.invalid-widget-layout"),
245 "UI widget layout is invalid");
246 UiStyleValue parsedStyle;
247 if (styleValueEntry) {
248 auto style = parseStyle(*styleValueEntry);
249 if (!style.ok())
250 return eve::editing::failed<UiWidgetSnapshot>(
251 EditorStatus::Rejected, RuleId("editor.ui.invalid-widget-style"), "UI widget style is invalid");
252 parsedStyle = std::move(style).value();
253 }
254 UiContentValue parsedContent;
255 if (contentValueEntry) {
256 auto content = parseContent(*contentValueEntry);
257 if (!content.ok())
258 return eve::editing::failed<UiWidgetSnapshot>(EditorStatus::Rejected,
259 RuleId("editor.ui.invalid-widget-content"),
260 "UI widget content skin is invalid");
261 parsedContent = std::move(content).value();
262 }
263 return eve::editing::applied<UiWidgetSnapshot>(
264 {ObjectId(*id), ObjectId(*parent), *type, *name, *text, *visible, *enabled,
265 std::move(parsedLayout).value(), std::move(parsedStyle), std::move(parsedContent)});
266}
267
269 EditorValue::Array widgets;
270 widgets.reserve(widgets_.size());
271 for (const auto& [id, widget] : widgets_) {
272 (void)id;
273 widgets.push_back(widgetValue(widget));
274 }
276 root["schemaVersion"] = 3;
277 root["widgets"] = EditorValue(std::move(widgets));
278 return EditorValue(std::move(root));
279}
280
282 const EditorValue* versionValue = field(snapshot, "schemaVersion");
283 const EditorValue* widgetsValue = field(snapshot, "widgets");
284 const auto* version = versionValue ? versionValue->getIf<int64_t>() : nullptr;
285 const auto* widgets = widgetsValue ? widgetsValue->getIf<EditorValue::Array>() : nullptr;
286 if (!version || (*version != 1 && *version != 2 && *version != 3) || !widgets)
287 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.ui.snapshot-format"),
288 "UI snapshot requires schemaVersion 1, 2 or 3 and a widget array");
289 std::map<ObjectId, UiWidgetSnapshot> candidate;
290 for (const EditorValue& value : *widgets) {
291 auto parsed = parseWidget(value);
292 if (!parsed.ok())
293 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.ui.snapshot-widget"),
294 "UI snapshot contains an invalid widget");
295 const auto& widget = parsed.value();
296 if (!candidate.emplace(widget.id, widget).second)
297 return eve::editing::failed<void>(EditorStatus::Conflict, RuleId("editor.ui.snapshot-duplicate"),
298 "UI snapshot contains duplicate widget ids");
299 }
300 for (const auto& [id, widget] : candidate) {
301 if ((!widget.parent.empty() && !candidate.contains(widget.parent)) ||
302 wouldCycle(id, widget.parent, candidate))
303 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.ui.snapshot-hierarchy"),
304 "UI snapshot hierarchy is missing a parent or contains a cycle");
305 }
306 widgets_ = std::move(candidate);
307 bumpRevision();
309 return eve::editing::applied<void>();
310}
311
312} // namespace eve::ui_editing
double y
double x
int root
Definition AnimSmr.cpp:119
std::string output
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::string layout
glm::vec4 clip
std::uint32_t height
std::uint32_t width
const ColourScope * parent
std::string id
Snapshot snapshot
size_t directions
Object::Ptr value
RoadLaneDirection direction
double number
bool found
std::string text
bool visible
Move-only operation result carrying either a value or Status.
Definition Result.h:176
const std::string & value() const noexcept
Returns the exact legacy or canonical compatibility spelling.
Definition Identity.h:365
void bumpRevision()
Advance the revision without touching the dirty region.
virtual void clearDirtyRegion()=0
Clears dirty region.
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
Type type() const
Return the active value type.
const T * getIf() const
Return an immediately borrowed typed value.
EditorValue snapshotValue() const
Capture deterministic UI document content.
Result< void > loadSnapshot(const EditorValue &snapshot)
Atomically load and validate a persisted UI document.
editing::ObjectId ObjectId
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
ui_editing::UiContentValue UiContentValue
ui_editing::UiStyleValue UiStyleValue
void keys(const Value &value, std::initializer_list< const char * > names)
editing::Value EditorValue
Definition UiDocument.h:46
bool enabled
std::string widget