Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
Button Class Reference
Inheritance diagram for Button:
Inheritance graph
[legend]
Collaboration diagram for Button:
Collaboration graph
[legend]

Public Member Functions

 Button (std::string text, int button, bool dark=false, int size=20, int width=0)
 
void render (Element *parent)
 display the current state of the display More...
 
bool process (InputEvents *event)
 process any input that is received for this element More...
 
void updateBounds ()
 
void updateText (const char *inc_text)
 
const std::string getText ()
 
- Public Member Functions inherited from Element
virtual bool process (InputEvents *event)
 process any input that is received for this element More...
 
virtual void render (Element *parent)
 display the current state of the display More...
 
bool onTouchDown (InputEvents *event)
 
bool onTouchDrag (InputEvents *event)
 
bool onTouchUp (InputEvents *event)
 
void hide ()
 
void unhide ()
 
void renderBackground (bool fill=true)
 
void append (Element *element)
 
void remove (Element *element)
 
void removeAll (bool moveToTrash=false)
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
void wipeAll (bool delSelf=false)
 
Elementchild (Element *child)
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementcenterHorizontallyIn (Element *parent)
 
ElementcenterVerticallyIn (Element *parent)
 
ElementcenterIn (Element *parent)
 
ElementsetAbsolute (bool isAbs)
 
Elementconstrain (int flags, int padding=0)
 
Elementanimate (int durationIn, std::function< void(float)> onStep, std::function< void()> onFinish)
 
ElementmoveToFront ()
 
ElementsetTouchable (bool touchable)
 
void screenshot (std::string path)
 Take a screenshot of this element and its children, and save it to the given path. More...
 

Static Public Member Functions

static std::string getControllerButtonImageForPlatform (int button, bool isGray, bool isOutline)
 

Public Attributes

std::string myLastSeenGamepad = ""
 
TextElement text
 
- Public Attributes inherited from Element
std::function< void()> action = NULL
 the action to call (from binded callback) on touch or button selection https://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples More...
 
std::function< void(InputEvents *event)> actionWithEvents = NULL
 
std::vector< Element * > elements
 visible GUI child elements of this element More...
 
float scale = 1.0f
 
bool touchable = false
 whether or not this element can be touched (highlights bounds) More...
 
bool dragging = false
 whether or not this element is currently being dragged More...
 
bool needsRedraw = false
 whether or not this element needs the screen redrawn next time it's processed More...
 
int futureRedrawCounter = 0
 whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws) More...
 
int lastMouseY = 0
 the last Y, X coordinate of the mouse (from a drag probably) More...
 
int lastMouseX = 0
 
bool hasBackground = false
 
rgb backgroundColor = {0, 0, 0}
 
bool isAbsolute = false
 
