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

Create custom-layers.md, update utils.md, and Create chap4_2 #63

Open
wants to merge 7 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
31 changes: 31 additions & 0 deletions handbook/vol4/chap4_2.md
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
168 changes: 168 additions & 0 deletions tutorials/custom-layers.md
Copy link
Contributor

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

K I fixed it

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();
}
};
```
23 changes: 22 additions & 1 deletion tutorials/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ auto hook1 = GEODE_UNWRAP(ObjcHook::create("EAGLView", "initWithFrame:", &MyFunc
auto hook2 = GEODE_UNWRAP(ObjcHook::create("EAGLView", "initWithFrame:", &MyFunc, &emptyFunc));
```

### geode::dirs

```cpp
// This is where geode is located!
auto geodeDir = dirs::getGeodeDir();

// This is where geodes resources are stored!
auto geodeResourcesDir = dirs::getGeodeResourcesDir();

// This is where geodes saves its files!
auto geodeSaveDir = dirs::getGeodeSaveDir();

// This is where mod's save files are!
auto modSaveDir = dirs::getModsSaveDir();

// This is where GD saves its files!
auto saveDir = dirs::getSaveDir();

// Other directories also exist
```

## Tasks

Visit the [tasks](tasks.md) page for more information.
Expand Down Expand Up @@ -304,4 +325,4 @@ timePointAsString`

### ColorProvider

No idea how to use this one.
No idea how to use this one.