载入中...
搜索中...
未找到
VoxelEditingCommands.cpp
浏览该文件的文档.
2
5
6#include <cstdint>
7#include <string>
8
9namespace eve::voxel_editing {
10namespace {
11
12const editing::Value* field(const editing::Value& value, const char* key) {
13 const auto* object = value.getIf<editing::Value::Object>();
14 if (!object) return nullptr;
15 const auto found = object->find(key);
16 return found == object->end() ? nullptr : &found->second;
17}
18
19const std::string* stringField(const editing::Value& value, const char* key) {
20 const editing::Value* entry = field(value, key);
21 return entry ? entry->getIf<std::string>() : nullptr;
22}
23
24bool readInt(const editing::Value& value, const char* key, int& output, bool required) {
25 const editing::Value* entry = field(value, key);
26 if (!entry) return !required;
27 if (const auto* integer = entry->getIf<int64_t>()) {
28 output = static_cast<int>(*integer);
29 return true;
30 }
31 if (const auto* real = entry->getIf<double>()) {
32 output = static_cast<int>(*real);
33 return true;
34 }
35 return false;
36}
37
38bool readBool(const editing::Value& value, const char* key, bool& output, bool required) {
39 const editing::Value* entry = field(value, key);
40 if (!entry) return !required;
41 const auto* flag = entry->getIf<bool>();
42 if (!flag) return false;
43 output = *flag;
44 return true;
45}
46
47editing::PropertyDescriptor stringProp(const char* path, const char* title) {
48 editing::PropertyDescriptor descriptor;
49 descriptor.path = editing::PropertyPath(path);
50 descriptor.displayNameKey = title;
52 return descriptor;
53}
54
55editing::PropertyDescriptor intProp(const char* path, const char* title) {
56 editing::PropertyDescriptor descriptor;
57 descriptor.path = editing::PropertyPath(path);
58 descriptor.displayNameKey = title;
60 return descriptor;
61}
62
63editing::PropertyDescriptor boolProp(const char* path, const char* title) {
64 editing::PropertyDescriptor descriptor;
65 descriptor.path = editing::PropertyPath(path);
66 descriptor.displayNameKey = title;
68 return descriptor;
69}
70
71editing::EditingCommandDescriptor makeDescriptor(const char* id, const char* displayName,
72 editing::PropertySchema schema) {
73 editing::EditingCommandDescriptor descriptor;
75 descriptor.ownerModule = "voxel_editing";
76 descriptor.displayName = displayName;
77 descriptor.category = "Voxel";
78 descriptor.automationAllowed = true;
79 descriptor.targetKinds = {"voxel-catalog", "voxel-model"};
80 descriptor.payloadSchema = std::move(schema);
81 descriptor.panelCapability = "voxel.sculpt";
82 descriptor.panelContext = "inspector";
83 return descriptor;
84}
85
86VoxelCatalogTarget* asCatalog(editing::IEditableTarget& target) {
87 return dynamic_cast<VoxelCatalogTarget*>(&target);
88}
89
90editing::SelectionSnapshot selectionFor(const VoxelCatalogTarget& target, const std::string& item) {
91 editing::SelectionSnapshot selection;
92 selection.channel = "voxel-catalog";
93 editing::SelectionItem row;
95 row.target = editing::TargetId(target.targetId());
96 row.item = editing::StableId(item);
97 row.type = "voxel.model";
98 selection.items.push_back(row);
99 selection.primary = row;
100 return selection;
101}
102
103editing::Result<editing::CommandPlan> planFrom(editing::Result<editing::DomainOperation> operation,
104 editing::Value summary) {
105 if (!operation.ok())
106 return eve::editing::failed<editing::CommandPlan>(operation.code(), editing::RuleId("voxel.editing.operation"),
107 "Voxel catalog rejected the planned edit");
108 editing::CommandPlan plan;
109 plan.operations.push_back(std::move(operation).takeValue());
110 plan.summary = std::move(summary);
111 return eve::editing::applied<editing::CommandPlan>(std::move(plan));
112}
113
114} // namespace
115
116std::vector<editing::EditingCommandDescriptor> voxelEditingCommandDescriptors() {
117 std::vector<editing::EditingCommandDescriptor> descriptors;
118 {
120 schema.typeId = "voxel.model.create.v1";
121 schema.properties.push_back(stringProp("id", "Id"));
122 schema.properties.push_back(stringProp("name", "Name"));
123 schema.properties.push_back(intProp("sizeX", "Size X"));
124 schema.properties.push_back(intProp("sizeY", "Size Y"));
125 schema.properties.push_back(intProp("sizeZ", "Size Z"));
126 descriptors.push_back(makeDescriptor("voxel.model.create.v1", "Create voxel model", std::move(schema)));
127 }
128 {
130 schema.typeId = "voxel.model.delete.v1";
131 schema.properties.push_back(stringProp("id", "Id"));
132 descriptors.push_back(makeDescriptor("voxel.model.delete.v1", "Delete voxel model", std::move(schema)));
133 }
134 {
136 schema.typeId = "voxel.model.voxel.set.v1";
137 schema.properties.push_back(stringProp("item", "Item"));
138 schema.properties.push_back(intProp("x", "X"));
139 schema.properties.push_back(intProp("y", "Y"));
140 schema.properties.push_back(intProp("z", "Z"));
141 schema.properties.push_back(boolProp("occupied", "Occupied"));
142 descriptors.push_back(makeDescriptor("voxel.model.voxel.set.v1", "Set voxel occupancy", std::move(schema)));
143 }
144 {
146 schema.typeId = "voxel.property.set.v1";
147 schema.properties.push_back(stringProp("item", "Item"));
148 schema.properties.push_back(stringProp("path", "Path"));
149 editing::PropertyDescriptor value = stringProp("value", "Value");
151 schema.properties.push_back(std::move(value));
152 descriptors.push_back(makeDescriptor("voxel.property.set.v1", "Set voxel catalog property", std::move(schema)));
153 }
154 return descriptors;
155}
156
158 auto descriptors = voxelEditingCommandDescriptors();
159 auto registered = registry.registerPlannedCommand(
161 auto* catalog = asCatalog(target);
162 const std::string* id = stringField(request.payload, "id");
163 if (!catalog || !id || id->empty())
164 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
165 editing::RuleId("voxel.editing.model-payload"),
166 "Voxel model create requires id");
167 const std::string* name = stringField(request.payload, "name");
170 model.name = name && !name->empty() ? *name : *id;
171 if (!readInt(request.payload, "sizeX", model.sizeX, false) ||
172 !readInt(request.payload, "sizeY", model.sizeY, false) ||
173 !readInt(request.payload, "sizeZ", model.sizeZ, false))
174 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
175 editing::RuleId("voxel.editing.size"),
176 "Voxel model size must be integers");
177 return planFrom(catalog->makeCreateModel(model), editing::Value::Object{{"id", *id}});
178 });
179 if (!registered.ok()) return registered;
180
181 registered = registry.registerPlannedCommand(
183 auto* catalog = asCatalog(target);
184 const std::string* id = stringField(request.payload, "id");
185 if (!catalog || !id || id->empty())
186 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
187 editing::RuleId("voxel.editing.model-payload"),
188 "Voxel model delete requires id");
189 return planFrom(catalog->makeDeleteModel(editing::ObjectId(*id)), editing::Value::Object{{"id", *id}});
190 });
191 if (!registered.ok()) return registered;
192
193 registered = registry.registerPlannedCommand(
194 descriptors[2], [](editing::IEditableTarget& target, const editing::CommandRequest& request) {
195 auto* catalog = asCatalog(target);
196 const std::string* item = stringField(request.payload, "item");
197 int x = 0, y = 0, z = 0;
198 bool occupied = true;
199 if (!catalog || !item || item->empty() || !readInt(request.payload, "x", x, true) ||
200 !readInt(request.payload, "y", y, true) || !readInt(request.payload, "z", z, true) ||
201 !readBool(request.payload, "occupied", occupied, true))
202 return eve::editing::failed<editing::CommandPlan>(
203 editing::Status::Rejected, editing::RuleId("voxel.editing.voxel-payload"),
204 "Voxel occupancy requires item, x, y, z and occupied");
205 return planFrom(catalog->makeSetVoxel(editing::ObjectId(*item), x, y, z, occupied),
206 editing::Value::Object{{"item", *item}});
207 });
208 if (!registered.ok()) return registered;
209
210 return registry.registerPlannedCommand(
211 descriptors[3], [](editing::IEditableTarget& target, const editing::CommandRequest& request) {
212 auto* catalog = asCatalog(target);
213 auto* properties = static_cast<editing::IPropertyProvider*>(
214 target.queryCapability(VoxelCatalogTarget::propertyCapabilityId()));
215 const std::string* item = stringField(request.payload, "item");
216 const std::string* path = stringField(request.payload, "path");
217 const editing::Value* value = field(request.payload, "value");
218 if (!catalog || !properties || !item || item->empty() || !path || !value)
219 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
220 editing::RuleId("voxel.editing.property-payload"),
221 "Voxel property requires item, path and value");
222 return planFrom(properties->makeSet(selectionFor(*catalog, *item), editing::PropertyPath(*path), *value,
223 editing::PropertySetMode::Absolute),
224 editing::Value::Object{{"item", *item}, {"path", *path}});
225 });
226}
227
228} // namespace eve::voxel_editing
double z
double y
double x
LogicalId target
ActionParameterOperation operation
std::string output
std::string descriptor
std::string title
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
const GltfImportRequest & request
std::map< std::string, PropertyRule > properties
bool required
std::string id
std::string path
Definition PlayHost.cpp:111
const eve::script::BindingContractRegistry & registry
Object::Ptr value
glm::mat4 model
bool found
bool occupied
Definition Shadow.cpp:155
MagicaVoxel-style sculpted models: bounded occupancy grids and hull sockets.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:313
IEditableTarget public API.
Lower-layer registration boundary implemented by an editor or runtime editing host.
std::map< std::string, Value > Object
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
StrongId< TargetIdTag > TargetId
Definition EditingIds.h:57
StrongId< ObjectIdTag > ObjectId
Definition EditingIds.h:65
StrongId< StableIdTag > StableId
Definition EditingIds.h:64
StrongId< RuleIdTag > RuleId
Definition EditingIds.h:60
StrongId< CommandIdTag > CommandId
Definition EditingIds.h:58
Result< bool > readBool(const Value::Object &object, std::string_view name)
Reads a required boolean.
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:870
editing::Result< void > registerEditingCommands(editing::IEditingCommandRegistry &registry)
Register voxel-catalog planned commands with a generic editing host.
std::vector< editing::EditingCommandDescriptor > voxelEditingCommandDescriptors()
Metadata for every Voxel planned command (shared with EditorPackage).
CommandRequest public API.
PropertyDescriptor public API.
EVENGINE_API_PLATFORM public API.
std::vector< PropertyDescriptor > properties
NodeType type
Definition Widget.h:16
One MagicaVoxel-style object: a bounded occupancy grid.