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
Chesto::Button Class Reference
Inheritance diagram for Chesto::Button:
Inheritance graph
[legend]
Collaboration diagram for Chesto::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 std::string &inc_text)
 
const std::string getText ()
 
- Public Member Functions inherited from Chesto::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 addNode (std::unique_ptr< Element > node)
 
void remove (Element *element)
 
void removeAll ()
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
float getEffectiveScale () const
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementsetAbsolute (bool isAbs)
 
template<typename T , typename... Args>
T * createNode (Args &&... args)
 
Elementconstrain (int flags, int padding=0)
 
ElementconstrainToTarget (Element *target, 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
 
int fixedWidth = 0
 
bool dark = false
 
- Public Attributes inherited from Chesto::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< std::unique_ptr< Element, std::function< void(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}
 
int backgroundOpacity = 0xff
 
bool isAbsolute = false
 
Elementparent = nullptr
 the parent element (reference only, not owned) 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 cornerRadius = 0
 
int tag = 0
 
std::vector< std::unique_ptr< Constraint > > constraints
 
std::vector< std::unique_ptr< 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 Chesto::Element
typedef Element super
 
- Protected Member Functions inherited from Chesto::Element
void addStackMember (Element *element)
 

Detailed Description

Definition at line 8 of file Button.hpp.

Constructor & Destructor Documentation

◆ Button()

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

Definition at line 11 of file Button.cpp.

12 : text(message, size, &colors[dark])
13 , dark(dark)
14 , physical(button)
15 , icon(getControllerButtonImageForPlatform(button, !dark, false))
16{
17
18 super::addStackMember(&text);
19 super::addStackMember(&icon);
20
21 // on initialization, store the last gamepad info
22 myLastSeenGamepad = "";
23
24 icon.resize(text.height*1.5, text.height*1.5);
25
26 if (shouldRenderGlossy()) {
27 icon.hide();
28 }
29
30 fixedWidth = width;
31
32 updateBounds();
33
34 this->touchable = true;
35 this->hasBackground = true;
36}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:132
bool touchable
whether or not this element can be touched (highlights bounds)
Definition: Element.hpp:89
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:213

Member Function Documentation

◆ getControllerButtonImageForPlatform()

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

Definition at line 83 of file Button.cpp.

84{
85 // grab the current gamepad info
86 GamepadInfo& gamepad = InputEvents::getLastGamepadInfo();
87
88 // find the button index in the gamepad.buttons array
89 // TODO: use a hashmap instead of an array
90 unsigned int button = buttonInc;
91 if (gamepad.buttons != nullptr) {
92 for (int i = 0; i < TOTAL_BUTTONS; i++)
93 {
94 if (gamepad.buttons[i] == button)
95 {
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";
99 return retVal;
100 }
101 }
102 }
103 // if we didn't find it, return an empty string
104 printf("Button %d not found in gamepad, returning empty string\n", button);
105 return "";
106}

◆ getText()

const std::string Chesto::Button::getText ( )

Definition at line 188 of file Button.cpp.

189{
190 return this->text.text;
191}

◆ process()

bool Chesto::Button::process ( InputEvents event)
virtual

process any input that is received for this element

Reimplemented from Chesto::Element.

Reimplemented in Chesto::DropDown.

Definition at line 155 of file Button.cpp.

156{
157 if (event->isKeyDown() && this->physical != 0 && event->held(this->physical))
158 {
159 // invoke our action, since we saw a physical button press that matches!
160 this->action();
161 return true;
162 }
163
164 bool ret = false;
165
166 // if the last gamepad is different from the current one, update the button image
167 if (myLastSeenGamepad != InputEvents::lastGamepadKey || needsRedraw)
168 {
169 needsRedraw = false;
170 if (shouldRenderGlossy()) {
171 icon.hide();
172 hasBackground = false; // we'll handle our own two-toned and outlined background drawing
173 cornerRadius = fmin(25, height / 4); // bound the corner radius to the height of our darker highlight
174 } else {
175 auto newPath = getControllerButtonImageForPlatform(this->physical, !dark, false);
176 icon.unhide();
177 hasBackground = true;
178 icon.loadPath(newPath);
179 icon.resize(text.height*1.5, text.height*1.5);
180 }
181 myLastSeenGamepad = InputEvents::lastGamepadKey;
182 }
183
184 bool parentHandled = super::process(event);
185 return parentHandled || ret;
186}
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:29
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
Definition: Element.hpp:95
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
Definition: Element.hpp:55
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
Definition: Texture.cpp:262

References Chesto::InputEvents::held().

◆ render()

void Chesto::Button::render ( Element parent)
virtual

display the current state of the display

Reimplemented from Chesto::Element.

Definition at line 108 of file Button.cpp.

109{
110 if (hidden) return;
111
112 CST_SetDrawBlend(RootDisplay::renderer, true);
113
114 this->recalcPosition(parent);
115
116 if (shouldRenderGlossy()) {
117 // draw a glossy button
118 auto r = backgroundColor.r * 0xff;
119 auto g = backgroundColor.g * 0xff;
120 auto b = backgroundColor.b * 0xff;
121
122 CST_roundedBoxRGBA(
123 RootDisplay::renderer,
124 xAbs, yAbs, xAbs + width, yAbs + height, cornerRadius,
125 r, g, b, 0xff
126 );
127 // slightly darker overlay for the second half of the button
128 auto darkenOffset = dark ? 0x10 : 0x00; // use a deeper color for dark buttons
129 CST_roundedBoxRGBA(
130 RootDisplay::renderer,
131 xAbs, yAbs + height / 2, xAbs + width, yAbs + height, cornerRadius,
132 0x00, 0x00, 0x00, 0x15 + darkenOffset
133 );
134 // inner white outline around the whole thing
135 auto p = 0.5;
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
140 );
141 // thicker outer gray outline
142 CST_roundedRectangleRGBA(
143 RootDisplay::renderer,
144 xAbs, yAbs, xAbs + width, yAbs + height, cornerRadius,
145 0x00, 0x00, 0x00, 0x80
146 );
147 }
148
149 // render text/icon and bg if needed
151
152 CST_SetDrawBlend(RootDisplay::renderer, false);
153}
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:89
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:119
Element * parent
the parent element (reference only, not owned)
Definition: Element.hpp:116

