载入中...
搜索中...
未找到
EditorSimulationPreview.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <set>
5#include <utility>
6#include "common/Exception.h"
10
11// EditorSimulationPreview.cpp
12namespace eve::editor {
13
15 if (!std::isfinite(seconds) || seconds <= 0.0 || seconds > 1.0)
16 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.simulation.invalid-fixed-delta"),
17 "Simulation preview delta must be finite and within (0, 1] seconds");
18 fixedDelta_ = seconds;
19 return eve::editing::applied<void>();
20}
21
22Result<SimulationPreviewFrame> SimulationPreviewController::advance(IEditorSimulationBackend& backend,
23 std::uint64_t tick) const {
24 auto stepped = backend.step(tick, fixedDelta_);
25 if (!stepped.ok()) return Result<SimulationPreviewFrame>::failure(stepped.status());
26 auto captured = backend.capture();
27 if (!captured.ok()) return Result<SimulationPreviewFrame>::failure(captured.status());
28 std::set<std::string> ids;
29 for (const auto& object : captured.value()) {
30 if (object.object.empty() || !ids.insert(object.object).second)
31 return eve::editing::failed<SimulationPreviewFrame>(
32 EditorStatus::Conflict, RuleId("editor.simulation.unstable-object-id"),
33 "Simulation preview samples require unique stable object ids");
34 const double values[]{object.positionX, object.positionY, object.positionZ, object.rotationX,
35 object.rotationY, object.rotationZ, object.rotationW};
36 if (std::any_of(std::begin(values), std::end(values), [](double value) { return !std::isfinite(value); }))
37 return eve::editing::failed<SimulationPreviewFrame>(EditorStatus::Failed,
38 RuleId("editor.simulation.nonfinite-sample"),
39 "Simulation preview produced a non-finite transform");
40 }
41 std::sort(captured.value().begin(), captured.value().end(),
42 [](const auto& a, const auto& b) { return a.object < b.object; });
43 return eve::editing::applied<SimulationPreviewFrame>(
44 {tick, static_cast<double>(tick) * fixedDelta_, std::move(captured.value())});
45}
46
48 if (!previewBackend_) previewBackend_ = source.cloneForPreview();
49 if (!previewBackend_)
50 return eve::editing::failed<SimulationPreviewFrame>(EditorStatus::Unsupported,
51 RuleId("editor.simulation.clone-unsupported"),
52 "Simulation backend cannot create an isolated preview");
53 auto result = advance(*previewBackend_, tick_ + 1);
54 if (result.ok()) ++tick_;
55 return result;
56}
57
59 tick_ = 0;
60 previewBackend_.reset();
61}
62
64 if (paused_) {
66 SimulationPreviewFrame{tick_, static_cast<double>(tick_) * fixedDelta_, {}},
67 eve::Status::success(EditorStatus::NoOp));
68 }
69 return singleStep(source);
70}
71
73 std::uint64_t sourceRevision, int steps, int maxSteps) const {
75 result.sourceRevision = sourceRevision;
76 result.fixedDelta = fixedDelta_;
77 if (steps < 0 || maxSteps <= 0 || steps > maxSteps) {
78 result.status = EditorStatus::Rejected;
79 result.diagnostics.push_back(eve::editing::ruleDiagnostic(
80 eve::DiagnosticCode::InvalidArgument, RuleId("editor.simulation.bake-budget"), DiagnosticSeverity::Error,
81 "Simulation bake step count exceeds the configured budget"));
82 return result;
83 }
84 auto backend = source.cloneForPreview();
85 if (!backend) {
86 result.status = EditorStatus::Unsupported;
87 result.diagnostics.push_back(eve::editing::ruleDiagnostic(
88 eve::DiagnosticCode::Unsupported, RuleId("editor.simulation.clone-unsupported"), DiagnosticSeverity::Error,
89 "Simulation backend cannot create an isolated preview"));
90 return result;
91 }
92 auto initial = backend->capture();
93 if (!initial.ok()) {
94 result.status = initial.code();
95 result.diagnostics = initial.diagnostics();
96 return result;
97 }
98 std::sort(initial.value().begin(), initial.value().end(),
99 [](const auto& a, const auto& b) { return a.object < b.object; });
100 result.frames.push_back({0, 0.0, std::move(initial.value())});
101 for (int index = 1; index <= steps; ++index) {
102 auto frame = advance(*backend, static_cast<std::uint64_t>(index));
103 if (!frame.ok()) {
104 result.status = frame.code();
105 result.diagnostics = frame.diagnostics();
106 return result;
107 }
108 result.frames.push_back(std::move(frame.value()));
109 }
110 result.status = EditorStatus::Applied;
111 return result;
112}
113
114} // namespace eve::editor
115
116// EditorToolbar.cpp
117namespace eve::editor {
118
120 tools_.clear();
121 active_.clear();
122}
123
124int EditorToolbar::findIndex(const std::string& id) const {
125 for (int i = 0; i < static_cast<int>(tools_.size()); ++i) {
126 if (tools_[i].id == id) return i;
127 }
128 return -1;
129}
130
131void EditorToolbar::addTool(const std::string& id, const std::string& label) {
132 if (id.empty()) throw Exception("EditorToolbar::addTool: empty id");
133 if (findIndex(id) >= 0) throw Exception("EditorToolbar::addTool: duplicate id");
134 tools_.push_back(Tool{id, label, "", ""});
135 if (active_.empty()) active_ = id;
136}
137
138void EditorToolbar::setIcon(const std::string& id, const std::string& icon) {
139 int i = findIndex(id);
140 if (i < 0) throw Exception("EditorToolbar::setIcon: unknown id");
141 tools_[i].icon = icon;
142}
143
144void EditorToolbar::setShortcut(const std::string& id, const std::string& key) {
145 int i = findIndex(id);
146 if (i < 0) throw Exception("EditorToolbar::setShortcut: unknown id");
147 tools_[i].shortcut = key;
148}
149
150bool EditorToolbar::setActive(const std::string& id) {
151 if (findIndex(id) < 0) return false;
152 active_ = id;
153 return true;
154}
155
156bool EditorToolbar::matchShortcut(const std::string& key) {
157 for (const auto& t : tools_) {
158 if (!t.shortcut.empty() && t.shortcut == key) {
159 active_ = t.id;
160 return true;
161 }
162 }
163 return false;
164}
165
166std::string EditorToolbar::getToolId(int index) const {
167 if (index < 0 || index >= static_cast<int>(tools_.size())) throw Exception("EditorToolbar::getToolId: bad index");
168 return tools_[index].id;
169}
170
171std::string EditorToolbar::getToolLabel(int index) const {
172 if (index < 0 || index >= static_cast<int>(tools_.size()))
173 throw Exception("EditorToolbar::getToolLabel: bad index");
174 return tools_[index].label;
175}
176
177std::string EditorToolbar::getToolIcon(int index) const {
178 if (index < 0 || index >= static_cast<int>(tools_.size())) throw Exception("EditorToolbar::getToolIcon: bad index");
179 return tools_[index].icon;
180}
181
182std::string EditorToolbar::getToolShortcut(int index) const {
183 if (index < 0 || index >= static_cast<int>(tools_.size()))
184 throw Exception("EditorToolbar::getToolShortcut: bad index");
185 return tools_[index].shortcut;
186}
187
188} // namespace eve::editor
189
190// EditorTransactionService.cpp
191namespace eve::editor {
192namespace {
193
194TransactionState transactionState(eve::transaction::CoordinatorState state) noexcept {
195 switch (state) {
196 case eve::transaction::CoordinatorState::Committed: return TransactionState::Committed;
197 case eve::transaction::CoordinatorState::Compensated: return TransactionState::RolledBack;
200 case eve::transaction::CoordinatorState::PartiallyCommitted: return TransactionState::Failed;
201 case eve::transaction::CoordinatorState::RolledBack: return TransactionState::RolledBack;
202 }
203 return TransactionState::Failed;
204}
205
206TransactionReceipt projectRecord(const EditorTransactionRecord& record) {
208 result.id = record.specification.id;
209 result.state = transactionState(record.coordinator.state);
210 if (record.authorityReceipt) {
211 result.beforeRevision = record.authorityReceipt->beforeRevision;
212 result.afterRevision = record.authorityReceipt->afterRevision;
213 result.affectedObjects = record.authorityReceipt->affectedObjects;
214 result.diagnostics = record.authorityReceipt->diagnostics;
215 result.authorityReceipt = record.authorityReceipt->authorityReceipt;
216 }
217 return result;
218}
219
220} // namespace
221
222Result<TransactionId> LocalTransactionBackend::project(eve::Result<TransactionId>&& result) {
223 // Result is eve::Result; no projection envelope exists at this boundary.
224 // Named rvalue-ref parameters are lvalues; move explicitly (not a local NRVO candidate).
225 return std::move(result);
226}
227
228Result<void> LocalTransactionBackend::project(eve::Result<void>&& result) { return std::move(result); }
229
230Result<TransactionReceipt> LocalTransactionBackend::project(eve::Result<EditorTransactionRecord>&& result) {
231 if (!result.ok()) return Result<TransactionReceipt>::failure(result.status());
232 EditorTransactionRecord record = std::move(result).takeValue();
233 return eve::editing::applied<TransactionReceipt>(projectRecord(record));
234}
235
236Result<EditorDryRunReport> LocalTransactionBackend::project(eve::Result<EditorDryRunReport>&& result) {
237 return std::move(result);
238}
239
241 return project(consumer_.setAuthority(authority));
242}
243
245 if (specification.id.empty() || specification.target.empty())
246 return eve::editing::failed<TransactionId>(EditorStatus::Rejected,
247 RuleId("editor.transaction.invalid-specification"),
248 "Transaction id and target are required");
249 return project(consumer_.begin(std::move(specification)));
250}
251
255
257
259
260Result<void> LocalTransactionBackend::discard() { return project(consumer_.discard()); }
261
263
265
267
269
270} // namespace eve::editor
271
272// EditorTransactions.cpp
273namespace eve::editor {
274
276 return consumer_.begin(std::move(specification));
277}
278
280 return consumer_.beginLegacy(std::move(label));
281}
282
283eve::Result<void> EditorTransactions::append(std::unique_ptr<IEditCommand> command) {
284 return consumer_.appendPreview(std::move(command));
285}
286
288
290
292
294
295bool EditorTransactions::begin(const std::string& name) {
297 return result.ok();
298}
299bool EditorTransactions::execute(std::unique_ptr<IEditCommand> command) {
300 auto result = append(std::move(command));
301 return result.ok();
302}
304 auto result = commitTransaction();
305 return result.ok();
306}
309 return result.ok();
310}
312 auto result = undoTransaction();
313 return result.ok();
314}
316 auto result = redoTransaction();
317 return result.ok();
318}
319void EditorTransactions::clear() { consumer_.clear(); }
320
321} // namespace eve::editor
ActionParameterOperation operation
SQObjectPtr key
Definition ECS.cpp:155
ScriptComponentRecord record
Definition ECS.cpp:228
std::string name
Definition ECS.cpp:154
Icon icon
Definition Icons.cpp:11
std::string label
std::string id
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanRenderBackend backend
ssq::Object frame
std::set< std::string > values
Object::Ptr value
int steps
float t
std::string_view source
SimulationTick tick
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
bool empty() const noexcept
True when no compatibility spelling has been assigned.
Definition Identity.h:377
Final validation and commit boundary for editor mutations.
std::string getToolLabel(int index) const
Returns the tool label.
std::string getToolId(int index) const
Returns the tool id.
void setIcon(const std::string &id, const std::string &icon)
Assigns a backend-neutral semantic icon name to an existing tool.
void addTool(const std::string &id, const std::string &label)
Adds tool.
bool setActive(const std::string &id)
Sets the active.
void setShortcut(const std::string &id, const std::string &key)
Sets the shortcut.
std::string getToolShortcut(int index) const
Returns the tool shortcut.
bool matchShortcut(const std::string &key)
Match shortcut.
std::string getToolIcon(int index) const
Returns the tool's semantic icon name.
void clear()
Clears active work and editor-owned undo/redo history.
eve::Result< TransactionId > begin(TransactionSpec specification)
Begins a transaction with an explicit stable editor identifier.
eve::Result< EditorTransactionRecord > undo()
Compensates the latest editor-owned commit and moves it to redo history.
eve::Result< void > append(std::unique_ptr< IEditCommand > command)
Appends a command without publishing its target mutation.
eve::Result< EditorTransactionRecord > retry(std::span< transaction::ITransactionParticipant * > additional={})
Retries a failed commit while retaining its pending work.
eve::Result< void > discard()
Explicitly discards the pending transaction and any preview.
eve::Result< EditorDryRunReport > dryRun() const
Runs structural and authority preflight without publishing state.
eve::Result< void > rollback()
Discards the active transaction and any unpublished preview.
eve::Result< EditorTransactionRecord > commit()
Commits the active editor participant set through Coordinator.
eve::Result< void > setAuthority(IEditAuthority *authority)
Changes the borrowed authority when no transaction is active.
eve::Result< void > appendPreview(std::unique_ptr< IEditCommand > command)
Compatibility-only append that applies a command as an editor preview.
eve::Result< TransactionId > beginLegacy(std::string label)
Begins a compatibility command transaction with an allocated identifier.
eve::Result< EditorTransactionRecord > redo()
Re-executes the latest compensated editor-owned transaction.
eve::Result< TransactionId > beginTransaction(TransactionSpec specification)
Begin a canonical checked transaction.
eve::Result< void > rollbackTransaction()
Discard the active transaction and restore its preview state.
eve::Result< void > append(std::unique_ptr< IEditCommand > command)
Stage and preview one command without discarding diagnostics.
eve::Result< EditorTransactionRecord > commitTransaction()
Commit the active transaction and return its complete record.
bool undo()
Compatibility-only boolean projection of undoTransaction().
bool commit()
Compatibility-only boolean projection of commitTransaction().
bool rollback()
Compatibility-only boolean projection of rollbackTransaction().
eve::Result< EditorTransactionRecord > undoTransaction()
Compensate the latest committed transaction.
bool redo()
Compatibility-only boolean projection of redoTransaction().
bool begin(const std::string &name)
Compatibility-only boolean facade over beginTransaction(label).
eve::Result< EditorTransactionRecord > redoTransaction()
Reapply the latest compensated transaction.
bool execute(std::unique_ptr< IEditCommand > command)
Compatibility-only boolean facade over append().
Cloneable backend boundary for physics/crowd/fluid editor simulation previews.
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< void > rollback()
Compatibility spelling for discard.
Result< EditorDryRunReport > preview() override
Preflight the active operation list without mutating target state.
Result< void > setAuthority(IEditAuthority *authority)
Change the non-owning authority when no transaction is active.
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.
Result< TransactionReceipt > retry() override
Retry the retained failed commit.
Result< SimulationPreviewFrame > singleStep(const IEditorSimulationBackend &source)
Advance one step even while paused, without mutating the live backend.
Result< void > setFixedDelta(double seconds)
Set the fixed preview delta in seconds.
void rewind()
Reset preview clock and discard the isolated preview clone.
SimulationBakeResult bake(const IEditorSimulationBackend &source, std::uint64_t sourceRevision, int steps, int maxSteps=10000) const
Bake a bounded non-destructive trajectory from a fresh source clone.
Result< SimulationPreviewFrame > update(const IEditorSimulationBackend &source)
Advance only when unpaused. Paused calls return NoOp.
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build an owning common diagnostic carrying an open editing rule identity.
eve::editing::TransactionState TransactionState
eve::editing::RuleId RuleId
Definition EditorIds.h:32
eve::editing::TransactionReceipt TransactionReceipt
CoordinatorState
Terminal or in-flight outcome of coordinator-managed participants.
Definition Transaction.h:82
DomainOperation public API.
TransactionSpec public API.
Non-destructive baked preview trajectory and diagnostics.
One deterministic editor-preview simulation frame.