|
| 1 | +#include <Geode/Geode.hpp> |
| 2 | +#include "SelectButtonManager.hpp" |
| 3 | + |
| 4 | + |
| 5 | +using namespace geode::prelude; |
| 6 | + |
| 7 | +SelectButtonManager* SelectButtonManager::get(){ |
| 8 | + static SelectButtonManager* instance = nullptr; |
| 9 | + if(!instance){ |
| 10 | + instance = new SelectButtonManager; |
| 11 | + } |
| 12 | + CCScheduler::get()->scheduleUpdateForTarget(instance, 0, false); |
| 13 | + return instance; |
| 14 | +} |
| 15 | + |
| 16 | +void SelectButtonManager::setButtons(){ |
| 17 | + |
| 18 | + ButtonActions actions; |
| 19 | + actions.selected = MenuCallback([this](CCMenuItem* sender){ |
| 20 | + /*if("change-button-state"_spr == sender->getID()){ |
| 21 | + sender->selected(); |
| 22 | + return; |
| 23 | + }*/ |
| 24 | + if(s_popUp && !sender->hasAncestor(s_popUp.data())){ |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + m_currBtn = sender; |
| 29 | + CCScheduler::get()->scheduleSelector(schedule_selector(SelectButtonManager::over), this, 0, 0, 0.5, false); |
| 30 | + sender->selected(); |
| 31 | + }); |
| 32 | + actions.unselected = MenuCallback([this](CCMenuItem* sender){ |
| 33 | + |
| 34 | + /*if("change-button-state"_spr == sender->getID()){ |
| 35 | + sender->unselected(); |
| 36 | + return; |
| 37 | + }*/ |
| 38 | + |
| 39 | + m_currBtn = nullptr; |
| 40 | + CCScheduler::get()->unscheduleSelector(schedule_selector(SelectButtonManager::over), this); |
| 41 | + //ButtonActionManager::get()->resetActivate(); |
| 42 | + sender->unselected(); |
| 43 | + }); |
| 44 | + |
| 45 | + actions.activate = MenuCallback([this](CCMenuItem* sender){ |
| 46 | + if(s_popUp && !sender->hasAncestor(s_popUp)){ |
| 47 | + return; |
| 48 | + } |
| 49 | + s_popUp = nullptr; |
| 50 | + sender->activate(); |
| 51 | + }); |
| 52 | + ButtonActionManager::get()->setActions(actions); |
| 53 | +} |
| 54 | + |
| 55 | +void SelectButtonManager::toggleManager(CCObject*){ |
| 56 | + static bool state = true; |
| 57 | + if(state){ |
| 58 | + state = false; |
| 59 | + //log::debug("haiufhsi"); |
| 60 | + setButtons(); |
| 61 | + return; |
| 62 | + } |
| 63 | + ButtonActionManager::get()->resetSelected(); |
| 64 | + state = true; |
| 65 | +} |
| 66 | + |
| 67 | +CCNode* SelectButtonManager::getTopLayer(CCNode* curr){ |
| 68 | + while(curr && !typeinfo_cast<CCScene*>(curr->getParent())){ // while parent exists and parent is not CCScene* |
| 69 | + curr = curr->getParent(); |
| 70 | + } |
| 71 | + return curr; |
| 72 | +} |
| 73 | + |
| 74 | +Result<std::vector<std::string>> SelectButtonManager::getPath(CCNode* curr){ |
| 75 | + if(curr->getID() == ""){ |
| 76 | + return Err("The button you tried to select does not have an ID."); |
| 77 | + } |
| 78 | + |
| 79 | + std::vector<std::string> path; |
| 80 | + path.push_back(curr->getID()); |
| 81 | + while(curr && !typeinfo_cast<CCScene*>(curr->getParent())){ // while parent exists and parent is not CCScene* |
| 82 | + curr = curr->getParent(); |
| 83 | + if(curr->getID() == ""){ |
| 84 | + return Err("One of the nodes in the path does not have an ID."); |
| 85 | + } |
| 86 | + path.push_back(curr->getID()); |
| 87 | + } |
| 88 | + return Ok(path); |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +// int getNumID(CCNode* node) { |
| 94 | +// auto parent = node->getParent(); |
| 95 | +// auto siblings = parent->getChildren(); |
| 96 | +// return siblings->indexOfObject(node); |
| 97 | +// } |
| 98 | + |
| 99 | +// std::vector<NodeID> getPathNum(CCNode* curr){ |
| 100 | +// std::vector<NodeID> path; |
| 101 | +// //struct NodeID test = {curr->getID(),getNumID(curr)}; |
| 102 | +// path.push_back({curr->getID(),getNumID(curr)}); |
| 103 | +// while(curr && !typeinfo_cast<CCScene*>(curr->getParent())){ // while parent exists and parent is not CCScene* |
| 104 | +// curr = curr->getParent(); |
| 105 | +// path.push_back({curr->getID(),getNumID(curr)}); |
| 106 | +// } |
| 107 | +// return path; |
| 108 | +// } |
| 109 | + |
| 110 | +void SelectButtonManager::over(float df){ |
| 111 | + |
| 112 | + //because evil |
| 113 | + auto evil = SelectButtonManager::get(); |
| 114 | + if(evil->m_currBtn){ |
| 115 | + |
| 116 | + //log::debug("{}", evil->m_currBtn->getID()); |
| 117 | + auto top = getTopLayer(evil->m_currBtn); |
| 118 | + std::string topName = ""; |
| 119 | + if(top){ |
| 120 | + topName = top->getID(); |
| 121 | + } |
| 122 | + auto path = getPath(evil->m_currBtn); |
| 123 | + auto pathNum = NodePath::create(evil->m_currBtn); |
| 124 | + |
| 125 | + s_popUp = FLAlertLayer::create("Selector", fmt::format("{} selected", pathNum.to_string()), "OK"); |
| 126 | + |
| 127 | + s_popUp->setID("SelectAlert"_spr); |
| 128 | + s_popUp->show(); |
| 129 | + |
| 130 | + /*if(path.isErr()){ |
| 131 | + FLAlertLayer::create("Selector", fmt::format("{}", path.err().value()), "OK")->show(); |
| 132 | + } else { |
| 133 | + FLAlertLayer::create("Selector", fmt::format("{} selected", path.ok().value()), "OK")->show(); |
| 134 | + }*/ |
| 135 | + |
| 136 | + SelectButtonManager::get()->m_currBtn->unselected(); |
| 137 | + } |
| 138 | +} |
0 commit comments