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)
 
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
 
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 = 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}
bool touchable
whether or not this element can be touched (highlights bounds)
Definition: Element.hpp:78
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
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:181

Member Function Documentation

◆ getControllerButtonImageForPlatform()

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

Definition at line 66 of file Button.cpp.

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}

◆ getText()

const std::string Button::getText ( )

Definition at line 112 of file Button.cpp.

113{
114 return this->text.text;
115}

◆ process()

bool Button::process ( InputEvents event)
virtual

process any input that is received for this element

Reimplemented from Element.

Definition at line 89 of file Button.cpp.

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}
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
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:230

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

◆ updateBounds()

void Button::updateBounds ( )

Definition at line 46 of file Button.cpp.

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}
void position(int x, int y)
position the element
Definition: Element.cpp:193

◆ updateText()

void Button::updateText ( const char *  inc_text)

Definition at line 59 of file Button.cpp.

60{
61 this->text.setText(inc_text);
62 this->text.update();
63 updateBounds();
64}
void update(bool forceUpdate=false)
update TextElement with changes
Definition: TextElement.cpp:89

Member Data Documentation

◆ myLastSeenGamepad

std::string Button::myLastSeenGamepad = ""

Definition at line 17 of file Button.hpp.

◆ text

TextElement Button::text

Definition at line 19 of file Button.hpp.


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