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#include <memory>
12
13#define DEEP_HIGHLIGHT 200
14#define THICK_HIGHLIGHT 150
15#define HIGHLIGHT 100
16#define NO_HIGHLIGHT 0
17
18#if defined(USE_RAMFS)
19#define RAMFS "resin:/"
20#else
21#define RAMFS "resin/"
22#endif
23
24namespace Chesto {
25
26class Constraint;
27
29{
30public:
31 Element();
32 virtual ~Element();
33
35 virtual bool process(InputEvents* event);
36
38 virtual void render(Element* parent);
39
40 // invoked on touchdown/up events
41 bool onTouchDown(InputEvents* event);
42 bool onTouchDrag(InputEvents* event);
43 bool onTouchUp(InputEvents* event);
44
45 // hide the element
46 void hide() { this->hidden = true; }
47 // unhide the element
48 void unhide() { this->hidden = false; }
49
50 // render the element's background
51 void renderBackground(bool fill = true);
52
55 std::function<void()> action = NULL;
56 std::function<void(InputEvents* event)> actionWithEvents = NULL;
57
59 std::vector<std::unique_ptr<Element, std::function<void(Element*)>>> elements;
60
61 // add already-built node to tree and transfer ownership to parent
62 void addNode(std::unique_ptr<Element> node);
63
64 // remove specific child by pointer
65 void remove(Element* element);
66
67 // clear all children, constraints, and animations
68 void removeAll();
69
70protected:
71 // Internal helper for stack-allocated members
72 void addStackMember(Element* element);
73
74public:
75
77 void position(int x, int y);
78
79 // recalculate xAbs and yAbs based on the given parent
80 void recalcPosition(Element* parent);
81
82 // the effective scale for this element after global scaling
83 float getEffectiveScale() const;
84
85 // the scale of the element (and its subelements!)
86 float scale = 1.0f;
87
89 bool touchable = false;
90
92 bool dragging = false;
93
95 bool needsRedraw = false;
96
99
101 int lastMouseY = 0, lastMouseX = 0;
102
103 // whether this element has a background
104 bool hasBackground = false;
105
106 // the color of the background
107 rgb backgroundColor = {0, 0, 0};
108
109 // opacity of the background (0-255)
110 int backgroundOpacity = 0xff;
111
112 // if this element should ignore parent position and just position itself
113 bool isAbsolute = false;
114
116 Element* parent = nullptr;
117
119 bool hidden = false;
120
121 // bounds on screen of this element
122 CST_Rect getBounds();
123
124 // whether this element is protected from automatic deletion logic TODO: do we sitl lneed this?
125 bool isProtected = false;
126
130
132 int width = 0, height = 0;
133
134 typedef Element super;
135
136 // relative position within parent (set by user or constraints)
137 int x = 0, y = 0;
138
139 // actual onscreen position (calculated by recalcPosition)
140 // these should be read-only, not set directly
141 int xAbs = 0, yAbs = 0;
142
144 double angle = 0;
145
146 // corner radius (when non-zero, backgrounds, textures, and rectangles are rounded)
147 int cornerRadius = 0;
148
149 // user-definable tag for finding elements (defaults to 0)
150 int tag = 0;
151
152 // internal get current renderer or default one
153 CST_Renderer* getRenderer();
154
155 // fun chain-able wrappers to some fields, returns back the same element
156 Element* setPosition(int x, int y);
157 Element* setAction(std::function<void()> func);
158 Element* setAbsolute(bool isAbs);
159
160 // Create and add node to tree in one step
161 // Returns raw pointer for convenience
162 template<typename T, typename... Args>
163 T* createNode(Args&&... args) {
164 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
165 T* rawPtr = ptr.get();
166 addNode(std::move(ptr));
167 return rawPtr;
168 }
169
170 // constraints that can be added and used by positioning functions
171 std::vector<std::unique_ptr<Constraint>> constraints;
172 Element* constrain(int flags, int padding = 0);
173 Element* constrainToTarget(Element* target, int flags, int padding = 0);
174
175 // animations that can be added and will tween over time (and remove when finished)
176 std::vector<std::unique_ptr<Animation>> animations;
177 Element* animate(
178 int durationIn,
179 std::function<void(float)> onStep,
180 std::function<void()> onFinish
181 );
182
183 Element* moveToFront();
184 Element* setTouchable(bool touchable);
185
187 void screenshot(std::string path);
188
190 bool useColorMask = false;
191
193 CST_Color maskColor = {0,0,0,0};
194
195 // a function to call to re-align according to all constraints
196 // std::function<void()> alignmentCommands;
197};
198
199} // namespace Chesto
std::vector< std::unique_ptr< Element, std::function< void(Element *)> > > elements
visible GUI child elements of this element
Definition: Element.hpp:59
double angle
rotation angle in degrees
Definition: Element.hpp:144
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
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
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
bool touchable
whether or not this element can be touched (highlights bounds)
Definition: Element.hpp:89
int lastMouseY
the last Y, X coordinate of the mouse (from a drag probably)
Definition: Element.hpp:101
CST_Color maskColor
The color to overlay on top.
Definition: Element.hpp:193
Element * parent
the parent element (reference only, not owned)
Definition: Element.hpp:116
void screenshot(std::string path)
Take a screenshot of this element and its children, and save it to the given path.
Definition: Element.cpp:544
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:129
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
Definition: Element.hpp:55
void position(int x, int y)
position the element
Definition: Element.cpp:257
int futureRedrawCounter
whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws)
Definition: Element.hpp:98
bool useColorMask
whether or not to overlay a color mask on top of this element
Definition: Element.hpp:190
bool dragging
whether or not this element is currently being dragged
Definition: Element.hpp:92