载入中...
搜索中...
未找到
FieldBrushTool.cpp
浏览该文件的文档.
2
6
7#include <algorithm>
8
9namespace eve::editor {
10
11std::unique_ptr<IEditCommand> PaintIntFieldOperation::createCommand(
12 IEditableTarget *target, const BrushSampleBuffer &samples, float strength) const {
13 if (!target || !target->query<IIntFieldTarget>()) return nullptr;
14 auto command = std::make_unique<IntFieldEditCommand>("paint integer field", target);
15 if (strength <= 0.f) return command;
16 for (int i = 0; i < samples.size(); ++i) {
17 const BrushPoint &point = samples.point(i);
18 if (point.weight > 0.f) command->record(point.x, point.y, value_);
19 }
20 return command;
21}
22
23std::unique_ptr<IEditCommand> AddScalarFieldOperation::createCommand(
24 IEditableTarget *target, const BrushSampleBuffer &samples, float strength) const {
25 auto *field = target ? target->query<IScalarFieldTarget>() : nullptr;
26 if (!field) return nullptr;
27 auto command = std::make_unique<ScalarFieldEditCommand>("add scalar field", target);
28 for (int i = 0; i < samples.size(); ++i) {
29 const BrushPoint &point = samples.point(i);
30 if (point.weight > 0.f && field->containsCell(point.x, point.y)) {
31 command->record(point.x, point.y,
32 field->readScalar(point.x, point.y) + strength * point.weight);
33 }
34 }
35 return command;
36}
37
38FieldBrushTool::FieldBrushTool(std::string id, std::string label, const IBrushKernel *kernel,
40 : descriptor_{std::move(id), std::move(label), {}}, kernel_(kernel), operation_(operation) {}
41
42void FieldBrushTool::setRadius(float radius) { radius_ = std::max(0.5f, radius); }
43
44bool FieldBrushTool::stamp(EditorContext &context, float x, float y) {
45 if (!kernel_ || !operation_ || !context.target()) return false;
46 samples_.clear();
47 kernel_->sample({x, y, radius_, 0.f}, samples_);
48 auto command = operation_->createCommand(context.target(), samples_, strength_);
49 return command && context.execute(std::move(command));
50}
51
52ToolResponse FieldBrushTool::pointerEvent(EditorContext &context, const EditorPointerEvent &event) {
53 cursorX_ = event.x;
54 cursorY_ = event.y;
55 if (event.phase == EditorPointerEvent::Phase::Down) {
56 if (!kernel_ || !operation_ || !context.target() ||
57 !context.transactions().begin(descriptor_.label)) return ToolResponse::ignored();
58 stroking_ = true;
59 stamp(context, event.x, event.y);
60 return ToolResponse::capture();
61 }
62 if (!stroking_) return ToolResponse::ignored();
63 if (event.phase == EditorPointerEvent::Phase::Move) {
64 stamp(context, event.x, event.y);
65 return ToolResponse::consumed();
66 }
67 if (event.phase == EditorPointerEvent::Phase::Up) {
68 context.transactions().commit();
69 stroking_ = false;
70 return ToolResponse::release();
71 }
72 cancel(context);
73 return ToolResponse::release();
74}
75
76void FieldBrushTool::cancel(EditorContext &context) {
77 if (stroking_) context.transactions().rollback();
78 stroking_ = false;
79}
80
81void FieldBrushTool::drawOverlay(EditorContext &, IEditorOverlay &overlay) {
82 overlay.circle({cursorX_, cursorY_, 0.f}, radius_, {0x55ffffffU, 1.f, false});
83}
84
85void FieldBrushTool::inspect(EditorContext &, IEditorInspector &inspector) {
86 inspector.beginGroup(descriptor_.id, descriptor_.label);
87 inspector.scalar("radius", "Radius", radius_, 0.5f, 256.f);
88 inspector.scalar("strength", "Strength", strength_, -100.f, 100.f);
89 inspector.endGroup();
90 setRadius(radius_);
91}
92
93} // namespace eve::editor
LogicalId target
ActionParameterOperation operation
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
std::string label
float radius
std::string id
Definition PlayHost.cpp:108
Heightmap stamp
const VegetationPresetContext & context
glm::vec3 point
IEditableTarget public API.
IIntFieldTarget public API.
IScalarFieldTarget public API.
std::unique_ptr< IEditCommand > createCommand(IEditableTarget *target, const BrushSampleBuffer &samples, float strength) const override
Creates command.
Convenience sink for previews, tests and command construction.
int size() const
Returns the size of .
const BrushPoint & point(int index) const
Point.
Context supplied by EditorSession to tools.
Definition EditorTool.h:71
FieldBrushTool(std::string id, std::string label, const IBrushKernel *kernel, const IFieldBrushOperation *operation)
Field brush tool.
Shape protocol shared by tile, terrain, mask and custom tools.
Definition BrushKernel.h:66
UI-independent property editor used by tools to expose settings.
virtual void endGroup()=0
End the current logical group.
virtual void beginGroup(const std::string &id, const std::string &label)=0
Begin a logical group of properties.
virtual bool scalar(const std::string &id, const std::string &label, float &value, float minimum, float maximum)=0
Present an editable scalar property.
Sink implemented by a 2D or 3D viewport to render tool feedback.
virtual void circle(const OverlayPoint &center, float radius, const OverlayStyle &style)=0
Draw a circle whose orientation is supplied by the viewport.
Replaceable operation that converts brush samples into a command.
std::unique_ptr< IEditCommand > createCommand(IEditableTarget *target, const BrushSampleBuffer &samples, float strength) const override
Creates command.
One weighted grid coordinate emitted by a brush kernel.
Definition BrushKernel.h:20
Pointer input normalized by an editor viewport adapter.
Definition EditorTool.h:20
Result returned by a tool after processing input.
Definition EditorTool.h:50