◆ updateBounds()

void Chesto::Button::updateBounds ( )

Definition at line 38 of file Button.cpp.

39{
40 float effectiveScale = getEffectiveScale();
41 int PADDING = (int)(10 * effectiveScale);
42
43 int bWidth = PADDING * 0.5 * (icon.width != 0); // gap space between button
44
45 if (shouldRenderGlossy()) {
46 icon.position(0, 0); // the icon is the overlay, same size as the button
47 } else {
48 // make room for the button label, if we have a button
49 text.position(PADDING * 2 + bWidth + icon.width, PADDING);
50 icon.position(PADDING * 1.7, PADDING + (text.height - icon.height) / 2);
51 }
52
53 this->width = (fixedWidth > 0) ? fixedWidth : text.width + PADDING * 4 + bWidth + (icon.hidden ? 0 : icon.width);
54 this->height = text.height + PADDING * 2;
55
56 if (shouldRenderGlossy()) {
57 text.position(width / 2 - text.width / 2, PADDING); // y is already centered
58 }
59
60 // update the bg color here too
61 if (dark)
62 {
63 backgroundColor = RootDisplay::mainDisplay->backgroundColor;
64 backgroundColor.r += 0x25/255.0;
65 backgroundColor.g += 0x25/255.0;
66 backgroundColor.b += 0x25/255.0;
67 // backgroundColor.r = fmin(backgroundColor.r, 1.0);
68 }
69 else
70 backgroundColor = (rgb){ 0xee/255.0, 0xee/255.0, 0xee/255.0 };
71
72 text.setColor(colors[dark]);
73 text.update();
74}
void position(int x, int y)
position the element
Definition: Element.cpp:257
void update(bool forceUpdate=false)
update TextElement with changes

◆ updateText()

void Chesto::Button::updateText ( const std::string &  inc_text)

Definition at line 76 of file Button.cpp.

77{
78 this->text.setText(inc_text);
79 this->text.update();
80 updateBounds();
81}

Member Data Documentation

◆ dark

bool Chesto::Button::dark = false

Definition at line 29 of file Button.hpp.

◆ fixedWidth

int Chesto::Button::fixedWidth = 0

Definition at line 26 of file Button.hpp.

◆ myLastSeenGamepad

std::string Chesto::Button::myLastSeenGamepad = ""

Definition at line 20 of file Button.hpp.

◆ text

TextElement Chesto::Button::text

Definition at line 22 of file Button.hpp.


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