diff --git a/loader/include/Geode/ui/Button.hpp b/loader/include/Geode/ui/Button.hpp index b2eebcfa7..1713857d8 100644 --- a/loader/include/Geode/ui/Button.hpp +++ b/loader/include/Geode/ui/Button.hpp @@ -27,7 +27,7 @@ namespace geode { static Button* create(ButtonCallback activateCallback = nullptr); /** - * Create a Button with a nodes on it. + * Create a Button with a node on it. * The node will be automatically positioned in the center. * @param node The node that will show on the button. * @param activateCallback The callback for when the button is activated @@ -56,6 +56,93 @@ namespace geode { */ static Button* createWithLabel(geode::ZStringView text, geode::ZStringView font, ButtonCallback activateCallback = nullptr); + /** + * Create a Button with a ButtonSprite + * @param text The text shown in the button + * @param activateCallback The callback for when the button is activated + */ + static Button* createWithButtonSprite(geode::ZStringView text, ButtonCallback activateCallback = nullptr); + + /** + * Create a Button with a ButtonSprite + * @param text The text shown in the button + * @param scale The scale of the text + * @param activateCallback The callback for when the button is activated + */ + static Button* createWithButtonSprite(geode::ZStringView text, float scale, ButtonCallback activateCallback = nullptr); + + /** + * Create a Button with a ButtonSprite + * @param text The text shown in the button + * @param font The font of the text + * @param bg The background of the button (not in a spritesheet) + * @param activateCallback The callback for when the button is activated + */ + static Button* createWithButtonSprite( + geode::ZStringView text, + geode::ZStringView font, + geode::ZStringView bg, + ButtonCallback activateCallback = nullptr + ); + + /** + * Create a Button with a ButtonSprite + * @param text The text shown in the button + * @param font The font of the text + * @param bg The background of the button (not in a spritesheet) + * @param scale The scale of the text + * @param activateCallback The callback for when the button is activated + */ + static Button* createWithButtonSprite( + geode::ZStringView text, + geode::ZStringView font, + geode::ZStringView bg, + float scale, + ButtonCallback activateCallback = nullptr + ); + + /** + * Create a Button with a ButtonSprite + * @param text The text shown in the button + * @param font The font of the text + * @param bg The background of the button (not in a spritesheet) + * @param width The width of the button (`absolute` must be true) + * @param absolute Whether to use absolute width or not + * @param scale The scale of the text + * @param activateCallback The callback for when the button is activated + */ + static Button* createWithButtonSprite( + geode::ZStringView text, + geode::ZStringView font, + geode::ZStringView bg, + int width, + bool absolute, + float scale, + ButtonCallback activateCallback = nullptr + ); + + /** + * Create a Button with a ButtonSprite + * @param text The text shown in the button + * @param font The font of the text + * @param bg The background of the button (not in a spritesheet) + * @param width The width of the button (`absolute` must be true) + * @param height The height of the button (0 automatically sets the height) + * @param absolute Whether to use absolute width or not + * @param scale The scale of the text + * @param activateCallback The callback for when the button is activated + */ + static Button* createWithButtonSprite( + geode::ZStringView text, + geode::ZStringView font, + geode::ZStringView bg, + int width, + float height, + bool absolute, + float scale, + ButtonCallback activateCallback = nullptr + ); + /** * Get the Display Node, which was created or passed in on button creation * Will be nullptr if you use the empty create method @@ -108,7 +195,7 @@ namespace geode { int getTouchPriority(); /** - * Set the touch multiplier, which increases the distance from the + * Set the touch multiplier, which increases the distance from the * center that the button can be pressed (default is 1) */ void setTouchMultiplier(float multipler); @@ -145,7 +232,7 @@ namespace geode { */ virtual void setEnabled(bool enabled); virtual bool isEnabled(); - + /** * Get the selected state of the button */ @@ -208,7 +295,7 @@ namespace geode { * Unregister a button (must be called in onExit) */ void unregisterButton(Button* button); - + /** * Pass moves from ccTouchMoved to all buttons sharing a parent */ diff --git a/loader/src/ui/nodes/Button.cpp b/loader/src/ui/nodes/Button.cpp index eedc6ba9e..fd97c12e5 100644 --- a/loader/src/ui/nodes/Button.cpp +++ b/loader/src/ui/nodes/Button.cpp @@ -1,5 +1,6 @@ #include #include +#include using namespace geode::prelude; @@ -19,7 +20,7 @@ class Button::Impl final { float m_touchMultiplier = 1.f; CCPoint m_offset = {0, -15}; - + float m_selectedDuration = 0.3f; float m_unselectedDuration = 0.4f; @@ -90,6 +91,69 @@ Button* Button::createWithLabel(ZStringView text, ZStringView font, ButtonCallba return nullptr; } +Button* Button::createWithButtonSprite(ZStringView text, ButtonCallback activateCallback) { + return Button::createWithButtonSprite(text, "goldFont.fnt", "GJ_button_01.png", 0, 0.f, false, 1.f, std::move(activateCallback)); +} + +Button* Button::createWithButtonSprite(ZStringView text, float scale, ButtonCallback activateCallback) { + return Button::createWithButtonSprite(text, "goldFont.fnt", "GJ_button_01.png", 0, 0.f, false, scale, std::move(activateCallback)); +} + +Button* Button::createWithButtonSprite(ZStringView text, ZStringView font, ZStringView bg, ButtonCallback activateCallback) { + return Button::createWithButtonSprite(text, font, bg, std::move(activateCallback)); +} + +Button* Button::createWithButtonSprite( + ZStringView text, + ZStringView font, + ZStringView bg, + float scale, + ButtonCallback activateCallback +) { + return Button::createWithButtonSprite(text, font, bg, 0, .0f, false, scale, std::move(activateCallback)); +} + +Button* Button::createWithButtonSprite( + ZStringView text, + ZStringView font, + ZStringView bg, + int width, + bool absolute, + float scale, + ButtonCallback activateCallback +) { + return Button::createWithButtonSprite(text, font, bg, width, 0, absolute, scale, std::move(activateCallback)); +} + +Button* Button::createWithButtonSprite( + ZStringView text, + ZStringView font, + ZStringView bg, + int width, + float height, + bool absolute, + float scale, + ButtonCallback activateCallback +) { + auto ret = new Button(); + auto spr = ButtonSprite::create( + text.c_str(), + width, + absolute, + font.c_str(), + bg.c_str(), + height, + scale + ); + if (!spr) return nullptr; + if (ret->initWithNode(spr, std::move(activateCallback))) { + ret->autorelease(); + return ret; + } + delete ret; + return nullptr; +} + bool Button::init(ButtonCallback activateCallback) { if (!CCNodeRGBA::init()) return false; m_impl->m_defaults = CCNodeRGBA::create(); @@ -187,7 +251,7 @@ CCActionInterval* Button::clickActionForType() { } return nullptr; -} +} CCActionInterval* Button::releaseActionForType() { switch (m_impl->m_animationType) { @@ -206,7 +270,7 @@ CCActionInterval* Button::releaseActionForType() { return CCEaseInOut::create(moveTo, 2.f); } } - + return nullptr; } @@ -297,7 +361,7 @@ void Button::selected() { void Button::unselected() { if (!m_impl->m_selected) return; - + stopAction(m_impl->m_activeClickAction); stopAction(m_impl->m_activeReleaseAction);