From 03e8220dfec34208c0d1ee0af12cb49c8953258d Mon Sep 17 00:00:00 2001 From: Explodingbill Date: Sat, 16 Nov 2024 13:48:59 +1100 Subject: [PATCH 1/2] Add OverlayManager --- loader/include/Geode/ui/OverlayManager.hpp | 44 +++++++++++++ loader/src/ui/nodes/OverlayManager.cpp | 75 ++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 loader/include/Geode/ui/OverlayManager.hpp create mode 100644 loader/src/ui/nodes/OverlayManager.cpp diff --git a/loader/include/Geode/ui/OverlayManager.hpp b/loader/include/Geode/ui/OverlayManager.hpp new file mode 100644 index 00000000..a569d345 --- /dev/null +++ b/loader/include/Geode/ui/OverlayManager.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "../DefaultInclude.hpp" + +#include +#include +#include +#include + +namespace geode +{ + /* + Because cocos only allows for one notification node (a node drawn last, above the fps counter and everything), + I added this, a simple class to add nodes to a general notification node so that mods dont interfere with each other. + */ + + class GEODE_DLL OverlayManager : public cocos2d::CCNode + { + private: + std::vector nodes; + + public: + /// @brief Get the overlay manager instance, and if it doesnt exist, sets the notification node to it + static OverlayManager* get(); + + /// @brief Adds a node to the overlay manager, overlays are sorted by ZOrder, the higher the order is the later it draws. This will retain the node + void addNode(cocos2d::CCNode* node); + + /// @brief Removes a node from the overlay manager, stopping it from being drawn. This will release the node + void removeNode(cocos2d::CCNode* node); + + /// @brief Sorts all overlays by their ZOrder and draws them + virtual void visit(); + + /// @brief Util to get the highest ZOrder of all nodes + int getHighestOverlayZOrder(); + + /// @brief Util to get the lowest ZOrder of all nodes + int getLowestOverlayZOrder(); + + /// @brief Gets all the overlays + std::vector getOverlays(); + }; +}; \ No newline at end of file diff --git a/loader/src/ui/nodes/OverlayManager.cpp b/loader/src/ui/nodes/OverlayManager.cpp new file mode 100644 index 00000000..012a62a7 --- /dev/null +++ b/loader/src/ui/nodes/OverlayManager.cpp @@ -0,0 +1,75 @@ +#include + +using namespace geode::prelude; + +OverlayManager* OverlayManager::get() +{ + static OverlayManager* instance; + + if (!instance) + { + instance = new OverlayManager(); + + CCDirector::get()->setNotificationNode(instance); + } + + return instance; +} + +void OverlayManager::addNode(CCNode* node) +{ + node->retain(); + + nodes.push_back(node); +} + +void OverlayManager::removeNode(CCNode* node) +{ + node->release(); + + std::erase(nodes, node); +} + +void OverlayManager::visit() +{ + std::sort(nodes.begin(), nodes.end(), [](CCNode* a, CCNode* b) + { + return a->getZOrder() < b->getZOrder(); + }); + + for (auto node : nodes) + { + node->visit(); + } +} + +int OverlayManager::getHighestOverlayZOrder() +{ + int z = 0; + + for (auto node : nodes) + { + if (node->getZOrder() > z) + z = node->getZOrder(); + } + + return z; +} + +int OverlayManager::getLowestOverlayZOrder() +{ + int z = 0; + + for (auto node : nodes) + { + if (node->getZOrder() < z) + z = node->getZOrder(); + } + + return z; +} + +std::vector OverlayManager::getOverlays() +{ + return nodes; +} \ No newline at end of file From bd6bfb661f1c92dd6cfa0e269eecbf0c93aad4aa Mon Sep 17 00:00:00 2001 From: Explodingbill Date: Sat, 16 Nov 2024 15:03:10 +1100 Subject: [PATCH 2/2] asdf --- loader/include/Geode/ui/OverlayManager.hpp | 5 +---- loader/src/ui/nodes/OverlayManager.cpp | 21 ++++----------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/loader/include/Geode/ui/OverlayManager.hpp b/loader/include/Geode/ui/OverlayManager.hpp index a569d345..cb853163 100644 --- a/loader/include/Geode/ui/OverlayManager.hpp +++ b/loader/include/Geode/ui/OverlayManager.hpp @@ -14,7 +14,7 @@ namespace geode I added this, a simple class to add nodes to a general notification node so that mods dont interfere with each other. */ - class GEODE_DLL OverlayManager : public cocos2d::CCNode + class GEODE_DLL OverlayManager : private cocos2d::CCNode { private: std::vector nodes; @@ -29,9 +29,6 @@ namespace geode /// @brief Removes a node from the overlay manager, stopping it from being drawn. This will release the node void removeNode(cocos2d::CCNode* node); - /// @brief Sorts all overlays by their ZOrder and draws them - virtual void visit(); - /// @brief Util to get the highest ZOrder of all nodes int getHighestOverlayZOrder(); diff --git a/loader/src/ui/nodes/OverlayManager.cpp b/loader/src/ui/nodes/OverlayManager.cpp index 012a62a7..c76131bb 100644 --- a/loader/src/ui/nodes/OverlayManager.cpp +++ b/loader/src/ui/nodes/OverlayManager.cpp @@ -18,34 +18,21 @@ OverlayManager* OverlayManager::get() void OverlayManager::addNode(CCNode* node) { - node->retain(); + this->addChild(node); nodes.push_back(node); } void OverlayManager::removeNode(CCNode* node) { - node->release(); + this->removeChild(node); std::erase(nodes, node); } -void OverlayManager::visit() -{ - std::sort(nodes.begin(), nodes.end(), [](CCNode* a, CCNode* b) - { - return a->getZOrder() < b->getZOrder(); - }); - - for (auto node : nodes) - { - node->visit(); - } -} - int OverlayManager::getHighestOverlayZOrder() { - int z = 0; + int z = INT_MIN; for (auto node : nodes) { @@ -58,7 +45,7 @@ int OverlayManager::getHighestOverlayZOrder() int OverlayManager::getLowestOverlayZOrder() { - int z = 0; + int z = INT_MAX; for (auto node : nodes) {