载入中...
搜索中...
未找到
EditConstraint.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4
5#include <algorithm>
6#include <utility>
7
8namespace eve::editor {
9
17 if (!constraint || std::find(constraints_.begin(), constraints_.end(), constraint) != constraints_.end()) return false;
18 constraints_.push_back(constraint);
19 return true;
20}
22 auto it = std::find(constraints_.begin(), constraints_.end(), constraint);
23 if (it == constraints_.end()) return false;
24 constraints_.erase(it);
25 return true;
26}
28 constraints_.clear();
29 diagnostics_.clear();
30 structuredDiagnostics_.clear();
31 rejected_ = false;
32}
37 diagnostics_.clear();
38 structuredDiagnostics_.clear();
39 rejected_ = false;
40 auto record = [&](DiagnosticSeverity severity, const char *rule, std::string message) {
41 if (message.empty() && severity != DiagnosticSeverity::Error) return;
42 if (message.empty()) message = "Editor command was rejected by a constraint";
43 structuredDiagnostics_.push_back(editing::ruleDiagnostic(
44 severity == DiagnosticSeverity::Error ? eve::DiagnosticCode::PreconditionViolation
46 RuleId(rule), severity, message));
47 diagnostics_.push_back(std::move(message));
48 };
49 for (auto *constraint : constraints_) {
50 ConstraintResult result = constraint->evaluate(context, command);
52 rejected_ = true;
53 record(DiagnosticSeverity::Error, "editor.command.constraint-rejected", std::move(result.message));
54 return Result<void>::failure(eve::Status(EditorStatus::Rejected, structuredDiagnostics_));
55 }
57 record(DiagnosticSeverity::Warning, "editor.command.constraint-warning", std::move(result.message));
58 else if (!result.message.empty())
59 record(DiagnosticSeverity::Info, "editor.command.constraint-info", std::move(result.message));
60 }
61 return eve::editing::applied<void>(structuredDiagnostics_);
62}
63const std::string &EditConstraintPipeline::diagnostic(int index) const {
64 if (index < 0 || index >= diagnosticCount()) throw Exception("EditConstraintPipeline::diagnostic: bad index");
65 return diagnostics_[static_cast<size_t>(index)];
66}
67
68} // namespace eve::editor
std::string message
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:480
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Structured status and zero or more diagnostics for an operation.
Definition Status.h:68
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...
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build a 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
Result of evaluating one replaceable edit constraint.
static ConstraintResult warning(std::string message)
Warning.
ConstraintDisposition disposition
static ConstraintResult reject(std::string message)
Reject.