Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Element.hpp
1#pragma once
2
3#include "InputEvents.hpp"
4#include "colorspaces.hpp"
5#include "DrawUtils.hpp"
6#include "Animation.hpp"
7
8#include <functional>
9#include <vector>
10#include <string>
11
12#define DEEP_HIGHLIGHT 200
13#define THICK_HIGHLIGHT 150
14#define HIGHLIGHT 100
15#define NO_HIGHLIGHT 0
16
17#if defined(USE_RAMFS)
18#define RAMFS "resin:/"
19#else
20#define RAMFS "resin/"
21#endif
22
23#if defined(_3DS) || defined(_3DS_MOCK)
24#define SCALER 2
25#else
26#define SCALER 1
27#endif
28
29class Constraint;
30
32{
33public:
34 Element();
35 virtual ~Element();
36
38 virtual bool process(InputEvents* event);
39
41 virtual void render(Element* parent);
42
43 // invoked on touchdown/up events
44 bool onTouchDown(InputEvents* event);
45 bool onTouchDrag(InputEvents* event);
46 bool onTouchUp(InputEvents* event);
47
48 // hide the element
49 void hide() { this->hidden = true; }
50 // unhide the element
51 void unhide() { this->hidden = false; }
52
53 // render the element's background
54 void renderBackground(bool fill = true);
55
58 std::function<void()> action = NULL;
59 std::function<void(InputEvents* event)> actionWithEvents = NULL;
60
62 std::vector<Element*> elements;
63
64 void append(Element* element);
65 void remove(Element* element);
66 void removeAll(bool moveToTrash = false);
67
69 void position(int x, int y);
70
71 // recalculate xAbs and yAbs based on the given parent
72 void recalcPosition(Element* parent);
73
74 // the scale of the element (and its subelements!)
75 float scale = 1.0f;
76
78 bool touchable = false;
79
81 bool dragging = false;
82
84 bool needsRedraw = false;
85
88
90 int lastMouseY = 0, lastMouseX = 0;
91
92 // whether this element has a background
93 bool hasBackground = false;
94
95 // the color of the background
96 rgb backgroundColor = {0, 0, 0};
97
98 // if this element should ignore parent position and just position itself
99 bool isAbsolute = false;
100
103
105 bool hidden = false;
106
107 // bounds on screen of this element
108 CST_Rect getBounds();
109
110 // whether or not this should be automatically free'd by wipeAll
111 bool isProtected = false;
112
116
118 int width = 0, height = 0;
119
120 typedef Element super;
121
122 // position relative to parent (if given) or screen (NULL parent)
123 int x = 0, y = 0;
124
125 // actual onscreen position (calculated at render time)
126 int xAbs = 0, yAbs = 0;
128 double angle = 0;
129
130 // x and y offsets (can be used for drawing relative to other elements)
131 int xOff = 0, yOff = 0;
132
133 // internal get current renderer or default one
134 CST_Renderer* getRenderer();
135
136 // delete this element's children, and their children, and so on
137 // if you've been using `new` everywhere, calling this can make sense in your
138 // top level component's destructor to free all the memory from that branch
139 // (not automatically invoked in case implementor wants to manage it)
140 void wipeAll(bool delSelf = false);
141
142 // fun chain-able wrappers to some fields, returns back the same element
143 Element* child(Element* child);
144 Element* setPosition(int x, int y);
145 Element* setAction(std::function<void()> func);
146
147 // alignment chainers
148 Element* centerHorizontallyIn(Element* parent);
149 Element* centerVerticallyIn(Element* parent);
150 Element* centerIn(Element* parent);
151 Element* setAbsolute(bool isAbs);
152
153 // constraints that can be added and used by positioning functions
154 std::vector<Constraint*> constraints;
155 Element* constrain(int flags, int padding = 0);
156
157 // animations that can be added and will tween over time (and remove when finished)
158 std::vector<Animation*> animations;
159 Element* animate(
160 int durationIn,
161 std::function<void(float)> onStep,
162 std::function<void()> onFinish
163 );
164
165 Element* moveToFront();
166 Element* setTouchable(bool touchable);
167
169 void screenshot(std::string path);
170
172 bool useColorMask = false;
173
175 CST_Color maskColor = {0,0,0,0};
176
177 // a function to call to re-align according to all constraints
178 // std::function<void()> alignmentCommands;
179};
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:17
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:105
bool dragging
whether or not this element is currently being dragged
Definition: Element.hpp:81
bool touchable
whether or not this element can be touched (highlights bounds)
Definition: Element.hpp:78
void screenshot(std::string path)
Take a screenshot of this element and its children, and save it to the given path.
Definition: Element.cpp:436
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
Definition: Element.hpp:58
int futureRedrawCounter
whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws)
Definition: Element.hpp:87
int elasticCounter
how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll,...
Definition: Element.hpp:115
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:60
bool useColorMask
whether or not to overlay a color mask on top of this element
Definition: Element.hpp:172
int lastMouseY
the last Y, X coordinate of the mouse (from a drag probably)
Definition: Element.hpp:90
void position(int x, int y)
position the element
Definition: Element.cpp:193
double angle
rotation angle in degrees
Definition: Element.hpp:128
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
Definition: Element.hpp:84
CST_Color maskColor
The color to overlay on top.
Definition: Element.hpp:175
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
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:102
std::vector< Element * > elements
visible GUI child elements of this element
Definition: Element.hpp:62