new id registering design that's pretty good now

This commit is contained in:
HJfod 2022-10-22 22:10:36 +03:00
parent 46381e7cbc
commit 40300afb25
12 changed files with 120 additions and 135 deletions

View file

@ -22,6 +22,7 @@ namespace { namespace format_strings {
char const* class_start = R"GEN(
class {class_name}{base_classes} {{
public:
static constexpr const char* CLASS_NAME = "{class_name}";
)GEN";
char const* monostate_constructor = R"GEN( GEODE_MONOSTATE_CONSTRUCTOR_GD({class_name}, {first_base})

View file

@ -34,9 +34,6 @@ namespace geode::modifier {{
static void apply() {{
using namespace geode::core::meta;
if constexpr (HasStringIDProvider<Derived>) {{
geode::NodeStringIDManager::get()->registerProvider("{class_name}", &Derived::provideStringIDs);
}}
)GEN";
// requires: index, class_name, arg_types, function_name, raw_arg_types, non_virtual

View file

@ -1,8 +1,8 @@
#pragma once
#include "ccMacros.h"
#include "cocoa/CCAffineTransform.h"
#include "cocoa/CCArray.h"
#include "../include/ccMacros.h"
#include "../cocoa/CCAffineTransform.h"
#include "../cocoa/CCArray.h"
#include <Geode/platform/platform.hpp>
#include <optional>
#include <unordered_map>

View file

@ -7,6 +7,11 @@ namespace cocos2d {
}
namespace geode {
template<class T>
concept IDProvidable = requires {
{ T::CLASS_NAME } -> std::convertible_to<const char*>;
};
class GEODE_DLL NodeStringIDManager {
public:
template<class T>
@ -18,20 +23,39 @@ namespace geode {
public:
static NodeStringIDManager* get();
template<class T>
void registerProvider(std::string const& id, void(GEODE_CALL* fun)(T*)) {
m_providers.insert({ id, static_cast<Provider<cocos2d::CCNode>>(fun) });
template<IDProvidable T>
void registerProvider(void(GEODE_CALL* fun)(T*)) {
m_providers.insert({
T::CLASS_NAME,
reinterpret_cast<Provider<cocos2d::CCNode>>(fun)
});
}
bool provide(std::string const& id, cocos2d::CCNode* layer);
template<IDProvidable T>
bool provide(T* layer) const {
if (m_providers.count(T::CLASS_NAME)) {
m_providers.at(T::CLASS_NAME)(layer);
return true;
}
return false;
}
};
}
#define $register_ids(Layer_, Var_) \
void GEODE_CALL GEODE_CONCAT(addIDs, Layer_)(Layer_*);\
namespace geode {
template<class For>
void GEODE_CALL geodeInternalProvideIDsFor(For* cls) {
cls->provide();
}
}
#define $register_ids(Layer_, ...) \
struct GEODE_CONCAT(ProvideIDsFor, Layer_);\
$execute {\
StringIDManager::get()->registerProvider(#Layer_, &GEODE_CONCAT(addIDs, Layer_));\
NodeStringIDManager::get()->registerProvider(\
&geodeInternalProvideIDsFor<GEODE_CONCAT(ProvideIDsFor, Layer_)>\
);\
};\
void GEODE_CALL GEODE_CONCAT(addIDs, Layer_)(Layer_* Var_)
struct GEODE_CONCAT(ProvideIDsFor, Layer_) : public Layer_ {\
void provide() __VA_ARGS__\
};\

View file

@ -1,5 +1,4 @@
#include <type_traits>
#include "IDManager.hpp"
/**
* Main class implementation, it has the structure
@ -36,10 +35,6 @@ namespace {
derived##Intermediate, \
derived<derived##Parent> \
> m_fields; \
template<class = void> \
bool addStringIDs() { \
return geode::NodeStringIDManager::get()->provide(#base, this); \
} \
}; \
} \
template <> struct GEODE_HIDDEN derived<derived##Parent> : derived##Intermediate \
@ -54,10 +49,6 @@ namespace {
derived##Intermediate, \
derived \
> m_fields; \
template<class = void> \
bool addStringIDs() { \
return geode::NodeStringIDManager::get()->provide(#base, this); \
} \
}; \
} \
struct GEODE_HIDDEN derived : derived##Intermediate \
@ -92,16 +83,14 @@ struct GEODE_HIDDEN derived : derived##Intermediate
#define GEODE_FIELD(type, field, name, default_) GEODE_ONLY_FIELD(type, field, default_) GEODE_INTERNAL_FIELD(type, field, name) //GEODE_EXTERNAL_FIELD(type, field, name)
#define GEODE_EXECUTE_FUNC(Line_) \
#define $execute \
template<class> \
void _##Line_##Function(); \
void GEODE_CONCAT(geodeExecFunction, __LINE__)(); \
namespace { \
struct _##Line_##Unique {}; \
struct GEODE_CONCAT(ExecFuncUnique, __LINE__) {}; \
} \
static inline auto _line = (Loader::get()->scheduleOnModLoad( \
nullptr, &_##Line_##Function<_##Line_##Unique> \
nullptr, &GEODE_CONCAT(geodeExecFunction, __LINE__)<GEODE_CONCAT(ExecFuncUnique, __LINE__)> \
), 0); \
template<class> \
void _##Line_##Function()
#define $execute GEODE_EXECUTE_FUNC(__LINE__)
void GEODE_CONCAT(geodeExecFunction, __LINE__)()

View file

@ -19,11 +19,6 @@ if constexpr (
); \
} \
template<class T>
concept HasStringIDProvider = requires {
T::provideStringIDs();
};
namespace geode::modifier {
template <class Derived, class Base>
class Modify;

View file

@ -1,7 +1,7 @@
#include <cocos2d.h>
#include <Geode/utils/WackyGeodeMacros.hpp>
#include <Geode/utils/cocos.hpp>
using namespace cocos2d;
USE_GEODE_NAMESPACE();
#pragma warning(disable: 4273)
@ -11,11 +11,13 @@ void CCNode::swapChildIndices(CCNode* first, CCNode* second) {
void RowLayout::apply(CCArray* nodes, CCSize const& availableSize) {
float totalWidth = .0f;
CCARRAY_FOREACH_B_BASE(nodes, node, CCNode*, ix) {
size_t ix = 0;
for (auto& node : CCArrayExt<CCNode*>(nodes)) {
totalWidth += node->getScaledContentSize().width;
if (ix) {
totalWidth += m_gap;
}
ix++;
}
float pos;
@ -25,7 +27,7 @@ void RowLayout::apply(CCArray* nodes, CCSize const& availableSize) {
case Alignment::Begin: pos = -totalWidth; break;
case Alignment::End: pos = 0.f; break;
}
CCARRAY_FOREACH_B_TYPE(nodes, node, CCNode) {
for (auto& node : CCArrayExt<CCNode*>(nodes)) {
auto sw = node->getScaledContentSize().width;
float disp;
switch (m_alignment) {
@ -69,7 +71,8 @@ RowLayout* RowLayout::setAlignVertically(std::optional<float> align) {
void ColumnLayout::apply(CCArray* nodes, CCSize const& availableSize) {
float totalHeight = .0f;
CCARRAY_FOREACH_B_BASE(nodes, node, CCNode*, ix) {
size_t ix = 0;
for (auto& node : CCArrayExt<CCNode*>(nodes)) {
totalHeight += node->getScaledContentSize().height;
if (ix) {
totalHeight += m_gap;
@ -83,7 +86,7 @@ void ColumnLayout::apply(CCArray* nodes, CCSize const& availableSize) {
case Alignment::Begin: pos = -totalHeight; break;
case Alignment::End: pos = 0.f; break;
}
CCARRAY_FOREACH_B_TYPE(nodes, node, CCNode) {
for (auto& node : CCArrayExt<CCNode*>(nodes)) {
auto sh = node->getScaledContentSize().height;
float disp;
switch (m_alignment) {

View file

@ -2,11 +2,8 @@
#include <Geode/utils/Ref.hpp>
#include <Geode/utils/cocos.hpp>
#include <Geode/modify/Field.hpp>
<<<<<<< HEAD
#include <Geode/utils/WackyGeodeMacros.hpp>
=======
#include <Geode/modify/CCNode.hpp>
>>>>>>> main
USE_GEODE_NAMESPACE();
using namespace geode::modifier;

View file

@ -80,7 +80,7 @@ class $modify(CustomMenuLayer, MenuLayer) {
// make sure to add the string IDs for nodes (Geode has no manual
// hook order support yet so gotta do this to ensure)
addIDsToMenuLayer(this);
NodeStringIDManager::get()->provide(this);
auto winSize = CCDirector::sharedDirector()->getWinSize();

View file

@ -5,8 +5,6 @@
USE_GEODE_NAMESPACE();
void addIDsToMenuLayer(MenuLayer* layer);
template<class T = CCNode>
requires std::is_base_of_v<CCNode, T>
T* setIDSafe(CCNode* node, int index, const char* id) {

View file

@ -6,11 +6,3 @@ NodeStringIDManager* NodeStringIDManager::get() {
static auto inst = new NodeStringIDManager;
return inst;
}
bool NodeStringIDManager::provide(std::string const& id, cocos2d::CCNode* layer) {
if (m_providers.count(id)) {
m_providers.at(id)(layer);
return true;
}
return false;
}

View file

@ -5,95 +5,84 @@
USE_GEODE_NAMESPACE();
class $modify(CustomMenuLayer, MenuLayer) {
void provideStringIDs() {
// set IDs to everything
this->setID("MenuLayer");
setIDSafe(this, 0, "main-menu-bg");
setIDSafe<CCSprite>(this, 0, "main-title");
$register_ids(MenuLayer, {
// set IDs to everything
this->setID("MenuLayer");
setIDSafe(this, 0, "main-menu-bg");
setIDSafe<CCSprite>(this, 0, "main-title");
// controller
if (PlatformToolbox::isControllerConnected()) {
setIDSafe<CCSprite>(this, 1, "play-gamepad-icon");
setIDSafe<CCSprite>(this, 2, "editor-gamepad-icon");
setIDSafe<CCSprite>(this, 3, "icon-kit-gamepad-icon");
// controller
if (PlatformToolbox::isControllerConnected()) {
setIDSafe<CCSprite>(this, 1, "play-gamepad-icon");
setIDSafe<CCSprite>(this, 2, "editor-gamepad-icon");
setIDSafe<CCSprite>(this, 3, "icon-kit-gamepad-icon");
setIDSafe<CCSprite>(this, 4, "settings-gamepad-icon");
setIDSafe<CCSprite>(this, 5, "mouse-gamepad-icon");
setIDSafe<CCSprite>(this, 6, "click-gamepad-icon");
setIDSafe<CCSprite>(this, 4, "settings-gamepad-icon");
setIDSafe<CCSprite>(this, 5, "mouse-gamepad-icon");
setIDSafe<CCSprite>(this, 6, "click-gamepad-icon");
setIDSafe<CCLabelBMFont>(this, 0, "mouse-gamepad-label");
setIDSafe<CCLabelBMFont>(this, 1, "click-gamepad-label");
setIDSafe<CCLabelBMFont>(this, 0, "mouse-gamepad-label");
setIDSafe<CCLabelBMFont>(this, 1, "click-gamepad-label");
setIDSafe<CCLabelBMFont>(this, 2, "player-username");
} else {
setIDSafe<CCLabelBMFont>(this, 0, "player-username");
setIDSafe<CCLabelBMFont>(this, 2, "player-username");
} else {
setIDSafe<CCLabelBMFont>(this, 0, "player-username");
}
// main menu
if (auto menu = getChildOfType<CCMenu>(this, 0)) {
menu->setID("main-menu");
auto playBtn = setIDSafe(menu, 0, "play-button");
auto iconBtn = setIDSafe(menu, 1, "icon-kit-button");
// the buttons are added in order play, icon, editor which doesn't work
// well with setLayout that deals with children in order
menu->swapChildIndices(playBtn, iconBtn);
setIDSafe(menu, 2, "editor-button");
if (auto pfp = setIDSafe(menu, 3, "profile-button")) {
pfp->setPositionHint(PositionHint::Absolute);
}
// main menu
if (auto menu = getChildOfType<CCMenu>(this, 0)) {
menu->setID("main-menu");
auto playBtn = setIDSafe(menu, 0, "play-button");
auto iconBtn = setIDSafe(menu, 1, "icon-kit-button");
// the buttons are added in order play, icon, editor which doesn't work
// well with setLayout that deals with children in order
menu->swapChildIndices(playBtn, iconBtn);
menu->setLayout(RowLayout::create(18.f, 0.f));
}
// bottom menu
if (auto menu = getChildOfType<CCMenu>(this, 1)) {
menu->setID("bottom-menu");
auto ach = setIDSafe(menu, 0, "achievements-button");
setIDSafe(menu, 1, "settings-button");
setIDSafe(menu, 2, "stats-button");
setIDSafe(menu, 3, "newgrounds-button");
setIDSafe(menu, 2, "editor-button");
if (auto pfp = setIDSafe(menu, 3, "profile-button")) {
pfp->setPositionHint(PositionHint::Absolute);
}
// move daily chest to its own menu
menu->setLayout(RowLayout::create(18.f, 0.f));
if (auto dailyChest = setIDSafe(menu, -1, "daily-chest-button")) {
detachIntoOwnMenu(this, dailyChest, "right-side-menu",
ColumnLayout::create(5.f, 0.f)
);
}
// bottom menu
if (auto menu = getChildOfType<CCMenu>(this, 1)) {
menu->setID("bottom-menu");
auto ach = setIDSafe(menu, 0, "achievements-button");
setIDSafe(menu, 1, "settings-button");
setIDSafe(menu, 2, "stats-button");
setIDSafe(menu, 3, "newgrounds-button");
// move daily chest to its own menu
menu->setLayout(RowLayout::create(5.f, ach->getPositionY()));
}
// social media menu
if (auto menu = getChildOfType<CCMenu>(this, 2)) {
menu->setID("social-media-menu");
setIDSafe(menu, 0, "robtop-logo-button");
setIDSafe(menu, 1, "facebook-button");
setIDSafe(menu, 2, "twitter-button");
setIDSafe(menu, 3, "youtube-button");
}
// more games menu
if (auto menu = getChildOfType<CCMenu>(this, 3)) {
menu->setID("more-games-menu");
setIDSafe(menu, 0, "more-games-button");
if (auto dailyChest = setIDSafe(menu, -1, "daily-chest-button")) {
detachIntoOwnMenu(this, dailyChest, "right-side-menu",
ColumnLayout::create(5.f, 0.f)
);
}
// move close button to its own menu
menu->setLayout(RowLayout::create(5.f, ach->getPositionY()));
}
// social media menu
if (auto menu = getChildOfType<CCMenu>(this, 2)) {
menu->setID("social-media-menu");
setIDSafe(menu, 0, "robtop-logo-button");
setIDSafe(menu, 1, "facebook-button");
setIDSafe(menu, 2, "twitter-button");
setIDSafe(menu, 3, "youtube-button");
}
// more games menu
if (auto menu = getChildOfType<CCMenu>(this, 3)) {
menu->setID("more-games-menu");
setIDSafe(menu, 0, "more-games-button");
// move close button to its own menu
if (auto closeBtn = setIDSafe(menu, 1, "close-button")) {
detachIntoOwnMenu(this, closeBtn, "close-menu",
RowLayout::create(5.f, 0.f)->setAlignment(Alignment::Begin)
);
}
if (auto closeBtn = setIDSafe(menu, 1, "close-button")) {
detachIntoOwnMenu(this, closeBtn, "close-menu",
RowLayout::create(5.f, 0.f)->setAlignment(Alignment::Begin)
);
}
}
bool init() {
if (!MenuLayer::init())
return false;
this->addStringIDs();
return true;
}
};
});