mirror of
https://github.com/geode-sdk/example-mod.git
synced 2024-11-14 10:55:07 -05:00
make template a little more featureful, add a button to menulayer
This commit is contained in:
parent
4cde4d30b3
commit
4d8baafcde
1 changed files with 76 additions and 23 deletions
99
src/main.cpp
99
src/main.cpp
|
@ -2,35 +2,88 @@
|
|||
* Include the Geode headers.
|
||||
*/
|
||||
#include <Geode/Geode.hpp>
|
||||
/**
|
||||
* Required to modify the MenuLayer class
|
||||
*/
|
||||
#include <Geode/modify/MenuLayer.hpp>
|
||||
|
||||
/**
|
||||
* Brings cocos2d and all Geode namespaces
|
||||
* to the current scope.
|
||||
* Brings cocos2d and all Geode namespaces to the current scope.
|
||||
*/
|
||||
using namespace geode::prelude;
|
||||
|
||||
/**
|
||||
* `$modify` lets you extend and modify GD's
|
||||
* classes; to hook a function in Geode,
|
||||
* simply $modify the class and write a new
|
||||
* function definition with the signature of
|
||||
* the one you want to hook.
|
||||
* `$modify` lets you extend and modify GD's classes.
|
||||
* To hook a function in Geode, simply $modify the class
|
||||
* and write a new function definition with the signature of
|
||||
* the function you want to hook.
|
||||
*
|
||||
* Here we use the overloaded `$modify` macro to set our own class name,
|
||||
* so that we can use it for button callbacks.
|
||||
*
|
||||
* Notice the header being included, you *must* include the header for
|
||||
* the class you are modifying, or you will get a compile error.
|
||||
*/
|
||||
class $modify(MenuLayer) {
|
||||
#include <Geode/modify/MenuLayer.hpp>
|
||||
class $modify(MyMenuLayer, MenuLayer) {
|
||||
/**
|
||||
* MenuLayer::onMoreGames is a GD function,
|
||||
* so we hook it by simply writing its name
|
||||
* inside a $modified MenuLayer class.
|
||||
*
|
||||
* Note that for all hooks, your signature
|
||||
* has to match exactly - `bool onMoreGames`
|
||||
* would not place a hook!
|
||||
*/
|
||||
void onMoreGames(CCObject*) {
|
||||
FLAlertLayer::create("Geode", "Hello from my custom mod!", "OK")->show();
|
||||
}
|
||||
* Typically classes in GD are initialized using the `init` function, (though not always!),
|
||||
* so here we use it to add our own button to the bottom menu.
|
||||
*
|
||||
* Note that for all hooks, your signature has to *match exactly*,
|
||||
* `void init()` would not place a hook!
|
||||
*/
|
||||
bool init() {
|
||||
/**
|
||||
* We call the original init function so that the
|
||||
* original class is properly initialized.
|
||||
*/
|
||||
if (!MenuLayer::init()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* See this page for more info about buttons
|
||||
* https://docs.geode-sdk.org/tutorials/buttons
|
||||
*/
|
||||
auto myButton = CCMenuItemSpriteExtra::create(
|
||||
CCSprite::createWithSpriteFrameName("GJ_likeBtn_001.png"),
|
||||
this,
|
||||
/**
|
||||
* Here we use the name we set earlier for our modify class.
|
||||
*/
|
||||
menu_selector(MyMenuLayer::onMyButton)
|
||||
);
|
||||
|
||||
/**
|
||||
* Here we access the `bottom-menu` node by its ID, and add our button to it.
|
||||
* Node IDs are a Geode feature, see this page for more info about it:
|
||||
* https://docs.geode-sdk.org/tutorials/nodetree
|
||||
*/
|
||||
auto menu = this->getChildByID("bottom-menu");
|
||||
menu->addChild(myButton);
|
||||
|
||||
/**
|
||||
* The `_spr` string literal operator just prefixes the string with
|
||||
* your mod id followed by a slash. This is good practice for setting your own node ids.
|
||||
*/
|
||||
myButton->setID("my-button"_spr);
|
||||
|
||||
/**
|
||||
* We update the layout of the menu to ensure that our button is properly placed.
|
||||
* This is yet another Geode feature, see this page for more info about it:
|
||||
* https://docs.geode-sdk.org/tutorials/layouts
|
||||
*/
|
||||
menu->updateLayout();
|
||||
|
||||
/**
|
||||
* We return `true` to indicate that the class was properly initialized.
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the callback function for the button we created earlier.
|
||||
* The signature for button callbacks must always be the same,
|
||||
* return type `void` and taking a `CCObject*`.
|
||||
*/
|
||||
void onMyButton(CCObject*) {
|
||||
FLAlertLayer::create("Geode", "Hello from my custom mod!", "OK")->show();
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue