-
Notifications
You must be signed in to change notification settings - Fork 38
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
Create custom-layers.md, update utils.md, and Create chap4_2 #63
Open
Chs000-00
wants to merge
7
commits into
geode-sdk:main
Choose a base branch
from
Chs000-00:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
39f2cf8
Create custom-layers.md
Chs000-00 ed037bd
Sort of chap4_2?
Chs000-00 936086a
geode::dirs::*
Chs000-00 f58ee1d
whoops
Chs000-00 ecf0772
Update custom-layers.md
Chs000-00 8d0aa8b
Update utils.md
Chs000-00 47bc30c
Fix indentation
Chs000-00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Chapter 4.2: Searching through the docs | ||
|
||
At some point or another, you will need to find a class to hook or a function to call. This chapter explains how to search through the geode classes tab. | ||
|
||
## Seaching for classes | ||
Geode docs have a useful "classes" tab to search for classes. If you do not know what class something is, it is best to search through devtools first, then search it up the docs. However, if devtools does not tell you the classname, then you would have to find the class yourself. | ||
|
||
## I found a class! What now? | ||
Congrats! You can now hook the object by copying the signiture of the function! [Geode's vscode](https://marketplace.visualstudio.com/items?itemName=GeodeSDK.geode) extension provides autocomplete for function signatures, so it is recommended to use that. | ||
|
||
## But what if I want to get said object from another object? | ||
Most objects have either a `::get()` or a `::sharedState()` (or even both [Note 1]!) static function. Most of the time, this function will return the object, however if the object does not exist, it will return a nullptr. | ||
c++ lets you check if an object is nullptr and run code if it does exist. | ||
|
||
```cpp | ||
if (auto level = GJBaseGameLayer::get()) { | ||
// If you are in a level, run this code with level being the level | ||
} | ||
|
||
else { | ||
// Otherwise, run this code. | ||
} | ||
``` | ||
|
||
If the object does not have a `::get()` method, you would need to find another way to get it. | ||
|
||
> :warning: Enums do not appear in the search! You would need to find them some other way! | ||
|
||
> :warning: Some functions may be inlined on some platforms! Hooking them would cause a compilation error. You would need to find some other function to hook. | ||
|
||
> [Note 1] `::get()` and `::sharedState()` methods act the **exact same** on most classes with both of them |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# Custom Layers | ||
|
||
At some point a modder may want to create a custom layer for their mod, for one reason or another. | ||
|
||
|
||
Creating a new scene is easy. You must create a new class extending `cocos2d:::CCLayer`, and add additional UI (such as buttons, popups, or even a level) by overriding the the `*::init` function. | ||
|
||
```cpp | ||
class MyVeryOriginalLayer : public CCLayer { | ||
protected: | ||
bool init() override { | ||
log::info("Hi from my very original layer"); | ||
return true; | ||
} | ||
|
||
public: | ||
static MyVeryOriginalLayer* create() { | ||
auto ret = new MyVeryOriginalLayer; | ||
if (ret->init()) { | ||
ret->autorelease(); | ||
return ret; | ||
} | ||
CC_SAFE_DELETE(ret); | ||
return nullptr; | ||
} | ||
} | ||
``` | ||
|
||
This code will create a new layer called MyVeryOriginalLayer. When we start up the game, we will not see the layer as we do transition to it. Calling `MyVeryOriginalLayer::create()` will not transition to the layer, as it only creates it, not replaces the current layer. To be able to transition to the layer, we can use `switchToScene(layer)`. We can create a static function to easily create and switch to the scene: | ||
|
||
```cpp | ||
static MyVeryOriginalLayer* scene() { | ||
auto layer = MyVeryOriginalLayer::create(); | ||
switchToScene(layer); | ||
return layer; | ||
} | ||
``` | ||
|
||
## UI | ||
|
||
Running this code and calling `MyVeryOriginalLayer::scene()` will transition the current scene to MyVeryOriginalLayer, however it is currently very barren. Geode provides utility functions to create background and side art. | ||
|
||
``` | ||
#include <Geode/ui/General.hpp> | ||
|
||
bool init() override { | ||
log::info("Hi from my very original layer"); | ||
|
||
// Create a new menu. UI elemnts should be added to here! | ||
menu = CCMenu::create(); | ||
|
||
// Add side art to the layer | ||
addSideArt(this, SideArt::All, SideArtStyle::Layer); | ||
|
||
// And a background to the layer | ||
auto background = createLayerBG(); | ||
background->setID("background"); | ||
this->addChild(background); | ||
return true; | ||
} | ||
``` | ||
|
||
The rest of the buttons and UI can be created the same way you would in a hook. | ||
|
||
> :warning: UI elements should **always** be added to a CCMenu object! | ||
|
||
|
||
## Back button | ||
|
||
Now the biggest problem with this layer is that you are stuck in it. To go back you would want to create a button whoes callback transitions to another scene. | ||
|
||
```cpp | ||
|
||
bool init() override { | ||
// Create a back button with the back button sprites | ||
auto backBtn = CCMenuItemSpriteExtra::create( | ||
CCSprite::createWithSpriteFrameName("GJ_arrow_01_001.png"), | ||
this, | ||
menu_selector(MyVeryOriginalLayer::onBack) | ||
); | ||
|
||
|
||
// Add a back button the the top-left corner of the screen with a small offset. | ||
menu->addChildAtPosition(backBtn, Anchor::TopLeft, {25, -25}); | ||
this->addChild(menu); | ||
return true; | ||
} | ||
|
||
// This function is called when the escape key is pressed! | ||
void keyBackClicked() override { | ||
this->onBack(nullptr); | ||
} | ||
|
||
void onBack(CCObject* sender) { | ||
CCDirector::get()->replaceScene(CCTransitionFade::create(0.5, MenuLayer::scene())); | ||
} | ||
|
||
``` | ||
|
||
> :information_source: You can also create a back button with `GameToolbox::addBackButton` | ||
|
||
> :warning: Pressing escape will **not transition the layer** back unless you override `keyBackClicked()`! | ||
|
||
|
||
|
||
## Example | ||
|
||
This code will transition to the MyVeryOriginalLayer when clicking the on more games button. | ||
|
||
```cpp | ||
#include <Geode/Geode.hpp> | ||
#include <Geode/ui/General.hpp> | ||
#include <Geode/modify/MenuLayer.hpp> | ||
using namespace geode::prelude; | ||
|
||
class MyVeryOriginalLayer : public CCLayer { | ||
protected: | ||
bool init() override { | ||
log::info("Hi from my very original layer"); | ||
|
||
// Create a new menu. UI elemnts should be added to here! | ||
menu = CCMenu::create(); | ||
|
||
// Add side art to the layer | ||
addSideArt(this, SideArt::All, SideArtStyle::Layer); | ||
|
||
// And a background to the layer | ||
auto background = createLayerBG(); | ||
background->setID("background"); | ||
this->addChild(background); | ||
return true; | ||
} | ||
|
||
// This function is called when the escape key is pressed! | ||
void keyBackClicked() override { | ||
this->onBack(nullptr); | ||
} | ||
|
||
public: | ||
static MyVeryOriginalLayer* create() { | ||
auto ret = new MyVeryOriginalLayer; | ||
if (ret->init()) { | ||
ret->autorelease(); | ||
return ret; | ||
} | ||
CC_SAFE_DELETE(ret); | ||
return nullptr; | ||
} | ||
|
||
static MyVeryOriginalLayer* MyVeryOriginalLayer::scene() { | ||
auto layer = MyVeryOriginalLayer::create(); | ||
switchToScene(layer); | ||
return layer; | ||
} | ||
|
||
|
||
void onBack(CCObject* sender) { | ||
CCDirector::get()->replaceScene(CCTransitionFade::create(0.5, MenuLayer::scene())); | ||
} | ||
|
||
} | ||
|
||
class $modify(MenuLayer) { | ||
void onMoreGames(CCObject* target) { | ||
MyVeryOriginalLayer::scene(); | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot to return true in the init function bro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K I fixed it