6CST_Color Button::colors[2] = {
7 { 0x00, 0x00, 0x00, 0xff },
8 { 0xff, 0xff, 0xff, 0xff },
11Button::Button(std::string message,
int button,
bool dark,
int size,
int width)
12 : text(message, size, &colors[dark])
15 , icon(getControllerButtonImageForPlatform(button, !dark, false))
18 super::addStackMember(&text);
19 super::addStackMember(&icon);
22 myLastSeenGamepad =
"";
24 icon.resize(text.height*1.5, text.height*1.5);
26 if (shouldRenderGlossy()) {
34 this->touchable =
true;
35 this->hasBackground =
true;
38void Button::updateBounds()
40 float effectiveScale = getEffectiveScale();
41 int PADDING = (int)(10 * effectiveScale);
43 int bWidth = PADDING * 0.5 * (icon.width != 0);
45 if (shouldRenderGlossy()) {
49 text.position(PADDING * 2 + bWidth + icon.width, PADDING);
50 icon.position(PADDING * 1.7, PADDING + (text.height - icon.height) / 2);
53 this->width = (fixedWidth > 0) ? fixedWidth : text.width + PADDING * 4 + bWidth + (icon.hidden ? 0 : icon.width);
54 this->height = text.height + PADDING * 2;
56 if (shouldRenderGlossy()) {
57 text.position(width / 2 - text.width / 2, PADDING);
63 backgroundColor = RootDisplay::mainDisplay->backgroundColor;
64 backgroundColor.r += 0x25/255.0;
65 backgroundColor.g += 0x25/255.0;
66 backgroundColor.b += 0x25/255.0;
70 backgroundColor = (rgb){ 0xee/255.0, 0xee/255.0, 0xee/255.0 };
72 text.setColor(colors[dark]);
76void Button::updateText(
const std::string &inc_text)
78 this->text.setText(inc_text);
83std::string Button::getControllerButtonImageForPlatform(
int buttonInc,
bool isGray,
bool isOutline)
86 GamepadInfo& gamepad = InputEvents::getLastGamepadInfo();
90 unsigned int button = buttonInc;
91 if (gamepad.buttons !=
nullptr) {
92 for (
int i = 0; i < TOTAL_BUTTONS; i++)
94 if (gamepad.buttons[i] == button)
96 auto outlineSuffix = isOutline ?
"_outline" :
"";
97 auto graySuffix = isGray ?
"_gray" :
"";
98 auto retVal = RAMFS
"res/controllers/buttons/" + gamepad.prefix +
"_" + gamepad.names[i] + outlineSuffix + graySuffix +
".svg";
104 printf(
"Button %d not found in gamepad, returning empty string\n", button);
112 CST_SetDrawBlend(RootDisplay::renderer,
true);
114 this->recalcPosition(parent);
116 if (shouldRenderGlossy()) {
118 auto r = backgroundColor.r * 0xff;
119 auto g = backgroundColor.g * 0xff;
120 auto b = backgroundColor.b * 0xff;
123 RootDisplay::renderer,
124 xAbs, yAbs, xAbs + width, yAbs + height, cornerRadius,
128 auto darkenOffset = dark ? 0x10 : 0x00;
130 RootDisplay::renderer,
131 xAbs, yAbs + height / 2, xAbs + width, yAbs + height, cornerRadius,
132 0x00, 0x00, 0x00, 0x15 + darkenOffset
136 CST_roundedRectangleRGBA(
137 RootDisplay::renderer,
138 xAbs+2*p, yAbs+2*p, xAbs + width - p, yAbs + height - p, cornerRadius,
139 0xff, 0xff, 0xff, 0x40
142 CST_roundedRectangleRGBA(
143 RootDisplay::renderer,
144 xAbs, yAbs, xAbs + width, yAbs + height, cornerRadius,
145 0x00, 0x00, 0x00, 0x80
150 super::render(parent);
152 CST_SetDrawBlend(RootDisplay::renderer,
false);
157 if (event->isKeyDown() && this->physical != 0 && event->
held(this->physical))
167 if (myLastSeenGamepad != InputEvents::lastGamepadKey || needsRedraw)
170 if (shouldRenderGlossy()) {
172 hasBackground =
false;
173 cornerRadius = fmin(25, height / 4);
175 auto newPath = getControllerButtonImageForPlatform(this->physical, !dark,
false);
177 hasBackground =
true;
178 icon.loadPath(newPath);
179 icon.resize(text.height*1.5, text.height*1.5);
181 myLastSeenGamepad = InputEvents::lastGamepadKey;
184 bool parentHandled = super::process(event);
185 return parentHandled || ret;
188const std::string Button::getText()
190 return this->text.text;
193bool Button::shouldRenderGlossy()
199 return InputEvents::lastGamepadKey ==
"Wii Remote" || InputEvents::lastGamepadKey ==
"Wii Remote and Nunchuk";