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