12 if (!
object)
return nullptr;
13 const auto found =
object->find(
key);
14 return found ==
object->end() ? nullptr : &
found->second;
19 return entry ? entry->getIf<
double>() : nullptr;
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});
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}};
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) {
79 return eve::editing::failed<UiStyleValue>(EditorStatus::Rejected,
RuleId(
"editor.ui.invalid-style"),
80 "UI style numeric fields are missing or invalid");
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;
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));
107EditorValue UiDocumentTarget::contentValue(
const UiContentValue& content) {
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}};
127Result<UiContentValue> UiDocumentTarget::parseContent(
const EditorValue&
value) {
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};
153 if (
const auto* entry =
field(
value,
"value")) {
154 const auto*
number = entry->getIf<
double>();
156 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected,
RuleId(
"editor.ui.invalid-fill"),
157 "Fill value must be in [0,1]");
160 if (
const auto* entry =
field(
value,
"fillDirection")) {
161 const auto*
direction = entry->getIf<std::string>();
163 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected,
RuleId(
"editor.ui.invalid-fill"),
164 "Invalid fill direction");
168 {std::pair{
"fillStretch", &content.fillStretch}, std::pair{
"fillClockwise", &content.fillClockwise}}) {
170 const auto* flag = entry->getIf<
bool>();
172 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected,
RuleId(
"editor.ui.invalid-fill"),
173 "Invalid fill flag");
177 if (
const auto* entry =
field(
value,
"fillStartDegrees")) {
178 const auto*
number = entry->getIf<
double>();
180 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected,
RuleId(
"editor.ui.invalid-fill"),
181 "Invalid fill angle");
182 content.fillStartDegrees = *
number;
184 if (
const auto* entry =
field(
value,
"fillInnerRadius")) {
185 const auto*
number = entry->getIf<
double>();
187 return eve::editing::failed<UiContentValue>(EditorStatus::Rejected,
RuleId(
"editor.ui.invalid-fill"),
188 "Invalid inner radius");
189 content.fillInnerRadius = *
number;
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));
220Result<UiWidgetSnapshot> UiDocumentTarget::parseWidget(
const EditorValue&
value) {
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;
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");
247 if (styleValueEntry) {
248 auto style = parseStyle(*styleValueEntry);
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();
255 if (contentValueEntry) {
256 auto content = parseContent(*contentValueEntry);
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();
263 return eve::editing::applied<UiWidgetSnapshot>(
265 std::move(parsedLayout).
value(), std::move(parsedStyle), std::move(parsedContent)});
270 widgets.reserve(widgets_.size());
271 for (
const auto& [
id,
widget] : widgets_) {
273 widgets.push_back(widgetValue(
widget));
276 root[
"schemaVersion"] = 3;
284 const auto*
version = versionValue ? versionValue->
getIf<int64_t>() :
nullptr;
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;
291 auto parsed = parseWidget(
value);
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();
297 return eve::editing::failed<void>(EditorStatus::Conflict, RuleId(
"editor.ui.snapshot-duplicate"),
298 "UI snapshot contains duplicate widget ids");
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");
306 widgets_ = std::move(candidate);
309 return eve::editing::applied<void>();
const ColourScope * parent
RoadLaneDirection direction
Move-only operation result carrying either a value or Status.
const std::string & value() const noexcept
Returns the exact legacy or canonical compatibility spelling.
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