载入中...
搜索中...
未找到
EditorVolumeTarget.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <optional>
4#include <utility>
6
7// EditorVolumeTarget.cpp
8namespace eve::editor {
9
12
13bool IntVolumeEditCommand::record(int x, int y, int z, int after) {
14 auto* volume = target_ ? target_->query<IIntVolumeTarget>() : nullptr;
15 if (!volume) return false;
16 for (auto& change : changes_) {
17 if (change.x == x && change.y == y && change.z == z) {
18 change.after = after;
19 return true;
20 }
21 }
22 const int before = volume->readInt3(x, y, z);
23 if (before == after) return false;
24 changes_.push_back({x, y, z, before, after});
25 dirty_.include(x, y, z);
26 return true;
27}
28
30 auto* volume = target_ ? target_->query<IIntVolumeTarget>() : nullptr;
31 if (!volume || changes_.empty()) return false;
32 for (std::size_t i = 0; i < changes_.size(); ++i) {
33 if (volume->writeInt3(changes_[i].x, changes_[i].y, changes_[i].z, changes_[i].after) ==
35 while (i > 0) {
36 --i;
37 (void)volume->writeInt3(changes_[i].x, changes_[i].y, changes_[i].z, changes_[i].before);
38 }
39 return false;
40 }
41 }
42 return true;
43}
44
46 auto* volume = target_ ? target_->query<IIntVolumeTarget>() : nullptr;
47 if (!volume) return;
48 for (auto it = changes_.rbegin(); it != changes_.rend(); ++it)
49 (void)volume->writeInt3(it->x, it->y, it->z, it->before);
50}
51
52std::unique_ptr<IEditCommand> IntVolumeEditCommand::clone() const {
53 return std::make_unique<IntVolumeEditCommand>(*this);
54}
55
57 const auto* later = dynamic_cast<const IntVolumeEditCommand*>(&laterBase);
58 if (!later || later->target_ != target_) return false;
59 for (const auto& incoming : later->changes_) {
60 bool merged = false;
61 for (auto& current : changes_) {
62 if (current.x == incoming.x && current.y == incoming.y && current.z == incoming.z) {
63 current.after = incoming.after;
64 merged = true;
65 break;
66 }
67 }
68 if (!merged) changes_.push_back(incoming);
69 }
70 dirty_.include(later->dirty_);
71 return true;
72}
73
74} // namespace eve::editor
75
76// EditorWorkspace.cpp
77namespace eve::editor {
78namespace {
79
80std::optional<WorkspaceRegion> parseWorkspaceRegion(const std::string& region) {
81 if (region == "left") return WorkspaceRegion::Left;
82 if (region == "right") return WorkspaceRegion::Right;
83 if (region == "top") return WorkspaceRegion::Top;
84 if (region == "bottom") return WorkspaceRegion::Bottom;
85 if (region == "center") return WorkspaceRegion::Center;
86 if (region == "floating") return WorkspaceRegion::Floating;
87 return std::nullopt;
88}
89
90} // namespace
91
93 switch (region) {
94 case WorkspaceRegion::Left: return "left";
95 case WorkspaceRegion::Right: return "right";
96 case WorkspaceRegion::Top: return "top";
97 case WorkspaceRegion::Bottom: return "bottom";
98 case WorkspaceRegion::Center: return "center";
99 case WorkspaceRegion::Floating: return "floating";
100 }
101 return "center";
102}
103
104EditorWorkspace::EditorWorkspace(std::string id, std::string title) : id_(std::move(id)), title_(std::move(title)) {}
105
106void EditorWorkspace::setTitle(const std::string& title) {
107 if (title_ == title) return;
108 title_ = title;
109 changed();
110}
111
112bool EditorWorkspace::registerPanel(const std::string& id, const std::string& title, const std::string& region,
113 int order) {
114 WorkspacePanelDescriptor descriptor{id, title, region, {}, {}, order, true, true};
115 return registerPanel(std::move(descriptor)).ok();
116}
117
119 if (descriptor.id.empty())
120 return eve::editing::failed<WorkspacePanelDescriptor>(
121 EditorStatus::Rejected, RuleId("editor.workspace.empty-panel-id"), "Workspace panel id must not be empty");
122 if (descriptor.title.empty())
123 return eve::editing::failed<WorkspacePanelDescriptor>(EditorStatus::Rejected,
124 RuleId("editor.workspace.empty-panel-title"),
125 "Workspace panel title must not be empty");
126 if (!parseWorkspaceRegion(descriptor.region))
127 return eve::editing::failed<WorkspacePanelDescriptor>(EditorStatus::Rejected,
128 RuleId("editor.workspace.invalid-region"),
129 "Workspace panel uses an unsupported dock region");
130 if (findPanel(descriptor.id))
131 return eve::editing::failed<WorkspacePanelDescriptor>(
132 EditorStatus::Conflict, RuleId("editor.workspace.duplicate-panel"),
133 "Workspace panel id is already registered: " + descriptor.id);
134 const std::string id = descriptor.id;
135 panels_.push_back(std::move(descriptor));
136 sortPanels();
137 if (activePanel_.empty()) activePanel_ = id;
138 changed();
139 return eve::editing::applied<WorkspacePanelDescriptor>(*findPanel(id));
140}
141
142bool EditorWorkspace::removePanel(const std::string& id) {
143 auto result = removePanel(StableId(id));
144 return result.code() == EditorStatus::Applied;
145}
146
148 const auto found =
149 std::find_if(panels_.begin(), panels_.end(), [&](const auto& panel) { return panel.id == id.value(); });
150 if (found == panels_.end())
151 return eve::editing::failed<WorkspacePanelDescriptor>(EditorStatus::NotFound,
152 RuleId("editor.workspace.panel-not-found"),
153 "Workspace panel is not registered: " + id.value());
155 panels_.erase(found);
156 if (activePanel_ == id.value()) activePanel_ = panels_.empty() ? std::string{} : panels_.front().id;
157 changed();
158 return eve::editing::applied<WorkspacePanelDescriptor>(std::move(removed));
159}
160
162 if (panels_.empty()) return;
163 panels_.clear();
164 activePanel_.clear();
165 changed();
166}
167
168bool EditorWorkspace::movePanel(const std::string& id, const std::string& region, int order) {
169 const auto parsed = parseWorkspaceRegion(region);
170 return parsed && movePanel(StableId(id), *parsed, order).ok();
171}
172
174 WorkspacePanelDescriptor* panel = findPanel(id.value());
175 if (!panel)
176 return eve::editing::failed<WorkspacePanelDescriptor>(EditorStatus::NotFound,
177 RuleId("editor.workspace.panel-not-found"),
178 "Workspace panel is not registered: " + id.value());
179 const std::string regionName(workspaceRegionName(region));
180 if (panel->region == regionName && panel->order == order) {
181 return Result<WorkspacePanelDescriptor>::success(*panel, eve::Status::success(EditorStatus::NoOp));
182 }
183 panel->region = regionName;
184 panel->order = order;
185 sortPanels();
186 changed();
187 return eve::editing::applied<WorkspacePanelDescriptor>(*findPanel(id.value()));
188}
189
191 if (index >= panels_.size())
192 return eve::editing::failed<WorkspacePanelDescriptor>(
193 EditorStatus::NotFound, RuleId("editor.workspace.panel-index"), "Workspace panel index is out of range");
194 return eve::editing::applied<WorkspacePanelDescriptor>(panels_[index]);
195}
196
197bool EditorWorkspace::setPanelCapability(const std::string& id, const std::string& capability) {
198 WorkspacePanelDescriptor* panel = findPanel(id);
199 if (!panel) return false;
200 if (panel->capability == capability) return true;
201 panel->capability = capability;
202 changed();
203 return true;
204}
205
206bool EditorWorkspace::setPanelContext(const std::string& id, const std::string& context) {
207 WorkspacePanelDescriptor* panel = findPanel(id);
208 if (!panel) return false;
209 if (panel->context == context) return true;
210 panel->context = context;
211 changed();
212 return true;
213}
214
215bool EditorWorkspace::setPanelVisible(const std::string& id, bool visible) {
216 WorkspacePanelDescriptor* panel = findPanel(id);
217 if (!panel) return false;
218 if (panel->visible == visible) return true;
219 panel->visible = visible;
220 if (!visible && activePanel_ == id) activePanel_.clear();
221 changed();
222 return true;
223}
224
225bool EditorWorkspace::setPanelSingleton(const std::string& id, bool singleton) {
226 WorkspacePanelDescriptor* panel = findPanel(id);
227 if (!panel) return false;
228 if (panel->singleton == singleton) return true;
229 panel->singleton = singleton;
230 changed();
231 return true;
232}
233
234bool EditorWorkspace::activatePanel(const std::string& id) { return activatePanel(StableId(id)).ok(); }
235
237 const WorkspacePanelDescriptor* panel = findPanel(id.value());
238 if (!panel)
239 return eve::editing::failed<WorkspacePanelDescriptor>(EditorStatus::NotFound,
240 RuleId("editor.workspace.panel-not-found"),
241 "Workspace panel is not registered: " + id.value());
242 if (!panel->visible)
243 return eve::editing::failed<WorkspacePanelDescriptor>(EditorStatus::Rejected,
244 RuleId("editor.workspace.panel-hidden"),
245 "Hidden workspace panels cannot be activated");
246 if (activePanel_ == id.value()) {
247 return Result<WorkspacePanelDescriptor>::success(*panel, eve::Status::success(EditorStatus::NoOp));
248 }
249 activePanel_ = id.value();
250 changed();
251 return eve::editing::applied<WorkspacePanelDescriptor>(*panel);
252}
253
254std::string EditorWorkspace::getPanelId(int index) const {
255 const auto* panel = panelAtUnchecked(index);
256 return panel ? panel->id : std::string{};
257}
258
259std::string EditorWorkspace::getPanelTitle(int index) const {
260 const auto* panel = panelAtUnchecked(index);
261 return panel ? panel->title : std::string{};
262}
263
265 const auto* panel = panelAtUnchecked(index);
266 return panel ? panel->region : std::string{};
267}
268
270 const auto* panel = panelAtUnchecked(index);
271 return panel ? panel->capability : std::string{};
272}
273
275 const auto* panel = panelAtUnchecked(index);
276 return panel ? panel->context : std::string{};
277}
278
280 const auto* panel = panelAtUnchecked(index);
281 return panel ? panel->order : 0;
282}
283
285 const auto* panel = panelAtUnchecked(index);
286 return panel && panel->visible;
287}
288
290 const auto* panel = panelAtUnchecked(index);
291 return panel && panel->singleton;
292}
293
294void EditorWorkspace::setRegionSize(const std::string& region, float pixels) {
296 changed();
297}
298
299void EditorWorkspace::layout(float width, float height) { dock_.layout(width, height); }
300
301float EditorWorkspace::getRegionX(const std::string& region) const { return dock_.getRegionX(region); }
302float EditorWorkspace::getRegionY(const std::string& region) const { return dock_.getRegionY(region); }
303float EditorWorkspace::getRegionW(const std::string& region) const { return dock_.getRegionW(region); }
304float EditorWorkspace::getRegionH(const std::string& region) const { return dock_.getRegionH(region); }
305
306bool EditorWorkspace::setMode(const std::string& mode) { return setModeId(StableId(mode)).ok(); }
307
309 if (mode.empty())
310 return eve::editing::failed<StableId>(EditorStatus::Rejected, RuleId("editor.workspace.empty-mode"),
311 "Workspace mode must not be empty");
312 if (mode_ == mode.value()) {
313 return Result<StableId>::success(std::move(mode), eve::Status::success(EditorStatus::NoOp));
314 }
315 mode_ = mode.value();
316 changed();
317 return eve::editing::applied<StableId>(std::move(mode));
318}
319
320bool EditorWorkspace::select(const std::string& channel, const std::string& domain, const std::string& target,
321 const std::string& item, const std::string& type, bool additive) {
322 SelectionDomain parsed;
323 if (channel.empty() || target.empty() || item.empty() || !parseDomain(domain, parsed)) return false;
324 return selectItem(channel, SelectionItem{parsed, TargetId(target), StableId(item), type}, additive).ok();
325}
326
328 if (channel.empty() || selected.target.empty() || selected.item.empty())
329 return eve::editing::failed<SelectionSnapshot>(EditorStatus::Rejected,
330 RuleId("editor.workspace.invalid-selection"),
331 "Selection requires a channel, target and item");
332 std::vector<SelectionItem> items;
333 if (additive) items = selection_.snapshot(channel).items;
334 if (std::find(items.begin(), items.end(), selected) == items.end()) items.push_back(selected);
335 auto result = selection_.set(channel, std::move(items), selected);
336 if (result.ok() && result.code() != EditorStatus::NoOp) changed();
337 return result;
338}
339
340bool EditorWorkspace::clearSelection(const std::string& channel) { return clearSelectionChecked(channel).ok(); }
341
343 auto result = selection_.clear(channel);
344 if (result.ok() && result.code() != EditorStatus::NoOp) changed();
345 return result;
346}
347
348int EditorWorkspace::getSelectionCount(const std::string& channel) const {
349 return static_cast<int>(selection_.snapshot(channel).items.size());
350}
351
352std::string EditorWorkspace::getSelectionItem(const std::string& channel, int index) const {
353 const auto snapshot = selection_.snapshot(channel);
354 return index >= 0 && index < static_cast<int>(snapshot.items.size())
355 ? snapshot.items[static_cast<std::size_t>(index)].item.value()
356 : std::string{};
357}
358
359std::string EditorWorkspace::getSelectionType(const std::string& channel, int index) const {
360 const auto snapshot = selection_.snapshot(channel);
361 return index >= 0 && index < static_cast<int>(snapshot.items.size())
362 ? snapshot.items[static_cast<std::size_t>(index)].type
363 : std::string{};
364}
365
366std::string EditorWorkspace::getPrimarySelection(const std::string& channel) const {
367 const auto snapshot = selection_.snapshot(channel);
368 return snapshot.primary ? snapshot.primary->item.value() : std::string{};
369}
370
371std::uint64_t EditorWorkspace::getSelectionSequence(const std::string& channel) const {
372 return selection_.snapshot(channel).sequence;
373}
374
375bool EditorWorkspace::focus(const std::string& channel, const std::string& surface, const std::string& item) {
376 return focusItem(channel, StableId(surface), StableId(item)).ok();
377}
378
380 auto result = focus_.focus(channel, std::move(surface), std::move(item));
381 if (result.ok() && result.code() != EditorStatus::NoOp) changed();
382 return result;
383}
384
385std::string EditorWorkspace::getFocusedSurface(const std::string& channel) const {
386 return focus_.snapshot(channel).surface.value();
387}
388
389bool EditorWorkspace::isRegion(const std::string& region) {
390 return region == "left" || region == "right" || region == "top" || region == "bottom" || region == "center" ||
391 region == "floating";
392}
393
394bool EditorWorkspace::parseDomain(const std::string& value, SelectionDomain& domain) {
395 if (value == "scene")
396 domain = SelectionDomain::Scene;
397 else if (value == "asset")
398 domain = SelectionDomain::Asset;
399 else if (value == "graph")
400 domain = SelectionDomain::Graph;
401 else if (value == "timeline")
402 domain = SelectionDomain::Timeline;
403 else if (value == "ui")
404 domain = SelectionDomain::UI;
405 else if (value == "custom")
406 domain = SelectionDomain::Custom;
407 else
408 return false;
409 return true;
410}
411
412WorkspacePanelDescriptor* EditorWorkspace::findPanel(const std::string& id) {
413 const auto found = std::find_if(panels_.begin(), panels_.end(), [&](const auto& panel) { return panel.id == id; });
414 return found == panels_.end() ? nullptr : &*found;
415}
416
417const WorkspacePanelDescriptor* EditorWorkspace::panelAtUnchecked(int index) const {
418 return index >= 0 && index < static_cast<int>(panels_.size()) ? &panels_[static_cast<std::size_t>(index)] : nullptr;
419}
420
421void EditorWorkspace::sortPanels() {
422 std::stable_sort(panels_.begin(), panels_.end(), [](const auto& left, const auto& right) {
423 if (left.region != right.region) return left.region < right.region;
424 if (left.order != right.order) return left.order < right.order;
425 return left.id < right.id;
426 });
427}
428
429void EditorWorkspace::changed() { ++revision_; }
430
431} // namespace eve::editor
double z
double y
double x
LogicalId target
std::string selected
std::string descriptor
double volume
std::string title
std::string name
Definition ECS.cpp:154
ScriptFieldRegion region
HexVec3 left
HexVec3 right
std::uint32_t height
std::uint32_t width
std::string id
Poco::JSON::Object::Ptr result
Snapshot snapshot
std::vector< std::int32_t > order
Object::Ptr value
uint8_t * pixels
std::string domain
bool found
int removed
double current
bool visible
uint32_t index
const VegetationPresetContext & context
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 Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:313
const std::string & value() const noexcept
Returns the exact legacy or canonical compatibility spelling.
Definition Identity.h:365
bool empty() const noexcept
True when no compatibility spelling has been assigned.
Definition Identity.h:377
IEditableTarget public API.
Stable capability for sparse integer volumes.
float getRegionX(const std::string &region) const
Returns the region x.
void setRegionSize(const std::string &region, float pixels)
Sets the region size.
float getRegionY(const std::string &region) const
Returns the region y.
void layout(float screenW, float screenH)
Layout.
float getRegionW(const std::string &region) const
Returns the region w.
float getRegionH(const std::string &region) const
Returns the region h.
EditorFocusSnapshot snapshot(const std::string &channel) const
Read the current focus of one channel.
Result< EditorFocusSnapshot > focus(std::string channel, StableId surface, StableId item={})
Set the focused surface/item for one channel.
Result< SelectionSnapshot > clear(const std::string &channel)
Clear a channel while retaining a monotonic sequence.
SelectionSnapshot snapshot(const std::string &channel) const
Read one channel, returning an empty sequence-zero snapshot when absent.
Result< SelectionSnapshot > set(std::string channel, std::vector< SelectionItem > items, std::optional< SelectionItem > primary=std::nullopt)
Atomically replace a channel selection and optional primary item.
std::string getPrimarySelection(const std::string &channel) const
Primary selected item id for a channel.
std::string getFocusedSurface(const std::string &channel) const
Focused surface id for a channel.
bool getPanelSingleton(int index) const
Panel singleton policy by deterministic index.
std::uint64_t getSelectionSequence(const std::string &channel) const
Monotonic selection sequence for MVVM change detection.
Result< WorkspacePanelDescriptor > panelAt(std::size_t index) const
Return an owning panel snapshot instead of an ambiguous empty field.
std::string getPanelCapability(int index) const
Panel capability requirement by deterministic index.
std::string getPanelRegion(int index) const
Panel dock region by deterministic index.
float getRegionH(const std::string &region) const
Computed region height.
std::string getPanelContext(int index) const
Panel input context by deterministic index.
Result< WorkspacePanelDescriptor > removePanel(const StableId &id)
Remove a panel and return its final owning descriptor.
bool setPanelContext(const std::string &id, const std::string &context)
Attach a semantic input context such as scene, graph, or timeline.
Result< EditorFocusSnapshot > focusItem(const std::string &channel, StableId surface, StableId item)
Focus a strongly identified surface and item without discarding diagnostics.
std::string getPanelTitle(int index) const
Panel title by deterministic index.
bool focus(const std::string &channel, const std::string &surface, const std::string &item)
Set UI-toolkit-independent focus for a channel.
Result< SelectionSnapshot > selectItem(std::string channel, SelectionItem item, bool additive)
Select one already typed item without string domain parsing.
std::string getSelectionItem(const std::string &channel, int index) const
Selected stable item id by channel/index.
Result< WorkspacePanelDescriptor > registerPanel(WorkspacePanelDescriptor descriptor)
Register a checked owning panel descriptor.
void setTitle(const std::string &title)
Change the display title without changing workspace identity.
std::string getSelectionType(const std::string &channel, int index) const
Selected item type by channel/index.
bool select(const std::string &channel, const std::string &domain, const std::string &target, const std::string &item, const std::string &type, bool additive)
Select one stable item in a semantic channel.
bool setMode(const std::string &mode)
Set a project-defined mode such as edit, play, material, or animation.
Result< WorkspacePanelDescriptor > activatePanel(const StableId &id)
Activate a visible panel with structured not-found/rejected results.
float getRegionY(const std::string &region) const
Computed region Y coordinate.
EditorWorkspace(std::string id, std::string title)
Create a workspace with stable identity and display title.
Result< WorkspacePanelDescriptor > movePanel(const StableId &id, WorkspaceRegion region, int order)
Move a panel using a strongly typed dock region.
void clearPanels()
Remove every panel descriptor.
Result< StableId > setModeId(StableId mode)
Set a strongly identified workspace mode.
Result< SelectionSnapshot > clearSelectionChecked(const std::string &channel)
Clear one selection channel without discarding diagnostics.
bool getPanelVisible(int index) const
Panel visibility by deterministic index.
bool setPanelCapability(const std::string &id, const std::string &capability)
Attach a capability requirement used by the host when filtering panels.
void layout(float width, float height)
Compute standard dock rectangles for the requested host size.
int getPanelOrder(int index) const
Panel order by deterministic index.
bool setPanelSingleton(const std::string &id, bool singleton)
Mark whether hosts should allow more than one instance of this panel type.
void setRegionSize(const std::string &region, float pixels)
Configure one standard dock region size in pixels.
std::string getPanelId(int index) const
Panel id by deterministic index.
float getRegionX(const std::string &region) const
Computed region X coordinate.
int getSelectionCount(const std::string &channel) const
Number of selected items in a channel.
bool setPanelVisible(const std::string &id, bool visible)
Show or hide a panel without removing its descriptor.
float getRegionW(const std::string &region) const
Computed region width.
bool clearSelection(const std::string &channel)
Clear one semantic selection channel.
Reversible unit accepted by the editor transaction manager.
Definition EditCommand.h:14
Reversible integer-volume command shared by voxel and custom volume targets.
std::unique_ptr< IEditCommand > clone() const override
Deep copy. @ownership Caller deletes.
bool record(int x, int y, int z, int after)
Record a desired value without applying it.
bool mergeWith(const IEditCommand &later) override
Merge with.
IntVolumeEditCommand(std::string name, IEditableTarget *target)
Create an unapplied command for one editable volume target.
SelectionDomain
SelectionDomain public API.
eve::editing::StableId StableId
Definition EditorIds.h:36
WorkspaceRegion
Supported semantic dock regions for UI-neutral workspace panels.
std::string_view workspaceRegionName(WorkspaceRegion region)
Return the stable serialized name for a workspace region.
eve::editing::TargetId TargetId
Definition EditorIds.h:29
eve::editing::RuleId RuleId
Definition EditorIds.h:32
eve::editing::SelectionDomain SelectionDomain
Semantic domain of one selection channel.
void include(int x, int y, int z)
Include.
SelectionItem public API.
std::vector< SelectionItem > items
UI-neutral description of one composable editor panel.