Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OverlayManager #1157

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions loader/include/Geode/ui/OverlayManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#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 : private 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 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();
};
};
62 changes: 62 additions & 0 deletions loader/src/ui/nodes/OverlayManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#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)
{
this->addChild(node);

nodes.push_back(node);
}

void OverlayManager::removeNode(CCNode* node)
{
this->removeChild(node);

std::erase(nodes, node);
}

int OverlayManager::getHighestOverlayZOrder()
{
int z = INT_MIN;

for (auto node : nodes)
{
if (node->getZOrder() > z)
z = node->getZOrder();
}

return z;
}

int OverlayManager::getLowestOverlayZOrder()
{
int z = INT_MAX;

for (auto node : nodes)
{
if (node->getZOrder() < z)
z = node->getZOrder();
}

return z;
}

std::vector<CCNode*> OverlayManager::getOverlays()
{
return nodes;
}