Skip to content
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
95 changes: 91 additions & 4 deletions loader/include/Geode/ui/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -145,7 +232,7 @@ namespace geode {
*/
virtual void setEnabled(bool enabled);
virtual bool isEnabled();

/**
* Get the selected state of the button
*/
Expand Down Expand Up @@ -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
*/
Expand Down
72 changes: 68 additions & 4 deletions loader/src/ui/nodes/Button.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Geode/ui/Button.hpp>
#include <Geode/utils/cocos.hpp>
#include <Geode/binding/ButtonSprite.hpp>

using namespace geode::prelude;

Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -187,7 +251,7 @@ CCActionInterval* Button::clickActionForType() {
}

return nullptr;
}
}

CCActionInterval* Button::releaseActionForType() {
switch (m_impl->m_animationType) {
Expand All @@ -206,7 +270,7 @@ CCActionInterval* Button::releaseActionForType() {
return CCEaseInOut::create(moveTo, 2.f);
}
}

return nullptr;
}

Expand Down Expand Up @@ -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);

Expand Down
Loading