载入中...
搜索中...
未找到
UiDocumentProperty.cpp
浏览该文件的文档.
2
3#include <cmath>
4#include <optional>
5#include <utility>
6
7namespace eve::ui_editing {
8namespace {
9
10PropertyDescriptor uiProperty(const char* path, const char* label, PropertyType type,
11 EditorValue defaultValue, const char* category) {
14 result.displayNameKey = label;
15 result.category = category;
16 result.type = type;
17 result.flags = PropertyFlag::Runtime | PropertyFlag::MultiEdit;
18 result.defaultValue = std::move(defaultValue);
19 return result;
20}
21
22EditorValue component(const UiWidgetSnapshot& widget, const PropertyPath& path) {
23 if (path == PropertyPath("widget.name")) return widget.name;
24 if (path == PropertyPath("widget.text")) return widget.text;
25 if (path == PropertyPath("content.fontAsset")) return widget.content.fontAsset;
26 if (path == PropertyPath("content.fontSize")) return widget.content.fontSize;
27 if (path == PropertyPath("content.textColor"))
28 return EditorValue::Array{widget.content.textR, widget.content.textG,
29 widget.content.textB, widget.content.textA};
30 if (path == PropertyPath("content.horizontalAlign")) return widget.content.horizontalAlign;
31 if (path == PropertyPath("content.verticalAlign")) return widget.content.verticalAlign;
32 if (path == PropertyPath("content.textureAsset")) return widget.content.textureAsset;
33 if (path == PropertyPath("content.imageFit")) return widget.content.imageFit;
34 if (path == PropertyPath("content.clip")) return widget.content.clip;
35 if (path == PropertyPath("content.value")) return widget.content.value;
36 if (path == PropertyPath("content.fillDirection")) return widget.content.fillDirection;
37 if (path == PropertyPath("content.fillStretch")) return widget.content.fillStretch;
38 if (path == PropertyPath("content.fillStartDegrees")) return widget.content.fillStartDegrees;
39 if (path == PropertyPath("content.fillClockwise")) return widget.content.fillClockwise;
40 if (path == PropertyPath("content.fillInnerRadius")) return widget.content.fillInnerRadius;
41 if (path == PropertyPath("widget.visible")) return widget.visible;
42 if (path == PropertyPath("widget.enabled")) return widget.enabled;
43 if (path == PropertyPath("layout.position")) return EditorValue::Array{widget.layout.x, widget.layout.y};
44 if (path == PropertyPath("layout.size"))
45 return EditorValue::Array{widget.layout.width, widget.layout.height};
46 if (path == PropertyPath("layout.anchor"))
47 return EditorValue::Array{widget.layout.anchorX, widget.layout.anchorY};
48 if (path == PropertyPath("layout.pivot"))
49 return EditorValue::Array{widget.layout.pivotX, widget.layout.pivotY};
50 if (path == PropertyPath("style.margin"))
51 return EditorValue::Array{widget.style.marginLeft, widget.style.marginTop,
52 widget.style.marginRight, widget.style.marginBottom};
53 if (path == PropertyPath("style.padding"))
54 return EditorValue::Array{widget.style.paddingLeft, widget.style.paddingTop,
55 widget.style.paddingRight, widget.style.paddingBottom};
56 if (path == PropertyPath("style.tint"))
57 return EditorValue::Array{widget.style.tintR, widget.style.tintG,
58 widget.style.tintB, widget.style.tintA};
59 if (path == PropertyPath("style.cornerRadius")) return widget.style.cornerRadius;
60 if (path == PropertyPath("style.gap")) return widget.style.gap;
61 if (path == PropertyPath("style.flexGrow")) return widget.style.flexGrow;
62 if (path == PropertyPath("style.direction")) return widget.style.direction;
63 if (path == PropertyPath("style.align")) return widget.style.align;
64 if (path == PropertyPath("style.justify")) return widget.style.justify;
65 return {};
66}
67
68bool assign(UiWidgetSnapshot& widget, const PropertyPath& path, const EditorValue& value) {
69 if (path == PropertyPath("content.value") || path == PropertyPath("content.fillInnerRadius") ||
70 path == PropertyPath("content.fillStartDegrees")) {
71 const auto* number = value.getIf<double>();
72 if (!number || !std::isfinite(*number)) return false;
73 if (path == PropertyPath("content.fillStartDegrees")) {
74 if (std::abs(*number) > 360000.0) return false;
75 widget.content.fillStartDegrees = *number;
76 } else {
77 if (*number < 0.0 || *number > 1.0) return false;
78 if (path == PropertyPath("content.value"))
79 widget.content.value = *number;
80 else
81 widget.content.fillInnerRadius = *number;
82 }
83 return true;
84 }
85 if (path == PropertyPath("content.fillStretch") || path == PropertyPath("content.fillClockwise")) {
86 const auto* flag = value.getIf<bool>();
87 if (!flag) return false;
88 if (path == PropertyPath("content.fillStretch"))
89 widget.content.fillStretch = *flag;
90 else
91 widget.content.fillClockwise = *flag;
92 return true;
93 }
94 if (path == PropertyPath("content.fillDirection")) {
95 const auto* direction = value.getIf<std::string>();
96 if (!direction || (*direction != "right" && *direction != "left" && *direction != "down" && *direction != "up"))
97 return false;
98 widget.content.fillDirection = *direction;
99 return true;
100 }
101 if (path == PropertyPath("content.fontAsset") || path == PropertyPath("content.textureAsset")) {
102 const auto* asset = value.getIf<std::string>(); if (!asset) return false;
103 if (path == PropertyPath("content.fontAsset")) widget.content.fontAsset = *asset;
104 else widget.content.textureAsset = *asset;
105 return true;
106 }
107 if (path == PropertyPath("content.fontSize")) {
108 const auto* number = value.getIf<double>();
109 if (!number || *number <= 0.0 || *number > 512.0) return false;
110 widget.content.fontSize = *number; return true;
111 }
112 if (path == PropertyPath("content.clip")) {
113 const auto* flag = value.getIf<bool>(); if (!flag) return false;
114 widget.content.clip = *flag; return true;
115 }
116 if (path == PropertyPath("content.horizontalAlign") ||
117 path == PropertyPath("content.verticalAlign") || path == PropertyPath("content.imageFit")) {
118 const auto* text = value.getIf<std::string>(); if (!text) return false;
119 if (path == PropertyPath("content.imageFit")) {
120 if (*text != "stretch" && *text != "contain" && *text != "cover") return false;
121 widget.content.imageFit = *text;
122 } else {
123 if (*text != "start" && *text != "center" && *text != "end") return false;
124 if (path == PropertyPath("content.horizontalAlign")) widget.content.horizontalAlign = *text;
125 else widget.content.verticalAlign = *text;
126 }
127 return true;
128 }
129 if (path == PropertyPath("widget.name") || path == PropertyPath("widget.text")) {
130 const auto* text = value.getIf<std::string>();
131 if (!text || (path == PropertyPath("widget.name") && text->empty())) return false;
132 if (path == PropertyPath("widget.name")) widget.name = *text;
133 else widget.text = *text;
134 return true;
135 }
136 if (path == PropertyPath("widget.visible") || path == PropertyPath("widget.enabled")) {
137 const auto* flag = value.getIf<bool>();
138 if (!flag) return false;
139 if (path == PropertyPath("widget.visible")) widget.visible = *flag;
140 else widget.enabled = *flag;
141 return true;
142 }
143 if (path == PropertyPath("style.cornerRadius") || path == PropertyPath("style.gap") ||
144 path == PropertyPath("style.flexGrow")) {
145 const auto* number = value.getIf<double>();
146 if (!number || *number < 0.0) return false;
147 if (path == PropertyPath("style.cornerRadius")) widget.style.cornerRadius = *number;
148 else if (path == PropertyPath("style.gap")) widget.style.gap = *number;
149 else widget.style.flexGrow = *number;
150 return true;
151 }
152 if (path == PropertyPath("style.direction") || path == PropertyPath("style.align") ||
153 path == PropertyPath("style.justify")) {
154 const auto* text = value.getIf<std::string>();
155 if (!text) return false;
156 if (path == PropertyPath("style.direction")) {
157 if (*text != "row" && *text != "column") return false;
158 widget.style.direction = *text;
159 } else if (path == PropertyPath("style.align")) {
160 if (*text != "start" && *text != "center" && *text != "end" && *text != "stretch") return false;
161 widget.style.align = *text;
162 } else {
163 if (*text != "start" && *text != "center" && *text != "end" &&
164 *text != "space-between" && *text != "space-around") return false;
165 widget.style.justify = *text;
166 }
167 return true;
168 }
169 const auto* tuple = value.getIf<EditorValue::Array>();
170 if ((path == PropertyPath("style.margin") || path == PropertyPath("style.padding") ||
171 path == PropertyPath("style.tint") || path == PropertyPath("content.textColor"))) {
172 if (!tuple || tuple->size() != 4) return false;
173 double values[4];
174 for (int i = 0; i < 4; ++i) { const auto* number = (*tuple)[i].getIf<double>(); if (!number || *number < 0.0) return false; values[i] = *number; }
175 if (path == PropertyPath("style.tint") || path == PropertyPath("content.textColor")) {
176 for (double number : values) if (number > 1.0) return false;
177 if (path == PropertyPath("style.tint")) {
178 widget.style.tintR = values[0]; widget.style.tintG = values[1]; widget.style.tintB = values[2]; widget.style.tintA = values[3];
179 } else {
180 widget.content.textR = values[0]; widget.content.textG = values[1]; widget.content.textB = values[2]; widget.content.textA = values[3];
181 }
182 } else if (path == PropertyPath("style.margin")) {
183 widget.style.marginLeft = values[0]; widget.style.marginTop = values[1]; widget.style.marginRight = values[2]; widget.style.marginBottom = values[3];
184 } else {
185 widget.style.paddingLeft = values[0]; widget.style.paddingTop = values[1]; widget.style.paddingRight = values[2]; widget.style.paddingBottom = values[3];
186 }
187 return true;
188 }
189 if (!tuple || tuple->size() != 2) return false;
190 const auto* first = (*tuple)[0].getIf<double>();
191 const auto* second = (*tuple)[1].getIf<double>();
192 if (!first || !second) return false;
193 if (path == PropertyPath("layout.position")) {
194 widget.layout.x = *first;
195 widget.layout.y = *second;
196 } else if (path == PropertyPath("layout.size")) {
197 if (*first < 0.0 || *second < 0.0) return false;
198 widget.layout.width = *first;
199 widget.layout.height = *second;
200 } else if (path == PropertyPath("layout.anchor")) {
201 if (*first < 0.0 || *first > 1.0 || *second < 0.0 || *second > 1.0) return false;
202 widget.layout.anchorX = *first;
203 widget.layout.anchorY = *second;
204 } else if (path == PropertyPath("layout.pivot")) {
205 if (*first < 0.0 || *first > 1.0 || *second < 0.0 || *second > 1.0) return false;
206 widget.layout.pivotX = *first;
207 widget.layout.pivotY = *second;
208 } else {
209 return false;
210 }
211 return true;
212}
213
214} // namespace
215
217 if (selection.items.empty())
219 eve::DiagnosticCode::InvalidArgument, "UI selection is empty", "editor.ui.selection", {},
220 "editor.UiDocumentTarget"));
221 for (const SelectionItem& item : selection.items)
222 if (item.target != TargetId(id_) || !widgets_.contains(ObjectId(item.item.value())))
224 eve::DiagnosticCode::InvalidArgument, "UI selection contains an invalid widget",
225 "editor.ui.selection", {}, "editor.UiDocumentTarget"));
227}
228
231 result.typeId = "ui.widget";
232 result.properties = {
233 uiProperty("widget.name", "editor.ui.name", PropertyType::String, "Widget", "widget"),
234 uiProperty("widget.text", "editor.ui.text", PropertyType::String, "", "content"),
235 uiProperty("content.fontAsset", "editor.ui.fontAsset", PropertyType::AssetRef, "", "content"),
236 uiProperty("content.fontSize", "editor.ui.fontSize", PropertyType::Float, 16.0, "content"),
237 uiProperty("content.textColor", "editor.ui.textColor", PropertyType::Color,
238 EditorValue::Array{1.0, 1.0, 1.0, 1.0}, "content"),
239 uiProperty("content.horizontalAlign", "editor.ui.horizontalAlign", PropertyType::Enum, "start", "content"),
240 uiProperty("content.verticalAlign", "editor.ui.verticalAlign", PropertyType::Enum, "start", "content"),
241 uiProperty("content.textureAsset", "editor.ui.textureAsset", PropertyType::AssetRef, "", "content"),
242 uiProperty("content.imageFit", "editor.ui.imageFit", PropertyType::Enum, "stretch", "content"),
243 uiProperty("content.value", "editor.ui.fillValue", PropertyType::Float, 0.0, "content"),
244 uiProperty("content.fillDirection", "editor.ui.fillDirection", PropertyType::Enum, "right", "content"),
245 uiProperty("content.fillStretch", "editor.ui.fillStretch", PropertyType::Bool, false, "content"),
246 uiProperty("content.fillStartDegrees", "editor.ui.fillStartDegrees", PropertyType::Float, -90.0, "content"),
247 uiProperty("content.fillClockwise", "editor.ui.fillClockwise", PropertyType::Bool, true, "content"),
248 uiProperty("content.fillInnerRadius", "editor.ui.fillInnerRadius", PropertyType::Float, 0.0, "content"),
249 uiProperty("content.clip", "editor.ui.clip", PropertyType::Bool, true, "content"),
250 uiProperty("widget.visible", "editor.ui.visible", PropertyType::Bool, true, "widget"),
251 uiProperty("widget.enabled", "editor.ui.enabled", PropertyType::Bool, true, "widget"),
252 uiProperty("layout.position", "editor.ui.position", PropertyType::Vec2, EditorValue::Array{0.0, 0.0}, "layout"),
253 uiProperty("layout.size", "editor.ui.size", PropertyType::Vec2, EditorValue::Array{0.0, 0.0}, "layout"),
254 uiProperty("layout.anchor", "editor.ui.anchor", PropertyType::Vec2, EditorValue::Array{0.0, 0.0}, "layout"),
255 uiProperty("layout.pivot", "editor.ui.pivot", PropertyType::Vec2, EditorValue::Array{0.0, 0.0}, "layout"),
256 uiProperty("style.margin", "editor.ui.margin", PropertyType::Vec4, EditorValue::Array{0.0, 0.0, 0.0, 0.0},
257 "style"),
258 uiProperty("style.padding", "editor.ui.padding", PropertyType::Vec4, EditorValue::Array{0.0, 0.0, 0.0, 0.0},
259 "style"),
260 uiProperty("style.tint", "editor.ui.tint", PropertyType::Color, EditorValue::Array{1.0, 1.0, 1.0, 1.0},
261 "style"),
262 uiProperty("style.cornerRadius", "editor.ui.cornerRadius", PropertyType::Float, 0.0, "style"),
263 uiProperty("style.gap", "editor.ui.gap", PropertyType::Float, 0.0, "flex"),
264 uiProperty("style.flexGrow", "editor.ui.flexGrow", PropertyType::Float, 0.0, "flex"),
265 uiProperty("style.direction", "editor.ui.direction", PropertyType::Enum, "row", "flex"),
266 uiProperty("style.align", "editor.ui.align", PropertyType::Enum, "start", "flex"),
267 uiProperty("style.justify", "editor.ui.justify", PropertyType::Enum, "start", "flex")};
268 return result;
269}
270
272 const PropertyPath& path) const {
273 std::optional<EditorValue> common;
274 for (const SelectionItem& item : selection.items) {
275 if (item.target != TargetId(id_)) return {};
276 const auto found = widgets_.find(ObjectId(item.item.value()));
277 if (found == widgets_.end()) return {};
278 EditorValue value = component(found->second, path);
279 if (value.type() == EditorValue::Type::Null) return {};
280 if (!common) common = value;
281 else if (*common != value) return {PropertyReadState::Mixed, {}, {}};
282 }
283 return common ? PropertyReadResult{PropertyReadState::Value, *common, {}} : PropertyReadResult{};
284}
285
287 const PropertyPath& path,
288 const EditorValue& value,
289 PropertySetMode mode) const {
290 if (mode == PropertySetMode::Reset) return makeReset(selection, path);
291 if (mode != PropertySetMode::Absolute || selection.items.empty())
292 return eve::editing::failed<DomainOperation>(EditorStatus::Unsupported, RuleId("editor.ui.property-mode"),
293 "UI properties require a non-empty absolute assignment");
294 if (!schema(selection).find(path))
295 return eve::editing::failed<DomainOperation>(EditorStatus::Unsupported, RuleId("editor.ui.property-path"),
296 "Unknown UI property: " + path.value());
298 EditorValue::Array inverse;
300 for (const SelectionItem& item : selection.items) {
301 if (item.target != TargetId(id_))
302 return eve::editing::failed<DomainOperation>(EditorStatus::Rejected, RuleId("editor.ui.property-target"),
303 "UI selection spans another target");
304 const auto found = widgets_.find(ObjectId(item.item.value()));
305 if (found == widgets_.end())
306 return eve::editing::failed<DomainOperation>(EditorStatus::NotFound, RuleId("editor.ui.widget-not-found"),
307 "Selected UI widget does not exist");
309 if (!assign(changed, path, value))
310 return eve::editing::failed<DomainOperation>(EditorStatus::Rejected, RuleId("editor.ui.property-value"),
311 "UI property value is invalid for " + path.value());
312 payload.push_back(widgetValue(changed));
313 inverse.push_back(widgetValue(found->second));
314 operation.affectedObjects.push_back({TargetId(id_), item.item.value(), 0});
315 }
316 operation.type = "ui.widget.multi-replace.v1";
317 operation.target = TargetId(id_);
318 operation.payload = EditorValue(std::move(payload));
319 operation.inverse = EditorValue(std::move(inverse));
320 operation.hasInverse = true;
321 operation.affectedProperties.push_back(path.value());
322 operation.mergeKey = "ui.selection:" + path.value();
323 return eve::editing::applied<DomainOperation>(std::move(operation));
324}
325
327 const PropertyPath& path) const {
328 auto property = schema(selection).find(path);
329 if (!property)
330 return eve::editing::failed<DomainOperation>(EditorStatus::Unsupported, RuleId("editor.ui.property-path"),
331 "Unknown UI property: " + path.value());
332 return makeSet(selection, path, property->defaultValue, PropertySetMode::Absolute);
333}
334
335} // namespace eve::ui_editing
ActionParameterOperation operation
Value::Object payload
bool changed
std::int32_t second
std::int32_t first
std::string label
Poco::JSON::Object::Ptr result
double defaultValue
std::string path
Definition PlayHost.cpp:111
std::set< std::string > values
Object::Ptr value
RoadLaneDirection direction
double number
bool found
std::string text
const char * category
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
const std::string & value() const noexcept
Returns the exact legacy or canonical compatibility spelling.
Definition Identity.h:365
Revision revisionValue() const noexcept
Return the revision as the shared protocol revision type.
Dotted property path (layout.size) compared and persisted as text.
Definition EditingIds.h:78
bool empty() const noexcept
Empty.
Definition EditingIds.h:92
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
const T * getIf() const
Return an immediately borrowed typed value.
PropertyReadResult read(const SelectionSnapshot &selection, const PropertyPath &path) const override
Reads .
PropertySchema schema(const SelectionSnapshot &selection) const override
Schema.
Result< DomainOperation > makeReset(const SelectionSnapshot &selection, const PropertyPath &path) const override
Make reset.
Result< DomainOperation > makeSet(const SelectionSnapshot &selection, const PropertyPath &path, const EditorValue &value, PropertySetMode mode) const override
Make set.
eve::Result< eve::Revision > currentRevision(const SelectionSnapshot &selection) const override
Current revision.
editing::PropertyDescriptor PropertyDescriptor
editing::PropertyPath PropertyPath
editing::Value EditorValue
Definition UiDocument.h:46
DomainOperation public API.
PropertyReadResult public API.
EVENGINE_API_PLATFORM public API.
PropertyDescriptorLookup find(const PropertyPath &path) const
Finds find.
SelectionItem public API.
SelectionSnapshot public API.
std::vector< SelectionItem > items
Immutable widget snapshot used across editor/runtime boundaries.
Definition UiDocument.h:102
std::string widget