载入中...
搜索中...
未找到
EditCommand.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <utility>
4#include "common/Exception.h"
6
7// EditCommand.cpp
8namespace eve::editor {
9
11 : name_(std::move(name)), target_(target) {}
12bool IntFieldEditCommand::record(int x, int y, int after) {
13 auto *field = target_ ? target_->query<IIntFieldTarget>() : nullptr;
14 if (!field || !field->containsCell(x, y)) return false;
15 for (auto &change : changes_) {
16 if (change.x == x && change.y == y) {
17 change.after = after;
18 return true;
19 }
20 }
21 const int before = field->readInt(x, y);
22 if (before == after) return false;
23 changes_.push_back({x, y, before, after});
24 dirty_.include(x, y);
25 return true;
26}
28 auto *field = target_ ? target_->query<IIntFieldTarget>() : nullptr;
29 if (!field || changes_.empty()) return false;
30 for (std::size_t i = 0; i < changes_.size(); ++i) {
31 if (field->writeInt(changes_[i].x, changes_[i].y, changes_[i].after) == FieldWriteStatus::Rejected) {
32 while (i > 0) {
33 --i;
34 (void)field->writeInt(changes_[i].x, changes_[i].y, changes_[i].before);
35 }
36 return false;
37 }
38 }
39 return true;
40}
42 auto *field = target_ ? target_->query<IIntFieldTarget>() : nullptr;
43 if (!field) return;
44 for (auto it = changes_.rbegin(); it != changes_.rend(); ++it) (void)field->writeInt(it->x, it->y, it->before);
45}
46std::unique_ptr<IEditCommand> IntFieldEditCommand::clone() const {
47 return std::make_unique<IntFieldEditCommand>(*this);
48}
50 auto *later = dynamic_cast<const IntFieldEditCommand *>(&laterBase);
51 if (!later || later->target_ != target_) return false;
52 for (const auto &incoming : later->changes_) {
53 bool merged = false;
54 for (auto &current : changes_) {
55 if (current.x == incoming.x && current.y == incoming.y) {
56 current.after = incoming.after;
57 merged = true;
58 break;
59 }
60 }
61 if (!merged) changes_.push_back(incoming);
62 }
63 dirty_.include(later->dirty_);
64 return true;
65}
66
69bool ScalarFieldEditCommand::record(int x, int y, float after) {
70 auto *field = target_ ? target_->query<IScalarFieldTarget>() : nullptr;
71 if (!field || !field->containsCell(x, y)) return false;
72 for (auto &change : changes_) {
73 if (change.x == x && change.y == y) {
74 change.after = after;
75 return true;
76 }
77 }
78 const float before = field->readScalar(x, y);
79 if (before == after) return false;
80 changes_.push_back({x, y, before, after});
81 dirty_.include(x, y);
82 return true;
83}
85 auto *field = target_ ? target_->query<IScalarFieldTarget>() : nullptr;
86 if (!field || changes_.empty()) return false;
87 for (std::size_t i = 0; i < changes_.size(); ++i) {
88 if (field->writeScalar(changes_[i].x, changes_[i].y, changes_[i].after) == FieldWriteStatus::Rejected) {
89 while (i > 0) {
90 --i;
91 (void)field->writeScalar(changes_[i].x, changes_[i].y, changes_[i].before);
92 }
93 return false;
94 }
95 }
96 return true;
97}
99 auto *field = target_ ? target_->query<IScalarFieldTarget>() : nullptr;
100 if (!field) return;
101 for (auto it = changes_.rbegin(); it != changes_.rend(); ++it)
102 static_cast<void>(field->writeScalar(it->x, it->y, it->before));
103}
104std::unique_ptr<IEditCommand> ScalarFieldEditCommand::clone() const {
105 return std::make_unique<ScalarFieldEditCommand>(*this);
106}
108 auto *later = dynamic_cast<const ScalarFieldEditCommand *>(&laterBase);
109 if (!later || later->target_ != target_) return false;
110 for (const auto &incoming : later->changes_) {
111 bool merged = false;
112 for (auto &current : changes_) {
113 if (current.x == incoming.x && current.y == incoming.y) {
114 current.after = incoming.after;
115 merged = true;
116 break;
117 }
118 }
119 if (!merged) changes_.push_back(incoming);
120 }
121 dirty_.include(later->dirty_);
122 return true;
123}
124
125} // namespace eve::editor
126
127// EditConstraint.cpp
128namespace eve::editor {
129
137 if (!constraint || std::find(constraints_.begin(), constraints_.end(), constraint) != constraints_.end())
138 return false;
139 constraints_.push_back(constraint);
140 return true;
141}
143 auto it = std::find(constraints_.begin(), constraints_.end(), constraint);
144 if (it == constraints_.end()) return false;
145 constraints_.erase(it);
146 return true;
147}
149 constraints_.clear();
150 diagnostics_.clear();
151 structuredDiagnostics_.clear();
152 rejected_ = false;
153}
158 diagnostics_.clear();
159 structuredDiagnostics_.clear();
160 rejected_ = false;
161 auto record = [&](DiagnosticSeverity severity, const char *rule, std::string message) {
162 if (message.empty() && severity != DiagnosticSeverity::Error) return;
163 if (message.empty()) message = "Editor command was rejected by a constraint";
164 structuredDiagnostics_.push_back(editing::ruleDiagnostic(severity == DiagnosticSeverity::Error
167 RuleId(rule), severity, message));
168 diagnostics_.push_back(std::move(message));
169 };
170 for (auto *constraint : constraints_) {
171 ConstraintResult result = constraint->evaluate(context, command);
172 if (result.disposition == ConstraintDisposition::Reject) {
173 rejected_ = true;
174 record(DiagnosticSeverity::Error, "editor.command.constraint-rejected", std::move(result.message));
175 return Result<void>::failure(eve::Status(EditorStatus::Rejected, structuredDiagnostics_));
176 }
177 if (result.disposition == ConstraintDisposition::Warning)
178 record(DiagnosticSeverity::Warning, "editor.command.constraint-warning", std::move(result.message));
179 else if (!result.message.empty())
180 record(DiagnosticSeverity::Info, "editor.command.constraint-info", std::move(result.message));
181 }
182 return eve::editing::applied<void>(structuredDiagnostics_);
183}
184const std::string &EditConstraintPipeline::diagnostic(int index) const {
185 if (index < 0 || index >= diagnosticCount()) throw Exception("EditConstraintPipeline::diagnostic: bad index");
186 return diagnostics_[static_cast<size_t>(index)];
187}
188
189} // namespace eve::editor
double y
double x
LogicalId target
ScriptComponentRecord record
Definition ECS.cpp:228
std::string name
Definition ECS.cpp:154
std::string message
Poco::JSON::Object::Ptr result
SceneSpatialConstraint constraint
double current
uint32_t index
const VegetationPresetContext & context
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:514
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
IEditableTarget public API.
IIntFieldTarget public API.
IScalarFieldTarget public API.
bool add(IEditConstraint *constraint)
Adds .
bool evaluate(EditorContext &context, IEditCommand &command)
Compatibility-only boolean facade over evaluateChecked().
Result< void > evaluateChecked(EditorContext &context, IEditCommand &command)
Evaluate the chain and return structured allow/warning/reject diagnostics.
const std::string & diagnostic(int index) const
Diagnostic.
bool remove(IEditConstraint *constraint)
Removes .
Context supplied by EditorSession to tools.
Definition EditorTool.h:71
Reversible unit accepted by the editor transaction manager.
Definition EditCommand.h:14
Protocol for game and art-style rules applied before command commit. Implementations may inspect or n...
Reversible edits to any target exposing IIntFieldTarget.
Definition EditCommand.h:46
IntFieldEditCommand(std::string name, IEditableTarget *target)
Int field edit command.
std::unique_ptr< IEditCommand > clone() const override
Deep copy. @ownership Caller deletes.
void revert() override
Revert.
bool record(int x, int y, int after)
Record.
bool apply() override
Applies .
bool mergeWith(const IEditCommand &later) override
Merge with.
Reversible edits to any target exposing IScalarFieldTarget.
Definition EditCommand.h:77
std::unique_ptr< IEditCommand > clone() const override
Deep copy. @ownership Caller deletes.
bool mergeWith(const IEditCommand &later) override
Merge with.
ScalarFieldEditCommand(std::string name, IEditableTarget *target)
Scalar field edit command.
bool apply() override
Applies .
bool record(int x, int y, float after)
Record.
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::RuleId RuleId
Definition EditorIds.h:32
Severity
Stable severity of a diagnostic, independent of its display text.
Definition Diagnostic.h:20
void include(int x, int y)
Include.
Result of evaluating one replaceable edit constraint.
static ConstraintResult warning(std::string message)
Warning.
static ConstraintResult reject(std::string message)
Reject.