载入中...
搜索中...
未找到
MaterialEditor.cpp
浏览该文件的文档.
2
3#include <stdexcept>
4#include <utility>
5#include <vector>
6
8using namespace eve::editing;
10using editing::Status;
12using editing::Value;
13
15 : target_(std::move(targetId)), authority_(&target_), transactions_(&authority_) {
16 if (target_.targetId().value().empty()) throw std::invalid_argument("Material target id is required");
17}
18
19SelectionSnapshot MaterialEditor::selection() const {
21 result.channel = "asset";
22 result.items.push_back(
23 {SelectionDomain::Asset, target_.targetId(), StableId(target_.targetId().value()), "graphics.material"});
24 result.primary = result.items.front();
25 return result;
26}
27
29 auto candidate = workspace;
30 struct Panel {
31 const char* id;
32 const char* title;
33 const char* region;
34 };
35 constexpr Panel panels[] = {{"toolbar", "Material Studio", "top"},
36 {"preview", "Material Preview", "center"},
37 {"inspector", "Material Properties", "right"}};
38 for (const auto& panel : panels) {
40 descriptor.id = panel.id;
41 descriptor.title = panel.title;
42 descriptor.region = panel.region;
43 descriptor.capability = std::string_view(panel.id) == "preview" ? "scene.viewport.3d" : "property.material";
44 auto registered = candidate.registerPanel(std::move(descriptor));
45 if (!registered.ok()) return Result<void>::failure(registered.status());
46 }
47 auto activated = candidate.activatePanel(StableId("preview"));
48 if (!activated.ok()) return Result<void>::failure(activated.status());
49 workspace = std::move(candidate);
50 return applied<void>();
51}
52
54 return setProperties({{std::string(path), value}});
55}
56
58 auto candidate = target_;
59 std::vector<DomainOperation> operations;
60 const auto metadata = target_.schema(selection());
61 for (const auto& [path, input] : properties) {
62 auto value = input;
63 const auto descriptor = metadata.find(PropertyPath(path));
64 if (descriptor && descriptor->type == PropertyType::Float) {
65 if (const auto* integer = value.getIf<std::int64_t>()) value = static_cast<double>(*integer);
66 }
67 if (descriptor && (descriptor->type == PropertyType::Color || descriptor->type == PropertyType::Vec2 ||
68 descriptor->type == PropertyType::Vec3 || descriptor->type == PropertyType::Vec4)) {
69 if (auto* components = value.getIf<Value::Array>()) {
70 for (auto& component : *components)
71 if (const auto* integer = component.getIf<std::int64_t>())
72 component = static_cast<double>(*integer);
73 }
74 }
75 auto current = target_.read(selection(), PropertyPath(path));
76 if (current.state == PropertyReadState::Value && current.value == value) continue;
77 auto operation = candidate.makeSet(selection(), PropertyPath(path), value, PropertySetMode::Absolute);
78 if (!operation.ok()) return Result<void>::failure(operation.status());
79 auto staged = candidate.applyDomainOperation(operation.value());
80 if (!staged.ok()) return staged;
81 operations.push_back(std::move(operation).takeValue());
82 }
83 const auto diagnostics = candidate.validate();
84 for (const auto& diagnostic : diagnostics) {
85 if (diagnostic.severity() == DiagnosticSeverity::Error)
86 return Result<void>::failure(eve::Status(Status::Rejected, diagnostics));
87 }
88 if (operations.empty()) return noOp(diagnostics);
90 spec.id = TransactionId("material.tx." + std::to_string(++sequence_));
91 spec.label = "Edit material";
92 spec.target = target_.targetId();
93 spec.baseRevision = target_.revision();
94 auto begun = transactions_.begin(std::move(spec));
95 if (!begun.ok()) return Result<void>::failure(begun.status());
96 for (auto& operation : operations) {
97 auto appended = transactions_.append(std::move(operation));
98 if (!appended.ok()) {
99 auto discarded = transactions_.discard();
100 if (!discarded.ok()) return discarded;
101 return appended;
102 }
103 }
104 auto committed = transactions_.commit();
105 if (!committed.ok()) {
106 auto discarded = transactions_.discard();
107 if (!discarded.ok()) return discarded;
108 return Result<void>::failure(committed.status());
109 }
110 return applied<void>(diagnostics);
111}
112
113Result<Value> MaterialEditor::read(std::string_view path) const {
114 auto property = target_.read(selection(), PropertyPath(std::string(path)));
115 if (property.state != PropertyReadState::Value)
116 return failed<Value>(Status::NotFound, RuleId("editor.material.property"),
117 "Unknown material property: " + std::string(path));
118 return applied<Value>(std::move(property.value));
119}
120
122 const auto metadata = target_.schema(selection());
124 for (const auto& field : metadata.properties) {
125 Value::Object item{{"path", field.path.value()},
126 {"label", field.displayNameKey},
127 {"category", field.category},
128 {"type", static_cast<std::int64_t>(field.type)},
129 {"defaultValue", field.defaultValue}};
131 for (const auto& choice : field.enumItems) choices.emplace_back(choice);
132 item["choices"] = std::move(choices);
133 if (field.numeric.minimum) item["minimum"] = *field.numeric.minimum;
134 if (field.numeric.maximum) item["maximum"] = *field.numeric.maximum;
135 properties.emplace_back(std::move(item));
136 }
137 return Value::Object{{"typeId", metadata.typeId},
138 {"version", static_cast<std::int64_t>(metadata.version)},
139 {"properties", std::move(properties)}};
140}
141
142Value MaterialEditor::snapshot() const { return target_.snapshotValue(); }
143
145 auto candidate = target_;
146 auto loaded = candidate.loadSnapshot(snapshot);
147 if (!loaded.ok()) return loaded;
148 const auto normalized = candidate.snapshotValue();
149 return setProperties(*normalized.getIf<Value::Object>()->at("properties").getIf<Value::Object>());
150}
151
154
155} // namespace eve::material_editor
ActionParameterOperation operation
std::string descriptor
std::string title
bool committed
EvpackChunkInput input
Definition Evpack.cpp:170
ScriptFieldRegion region
std::map< std::string, PropertyRule > properties
std::set< std::string > choices
std::string id
Poco::JSON::Object::Ptr result
std::string path
Definition PlayHost.cpp:111
std::unordered_set< std::string > loaded
Definition Plugins.cpp:24
std::uint32_t targetId
Object::Ptr value
double current
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Structured status and zero or more diagnostics for an operation.
Definition Status.h:70
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:313
std::uint64_t revision() const override
Return the current revision.
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
Composition model shared by project-specific developer and runtime editors.
Result< TransactionReceipt > commit() override
Preflight and commit all active operations through the authority.
Result< TransactionId > begin(TransactionSpec specification) override
Begin a transaction with a stable id and base revision.
Result< TransactionReceipt > undo() override
Compensate the most recent committed transaction.
Result< void > discard() override
Discard the active, not-yet-committed operation list.
Result< TransactionReceipt > redo() override
Reapply the most recently compensated transaction.
Result< void > append(DomainOperation operation) override
Append an operation to the active transaction without applying it.
PropertySchema schema(const SelectionSnapshot &selection) const override
Schema.
PropertyReadResult read(const SelectionSnapshot &selection, const PropertyPath &path) const override
Reads .
EditorValue snapshotValue() const override
Capture deterministic content suitable for DocumentService persistence.
TargetId targetId() const override
Target id.
Result< void > loadSnapshot(const EditorValue &snapshot)
Replace content when opening a persisted material document.
editing::Value snapshot() const
Return the canonical versioned material snapshot.
editing::Result< void > setProperties(const editing::Value::Object &properties)
Apply a property patch as one undo record; invalid patches leave document and history unchanged.
editing::Result< void > restore(const editing::Value &snapshot)
Import a canonical versioned snapshot as one undoable edit; failure preserves state....
MaterialEditor(std::string targetId)
Create a document with the canonical material defaults.
editing::Result< void > setProperty(std::string_view path, const editing::Value &value)
Set one schema property through the same atomic patch path.
editing::Value schema() const
Return detached schema metadata for custom inspectors.
editing::Result< editor::TransactionReceipt > redo()
Redo the latest undone patch.
editing::Result< editing::Value > read(std::string_view path) const
Read a property; unknown paths return NotFound rather than a default value.
editing::Result< editor::TransactionReceipt > undo()
Undo the latest patch.
editing::Result< void > configureWorkspace(editor::EditorWorkspace &workspace) const
Install default panels atomically; callers may customize the workspace afterwards.
StrongId< TransactionIdTag > TransactionId
Definition EditingIds.h:62
StrongId< StableIdTag > StableId
Definition EditingIds.h:64
StrongId< RuleIdTag > RuleId
Definition EditingIds.h:60
Result< void > noOp(std::vector< Diagnostic > diagnostics={})
Construct a successful void result for an operation that made no change.
StatusCode
Stable outcome category for an operation.
Definition Status.h:28
PropertyDescriptorLookup find(const PropertyPath &path) const
Finds find.
SelectionSnapshot public API.
TransactionSpec public API.
UI-neutral description of one composable editor panel.