Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Button.cpp
1#include "Button.hpp"
2#include <iostream>
3
4CST_Color Button::colors[2] = {
5 { 0x00, 0x00, 0x00, 0xff }, // light
6 { 0xff, 0xff, 0xff, 0xff }, // dark
7};
8
9Button::Button(std::string message, int button, bool dark, int size, int width)
10 : physical(button)
11 , dark(dark)
12 , icon(getControllerButtonImageForPlatform(button, !dark, false))
13 , text(message, (size / SCALER), &colors[dark])
14{
15
16 super::append(&text);
17 super::append(&icon);
18
19 // on initialization, store the last gamepad info
20 myLastSeenGamepad = InputEvents::lastGamepadKey;
21
22 icon.resize(text.height*1.5, text.height*1.5);
23
24 fixedWidth = width;
25
26 updateBounds();
27
28 this->touchable = true;
29 this->hasBackground = true;
30
31 // protect "stack" children
32 text.isProtected = icon.isProtected = true;
33
34 if (dark)
35 {
36 backgroundColor = RootDisplay::mainDisplay->backgroundColor;
37 backgroundColor.r += 0x25/255.0;
38 backgroundColor.g += 0x25/255.0;
39 backgroundColor.b += 0x25/255.0;
40 // backgroundColor.r = fmin(backgroundColor.r, 1.0);
41 }
42 else
43 backgroundColor = (rgb){ 0xee/255.0, 0xee/255.0, 0xee/255.0 };
44}
45
46void Button::updateBounds()
47{
48 int PADDING = 10 / SCALER;
49
50 int bWidth = PADDING * 0.5 * (icon.width != 0); // gap space between button
51
52 text.position(PADDING * 2 + bWidth + icon.width, PADDING);
53 icon.position(PADDING * 1.7, PADDING + (text.height - icon.height) / 2);
54
55 this->width = (fixedWidth > 0) ? fixedWidth : text.width + PADDING * 4 + bWidth + icon.width;
56 this->height = text.height + PADDING * 2;
57}
58
59void Button::updateText(const char* inc_text)
60{
61 this->text.setText(inc_text);
62 this->text.update();
63 updateBounds();
64}
65
66std::string Button::getControllerButtonImageForPlatform(int button, bool isGray, bool isOutline)
67{
68 // grab the current gamepad info
69 GamepadInfo& gamepad = InputEvents::getLastGamepadInfo();
70 // find the button index in the gamepad.buttons array
71 // TODO: use a hashmap instead of an array
72 if (gamepad.buttons != nullptr) {
73 for (int i = 0; i < TOTAL_BUTTONS; i++)
74 {
75 if (gamepad.buttons[i] == button)
76 {
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";
80 return retVal;
81 }
82 }
83 }
84 // if we didn't find it, return an empty string
85 printf("Button %d not found in gamepad, returning empty string\n", button);
86 return "";
87}
88
90{
91 if (event->isKeyDown() && this->physical != 0 && event->held(this->physical))
92 {
93 // invoke our action, since we saw a physical button press that matches!
94 this->action();
95 return true;
96 }
97
98 bool ret = false;
99
100 // if the last gamepad is different from the current one, update the button image
101 if (myLastSeenGamepad != InputEvents::lastGamepadKey)
102 {
103 auto newPath = getControllerButtonImageForPlatform(this->physical, !dark, false);
104 icon.loadPath(newPath);
105 icon.resize(text.height*1.5, text.height*1.5);
106 myLastSeenGamepad = InputEvents::lastGamepadKey;
107 }
108
109 return super::process(event) || ret;
110}
111
112const std::string Button::getText()
113{
114 return this->text.text;
115}
bool process(InputEvents *event)
process any input that is received for this element
Definition: Button.cpp:89
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:17
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
Definition: Element.hpp:58
void position(int x, int y)
position the element
Definition: Element.cpp:193
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:118
bool held(int buttons)
whether or not a button is pressed during this cycle
void update(bool forceUpdate=false)
update TextElement with changes
Definition: TextElement.cpp:89
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:181
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
Definition: Texture.cpp:230