载入中...
搜索中...
未找到
EditingProperty.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <type_traits>
5#include <variant>
6
7namespace eve::editing {
8
10 auto found = std::find_if(properties.begin(), properties.end(),
11 [&](const PropertyDescriptor& property) { return property.path == path; });
12 if (found == properties.end()) return {};
14}
15
16namespace {
17
20 switch (type) {
21 case PropertyType::Bool: return Kind::Bool;
22 case PropertyType::Int: return Kind::Integer;
23 case PropertyType::Float: return Kind::Number;
24 case PropertyType::String: return Kind::String;
25 case PropertyType::Enum: return Kind::Enum;
26 case PropertyType::Color: return Kind::Color;
27 case PropertyType::Vec2: return Kind::Vec2;
28 case PropertyType::Vec3: return Kind::Vec3;
29 case PropertyType::Vec4: return Kind::Vec4;
31 case PropertyType::Struct: return Kind::Struct;
32 case PropertyType::AssetRef: return Kind::AssetRef;
33 case PropertyType::ObjectRef: return Kind::ObjectRef;
34 case PropertyType::Array: return Kind::Array;
35 case PropertyType::Map: return Kind::Map;
36 case PropertyType::Action: return Kind::Action;
37 case PropertyType::ReadOnlyText: return Kind::ReadOnlyText;
38 }
39 return Kind::Auto;
40}
41
44 Flag result = Flag::None;
45 if (hasPropertyFlag(flags, PropertyFlag::ReadOnly)) result = result | Flag::ReadOnly;
46 if (hasPropertyFlag(flags, PropertyFlag::Advanced)) result = result | Flag::Advanced;
47 if (hasPropertyFlag(flags, PropertyFlag::EditorOnly)) result = result | Flag::EditorOnly;
48 if (hasPropertyFlag(flags, PropertyFlag::Runtime)) result = result | Flag::Runtime;
49 if (hasPropertyFlag(flags, PropertyFlag::Transient)) result = result | Flag::Transient;
50 if (hasPropertyFlag(flags, PropertyFlag::Dangerous)) result = result | Flag::Dangerous;
51 if (hasPropertyFlag(flags, PropertyFlag::MultiEdit)) result = result | Flag::MultiEdit;
52 return result;
53}
54
55const char* legacyRule(const std::string& code) {
56 if (code == "property_access.property.type") return "editor.property.type-mismatch";
57 if (code == "property_access.property.read-only") return "editor.property.read-only";
58 if (code == "property_access.property.choice") return "editor.property.invalid-enum";
59 if (code == "property_access.property.finite") return "editor.property.finite";
60 if (code == "property_access.property.minimum") return "editor.property.below-minimum";
61 if (code == "property_access.property.maximum") return "editor.property.above-maximum";
62 return "editor.property.invalid-value";
63}
64
65template <class ResultValue>
66Result<ResultValue> validationError(const eve::Result<void>& validation, const PropertyDescriptor& descriptor) {
67 std::string rule = "editor.property.invalid-value";
68 std::string message = "Property value was rejected";
69 if (const eve::Diagnostic* error = validation.error()) {
70 rule = legacyRule(std::string(property_access::diagnosticRule(*error)));
71 message = error->message();
72 }
73 return failed<ResultValue>(Status::Rejected, eve::DiagnosticCode::PreconditionViolation, RuleId(std::move(rule)),
74 message + ": " + descriptor.path.value());
75}
76
77} // namespace
78
81 result.path = source.path.value();
82 result.displayName = source.displayNameKey;
83 result.description = source.descriptionKey;
84 result.category = source.category;
85 result.kind = convertKind(source.type);
86 result.flags = convertFlags(source.flags);
87 result.defaultValue = toPresentationValue(source.defaultValue);
88 result.numeric.minimum = source.numeric.minimum;
89 result.numeric.maximum = source.numeric.maximum;
90 result.numeric.step = source.numeric.step;
91 result.numeric.units = source.numeric.units;
92 result.numeric.precision = source.numeric.precision;
93 result.choices = source.enumItems;
94 result.presenterHint = source.presenterHint;
95 return result;
96}
97
99 return std::visit(
100 [](const auto& current) -> eve::Value {
101 using T = std::decay_t<decltype(current)>;
102 if constexpr (std::is_same_v<T, std::monostate>) {
103 return {};
104 } else if constexpr (std::is_same_v<T, Value::Array>) {
106 result.reserve(current.size());
107 for (const Value& entry : current) result.push_back(toPresentationValue(entry));
108 return result;
109 } else if constexpr (std::is_same_v<T, Value::Object>) {
111 for (const auto& [key, entry] : current) result.emplace(key, toPresentationValue(entry));
112 return result;
113 } else {
114 return eve::Value(current);
115 }
116 },
117 value.storage());
118}
119
121 return std::visit(
122 [](const auto& current) -> Value {
123 using T = std::decay_t<decltype(current)>;
124 if constexpr (std::is_same_v<T, std::monostate>) {
125 return {};
126 } else if constexpr (std::is_same_v<T, eve::Value::Array>) {
128 result.reserve(current.size());
129 for (const eve::Value& entry : current) result.push_back(toEditingValue(entry));
130 return result;
131 } else if constexpr (std::is_same_v<T, eve::Value::Object>) {
133 for (const auto& [key, entry] : current) result.emplace(key, toEditingValue(entry));
134 return result;
135 } else {
136 return Value(current);
137 }
138 },
139 value.storage());
140}
141
143 eve::Result<void> validation =
145 if (!validation.ok()) return validationError<void>(validation, descriptor);
147 return eve::editing::failed<void>(Status::Rejected, RuleId("editor.property.type-mismatch"),
148 "Value type does not match property: " + descriptor.path.value());
149 return eve::editing::applied<void>();
150}
151
152} // namespace eve::editing
std::string descriptor
SQObjectPtr key
Definition ECS.cpp:155
std::string message
DiagnosticCode code
Poco::JSON::Object::Ptr result
std::string error
Definition Package.cpp:174
std::string path
Definition PlayHost.cpp:111
Object::Ptr value
bool found
std::string_view source
double current
uint32_t flags
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
Move-only operation result carrying either a value or Status.
Definition Result.h:176
const Diagnostic * error() const noexcept
Inspect the first diagnostic, or null when none was supplied.
Definition Result.h:329
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:286
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
PropertyDescriptorLookup public API.
Dotted property path (layout.size) compared and persisted as text.
Definition EditingIds.h:78
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
eve::Value toPresentationValue(const Value &value)
To presentation value.
PropertyType
PropertyType public API.
StrongId< RuleIdTag > RuleId
Definition EditingIds.h:60
PropertyFlag
PropertyFlag public API.
Result< void > validatePropertyValue(const PropertyDescriptor &descriptor, const Value &value)
Validate property value.
Value toEditingValue(const eve::Value &value)
To editing value.
property_access::PropertyDescriptor toPresentationDescriptor(const PropertyDescriptor &source)
To presentation descriptor.
std::string_view diagnosticRule(const Diagnostic &diagnostic)
Return the projected property-access rule id, or empty when absent.
PropertyFlag
Cross-host property visibility and editing flags.
PropertyKind
Semantic property kind used by renderer-independent presenters.
Result< void > validatePropertyValue(const PropertyDescriptor &property, const Value &value)
Validate one candidate value against the shared property contract.
PropertyDescriptor public API.
std::vector< PropertyDescriptor > properties
PropertyDescriptorLookup find(const PropertyPath &path) const
Finds find.
One stable property description consumed by any UI shell.