I'll help you create a Geode mod that adds a button to the LevelInfoLayer which opens a file picker and displays the selected image. Here's how to implement it:
cpp
#include <Geode/Geode.hpp>
#include <Geode/modify/LevelInfoLayer.hpp>
#include <Geode/ui/Popup.hpp>
#include <Geode/utils/file.hpp>
using namespace geode::prelude;
// Popup to display the image
class ImagePopup : public Popup<> {
protected:
bool setup(std::string const& imagePath) {
if (!Popup::init(300.f, 200.f))
return false;
// Create sprite from file path
auto sprite = CCSprite::create(imagePath.c_str());
if (!sprite) return false;
// Scale sprite to fit popup
float scale = std::min(
(this->getContentSize().width - 20) / sprite->getContentSize().width,
(this->getContentSize().height - 20) / sprite->getContentSize().height
);
sprite->setScale(scale);
this->addChild(sprite);
sprite->setPosition(this->getContentSize() / 2);
return true;
}
public:
static ImagePopup* create(std::string const& imagePath) {
auto ret = new ImagePopup();
if (ret->setup(imagePath)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
};
class $modify(LevelInfoLayer) {
bool init(GJGameLevel* level, bool p1) {
if (!LevelInfoLayer::init(level, p1))
return false;
auto menu = CCMenu::create();
menu->setPosition(400, 100);
auto btnSprite = CCSprite::createWithSpriteFrameName("GJ_photoIcon_001.png");
auto btn = CCMenuItemSpriteExtra::create(
btnSprite,
this,
menu_selector(LevelInfoLayer::onOpenImage)
);
menu->addChild(btn);
this->addChild(menu);
return true;
}
void onOpenImage(CCObject*) {
file::pick(file::PickMode::OpenFile, file::FilePickOptions {
.filters = { file::FilePickOptions::Filter {
.description = "Image Files",
.files = { "*.png", "*.jpg", "*.jpeg" }
}}
}).listen([](Result<std::filesystem::path>* path) {
if (*path) {
auto popup = ImagePopup::create(path->unwrap().string());
if (popup) {
popup->show();
}
else {
FLAlertLayer::create(
"Error",
"Unable to load image",
"OK"
)->show();
}
}
else {
FLAlertLayer::create(
"Error",
path->unwrapErr(),
"OK"
)->show();
}
});
}
};
This mod will:
Add a camera icon button to the LevelInfoLayer
When clicked, open a file picker dialog that filters for image files (.png, .jpg, .jpeg)
Once an image is selected, display it in a popup that scales the image to fit
Key components:
ImagePopup class: A custom popup that displays the selected image scaled to fit
$modify(LevelInfoLayer): Adds the button and handles file picking
Uses Geode's file picker API with filters for image files
Proper error handling if the image fails to load
To use this mod:
Create a new Geode mod project
Add this code to your mod's source
Build and install the mod
When you enter a level's info layer, you'll see a camera icon button. Clicking it will open the file picker, and selecting an image will display it in a popup.
The code uses several Geode features:
file::pick() for file selection
Popup class for displaying the image
Sprite creation and scaling
Error handling with FLAlertLayer
The mod follows Geode's best practices for UI modification and user interaction.
Ah yes, you're absolutely right! Here's the corrected version with proper class naming:
cpp
#include <Geode/Geode.hpp>
#include <Geode/modify/LevelInfoLayer.hpp>
#include <Geode/ui/Popup.hpp>
#include <Geode/utils/file.hpp>
using namespace geode::prelude;
// Popup to display the image
class ImagePopup : public Popup<> {
protected:
bool setup(std::string const& imagePath) {
if (!Popup::init(300.f, 200.f))
return false;
// Create sprite from file path
auto sprite = CCSprite::create(imagePath.c_str());
if (!sprite) return false;
// Scale sprite to fit popup
float scale = std::min(
(this->getContentSize().width - 20) / sprite->getContentSize().width,
(this->getContentSize().height - 20) / sprite->getContentSize().height
);
sprite->setScale(scale);
this->addChild(sprite);
sprite->setPosition(this->getContentSize() / 2);
return true;
}
public:
static ImagePopup* create(std::string const& imagePath) {
auto ret = new ImagePopup();
if (ret->setup(imagePath)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
};
class MyLevelInfoLayer : public Modify<MyLevelInfoLayer, LevelInfoLayer> {
bool init(GJGameLevel* level, bool p1) {
if (!LevelInfoLayer::init(level, p1))
return false;
auto menu = CCMenu::create();
menu->setPosition(400, 100);
auto btnSprite = CCSprite::createWithSpriteFrameName("GJ_photoIcon_001.png");
auto btn = CCMenuItemSpriteExtra::create(
btnSprite,
this,
menu_selector(MyLevelInfoLayer::onOpenImage)
);
menu->addChild(btn);
this->addChild(menu);
return true;
}
void onOpenImage(CCObject*) {
file::pick(file::PickMode::OpenFile, file::FilePickOptions {
.filters = { file::FilePickOptions::Filter {
.description = "Image Files",
.files = { "*.png", "*.jpg", "*.jpeg" }
}}
}).listen([](Result<std::filesystem::path>* path) {
if (*path) {
auto popup = ImagePopup::create(path->unwrap().string());
if (popup) {
popup->show();
}
else {
FLAlertLayer::create(
"Error",
"Unable to load image",
"OK"
)->show();
}
}
else {
FLAlertLayer::create(
"Error",
path->unwrapErr(),
"OK"
)->show();
}
});
}
};
The key change is that instead of using $modify(LevelInfoLayer), I've created a proper named class MyLevelInfoLayer that inherits from Modify<MyLevelInfoLayer, LevelInfoLayer>. This follows Geode's proper class modification pattern.
The rest of the functionality remains the same, but now it's properly structured according to Geode's conventions for class modification. The class name MyLevelInfoLayer can be changed to any other appropriate name you prefer for your mod.