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

Public Member Functions

 Grid (int columns, int width, int cellPadding=0, int rowPadding=0)
 
void refresh ()
 Recalculate positions for all child elements. More...
 
bool process (InputEvents *event) override
 process any input that is received for this element More...
 
- 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...
 

Public Attributes

int columns
 
int width
 
int cellPadding
 
int rowPadding
 
int highlighted = -1
 Cursor state. More...
 
bool touchMode = true
 
- 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 Grid.hpp.

Constructor & Destructor Documentation

◆ Grid()

Chesto::Grid::Grid ( int  columns,
int  width,
int  cellPadding = 0,
int  rowPadding = 0 
)

The Grid component is similar to Container in that it has to deal with positioning its child elements, but unlike container, it is designed to take in the full list of elements, and then arrange them based on some fixed width.

Calling refresh rebuilds the grid, again according to the width and its elements, which allows for reflowing when container sizes change.

Definition at line 14 of file Grid.cpp.

15 : columns(columns)
16 , width(width)
17 , cellPadding(cellPadding)
18 , rowPadding(rowPadding)
19{
20 this->width = width;
21 this->height = 0; // calculated on refresh()
22}

Member Function Documentation

◆ process()

bool Chesto::Grid::process ( InputEvents event)
overridevirtual

process any input that is received for this element

Reimplemented from Chesto::Element.

Definition at line 72 of file Grid.cpp.

73{
74 // This method lets the children elements handle their
75 // touch events, but manages grid-like cursor navigation
76 bool ret = false;
77
78 // normal event handling
79 ret |= Element::process(event);
80
81 if (elements.empty()) {
82 return ret;
83 }
84
85 if (touchMode) {
86 return ret;
87 }
88
89 // cursor logic from HBAS's AppList
90 // int origHighlighted = highlighted;
91
92 // start highlight if none
93 if (highlighted < 0 && !elements.empty()) {
94 highlighted = 0;
95 }
96
97 // ensure highlighted is in bounds
98 if (highlighted >= (int)elements.size()) {
99 highlighted = elements.size() - 1;
100 }
101
102 // TODO: fiinsh grid navigation logic
103 ret = true;
104
105 return ret;
106}
std::vector< std::unique_ptr< Element, std::function< void(Element *)> > > elements
visible GUI child elements of this element
Definition: Element.hpp:59
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:29
int highlighted
Cursor state.
Definition: Grid.hpp:23

References Chesto::Element::elements, highlighted, and Chesto::Element::process().

◆ refresh()

void Chesto::Grid::refresh ( )

Recalculate positions for all child elements.

Definition at line 24 of file Grid.cpp.

25{
26 if (elements.empty()) {
27 this->height = 0;
28 return;
29 }
30
31 // calculate cell width: (total width - padding between cells) / columns
32 int cellWidth = (this->width - (cellPadding * (columns - 1))) / columns;
33
34 int xPos = 0;
35 int yPos = 0;
36 int currentRowHeight = 0;
37 int col = 0;
38
39 for (auto& elem : elements) {
40 // actually position the element
41 elem->x = xPos;
42 elem->y = yPos;
43
44 // the tallest element in this row
45 if (elem->height > currentRowHeight) {
46 currentRowHeight = elem->height;
47 }
48
49 // advance column
50 col++;
51 xPos += cellWidth + cellPadding;
52
53 // do we need to start a new row?
54 if (col >= columns) {
55 col = 0;
56 xPos = 0;
57 yPos += currentRowHeight + rowPadding;
58 currentRowHeight = 0;
59 }
60 }
61
62 // final grid height
63 if (col > 0) {
64 yPos += currentRowHeight; // commit last row height
65 } else if (yPos > 0) {
66 yPos -= rowPadding;
67 }
68
69 this->height = yPos;
70}

References Chesto::Element::elements.

Member Data Documentation

◆ cellPadding

int Chesto::Grid::cellPadding

Definition at line 19 of file Grid.hpp.

◆ columns

int Chesto::Grid::columns

Definition at line 17 of file Grid.hpp.

◆ highlighted

int Chesto::Grid::highlighted = -1

Cursor state.

Definition at line 23 of file Grid.hpp.

Referenced by process().

◆ rowPadding

int Chesto::Grid::rowPadding

Definition at line 20 of file Grid.hpp.

◆ touchMode

bool Chesto::Grid::touchMode = true

Definition at line 24 of file Grid.hpp.

◆ width

int Chesto::Grid::width

Definition at line 18 of file Grid.hpp.


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