Skip to content

Commit a316092

Browse files
committed
draw dragged node as floating text
1 parent 3cd82d8 commit a316092

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# fix line endings
2+
3cd82d8adebbfce5b84847ed42c3cc40bffd8185

src/pages/Tree.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44
#include "../platform/utils.hpp"
55
#include <misc/cpp/imgui_stdlib.h>
66
#include <Geode/utils/string.hpp>
7+
#include "../ImGui.hpp"
78

89
#ifndef GEODE_IS_WINDOWS
910
#include <cxxabi.h>
1011
#endif
1112

1213
using namespace geode::prelude;
1314

15+
std::string formatNodeName(CCNode* node, size_t index) {
16+
std::string name = fmt::format("[{}] {} ", index, geode::cocos::getObjectName(node));
17+
if (node->getTag() != -1) {
18+
name += fmt::format("({}) ", node->getTag());
19+
}
20+
if (node->getID().size()) {
21+
name += fmt::format("\"{}\" ", node->getID());
22+
}
23+
if (node->getChildrenCount()) {
24+
name += fmt::format("<{}> ", node->getChildrenCount());
25+
}
26+
return name;
27+
}
28+
1429
void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
15-
if (!searchBranch(node)) {
30+
if (!this->searchBranch(node)) {
1631
return;
1732
}
1833

@@ -32,19 +47,21 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
3247
if (auto dragged = DevTools::get()->getDraggedNode(); dragged && dragged != node && !drag) {
3348
float mouse = ImGui::GetMousePos().y;
3449
float cursor = ImGui::GetCursorPosY() + ImGui::GetWindowPos().y - ImGui::GetScrollY();
50+
float height = ImGui::GetTextLineHeight();
3551

36-
if (mouse <= cursor + 18 && mouse > cursor) {
52+
if (mouse <= cursor + height && mouse > cursor) {
3753
flags |= ImGuiTreeNodeFlags_Selected;
3854

3955
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
40-
for(CCNode* n = node;; n = n->getParent()) {
41-
if (n == dragged) { // can't drag a parent into its own child
56+
for (CCNode* n = node;; n = n->getParent()) {
57+
if (n == dragged) {
58+
// can't drag a parent into its own child
4259
break;
4360
} else if (n == nullptr) {
4461
auto parent = dragged->getParent();
4562
dragged->removeFromParentAndCleanup(false);
46-
parent->updateLayout();
4763
node->addChild(dragged);
64+
parent->updateLayout();
4865
node->updateLayout();
4966
break;
5067
}
@@ -60,22 +77,11 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
6077
ImGui::SetNextItemOpen(true);
6178
}
6279

63-
std::stringstream name;
64-
name << "[" << index << "] " << geode::cocos::getObjectName(node) << " ";
65-
if (node->getTag() != -1) {
66-
name << "(" << node->getTag() << ") ";
67-
}
68-
if (node->getID().size()) {
69-
name << "\"" << node->getID() << "\" ";
70-
}
71-
if (node->getChildrenCount()) {
72-
name << "<" << node->getChildrenCount() << "> ";
73-
}
80+
const auto name = formatNodeName(node, index);
7481
// The order here is unusual due to imgui weirdness; see the second-to-last paragraph in https://kahwei.dev/2022/06/20/imgui-tree-node/
75-
bool expanded = ImGui::TreeNodeEx(node, flags, "%s", name.str().c_str());
82+
bool expanded = ImGui::TreeNodeEx(node, flags, "%s", name.c_str());
7683
float height = ImGui::GetItemRectSize().y;
7784

78-
7985
if (ImGui::IsItemClicked()) {
8086
DevTools::get()->selectNode(node);
8187
selected = true;
@@ -113,8 +119,9 @@ void DevTools::drawTreeBranch(CCNode* node, size_t index, bool drag) {
113119
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0, 0.75f));
114120
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 4);
115121

116-
std::string btnText = (U8STR(FEATHER_MENU"##menu_")) + std::to_string((long)node);
117-
ImGui::Button(btnText.c_str(), {0, height});
122+
ImGui::PushID(node);
123+
ImGui::Button(U8STR(FEATHER_MENU), {0, height});
124+
ImGui::PopID();
118125

119126
isDrag = ImGui::IsItemActive();
120127
if (isDrag) {
@@ -153,6 +160,19 @@ void DevTools::drawTree() {
153160

154161
this->drawTreeBranch(CCDirector::get()->getRunningScene(), 0, false);
155162

163+
if (auto* dragged = this->getDraggedNode()) {
164+
const auto name = formatNodeName(dragged, 0);
165+
auto bgColor = ImGui::GetColorU32(ImGui::GetStyle().Colors[ImGuiCol_Header]);
166+
auto textColor = ImGui::GetColorU32(ImGui::GetStyle().Colors[ImGuiCol_Text]);
167+
168+
auto textSize = ImGui::CalcTextSize(name.c_str());
169+
auto pos = ImGui::GetMousePos() - textSize / 2.f;
170+
171+
auto* drawing = ImGui::GetWindowDrawList();
172+
drawing->AddRectFilled(pos, pos + textSize, bgColor);
173+
drawing->AddText(pos, textColor, name.c_str(), name.c_str() + name.size());
174+
}
175+
156176
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
157177
DevTools::get()->setDraggedNode(nullptr);
158178
}

0 commit comments

Comments
 (0)