mirror of
https://github.com/geode-sdk/geode.git
synced 2025-02-17 00:30:26 -05:00
Add OverlayManager
This commit is contained in:
parent
d6f0c597f1
commit
03e8220dfe
2 changed files with 119 additions and 0 deletions
44
loader/include/Geode/ui/OverlayManager.hpp
Normal file
44
loader/include/Geode/ui/OverlayManager.hpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#pragma once
|
||||
|
||||
#include "../DefaultInclude.hpp"
|
||||
|
||||
#include <cocos2d.h>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <Geode/utils/cocos.hpp>
|
||||
|
||||
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<cocos2d::CCNode*> 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<cocos2d::CCNode*> getOverlays();
|
||||
};
|
||||
};
|
75
loader/src/ui/nodes/OverlayManager.cpp
Normal file
75
loader/src/ui/nodes/OverlayManager.cpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <Geode/ui/OverlayManager.hpp>
|
||||
|
||||
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<CCNode*> OverlayManager::getOverlays()
|
||||
{
|
||||
return nodes;
|
||||
}
|
Loading…
Reference in a new issue