Elementparent = NULL
 the parent element (can sometimes be null if it isn't set) More...
 
bool hidden = false
 whether this element should skip rendering or not More...
 
bool isProtected = false
 
int elasticCounter = 0
 how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll, and counts down every frame More...
 
int width = 0
 width and height of this element (must be manually set, isn't usually calculated (but is in some cases, like text or images)) More...
 
int height = 0
 
int x = 0
 
int y = 0
 
int xAbs = 0
 
int yAbs = 0
 
double angle = 0
 rotation angle in degrees More...
 
int xOff = 0
 
int yOff = 0
 
int cornerRadius = 0
 
std::vector< Constraint * > constraints
 
std::vector< Animation * > animations
 
bool useColorMask = false
 whether or not to overlay a color mask on top of this element More...
 
CST_Color maskColor = {0,0,0,0}
 The color to overlay on top. More...
 

Additional Inherited Members

- Public Types inherited from Element
typedef Element super
 

Detailed Description

Definition at line 6 of file Button.hpp.

Constructor & Destructor Documentation

◆ Button()

Button::Button ( std::string  text,
int  button,
bool  dark = false,
int  size = 20,
int  width = 0 
)

Definition at line 9 of file Button.cpp.

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 = "";
21
22 icon.resize(text.height*1.5, text.height*1.5);
23
24 if (shouldRenderGlossy()) {
25 icon.hide();
26 }
27
28 fixedWidth = width;
29
30 updateBounds();
31
32 this->touchable = true;
33 this->hasBackground = true;
34
35 // protect "stack" children
36 text.isProtected = icon.isProtected = true;
37
38 if (dark)
39 {
40 backgroundColor = RootDisplay::mainDisplay->backgroundColor;
41 backgroundColor.r += 0x25/255.0;
42 backgroundColor.g += 0x25/255.0;
43 backgroundColor.b += 0x25/255.0;
44 // backgroundColor.r = fmin(backgroundColor.r, 1.0);
45 }
46 else
47 backgroundColor = (rgb){ 0xee/255.0, 0xee/255.0, 0xee/255.0 };
48}
bool touchable
whether or not this element can be touched (highlights bounds)
Definition: Element.hpp:80
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:120
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:182

Member Function Documentation

◆ getControllerButtonImageForPlatform()

std::string Button::getControllerButtonImageForPlatform ( int  button,
bool  isGray,
bool  isOutline 
)
static

Definition at line 78 of file Button.cpp.

79{
80 // grab the current gamepad info
81 GamepadInfo& gamepad = InputEvents::getLastGamepadInfo();
82
83 // find the button index in the gamepad.buttons array
84 // TODO: use a hashmap instead of an array
85 if (gamepad.buttons != nullptr) {
86 for (int i = 0; i < TOTAL_BUTTONS; i++)
87 {
88 if (gamepad.buttons[i] == button)
89 {
90 auto outlineSuffix = isOutline ? "_outline" : "";
91 auto graySuffix = isGray ? "_gray" : "";
92 auto retVal = RAMFS "res/controllers/buttons/" + gamepad.prefix + "_" + gamepad.names[i] + outlineSuffix + graySuffix + ".svg";
93 return retVal;
94 }
95 }
96 }
97 // if we didn't find it, return an empty string
98 printf("Button %d not found in gamepad, returning empty string\n", button);
99 return "";
100}

◆ getText()

const std::string Button::getText ( )

Definition at line 180 of file Button.cpp.

181{
182 return this->text.text;
183}

◆ process()

bool Button::process ( InputEvents event)
virtual

process any input that is received for this element

Reimplemented from Element.

Definition at line 149 of file Button.cpp.

150{
151 if (event->isKeyDown() && this->physical != 0 && event->held(this->physical))
152 {
153 // invoke our action, since we saw a physical button press that matches!
154 this->action();
155 return true;
156 }
157
158 bool ret = false;
159
160 // if the last gamepad is different from the current one, update the button image
161 if (myLastSeenGamepad != InputEvents::lastGamepadKey)
162 {
163 if (shouldRenderGlossy()) {
164 icon.hide();
165 hasBackground = false; // we'll handle our own two-toned and outlined background drawing
166 cornerRadius = fmin(25, height / 4); // bound the corner radius to the height of our darker highlight
167 } else {
168 auto newPath = getControllerButtonImageForPlatform(this->physical, !dark, false);
169 icon.unhide();
170 hasBackground = true;
171 icon.loadPath(newPath);
172 icon.resize(text.height*1.5, text.height*1.5);
173 }
174 myLastSeenGamepad = InputEvents::lastGamepadKey;
175 }
176
177 return super::process(event) || ret;
178}
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:60
bool held(int buttons)
whether or not a button is pressed during this cycle
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
Definition: Texture.cpp:231

References Element::action, InputEvents::held(), Texture::loadPath(), Element::process(), and Texture::resize().

◆ render()

void Button::render ( Element parent)
virtual

display the current state of the display

Reimplemented from Element.

Definition at line 102 of file Button.cpp.

103{
104 if (hidden) return;
105
106 CST_SetDrawBlend(RootDisplay::renderer, true);
107
108 this->recalcPosition(parent);
109
110 if (shouldRenderGlossy()) {
111 // draw a glossy button
112 auto r = backgroundColor.r * 0xff;
113 auto g = backgroundColor.g * 0xff;
114 auto b = backgroundColor.b * 0xff;
115
116 CST_roundedBoxRGBA(
117 RootDisplay::renderer,
118 xAbs, yAbs, xAbs + width, yAbs + height, cornerRadius,
119 r, g, b, 0xff
120 );
121 // slightly darker overlay for the second half of the button
122 auto darkenOffset = dark ? 0x10 : 0x00; // use a deeper color for dark buttons
123 CST_roundedBoxRGBA(
124 RootDisplay::renderer,
125 xAbs, yAbs + height / 2, xAbs + width, yAbs + height, cornerRadius,
126 0x00, 0x00, 0x00, 0x15 + darkenOffset
127 );
128 // inner white outline around the whole thing
129 auto p = 0.5;
130 CST_roundedRectangleRGBA(
131 RootDisplay::renderer,
132 xAbs+2*p, yAbs+2*p, xAbs + width - p, yAbs + height - p, cornerRadius,
133 0xff, 0xff, 0xff, 0x40
134 );
135 // thicker outer gray outline
136 CST_roundedRectangleRGBA(
137 RootDisplay::renderer,
138 xAbs, yAbs, xAbs + width, yAbs + height, cornerRadius,
139 0x00, 0x00, 0x00, 0x80
140 );
141 }
142
143 // render text/icon and bg if needed
145
146 CST_SetDrawBlend(RootDisplay::renderer, false);
147}
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:107
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:60
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:104

References Element::hidden, Element::parent, Element::render(), and Element::width.

◆ updateBounds()

void Button::updateBounds ( )

Definition at line 50 of file Button.cpp.

51{
52 int PADDING = 10 / SCALER;
53
54 int bWidth = PADDING * 0.5 * (icon.width != 0); // gap space between button
55
56 if (shouldRenderGlossy()) {
57 icon.position(0, 0); // the icon is the overlay, same size as the button
58 } else {
59 text.position(PADDING * 2 + bWidth + icon.width, PADDING);
60 icon.position(PADDING * 1.7, PADDING + (text.height - icon.height) / 2);
61 }
62
63 this->width = (fixedWidth > 0) ? fixedWidth : text.width + PADDING * 4 + bWidth + (icon.hidden ? 0 : icon.width);
64 this->height = text.height + PADDING * 2;
65
66 if (shouldRenderGlossy()) {
67 text.position(width / 2 - text.width / 2, PADDING); // y is already centered
68 }
69}
void position(int x, int y)
position the element
Definition: Element.cpp:206

◆ updateText()

void Button::updateText ( const char *  inc_text)

Definition at line 71 of file Button.cpp.

72{
73 this->text.setText(inc_text);
74 this->text.update();
75 updateBounds();
76}
void update(bool forceUpdate=false)
update TextElement with changes
Definition: TextElement.cpp:90

Member Data Documentation

◆ myLastSeenGamepad

std::string Button::myLastSeenGamepad = ""

Definition at line 18 of file Button.hpp.

◆ text

TextElement Button::text

Definition at line 20 of file Button.hpp.


The documentation for this class was generated from the following files: