Skip to content

Commit

Permalink
Merge branch 'EclipseMenu:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustring authored Jul 12, 2024
2 parents c9957a4 + 3ec0e1a commit c1421e6
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 9 deletions.
144 changes: 140 additions & 4 deletions src/hacks/Level/StartPosSwitcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
#include <modules/keybinds/manager.hpp>

#include <Geode/modify/PlayLayer.hpp>
#include <Geode/modify/UILayer.hpp>

namespace eclipse::hacks::Level {

static std::vector<StartPosObject*> startPosObjects;
static int32_t currentStartPosIndex = 0;

class StartPosSwitcher : public hack::Hack {
public:
void init() override {
config::setIfEmpty("level.startpos_switcher", false);
config::setIfEmpty("level.startpos_switcher.reset_camera", false);
config::setIfEmpty("level.startpos_switcher.previous", keybinds::Keys::Q);
config::setIfEmpty("level.startpos_switcher.next", keybinds::Keys::E);
config::setIfEmpty("level.startpos_switcher.label", true);
config::setIfEmpty("label.startpos_switcher.scale", 0.7f);
config::setIfEmpty("label.startpos_switcher.buttons", true);
config::setIfEmpty("label.startpos_switcher.color", gui::Color(1.f, 1.f, 1.f, 0.6f));
config::setIfEmpty("label.startpos_switcher.alpha_mod", 0.4f);

auto tab = gui::MenuTab::find("Level");
tab->addToggle("StartPos Switcher", "level.startpos_switcher")
Expand All @@ -24,6 +31,13 @@ namespace eclipse::hacks::Level {
options->addKeybind("Previous StartPos", "level.startpos_switcher.previous");
options->addKeybind("Next StartPos", "level.startpos_switcher.next");
options->addToggle("Reset Camera", "level.startpos_switcher.reset_camera");
options->addToggle("Show Label", "level.startpos_switcher.label")
->addOptions([](auto* options) {
options->addSlider("Label Scale", "label.startpos_switcher.scale", 0.1f, 2.f, "%.2fx");
options->addSlider("Opacity Modifier", "label.startpos_switcher.alpha_mod", 0.f, 1.f);
options->addColorComponent("Label Color", "label.startpos_switcher.color", true);
options->addToggle("Show Buttons", "label.startpos_switcher.buttons");
});
});
}

Expand Down Expand Up @@ -70,6 +84,127 @@ namespace eclipse::hacks::Level {

REGISTER_HACK(StartPosSwitcher)

class StartposSwitcherNode : public cocos2d::CCMenu {
public:
static StartposSwitcherNode* create(PlayLayer* playLayer) {
auto* ret = new StartposSwitcherNode;
if (ret->init(playLayer)) {
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}

bool init(PlayLayer* playLayer) {
if (!CCMenu::init()) return false;

m_playLayer = playLayer;

m_previous = CCMenuItemSpriteExtra::create(
cocos2d::CCSprite::createWithSpriteFrameName("GJ_arrow_02_001.png"),
this, menu_selector(StartposSwitcherNode::onPrevious));
m_previous->setID("arrow-previous");

auto* sprite = cocos2d::CCSprite::createWithSpriteFrameName("GJ_arrow_02_001.png");
m_next = CCMenuItemSpriteExtra::create(sprite, this, menu_selector(StartposSwitcherNode::onNext));
sprite->setFlipX(true);
m_next->setID("arrow-next");

m_labelText = fmt::format("{}/{}", currentStartPosIndex + 1, startPosObjects.size());
m_label = cocos2d::CCLabelBMFont::create(m_labelText.c_str(), "bigFont.fnt");
m_label->setID("label");

m_previous->setPosition(-65.0f, 0.0f);
m_next->setPosition(65.0f, 0.0f);
m_label->setPosition(0.0f, 0.0f);

auto scale = config::get<float>("label.startpos_switcher.scale", 0.7f);
auto winSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
this->setPosition(winSize.width / 2.f, 30.f * scale);
this->setAnchorPoint({0.f, 0.f});

this->addChild(m_previous);
this->addChild(m_next);
this->addChild(m_label);

auto color = config::get<gui::Color>("label.startpos_switcher.color", gui::Color(1.f, 1.f, 1.f, 0.6f));
auto buttons = config::get<bool>("label.startpos_switcher.buttons", true);

this->setScale(scale);
m_label->setColor(color.toCCColor3B());
this->setOpacity(color.a * 255);
m_previous->setVisible(buttons);
m_next->setVisible(buttons);

this->schedule(schedule_selector(StartposSwitcherNode::update), 0.f);

return true;
}

void onPrevious(CCObject* sender) {
StartPosSwitcher::pickStartPos(m_playLayer, currentStartPosIndex - 1);
}

void onNext(CCObject* sender) {
StartPosSwitcher::pickStartPos(m_playLayer, currentStartPosIndex + 1);
}

void setVisibility(bool visible) {
m_label->setVisible(visible);
bool buttons = !visible ? false : config::get<bool>("label.startpos_switcher.buttons", true);
m_previous->setVisible(buttons);
m_next->setVisible(buttons);
}

void update(float dt) override {
if (!config::get<bool>("level.startpos_switcher", false) ||
!config::get<bool>("level.startpos_switcher.label", true)) {
setVisibility(false);
m_timeSinceAction = 0.f;
return;
}

setVisibility(true);
m_timeSinceAction += dt;

// Update scale and color
auto scale = config::get<float>("label.startpos_switcher.scale", 0.7f);
auto color = config::get<gui::Color>("label.startpos_switcher.color", gui::Color(1.f, 1.f, 1.f, 0.6f));
this->setScale(scale);
m_label->setColor(color.toCCColor3B());


constexpr float animationTime = 0.5f;
if (m_timeSinceAction < 3.f) {
this->setOpacity(color.a * 255);
} else {
auto opacityMod = 1.f - config::get<float>("label.startpos_switcher.alpha_mod", 0.4f);
auto clampedTime = std::clamp(m_timeSinceAction - 3.f, 0.f, animationTime) / animationTime;
auto modifier = 1.f - (clampedTime * opacityMod);
this->setOpacity(color.a * 255 * modifier);
}

// Center the node on the screen (center bottom)
auto winSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
this->setPosition(winSize.width / 2.f, 30.f * scale);

auto label = fmt::format("{}/{}", currentStartPosIndex + 1, startPosObjects.size());
if (label != m_labelText) {
m_labelText = label;
m_label->setCString(m_labelText.c_str());
m_timeSinceAction = 0.f;
}
}
private:
CCMenuItemSpriteExtra* m_previous;
CCMenuItemSpriteExtra* m_next;
cocos2d::CCLabelBMFont* m_label;
PlayLayer* m_playLayer;
std::string m_labelText;
float m_timeSinceAction = 0.f;
};

class $modify(PlayLayer) {
bool init(GJGameLevel *level, bool useReplay, bool dontCreateObjects) {
startPosObjects.clear();
Expand All @@ -79,8 +214,6 @@ namespace eclipse::hacks::Level {
void resetLevel() {
PlayLayer::resetLevel();

// TODO: Update label

// Reset camera
if (currentStartPosIndex >= 0 && config::get<bool>("level.startpos_switcher.reset_camera", true)) {
this->resetCamera();
Expand All @@ -98,6 +231,7 @@ namespace eclipse::hacks::Level {

void createObjectsFromSetupFinished() {
PlayLayer::createObjectsFromSetupFinished();
if (startPosObjects.empty()) return;

std::sort(startPosObjects.begin(), startPosObjects.end(), [](GameObject *a, GameObject *b) {
return a->getPositionX() < b->getPositionX();
Expand All @@ -111,8 +245,10 @@ namespace eclipse::hacks::Level {
}
}

// TODO: Spawn label
auto* switcher = StartposSwitcherNode::create(this);
switcher->setID("startpos-switcher"_spr);
m_uiLayer->addChild(switcher, 1000);
}
};

}
}
6 changes: 5 additions & 1 deletion src/modules/gui/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace eclipse::gui {
return ImGui::ColorConvertFloat4ToU32(ImVec4(r, g, b, a));
}

operator cocos2d::ccColor4F() const {
return {r, g, b, a};
}

/// @brief Returns a pointer to the color data
/// @return Pointer to the color data
float *data() {
Expand Down Expand Up @@ -170,4 +174,4 @@ namespace eclipse::gui {
void to_json(nlohmann::json &j, const Color &e);
void from_json(const nlohmann::json &j, Color &e);

}
}
10 changes: 6 additions & 4 deletions src/modules/gui/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ namespace eclipse::gui {
/// @brief Input text component to get user input as a string.
class ColorComponent : public Component {
public:
explicit ColorComponent(std::string title, std::string id)
: m_title(std::move(title)), m_id(std::move(id)) {}
explicit ColorComponent(std::string title, std::string id, bool hasOpacity = false)
: m_title(std::move(title)), m_id(std::move(id)), m_hasOpacity(hasOpacity) {}

void onInit() override {};
void onUpdate() override {}
Expand All @@ -358,6 +358,7 @@ namespace eclipse::gui {

[[nodiscard]] const std::string& getId() const override { return m_id; }
[[nodiscard]] const std::string& getTitle() const override { return m_title; }
[[nodiscard]] bool hasOpacity() const { return m_hasOpacity; }

void triggerCallback(gui::Color value) {
if (m_callback) m_callback(value);
Expand All @@ -366,6 +367,7 @@ namespace eclipse::gui {
private:
std::string m_id;
std::string m_title;
bool m_hasOpacity;
std::function<void(gui::Color)> m_callback;
};

Expand Down Expand Up @@ -508,8 +510,8 @@ namespace eclipse::gui {
}

/// @brief Add a color picker to the tab.
ColorComponent* addColorComponent(const std::string& title, const std::string& id) {
auto* color = new ColorComponent(title, id);
ColorComponent* addColorComponent(const std::string& title, const std::string& id, bool hasOpacity = false) {
auto* color = new ColorComponent(title, id, hasOpacity);
addComponent(color);
return color;
}
Expand Down

0 comments on commit c1421e6

Please sign in to comment.