Skip to content

Commit c0fef8e

Browse files
authored
some button changes (#70)
* make button no longer a singleton, fix button appearing on save regardless of platform, allow showing button on pc * oops * add DevTools::isButtonEnabled instead of using GEODE_DESKTOP everywhere
1 parent dbd8989 commit c0fef8e

File tree

6 files changed

+73
-41
lines changed

6 files changed

+73
-41
lines changed

src/DevTools.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <Geode/loader/Log.hpp>
1010
#include <Geode/loader/Mod.hpp>
1111
#include "ImGui.hpp"
12-
#include "nodes/DragButton.hpp"
1312

1413
template<>
1514
struct matjson::Serialize<Settings> {
@@ -35,6 +34,7 @@ struct matjson::Serialize<Settings> {
3534
assign(value["button_y"], s.buttonPos.y);
3635
assign(value["button_editor"], s.buttonInEditor);
3736
assign(value["button_game"], s.buttonInGame);
37+
assign(value["button_enabled"], s.buttonEnabled);
3838
assign(value["tree_drag_reorder"], s.treeDragReorder);
3939

4040
return Ok(s);
@@ -57,6 +57,7 @@ struct matjson::Serialize<Settings> {
5757
{ "button_y", settings.buttonPos.y },
5858
{ "button_editor", settings.buttonInEditor },
5959
{ "button_game", settings.buttonInGame },
60+
{ "button_enabled", settings.buttonEnabled },
6061
{ "tree_drag_reorder", settings.treeDragReorder }
6162
});
6263
}
@@ -70,11 +71,9 @@ DevTools* DevTools::get() {
7071
}
7172

7273
void DevTools::loadSettings() { m_settings = Mod::get()->getSavedValue<Settings>("settings"); }
73-
void DevTools::saveSettings() {
74-
m_settings.buttonPos = DragButton::get()->getPosition();
75-
Mod::get()->setSavedValue("settings", m_settings);
76-
}
74+
void DevTools::saveSettings() { Mod::get()->setSavedValue("settings", m_settings); }
7775
Settings DevTools::getSettings() { return m_settings; }
76+
void DevTools::setBallPosition(CCPoint pos) { m_settings.buttonPos = std::move(pos); }
7877

7978
bool DevTools::shouldPopGame() const {
8079
return m_visible && m_settings.GDInWindow;
@@ -116,6 +115,38 @@ void DevTools::addCustomCallback(std::function<void(CCNode*)> callback) {
116115
m_customCallbacks.push_back(std::move(callback));
117116
}
118117

118+
DragButton* DevTools::getDragButton() {
119+
return m_dragButton;
120+
}
121+
122+
void DevTools::setupDragButton() {
123+
auto spr = CircleButtonSprite::createWithSprite("devtools.png"_spr, 1, CircleBaseColor::Green, CircleBaseSize::MediumAlt);
124+
spr->setScale(.8f);
125+
m_dragButton = DragButton::create(spr, [this](){
126+
this->toggle();
127+
});
128+
m_dragButton->setPosition(m_settings.buttonPos);
129+
m_dragButton->setZOrder(10000);
130+
m_dragButton->setID("devtools-button"_spr);
131+
SceneManager::get()->keepAcrossScenes(m_dragButton);
132+
}
133+
134+
void DevTools::removeDragButton() {
135+
if (m_dragButton) {
136+
SceneManager::get()->forget(m_dragButton);
137+
m_dragButton->removeFromParent();
138+
m_dragButton = nullptr;
139+
}
140+
}
141+
142+
bool DevTools::isButtonEnabled() {
143+
#ifdef GEODE_IS_MOBILE
144+
return true;
145+
#else
146+
return m_settings.buttonEnabled;
147+
#endif
148+
}
149+
119150
// Scroll when dragging empty space
120151
void mobileScrollBehavior() {
121152
auto* ctx = ImGui::GetCurrentContext();

src/DevTools.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <Geode/loader/Loader.hpp>
1010
#include <Geode/loader/ModMetadata.hpp>
1111

12+
#include "nodes/DragButton.hpp"
13+
1214
using namespace geode::prelude;
1315

1416
enum class HighlightMode {
@@ -32,6 +34,7 @@ struct Settings {
3234
CCPoint buttonPos = {50, 50};
3335
bool buttonInEditor = false;
3436
bool buttonInGame = false;
37+
bool buttonEnabled = false;
3538
bool treeDragReorder = false;
3639
};
3740

@@ -57,6 +60,7 @@ class DevTools {
5760
std::string m_searchQuery;
5861
std::string m_prevQuery;
5962
std::unordered_map<CCNode*, bool> m_nodeOpen;
63+
DragButton* m_dragButton = nullptr;
6064

6165
void setupFonts();
6266
void setupPlatform();
@@ -103,6 +107,7 @@ class DevTools {
103107
void loadSettings();
104108
void saveSettings();
105109
Settings getSettings();
110+
void setBallPosition(CCPoint pos);
106111
bool shouldUseGDWindow() const;
107112

108113
bool shouldPopGame() const;
@@ -118,6 +123,11 @@ class DevTools {
118123

119124
void addCustomCallback(std::function<void(CCNode*)> callback);
120125

126+
DragButton* getDragButton();
127+
void setupDragButton();
128+
void removeDragButton();
129+
bool isButtonEnabled();
130+
121131
void sceneChanged();
122132

123133
void render(GLRenderCtx* ctx);

src/main.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,18 @@ class $modify(CCKeyboardDispatcher) {
3333
}
3434
};
3535

36-
#ifdef GEODE_IS_MOBILE
37-
3836
#include <Geode/loader/GameEvent.hpp>
3937
$execute {
4038
new EventListener<GameEventFilter>(+[](GameEvent*) {
41-
DragButton::get();
39+
if (DevTools::get()->isButtonEnabled()) DevTools::get()->setupDragButton();
4240
}, GameEventFilter(GameEventType::Loaded));
4341
}
4442

4543
#include <Geode/modify/CCScene.hpp>
4644
class $modify(CCScene) {
4745
int getHighestChildZ() {
48-
auto btn = DragButton::get();
46+
auto btn = DevTools::get()->getDragButton();
47+
if (!btn) return CCScene::getHighestChildZ();
4948
int z = btn->getZOrder();
5049
btn->setZOrder(-1);
5150
int ret = CCScene::getHighestChildZ();
@@ -54,8 +53,6 @@ class $modify(CCScene) {
5453
}
5554
};
5655

57-
#endif
58-
5956
class $modify(GameToolbox) {
6057
static void preVisitWithClippingRect(CCNode* node, CCRect clipRect) {
6158
if (!node->isVisible() || !DevTools::get()->isVisible())

src/nodes/DragButton.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../DevTools.hpp"
33

44
#include <Geode/Geode.hpp>
5+
#include <utility>
56

67
using namespace geode::prelude;
78

@@ -15,7 +16,7 @@ bool DragButton::init(CCNode* node, std::function<void()> onPress) {
1516
this->setContentSize(node->getScaledContentSize());
1617
this->addChildAtPosition(node, Anchor::Center, CCPoint{0, 0});
1718
}
18-
this->m_onPress = onPress;
19+
this->m_onPress = std::move(onPress);
1920
return true;
2021
}
2122

@@ -59,7 +60,7 @@ void DragButton::registerWithTouchDispatcher() {
5960

6061
DragButton* DragButton::create(CCNode* node, std::function<void ()> onPress) {
6162
auto ret = new DragButton;
62-
if (ret->init(node, onPress)) {
63+
if (ret->init(node, std::move(onPress))) {
6364
ret->autorelease();
6465
return ret;
6566
}
@@ -84,22 +85,6 @@ void DragButton::setPosition(cocos2d::CCPoint const& pos_) {
8485
auto pos = pos_;
8586
pos.x = std::clamp(pos.x, pad, winSize.width - pad);
8687
pos.y = std::clamp(pos.y, pad, winSize.height - pad);
88+
DevTools::get()->setBallPosition(pos);
8789
CCNode::setPosition(pos);
88-
}
89-
90-
DragButton* DragButton::get() {
91-
static DragButton* instance = nullptr;
92-
if (!instance) {
93-
auto spr = CircleButtonSprite::createWithSprite("devtools.png"_spr, 1, CircleBaseColor::Green, CircleBaseSize::MediumAlt);
94-
spr->setScale(.8f);
95-
instance = DragButton::create(spr, [](){
96-
DevTools::get()->toggle();
97-
});
98-
instance->setPosition(DevTools::get()->getSettings().buttonPos);
99-
instance->setZOrder(10000);
100-
instance->setID("devtools-button"_spr);
101-
CCScene::get()->addChild(instance);
102-
SceneManager::get()->keepAcrossScenes(instance);
103-
}
104-
return instance;
105-
}
90+
}

src/nodes/DragButton.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "Geode/cocos/base_nodes/CCNode.h"
44
#include "Geode/cocos/layers_scenes_transitions_nodes/CCLayer.h"
5-
#include "Geode/cocos/touch_dispatcher/CCTouchDelegateProtocol.h"
65

76
class DragButton : public cocos2d::CCLayer {
87
protected:
@@ -20,7 +19,6 @@ class DragButton : public cocos2d::CCLayer {
2019
void update(float dt) override;
2120
public:
2221
static DragButton* create(cocos2d::CCNode* node, std::function<void()> onPress);
23-
static DragButton* get();
2422

2523
void setPosition(cocos2d::CCPoint const& position) override;
2624
};

src/pages/Settings.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,30 @@ void DevTools::drawSettings() {
161161
static_cast<int>(frameSize.width / ratio),
162162
static_cast<int>(frameSize.height / ratio)
163163
);
164-
#else
165-
ImGui::Checkbox("Button In Editor", &m_settings.buttonInEditor);
166-
if (ImGui::IsItemHovered()) {
167-
ImGui::SetTooltip(
168-
"Shows the mobile button in the editor."
169-
);
164+
if (ImGui::Checkbox("Button Enabled", &m_settings.buttonEnabled)) {
165+
if (m_settings.buttonEnabled) setupDragButton();
166+
else removeDragButton();
170167
}
171-
ImGui::Checkbox("Button In Game", &m_settings.buttonInGame);
172168
if (ImGui::IsItemHovered()) {
173169
ImGui::SetTooltip(
174-
"Shows the mobile button in levels."
170+
"Shows the mobile button."
175171
);
176172
}
177173
#endif
174+
if (isButtonEnabled()) {
175+
ImGui::Checkbox("Button In Editor", &m_settings.buttonInEditor);
176+
if (ImGui::IsItemHovered()) {
177+
ImGui::SetTooltip(
178+
"Shows the mobile button in the editor."
179+
);
180+
}
181+
ImGui::Checkbox("Button In Game", &m_settings.buttonInGame);
182+
if (ImGui::IsItemHovered()) {
183+
ImGui::SetTooltip(
184+
"Shows the mobile button in levels."
185+
);
186+
}
187+
}
178188

179189
#if 0
180190
static Ref<CCSet> PAUSED_TARGETS = nullptr;
@@ -261,3 +271,4 @@ void DevTools::drawSettings() {
261271
}
262272
}
263273
};*/
274+

0 commit comments

Comments
 (0)