Skip to content

Commit

Permalink
improve on mod settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBearodactyl committed May 21, 2024
1 parent ef01513 commit 11c7493
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ file(GLOB_RECURSE SOURCES
src/*.cpp
src/utils/*.cpp
src/trail_customization/*.cpp
src/settings/*.cpp
)

add_library(${PROJECT_NAME} SHARED ${SOURCES})
Expand Down
7 changes: 7 additions & 0 deletions mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
"type": "bool",
"default": true
},
"gaydient-title-section": {
"name": "Gaydient Settings",
"type": "custom",
"description": "Settings for the gaydient\n(custom colors)",
"scale": 300,
"x_pos": 80
},
"use-gradient": {
"name": "Enable the GAYDIENT",
"description": "now with 4 colors! full 256\ncolor rainbow NO MORE\n(enables the usage of\nthe below custom colors\ninstead of a vanilla rainbow)",
Expand Down
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cmath>

#include "trail_customization/rainbow_trail.hpp"
#include "settings/section_setting.hpp"

using namespace geode::prelude;
using namespace cocos2d;
Expand Down Expand Up @@ -90,3 +91,7 @@ class $modify(PlayLayer) {
}
}
};

$on_mod(Loaded) {
Mod::get()->addCustomSetting<SectionValue>("gaydient-title-section", "none");
}
9 changes: 9 additions & 0 deletions src/settings/section_setting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "section_setting.hpp"
#include <Geode/loader/Dirs.hpp>
#include <filesystem>

using namespace geode::prelude;

SettingNode* SectionValue::createNode(float width) {
return SectionNode::create(this, width);
}
123 changes: 123 additions & 0 deletions src/settings/section_setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Based on similar code by TpdeaX
#pragma once

#include <Geode/loader/SettingNode.hpp>
#include <Geode/modify/FLAlertLayer.hpp>

#include <matjson.hpp>
#include <string>

using namespace geode::prelude;

using String = std::string;
using CRString = String const &;
using Val = matjson::Value;
using CRVal = Val const &;

class SectionValue;

class SectionValue : public SettingValue {
protected:
String m_placeholder;

public:
SectionValue(CRString key, CRString modID, CRString placeholder)
: SettingValue(key, modID), m_placeholder(placeholder) {}

bool load(CRVal json) override {
return true;
}

bool save(Val &json) const override {
return true;
}

SettingNode *createNode(float width) override;
};

class SectionNode : public SettingNode {
protected:
bool init(SectionValue *value, float width) {
if (!SettingNode::init(value)) {
return false;
}

CCMenu *menu = CCMenu::create();
CCSprite *info_spr = CCSprite::createWithSpriteFrameName("GJ_infoIcon_001.png");
String section_name = Mod::get()->getSettingDefinition(
value->getKey())->get<CustomSetting>()->json->get<String>("name");

this->setContentSize({width, 40.f});
info_spr->setScale(0.7f);

auto info_btn = CCMenuItemSpriteExtra::create(
info_spr,
this,
menu_selector(SectionNode::on_info_btn)
);

info_btn->setID(value->getKey());
info_btn->setPositionX(Mod::get()->getSettingDefinition(value->getKey())->get<CustomSetting>()->json->get<int>("x_pos"));

auto label = CCLabelBMFont::create(section_name.c_str(), "goldFont.fnt");

label->setScale(0.6f);

menu->setPosition(width / 2, 23.f);
menu->addChild(label);
menu->addChild(info_btn);

this->addChild(menu);

return true;
}

void on_info_btn(CCObject *sender) {
auto *node = reinterpret_cast<CCMenuItemSpriteExtra *>(sender);

if (node == nullptr) return;

auto info_name = Mod::get()->getSettingDefinition(node->getID())->get<CustomSetting>()->json->get<std::string>("name").c_str();
auto info_desc = Mod::get()->getSettingDefinition(node->getID())->get<CustomSetting>()->json->get<std::string>("description").c_str();
auto scale = Mod::get()->getSettingDefinition(node->getID())->get<CustomSetting>()->json->get<int>("scale");

auto alert = FLAlertLayer::create(
info_name,
info_desc,
"OK"
);

auto scale_as_f = static_cast<float>(scale);
alert->setScale(scale_as_f);

alert->show();
}

public:
void commit() override {
this->dispatchCommitted();
}

bool hasUncommittedChanges() override {
return false;
}

bool hasNonDefaultValue() override {
return true;
}

void resetToDefault() override {}

static SectionNode* create(SectionValue* value, float width) {
auto ret = new SectionNode;

if (ret->init(value, width)) {
ret->autorelease();
return ret;
}

CC_SAFE_DELETE(ret);

return nullptr;
}
};

0 comments on commit 11c7493

Please sign in to comment.