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#elif defined(WII) || defined(WII_MOCK)
26#define SCALER 1.5
27#else
28#define SCALER 1
29#endif
30
31class Constraint;
32
34{
35public:
36 Element();
37 virtual ~Element();
38
40 virtual bool process(InputEvents* event);
41
43 virtual void render(Element* parent);
44
45 // invoked on touchdown/up events
46 bool onTouchDown(InputEvents* event);
47 bool onTouchDrag(InputEvents* event);
48 bool onTouchUp(InputEvents* event);
49
50 // hide the element
51 void hide() { this->hidden = true; }
52 // unhide the element
53 void unhide() { this->hidden = false; }
54
55 // render the element's background
56 void renderBackground(bool fill = true);
57
60 std::function<void()> action = NULL;
61 std::function<void(InputEvents* event)> actionWithEvents = NULL;
62
64 std::vector<Element*> elements;
65
66 void append(Element* element);
67 void remove(Element* element);
68 void removeAll(bool moveToTrash = false);
69
71 void position(int x, int y);
72
73 // recalculate xAbs and yAbs based on the given parent
74 void recalcPosition(Element* parent);
75
76 // the scale of the element (and its subelements!)
77 float scale = 1.0f;
78
80 bool touchable = false;
81
83 bool dragging = false;
84
86 bool needsRedraw = false;
87
90
92 int lastMouseY = 0, lastMouseX = 0;
93
94 // whether this element has a background
95 bool hasBackground = false;
96
97 // the color of the background
98 rgb backgroundColor = {0, 0, 0};
99
100 // if this element should ignore parent position and just position itself
101 bool isAbsolute = false;
102
105
107 bool hidden = false;
108
109 // bounds on screen of this element
110 CST_Rect getBounds();
111
112 // whether or not this should be automatically free'd by wipeAll
113 bool isProtected = false;
114
118
120 int width = 0, height = 0;
121
122 typedef Element super;
123
124 // position relative to parent (if given) or screen (NULL parent)
125 int x = 0, y = 0;
126
127 // actual onscreen position (calculated at render time)
128 int xAbs = 0, yAbs = 0;
130 double angle = 0;
131
132 // x and y offsets (can be used for drawing relative to other elements)
133 int xOff = 0, yOff = 0;
134
135 // corner radius (when non-zero, backgrounds, textures, and rectangles are rounded)
136 int cornerRadius = 0;
137
138 // internal get current renderer or default one
139 CST_Renderer* getRenderer();
140
141 // delete this element's children, and their children, and so on
142 // if you've been using `new` everywhere, calling this can make sense in your
143 // top level component's destructor to free all the memory from that branch
144 // (not automatically invoked in case implementor wants to manage it)
145 void wipeAll(bool delSelf = false);
146
147 // fun chain-able wrappers to some fields, returns back the same element
148 Element* child(Element* child);
149 Element* setPosition(int x, int y);
150 Element* setAction(std::function<void()> func);
151
152 // alignment chainers
153 Element* centerHorizontallyIn(Element* parent);
154 Element* centerVerticallyIn(Element* parent);
155 Element* centerIn(Element* parent);
156 Element* setAbsolute(bool isAbs);
157
158 // constraints that can be added and used by positioning functions
159 std::vector<Constraint*> constraints;
160 Element* constrain(int flags, int padding = 0);
161
162 // animations that can be added and will tween over time (and remove when finished)
163 std::vector<Animation*> animations;
164 Element* animate(
165 int durationIn,
166 std::function<void(float)> onStep,
167 std::function<void()> onFinish
168 );
169
170 Element* moveToFront();
171 Element* setTouchable(bool touchable);
172
174 void screenshot(std::string path);
175
177 bool useColorMask = false;
178
180 CST_Color maskColor = {0,0,0,0};
181
182 // a function to call to re-align according to all constraints
183 // std::function<void()> alignmentCommands;
184};
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:107
bool dragging
whether or not this element is currently being dragged
Definition: Element.hpp:83
bool touchable
whether or not this element can be touched (highlights bounds)
Definition: Element.hpp:80
void screenshot(std::string path)
Take a screenshot of this element and its children, and save it to the given path.
Definition: Element.cpp:449
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
Definition: Element.hpp:60
int futureRedrawCounter
whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws)
Definition: Element.hpp:89
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:117
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:177
int lastMouseY
the last Y, X coordinate of the mouse (from a drag probably)
Definition: Element.hpp:92
void position(int x, int y)
position the element
Definition: Element.cpp:206
double angle
rotation angle in degrees
Definition: Element.hpp:130
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
Definition: Element.hpp:86
CST_Color maskColor
The color to overlay on top.
Definition: Element.hpp:180
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
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:104
std::vector< Element * > elements
visible GUI child elements of this element
Definition: Element.hpp:64