Skip to content

Commit 6db55a4

Browse files
committed
allow dragging to inbetween nodes, better ui
1 parent cdf28dd commit 6db55a4

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

src/pages/Tree.cpp

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ std::string formatNodeName(CCNode* node, size_t index) {
2626
return name;
2727
}
2828

29+
bool isNodeParentOf(CCNode* parent, CCNode* child) {
30+
for (CCNode* cur = child; cur != nullptr; cur = cur->getParent()) {
31+
if (cur == parent) {
32+
return true;
33+
}
34+
}
35+
return false;
36+
}
37+
2938
void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
3039
if (!this->searchBranch(node)) {
3140
return;
@@ -44,26 +53,43 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
4453
flags |= ImGuiTreeNodeFlags_OpenOnArrow;
4554
}
4655

47-
if (auto dragged = DevTools::get()->getDraggedNode(); dragged && dragged != node && !drag) {
48-
float mouse = ImGui::GetMousePos().y;
49-
float cursor = ImGui::GetCursorPosY() + ImGui::GetWindowPos().y - ImGui::GetScrollY();
50-
float height = ImGui::GetTextLineHeight();
56+
bool drawSeparator = false;
5157

52-
if (mouse <= cursor + height && mouse > cursor) {
53-
flags |= ImGuiTreeNodeFlags_Selected;
54-
55-
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
56-
for (CCNode* n = node;; n = n->getParent()) {
57-
if (n == dragged) {
58-
// can't drag a parent into its own child
59-
break;
60-
} else if (n == nullptr) {
61-
auto parent = dragged->getParent();
58+
if (auto dragged = this->getDraggedNode(); dragged && dragged != node && !drag) {
59+
float mouseY = ImGui::GetMousePos().y;
60+
float cursorY = ImGui::GetCursorPosY() + ImGui::GetWindowPos().y - ImGui::GetScrollY();
61+
float height = ImGui::GetTextLineHeight();
62+
float spacing = ImGui::GetStyle().FramePadding.y;
63+
64+
if (mouseY > cursorY && mouseY <= cursorY + height + spacing) {
65+
if (mouseY <= cursorY + height) {
66+
flags |= ImGuiTreeNodeFlags_Selected;
67+
68+
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left) && !isNodeParentOf(dragged, node)) {
69+
auto parent = dragged->getParent();
70+
dragged->removeFromParentAndCleanup(false);
71+
node->addChild(dragged);
72+
parent->updateLayout();
73+
node->updateLayout();
74+
}
75+
} else {
76+
drawSeparator = true;
77+
78+
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left) && !isNodeParentOf(dragged, node)) {
79+
// place the dragged node right after `node`
80+
auto newParent = node->getParent();
81+
if (newParent) {
82+
auto oldParent = dragged->getParent();
83+
auto children = newParent->getChildrenExt();
84+
for (int i = index + 1; i < children.size(); i++) {
85+
children[i]->m_uOrderOfArrival++;
86+
}
6287
dragged->removeFromParentAndCleanup(false);
63-
node->addChild(dragged);
64-
parent->updateLayout();
65-
node->updateLayout();
66-
break;
88+
newParent->addChild(dragged, node->getZOrder());
89+
dragged->m_uOrderOfArrival = node->m_uOrderOfArrival + 1;
90+
91+
if (oldParent != newParent) oldParent->updateLayout();
92+
newParent->updateLayout();
6793
}
6894
}
6995
}
@@ -111,24 +137,13 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
111137

112138
bool isDrag = false;
113139

114-
if (m_settings.enableMoving) {
115-
ImGui::SameLine();
116-
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
117-
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));
118-
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0));
119-
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0, 0.75f));
120-
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 4);
121-
122-
ImGui::PushID(node);
123-
ImGui::Button(U8STR(FEATHER_MENU), {0, height});
124-
ImGui::PopID();
125-
126-
isDrag = ImGui::IsItemActive();
127-
if (isDrag) {
128-
DevTools::get()->setDraggedNode(node);
140+
if (m_settings.enableMoving && ImGui::IsItemActive()) {
141+
if (ImGui::IsMouseDragging(ImGuiMouseButton_Left, height / 2.f)) {
142+
isDrag = true;
143+
if (this->getDraggedNode() != node) {
144+
this->setDraggedNode(node);
145+
}
129146
}
130-
ImGui::PopStyleColor(3);
131-
ImGui::PopStyleVar();
132147
}
133148

134149
if (expanded) {
@@ -141,6 +156,10 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
141156
}
142157
ImGui::TreePop();
143158
}
159+
// on leaf nodes expanded is true
160+
if (drawSeparator && (!expanded || !node->getChildrenCount())) {
161+
ImGui::Separator();
162+
}
144163
}
145164

146165
void DevTools::drawTree() {
@@ -174,7 +193,7 @@ void DevTools::drawTree() {
174193
}
175194

176195
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
177-
DevTools::get()->setDraggedNode(nullptr);
196+
this->setDraggedNode(nullptr);
178197
}
179198
}
180199

0 commit comments

Comments
 (0)