载入中...
搜索中...
未找到
BiomeEditingCommands.cpp
浏览该文件的文档.
3
6
7#include <cmath>
8#include <cstdint>
9#include <string>
10
12namespace {
13
14const editing::Value* field(const editing::Value& value, const char* key) {
15 const auto* object = value.getIf<editing::Value::Object>();
16 if (!object) return nullptr;
17 const auto found = object->find(key);
18 return found == object->end() ? nullptr : &found->second;
19}
20
21const std::string* stringField(const editing::Value& value, const char* key) {
22 const editing::Value* entry = field(value, key);
23 return entry ? entry->getIf<std::string>() : nullptr;
24}
25
26bool readNumber(const editing::Value& value, const char* key, double& output, bool required) {
27 const auto* entry = field(value, key);
28 if (!entry) return !required;
29 auto number = editing::numericValue(*entry);
30 if (!number) return false;
31 output = number.value();
32 return std::isfinite(output);
33}
34
35bool readInt(const editing::Value& value, const char* key, int& output, bool required) {
36 if (!field(value, key)) return !required;
37 double parsed = 0.0;
38 if (!readNumber(value, key, parsed, true) || parsed < -100000.0 || parsed > 100000.0) return false;
39 output = static_cast<int>(parsed);
40 return true;
41}
42
43bool readBool(const editing::Value& value, const char* key, bool& output, bool required) {
44 const editing::Value* entry = field(value, key);
45 if (!entry) return !required;
46 const auto* flag = entry->getIf<bool>();
47 if (!flag) return false;
48 output = *flag;
49 return true;
50}
51
52editing::Result<void> addCommand(editing::IEditingCommandRegistry& registry, const char* id, const char* displayName,
56 descriptor.ownerModule = "biome_editing";
57 descriptor.displayName = displayName;
58 descriptor.category = "Biome";
59 descriptor.automationAllowed = true;
60 return registry.registerPlannedCommand(std::move(descriptor), std::move(planner));
61}
62
64 return dynamic_cast<BiomeDocumentTarget*>(&target);
65}
66
67editing::SelectionSnapshot selectionFor(const BiomeDocumentTarget& target, const std::string& item,
68 const std::string& type) {
70 selection.channel = "biome";
73 row.target = editing::TargetId(target.targetId());
74 row.item = editing::StableId(item);
75 row.type = type;
76 selection.items.push_back(row);
77 selection.primary = row;
78 return selection;
79}
80
82 editing::Value summary) {
83 if (!operation.ok())
84 return eve::editing::failed<editing::CommandPlan>(operation.code(), editing::RuleId("biome.editing.operation"),
85 "Biome target rejected the planned edit");
87 plan.operations.push_back(std::move(operation).takeValue());
88 plan.summary = std::move(summary);
89 return eve::editing::applied<editing::CommandPlan>(std::move(plan));
90}
91
92} // namespace
93
95 auto registered = addCommand(
96 registry, "biome.layer.create.v1", "Create biome layer",
98 auto* biome = asBiome(target);
99 const std::string* id = stringField(request.payload, "id");
100 const std::string* spatial = stringField(request.payload, "spatial");
101 if (!biome || !id || id->empty() || !spatial || spatial->empty())
102 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
103 editing::RuleId("biome.editing.layer-payload"),
104 "Biome layer create requires id and spatial asset");
105 const std::string* name = stringField(request.payload, "name");
108 layer.name = name && !name->empty() ? *name : *id;
109 layer.spatialAsset = *spatial;
110 layer.priority = 0;
111 layer.density = 1.f;
112 double density = 1.0;
113 if (!readInt(request.payload, "priority", layer.priority, false) ||
114 !readNumber(request.payload, "density", density, false))
115 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
116 editing::RuleId("biome.editing.layer-number"),
117 "Biome layer priority and density must be numbers");
118 layer.density = static_cast<float>(density);
119 return planFrom(biome->makeCreateLayer(layer), editing::Value::Object{{"id", *id}});
120 });
121 if (!registered.ok()) return registered;
122
123 registered = addCommand(
124 registry, "biome.asset.create.v1", "Create biome asset",
126 auto* biome = asBiome(target);
127 const std::string* layer = stringField(request.payload, "layer");
128 const std::string* id = stringField(request.payload, "id");
129 const std::string* asset = stringField(request.payload, "asset");
130 if (!biome || !layer || layer->empty() || !id || id->empty() || !asset || asset->empty())
131 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
132 editing::RuleId("biome.editing.asset-payload"),
133 "Biome asset create requires layer, id and asset");
134 BiomeAssetValue entry;
135 entry.id = editing::ObjectId(*id);
136 entry.asset = *asset;
137 double weight = 1.0, minScale = 1.0, maxScale = 1.0;
138 if (!readNumber(request.payload, "weight", weight, false) ||
139 !readNumber(request.payload, "minScale", minScale, false) ||
140 !readNumber(request.payload, "maxScale", maxScale, false) ||
141 !readBool(request.payload, "randomYaw", entry.randomYaw, false))
142 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
143 editing::RuleId("biome.editing.asset-number"),
144 "Biome asset weight, scale and yaw must be valid");
145 entry.weight = static_cast<float>(weight);
146 entry.minScale = static_cast<float>(minScale);
147 entry.maxScale = static_cast<float>(maxScale);
148 return planFrom(biome->makeCreateAsset(editing::ObjectId(*layer), entry),
149 editing::Value::Object{{"id", *id}, {"layer", *layer}});
150 });
151 if (!registered.ok()) return registered;
152
153 return addCommand(
154 registry, "biome.property.set.v1", "Set biome property",
155 [](editing::IEditableTarget& target, const editing::CommandRequest& request) {
156 auto* biome = asBiome(target);
157 auto* properties = static_cast<editing::IPropertyProvider*>(
158 target.queryCapability(BiomeDocumentTarget::propertyCapabilityId()));
159 const std::string* item = stringField(request.payload, "item");
160 const std::string* type = stringField(request.payload, "type");
161 const std::string* path = stringField(request.payload, "path");
162 const editing::Value* value = field(request.payload, "value");
163 if (!biome || !properties || !item || item->empty() || !type || !path || !value)
164 return eve::editing::failed<editing::CommandPlan>(editing::Status::Rejected,
165 editing::RuleId("biome.editing.property-payload"),
166 "Biome property requires item, type, path and value");
167 return planFrom(properties->makeSet(selectionFor(*biome, *item, *type), editing::PropertyPath(*path),
168 *value, editing::PropertySetMode::Absolute),
169 editing::Value::Object{{"item", *item}, {"path", *path}});
170 });
171}
172
173} // namespace eve::biome_editing
LogicalId target
ActionParameterOperation operation
std::string output
std::string descriptor
eve::action::ActionSpatialBinding spatial
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
float density
const GltfImportRequest & request
int biome
std::map< std::string, PropertyRule > properties
bool required
std::string id
TileLayer * layer
std::string path
Definition PlayHost.cpp:111
const eve::script::BindingContractRegistry & registry
Object::Ptr value
double number
bool found
double weight
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
Revisioned BiomeRules asset.
Definition BiomeTarget.h:64
IEditableTarget public API.
Lower-layer registration boundary implemented by an editor or runtime editing host.
Deterministic owning value tree shared by authoring hosts.
std::map< std::string, Value > Object
const T * getIf() const
Return an immediately borrowed typed value.
editing::Result< void > registerEditingCommands(editing::IEditingCommandRegistry &registry)
Register Biome-owned planned commands with a generic editing host.
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
std::function< Result< CommandPlan >(IEditableTarget &, const CommandRequest &)> EditingCommandPlanner
Side-effect-free planner invoked with the resolved target.
StrongId< StableIdTag > StableId
Definition EditingIds.h:64
StrongId< CommandIdTag > CommandId
Definition EditingIds.h:58
Result< double > numericValue(const Value &value)
Read an Integer or Number as an owning double without applying domain validation.
Stable authored biome layer referencing a spatial asset.
Definition BiomeTarget.h:51
CommandPlan public API.
CommandRequest public API.
Domain-owned metadata for one planned editing command.
SelectionItem public API.
SelectionSnapshot public API.
std::vector< SelectionItem > items
std::optional< SelectionItem > primary
NodeType type
Definition Widget.h:16