-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathDevTools.cpp
More file actions
391 lines (325 loc) · 12.1 KB
/
DevTools.cpp
File metadata and controls
391 lines (325 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <imgui_internal.h>
#include "DevTools.hpp"
#include "fonts/FeatherIcons.hpp"
#include "fonts/OpenSans.hpp"
#include "fonts/GeodeIcons.hpp"
#include "fonts/RobotoMono.hpp"
#include "fonts/SourceCodeProLight.hpp"
#include "platform/platform.hpp"
#include <Geode/loader/Log.hpp>
#include <Geode/loader/Mod.hpp>
#include "ImGui.hpp"
template<>
struct matjson::Serialize<Settings> {
static Result<Settings> fromJson(const matjson::Value& value) {
Settings s;
const auto assign = [](const matjson::Value& json, auto& prop) {
if (auto res = json.as<std::decay_t<decltype(prop)>>()) prop = res.unwrap();
};
assign(value["game_in_window"], s.GDInWindow);
assign(value["attributes_in_tree"], s.attributesInTree);
assign(value["always_highlight"], s.alwaysHighlight);
assign(value["highlight_layouts"], s.highlightLayouts);
assign(value["arrow_expand"], s.arrowExpand);
assign(value["order_children"], s.orderChildren);
assign(value["advanced_settings"], s.advancedSettings);
assign(value["show_memory_viewer"], s.showMemoryViewer);
assign(value["show_mod_graph"], s.showModGraph);
assign(value["theme"], s.theme);
assign(value["theme_color"], s.themeColor);
assign(value["button_x"], s.buttonPos.x);
assign(value["button_y"], s.buttonPos.y);
assign(value["button_editor"], s.buttonInEditor);
assign(value["button_game"], s.buttonInGame);
assign(value["button_enabled"], s.buttonEnabled);
assign(value["tree_drag_reorder"], s.treeDragReorder);
return Ok(s);
}
static matjson::Value toJson(const Settings& settings) {
return matjson::makeObject({
{ "game_in_window", settings.GDInWindow },
{ "attributes_in_tree", settings.attributesInTree },
{ "always_highlight", settings.alwaysHighlight },
{ "highlight_layouts", settings.highlightLayouts },
{ "arrow_expand", settings.arrowExpand },
{ "order_children", settings.orderChildren },
{ "advanced_settings", settings.advancedSettings },
{ "show_memory_viewer", settings.showMemoryViewer },
{ "show_mod_graph", settings.showModGraph },
{ "theme", settings.theme },
{ "theme_color", settings.themeColor },
{ "button_x", settings.buttonPos.x },
{ "button_y", settings.buttonPos.y },
{ "button_editor", settings.buttonInEditor },
{ "button_game", settings.buttonInGame },
{ "button_enabled", settings.buttonEnabled },
{ "tree_drag_reorder", settings.treeDragReorder }
});
}
};
$on_mod(DataSaved) { DevTools::get()->saveSettings(); }
DevTools* DevTools::get() {
static auto inst = new DevTools();
return inst;
}
// i wish i didnt have to do this but none of the lead devs have 4k monitors apparently!?!?!?
void DevTools::loadSettings() {
if (!m_mod) {
m_mod = Mod::get();
}
m_settings = m_mod->getSavedValue<Settings>("settings");
m_settings.fontScale = m_mod->getSavedValue<float>("font-scale", m_settings.fontScale);
}
void DevTools::saveSettings() {
if (!m_mod) {
m_mod = Mod::get();
}
m_mod->setSavedValue("settings", m_settings);
m_mod->setSavedValue("font-scale", m_settings.fontScale);
}
Settings DevTools::getSettings() { return m_settings; }
void DevTools::setBallPosition(CCPoint pos) { m_settings.buttonPos = std::move(pos); }
bool DevTools::shouldPopGame() const {
return m_visible && m_settings.GDInWindow;
}
bool DevTools::pausedGame() const {
return m_pauseGame;
}
bool DevTools::isSetup() const {
return m_setup;
}
bool DevTools::shouldOrderChildren() const {
return m_settings.orderChildren;
}
CCNode* DevTools::getSelectedNode() const {
return m_selectedNode;
}
void DevTools::selectNode(CCNode* node) {
m_selectedNode = node;
}
void DevTools::highlightNode(CCNode* node, HighlightMode mode) {
m_toHighlight.push_back({ node, mode });
}
CCNode* DevTools::getDraggedNode() const {
return m_draggedNode;
}
void DevTools::setDraggedNode(CCNode* node) {
m_draggedNode = node;
}
void DevTools::addCustomCallback(Function<void(CCNode*)>&& callback) {
m_customCallbacks.push_back(std::forward<Function<void(CCNode*)>>(callback));
}
DragButton* DevTools::getDragButton() {
return m_dragButton;
}
void DevTools::setupDragButton() {
auto spr = CircleButtonSprite::createWithSprite("devtools.png"_spr, 1, CircleBaseColor::Green, CircleBaseSize::MediumAlt);
spr->setScale(.8f);
m_dragButton = DragButton::create(spr, [this](){
this->toggle();
});
m_dragButton->setPosition(m_settings.buttonPos);
m_dragButton->setZOrder(10000);
m_dragButton->setID("devtools-button"_spr);
OverlayManager::get()->addChild(m_dragButton);
}
void DevTools::removeDragButton() {
if (m_dragButton) {
m_dragButton->removeFromParent();
m_dragButton = nullptr;
}
}
bool DevTools::isButtonEnabled() {
#ifdef GEODE_IS_MOBILE
return true;
#else
return m_settings.buttonEnabled;
#endif
}
// Scroll when dragging empty space
void mobileScrollBehavior() {
auto* ctx = ImGui::GetCurrentContext();
auto* window = ctx->CurrentWindow;
if (!window) return;
bool hovered = false;
bool held = false;
ImGuiID id = window->GetID("##scroll_dragging_overlay");
ImGui::KeepAliveID(id);
// If nothing hovered so far in the frame (not same as IsAnyItemHovered()!)
if (ctx->HoveredId == 0) {
ImGui::ButtonBehavior(window->Rect(), id, &hovered, &held, ImGuiButtonFlags_MouseButtonLeft);
}
if (held) {
ImVec2 delta = ImGui::GetIO().MouseDownDuration[0] > 0.1 ? -ImGui::GetIO().MouseDelta : ImVec2(0, 0);
if (std::abs(delta.x) >= 0.1f || std::abs(delta.y) >= 0.1f) {
ImGui::SetScrollX(window, window->Scroll.x + delta.x);
ImGui::SetScrollY(window, window->Scroll.y + delta.y);
}
}
}
void DevTools::drawPage(const char* name, void(DevTools::*pageFun)()) {
if (ImGui::Begin(name, nullptr, ImGuiWindowFlags_HorizontalScrollbar)) {
(this->*pageFun)();
#ifdef GEODE_IS_MOBILE
mobileScrollBehavior();
#endif
}
ImGui::End();
}
void DevTools::drawPages() {
const auto size = CCDirector::sharedDirector()->getOpenGLView()->getFrameSize();
if ((!Mod::get()->setSavedValue("layout-loaded", true) || m_shouldRelayout)) {
m_shouldRelayout = false;
auto id = m_dockspaceID;
ImGui::DockBuilderRemoveNode(id);
ImGui::DockBuilderAddNode(id, ImGuiDockNodeFlags_PassthruCentralNode);
auto leftDock = ImGui::DockBuilderSplitNode(m_dockspaceID, ImGuiDir_Left, 0.3f, nullptr, &id);
auto topLeftDock = ImGui::DockBuilderSplitNode(leftDock, ImGuiDir_Up, 0.4f, nullptr, &leftDock);
auto bottomLeftTopHalfDock = ImGui::DockBuilderSplitNode(leftDock, ImGuiDir_Up, 0.6f, nullptr, &leftDock);
ImGui::DockBuilderDockWindow("###devtools/tree", topLeftDock);
ImGui::DockBuilderDockWindow("###devtools/settings", topLeftDock);
ImGui::DockBuilderDockWindow("###devtools/advanced/settings", topLeftDock);
ImGui::DockBuilderDockWindow("###devtools/attributes", bottomLeftTopHalfDock);
ImGui::DockBuilderDockWindow("###devtools/preview", leftDock);
ImGui::DockBuilderDockWindow("###devtools/geometry-dash", id);
ImGui::DockBuilderDockWindow("###devtools/advanced/mod-graph", topLeftDock);
ImGui::DockBuilderDockWindow("###devtools/advanced/mod-index", topLeftDock);
ImGui::DockBuilderFinish(id);
}
this->drawPage(
U8STR(FEATHER_GIT_MERGE " Tree###devtools/tree"),
&DevTools::drawTree
);
this->drawPage(
U8STR(FEATHER_SETTINGS " Settings###devtools/settings"),
&DevTools::drawSettings
);
// if advanced ever has more than one option, add it back
#if 0
if (m_settings.advancedSettings) {
this->drawPage(
U8STR(FEATHER_SETTINGS " Advanced Settings###devtools/advanced/settings"),
&DevTools::drawAdvancedSettings
);
}
#endif
this->drawPage(
U8STR(FEATHER_TOOL " Attributes###devtools/attributes"),
&DevTools::drawAttributes
);
// TODO: fix preview tab
#if 0
this->drawPage(
U8STR(FEATHER_DATABASE " Preview###devtools/preview"),
&DevTools::drawPreview
);
#endif
if (m_settings.showModGraph) {
this->drawPage(
U8STR(FEATHER_SHARE_2 " Mod Graph###devtools/advanced/mod-graph"),
&DevTools::drawModGraph
);
}
if (m_settings.showMemoryViewer) {
this->drawPage(
U8STR(FEATHER_TERMINAL " Memory viewer"),
&DevTools::drawMemory
);
}
}
void DevTools::draw(GLRenderCtx* ctx) {
if (m_visible) {
if (m_reloadTheme) {
applyTheme(m_settings.theme);
m_reloadTheme = false;
}
m_dockspaceID = ImGui::DockSpaceOverViewport(
0, nullptr, ImGuiDockNodeFlags_PassthruCentralNode
);
ImGui::PushFont(m_defaultFont);
this->drawPages();
if (m_selectedNode) {
this->highlightNode(m_selectedNode, HighlightMode::Selected);
}
if (this->shouldUseGDWindow()) this->drawGD(ctx);
ImGui::PopFont();
}
#ifdef GEODE_IS_WINDOWS
setMouseCursor();
#endif
}
void DevTools::setupFonts() {
static const ImWchar icon_ranges[] = { FEATHER_MIN_FA, FEATHER_MAX_FA, 0 };
static const ImWchar box_ranges[] = { BOX_DRAWING_MIN_FA, BOX_DRAWING_MAX_FA, 0 };
static const ImWchar* def_ranges = ImGui::GetIO().Fonts->GetGlyphRangesDefault();
static constexpr auto add_font = [](
void* font, size_t realSize, float size, const ImWchar* range
) {
auto& io = ImGui::GetIO();
// AddFontFromMemoryTTF assumes ownership of the passed data unless you configure it not to.
// Our font data has static lifetime, so we're handling the ownership.
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
auto* result = io.Fonts->AddFontFromMemoryTTF(
font, realSize, size, &config, range
);
config.MergeMode = true;
io.Fonts->AddFontFromMemoryTTF(
Font_FeatherIcons, sizeof(Font_FeatherIcons), size - 4.f, &config, icon_ranges
);
io.Fonts->Build();
return result;
};
m_defaultFont = add_font(Font_OpenSans, sizeof(Font_OpenSans), 18.f, def_ranges);
m_smallFont = add_font(Font_OpenSans, sizeof(Font_OpenSans), 10.f, def_ranges);
m_monoFont = add_font(Font_RobotoMono, sizeof(Font_RobotoMono), 18.f, def_ranges);
m_boxFont = add_font(Font_SourceCodeProLight, sizeof(Font_SourceCodeProLight), 23.f, box_ranges);
}
void DevTools::setup() {
if (m_setup) return;
m_setup = true;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
auto& io = ImGui::GetIO();
io.FontGlobalScale = m_settings.fontScale;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
// if this is true then it just doesnt work :( why
io.ConfigDockingWithShift = false;
// io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
io.ConfigWindowsResizeFromEdges = true;
this->setupFonts();
this->setupPlatform();
#ifdef GEODE_IS_MOBILE
ImGui::GetStyle().ScrollbarSize = 60.f;
// ImGui::GetStyle().TabBarBorderSize = 60.f;
#endif
}
void DevTools::destroy() {
if (!m_setup) return;
this->show(false);
auto& io = ImGui::GetIO();
io.BackendPlatformUserData = nullptr;
m_fontTexture->release();
m_fontTexture = nullptr;
ImGui::DestroyContext();
m_setup = false;
m_reloadTheme = true;
}
void DevTools::show(bool visible) {
m_visible = visible;
auto& io = ImGui::GetIO();
io.WantCaptureMouse = visible;
io.WantCaptureKeyboard = visible;
}
void DevTools::toggle() {
this->show(!m_visible);
}
bool DevTools::isVisible() {
return m_visible;
}
void DevTools::sceneChanged() {
m_selectedNode = nullptr;
}
bool DevTools::shouldUseGDWindow() const {
return Mod::get()->getSettingValue<bool>("should-use-gd-window");
}