diff --git a/src/DevTools.cpp b/src/DevTools.cpp index 8d752c9..9694496 100644 --- a/src/DevTools.cpp +++ b/src/DevTools.cpp @@ -9,7 +9,6 @@ #include #include #include "ImGui.hpp" -#include "nodes/DragButton.hpp" template<> struct matjson::Serialize { @@ -35,6 +34,7 @@ struct matjson::Serialize { 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); @@ -57,6 +57,7 @@ struct matjson::Serialize { { "button_y", settings.buttonPos.y }, { "button_editor", settings.buttonInEditor }, { "button_game", settings.buttonInGame }, + { "button_enabled", settings.buttonEnabled }, { "tree_drag_reorder", settings.treeDragReorder } }); } @@ -70,11 +71,9 @@ DevTools* DevTools::get() { } void DevTools::loadSettings() { m_settings = Mod::get()->getSavedValue("settings"); } -void DevTools::saveSettings() { - m_settings.buttonPos = DragButton::get()->getPosition(); - Mod::get()->setSavedValue("settings", m_settings); -} +void DevTools::saveSettings() { Mod::get()->setSavedValue("settings", m_settings); } 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; @@ -116,6 +115,38 @@ void DevTools::addCustomCallback(std::function callback) { m_customCallbacks.push_back(std::move(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); + SceneManager::get()->keepAcrossScenes(m_dragButton); +} + +void DevTools::removeDragButton() { + if (m_dragButton) { + SceneManager::get()->forget(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(); diff --git a/src/DevTools.hpp b/src/DevTools.hpp index f6a808a..0a223e5 100644 --- a/src/DevTools.hpp +++ b/src/DevTools.hpp @@ -9,6 +9,8 @@ #include #include +#include "nodes/DragButton.hpp" + using namespace geode::prelude; enum class HighlightMode { @@ -32,6 +34,7 @@ struct Settings { CCPoint buttonPos = {50, 50}; bool buttonInEditor = false; bool buttonInGame = false; + bool buttonEnabled = false; bool treeDragReorder = false; }; @@ -57,6 +60,7 @@ class DevTools { std::string m_searchQuery; std::string m_prevQuery; std::unordered_map m_nodeOpen; + DragButton* m_dragButton = nullptr; void setupFonts(); void setupPlatform(); @@ -103,6 +107,7 @@ class DevTools { void loadSettings(); void saveSettings(); Settings getSettings(); + void setBallPosition(CCPoint pos); bool shouldUseGDWindow() const; bool shouldPopGame() const; @@ -118,6 +123,11 @@ class DevTools { void addCustomCallback(std::function callback); + DragButton* getDragButton(); + void setupDragButton(); + void removeDragButton(); + bool isButtonEnabled(); + void sceneChanged(); void render(GLRenderCtx* ctx); diff --git a/src/main.cpp b/src/main.cpp index 99c24ed..edcd624 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,19 +33,18 @@ class $modify(CCKeyboardDispatcher) { } }; -#ifdef GEODE_IS_MOBILE - #include $execute { new EventListener(+[](GameEvent*) { - DragButton::get(); + if (DevTools::get()->isButtonEnabled()) DevTools::get()->setupDragButton(); }, GameEventFilter(GameEventType::Loaded)); } #include class $modify(CCScene) { int getHighestChildZ() { - auto btn = DragButton::get(); + auto btn = DevTools::get()->getDragButton(); + if (!btn) return CCScene::getHighestChildZ(); int z = btn->getZOrder(); btn->setZOrder(-1); int ret = CCScene::getHighestChildZ(); @@ -54,8 +53,6 @@ class $modify(CCScene) { } }; -#endif - class $modify(GameToolbox) { static void preVisitWithClippingRect(CCNode* node, CCRect clipRect) { if (!node->isVisible() || !DevTools::get()->isVisible()) diff --git a/src/nodes/DragButton.cpp b/src/nodes/DragButton.cpp index 19e7369..716d99e 100644 --- a/src/nodes/DragButton.cpp +++ b/src/nodes/DragButton.cpp @@ -2,6 +2,7 @@ #include "../DevTools.hpp" #include +#include using namespace geode::prelude; @@ -15,7 +16,7 @@ bool DragButton::init(CCNode* node, std::function onPress) { this->setContentSize(node->getScaledContentSize()); this->addChildAtPosition(node, Anchor::Center, CCPoint{0, 0}); } - this->m_onPress = onPress; + this->m_onPress = std::move(onPress); return true; } @@ -59,7 +60,7 @@ void DragButton::registerWithTouchDispatcher() { DragButton* DragButton::create(CCNode* node, std::function onPress) { auto ret = new DragButton; - if (ret->init(node, onPress)) { + if (ret->init(node, std::move(onPress))) { ret->autorelease(); return ret; } @@ -84,22 +85,6 @@ void DragButton::setPosition(cocos2d::CCPoint const& pos_) { auto pos = pos_; pos.x = std::clamp(pos.x, pad, winSize.width - pad); pos.y = std::clamp(pos.y, pad, winSize.height - pad); + DevTools::get()->setBallPosition(pos); CCNode::setPosition(pos); -} - -DragButton* DragButton::get() { - static DragButton* instance = nullptr; - if (!instance) { - auto spr = CircleButtonSprite::createWithSprite("devtools.png"_spr, 1, CircleBaseColor::Green, CircleBaseSize::MediumAlt); - spr->setScale(.8f); - instance = DragButton::create(spr, [](){ - DevTools::get()->toggle(); - }); - instance->setPosition(DevTools::get()->getSettings().buttonPos); - instance->setZOrder(10000); - instance->setID("devtools-button"_spr); - CCScene::get()->addChild(instance); - SceneManager::get()->keepAcrossScenes(instance); - } - return instance; -} +} \ No newline at end of file diff --git a/src/nodes/DragButton.hpp b/src/nodes/DragButton.hpp index e4b70c0..9325b51 100644 --- a/src/nodes/DragButton.hpp +++ b/src/nodes/DragButton.hpp @@ -2,7 +2,6 @@ #include "Geode/cocos/base_nodes/CCNode.h" #include "Geode/cocos/layers_scenes_transitions_nodes/CCLayer.h" -#include "Geode/cocos/touch_dispatcher/CCTouchDelegateProtocol.h" class DragButton : public cocos2d::CCLayer { protected: @@ -20,7 +19,6 @@ class DragButton : public cocos2d::CCLayer { void update(float dt) override; public: static DragButton* create(cocos2d::CCNode* node, std::function onPress); - static DragButton* get(); void setPosition(cocos2d::CCPoint const& position) override; }; diff --git a/src/pages/Settings.cpp b/src/pages/Settings.cpp index eb5d95f..57f4b4e 100644 --- a/src/pages/Settings.cpp +++ b/src/pages/Settings.cpp @@ -161,20 +161,30 @@ void DevTools::drawSettings() { static_cast(frameSize.width / ratio), static_cast(frameSize.height / ratio) ); -#else - ImGui::Checkbox("Button In Editor", &m_settings.buttonInEditor); - if (ImGui::IsItemHovered()) { - ImGui::SetTooltip( - "Shows the mobile button in the editor." - ); + if (ImGui::Checkbox("Button Enabled", &m_settings.buttonEnabled)) { + if (m_settings.buttonEnabled) setupDragButton(); + else removeDragButton(); } - ImGui::Checkbox("Button In Game", &m_settings.buttonInGame); if (ImGui::IsItemHovered()) { ImGui::SetTooltip( - "Shows the mobile button in levels." + "Shows the mobile button." ); } #endif + if (isButtonEnabled()) { + ImGui::Checkbox("Button In Editor", &m_settings.buttonInEditor); + if (ImGui::IsItemHovered()) { + ImGui::SetTooltip( + "Shows the mobile button in the editor." + ); + } + ImGui::Checkbox("Button In Game", &m_settings.buttonInGame); + if (ImGui::IsItemHovered()) { + ImGui::SetTooltip( + "Shows the mobile button in levels." + ); + } + } #if 0 static Ref PAUSED_TARGETS = nullptr; @@ -261,3 +271,4 @@ void DevTools::drawSettings() { } } };*/ +