yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
drop_zone_suggester.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <utility>
5
6namespace yaze {
7namespace editor {
8namespace layout_designer {
9
10DropSuggestion SuggestDrop(const ImRect& leaf_rect, ImVec2 mouse) {
12 if (!leaf_rect.Contains(mouse))
13 return s;
14
15 const float width = leaf_rect.GetWidth();
16 const float height = leaf_rect.GetHeight();
17 if (width <= 0.0f || height <= 0.0f)
18 return s;
19
20 const float u = (mouse.x - leaf_rect.Min.x) / width;
21 const float v = (mouse.y - leaf_rect.Min.y) / height;
22 // Distance (in normalized units) to the nearest horizontal / vertical
23 // edge. Smaller == closer.
24 const float du = std::min(u, 1.0f - u);
25 const float dv = std::min(v, 1.0f - v);
26
27 if (du >= kDropEdgeFraction && dv >= kDropEdgeFraction) {
29 return s;
30 }
31
32 // Whichever axis is closer to its edge wins — ties break to horizontal
33 // which matches most left-right dominant editor layouts.
34 if (du <= dv) {
37 } else {
40 }
41 return s;
42}
43
44ImRect ComputeDropPreviewRect(const ImRect& leaf_rect,
45 const DropSuggestion& suggestion) {
46 switch (suggestion.kind) {
48 return ImRect(leaf_rect.Min, leaf_rect.Min);
50 return leaf_rect;
52 const float w = leaf_rect.GetWidth() * kDropSplitRatio;
53 return ImRect(leaf_rect.Min,
54 ImVec2(leaf_rect.Min.x + w, leaf_rect.Max.y));
55 }
57 const float w = leaf_rect.GetWidth() * kDropSplitRatio;
58 return ImRect(ImVec2(leaf_rect.Max.x - w, leaf_rect.Min.y),
59 leaf_rect.Max);
60 }
62 const float h = leaf_rect.GetHeight() * kDropSplitRatio;
63 return ImRect(leaf_rect.Min,
64 ImVec2(leaf_rect.Max.x, leaf_rect.Min.y + h));
65 }
67 const float h = leaf_rect.GetHeight() * kDropSplitRatio;
68 return ImRect(ImVec2(leaf_rect.Min.x, leaf_rect.Max.y - h),
69 leaf_rect.Max);
70 }
71 }
72 return ImRect(leaf_rect.Min, leaf_rect.Min);
73}
74
76 const DropSuggestion& suggestion,
77 PanelEntry panel) {
78 if (tree == nullptr || leaf == nullptr)
79 return nullptr;
80 if (suggestion.kind == DropSuggestion::Kind::kNone)
81 return nullptr;
82 if (leaf->type != DockNode::Type::kLeaf)
83 return nullptr;
84 if (panel.panel_id.empty())
85 return nullptr;
86 if (tree->root != nullptr &&
87 tree->root->FindPanel(panel.panel_id) != nullptr) {
88 return nullptr;
89 }
90
91 switch (suggestion.kind) {
93 return nullptr;
95 leaf->panels.push_back(std::move(panel));
96 leaf->active_tab_index = static_cast<int>(leaf->panels.size()) - 1;
97 return leaf;
100 DockNode::MakeLeaf({std::move(panel)}),
101 /*new_child_first=*/true);
102 return leaf->child_a.get();
105 DockNode::MakeLeaf({std::move(panel)}),
106 /*new_child_first=*/false);
107 return leaf->child_b.get();
110 DockNode::MakeLeaf({std::move(panel)}),
111 /*new_child_first=*/true);
112 return leaf->child_a.get();
115 DockNode::MakeLeaf({std::move(panel)}),
116 /*new_child_first=*/false);
117 return leaf->child_b.get();
118 }
119 return nullptr;
120}
121
122} // namespace layout_designer
123} // namespace editor
124} // namespace yaze
DropSuggestion SuggestDrop(const ImRect &leaf_rect, ImVec2 mouse)
ImRect ComputeDropPreviewRect(const ImRect &leaf_rect, const DropSuggestion &suggestion)
DockNode * ApplyDropSuggestion(DockTree *tree, DockNode *leaf, const DropSuggestion &suggestion, PanelEntry panel)
Represents a dock node in the layout tree.
Definition dock_tree.h:61
static std::unique_ptr< DockNode > MakeLeaf(std::vector< PanelEntry > panels)
Definition dock_tree.cc:44
std::unique_ptr< DockNode > child_a
Definition dock_tree.h:78
std::unique_ptr< DockNode > child_b
Definition dock_tree.h:79
std::vector< PanelEntry > panels
Definition dock_tree.h:72
void SplitInPlace(SplitDirection dir, float ratio, std::unique_ptr< DockNode > new_child, bool new_child_first)
Definition dock_tree.cc:84
std::unique_ptr< DockNode > root
Definition dock_tree.h:115