4CST_Color Button::colors[2] = {
5 { 0x00, 0x00, 0x00, 0xff },
6 { 0xff, 0xff, 0xff, 0xff },
9Button::Button(std::string message,
int button,
bool dark,
int size,
int width)
12 , icon(getControllerButtonImageForPlatform(button, !dark, false))
13 , text(message, (size / SCALER), &colors[dark])
20 myLastSeenGamepad = InputEvents::lastGamepadKey;
22 icon.resize(text.height*1.5, text.height*1.5);
28 this->touchable =
true;
29 this->hasBackground =
true;
32 text.isProtected = icon.isProtected =
true;
36 backgroundColor = RootDisplay::mainDisplay->backgroundColor;
37 backgroundColor.r += 0x25/255.0;
38 backgroundColor.g += 0x25/255.0;
39 backgroundColor.b += 0x25/255.0;
43 backgroundColor = (
rgb){ 0xee/255.0, 0xee/255.0, 0xee/255.0 };
46void Button::updateBounds()
48 int PADDING = 10 / SCALER;
50 int bWidth = PADDING * 0.5 * (icon.
width != 0);
53 icon.
position(PADDING * 1.7, PADDING + (text.height - icon.height) / 2);
55 this->width = (fixedWidth > 0) ? fixedWidth : text.
width + PADDING * 4 + bWidth + icon.
width;
56 this->height = text.height + PADDING * 2;
59void Button::updateText(
const char* inc_text)
61 this->text.setText(inc_text);
66std::string Button::getControllerButtonImageForPlatform(
int button,
bool isGray,
bool isOutline)
69 GamepadInfo& gamepad = InputEvents::getLastGamepadInfo();
72 if (gamepad.buttons !=
nullptr) {
73 for (
int i = 0; i < TOTAL_BUTTONS; i++)
75 if (gamepad.buttons[i] == button)
77 auto outlineSuffix = isOutline ?
"_outline" :
"";
78 auto graySuffix = isGray ?
"_gray" :
"";
79 auto retVal = RAMFS
"res/controllers/buttons/" + gamepad.prefix +
"_" + gamepad.names[i] + outlineSuffix + graySuffix +
".svg";
85 printf(
"Button %d not found in gamepad, returning empty string\n", button);
91 if (event->isKeyDown() && this->physical != 0 && event->
held(this->physical))
101 if (myLastSeenGamepad != InputEvents::lastGamepadKey)
103 auto newPath = getControllerButtonImageForPlatform(this->physical, !dark,
false);
105 icon.
resize(text.height*1.5, text.height*1.5);
106 myLastSeenGamepad = InputEvents::lastGamepadKey;
112const std::string Button::getText()
114 return this->text.text;
virtual bool process(InputEvents *event)
process any input that is received for this element
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
void position(int x, int y)
position the element
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
void update(bool forceUpdate=false)
update TextElement with changes
void resize(int w, int h)
Resizes the texture.
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture