Skyward boardcore
Loading...
Searching...
No Matches
View.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <mxgui/display.h>
26#include <mxgui/misc_inst.h>
27#include <utils/Debug.h>
28
29#include <functional>
30
31#include "Misc.h"
32
33namespace Boardcore
34{
35
36struct Size
37{
38 short int width;
39 short int height;
40};
41
43{
44 short int x;
45 short int y;
46
47 operator mxgui::Point() { return mxgui::Point(x, y); }
48
50 {
51 return {short(x + other.x), short(y + other.y)};
52 }
53
55 {
56 return {short(x - other.x), short(y - other.y)};
57 }
58
59 void operator+=(const Position& other)
60 {
61 x += other.x;
62 y += other.y;
63 }
64
65 void operator-=(const Position& other)
66 {
67 x -= other.x;
68 y -= other.y;
69 }
70
71 void print() { TRACE("pos: %d %d\n", x, y); }
72};
73
74struct Bounds
75{
78
79 Position topLeft() { return {pos.x, pos.y}; }
80 Position topRight() { return {short(pos.x + size.width - 1), pos.y}; }
81
83 {
84 return {short(pos.x + size.width - 1), short(pos.y + size.height - 1)};
85 }
86
88 {
89 return {pos.x, short(pos.y + short(size.height - 1))};
90 }
91
92 void print()
93 {
94 TRACE("Bounds: p:{%d %d} s:{%d %d}\n", pos.x, pos.y, size.width,
95 size.height);
96 }
97};
98
99enum class VertAlignment
100{
101 TOP,
102 CENTER,
103 BOTTOM
104};
105
107{
108 LEFT,
109 CENTER,
110 RIGHT
111};
112
113enum class Interaction
114{
115 BTN_DOWN,
116 BTN_UP,
117 CLICK,
119};
120
125class View
126{
127public:
128 using OnInteractionListener = std::function<void(View*, Interaction)>;
129
130 View() {}
131 virtual ~View() {}
132
136 virtual void setBounds(Bounds bounds)
137 {
138 this->bounds = bounds;
139 invalidate();
140 }
141
142 Bounds getBounds() { return bounds; }
143
149 {
150 if (!invalidated)
151 invalidated = true;
152 }
153
158 {
159 invalidate();
160
161 std::vector<View*> childs = getChilds();
162 for (auto child : childs)
163 child->invalidateTree();
164 }
165
171 virtual void draw(mxgui::DrawingContext& dc)
172 {
173 if (invalidated)
174 {
175 invalidated = false;
176
177 dc.clear(bounds.topLeft(), bounds.bottomRight(),
179
180 dc.drawRectangle(bounds.topLeft(), bounds.bottomRight(),
181 selected ? colSelected : getBackgroundColor());
182 }
183 }
184
190 virtual std::vector<View*> getChilds()
191 {
192 // Default behavious is no childs
193 return {};
194 }
195
196 virtual void setBackgroundColor(mxgui::Color color)
197 {
198 if (color != colBg)
199 {
200 colBg = color;
201 invalidate();
202 }
203 }
204
205 virtual mxgui::Color getBackgroundColor() { return colBg; }
206
207 void setSelectedColor(mxgui::Color color)
208 {
209 colSelected = color;
210 invalidate();
211 }
212
213 void setSelectable(bool selectable) { this->selectable = selectable; }
214
215 bool isSelectable() { return selectable; }
216
217 void setSelected(bool selected)
218 {
219 this->selected = selected;
220 invalidate();
221 }
222
223 bool isSelected() { return selected && selectable; }
224
226 {
227 listeners.push_back(listener);
228 }
229
230 virtual void performInteraction(Interaction action)
231 {
232 for (auto lst : listeners)
233 if (lst)
234 lst(this, action);
235 }
236
237protected:
238 bool isInvalidated() { return invalidated; }
239
240private:
241 mxgui::Color colSelected = mxgui::red;
242 mxgui::Color colBg = mxgui::black;
243
244 std::vector<OnInteractionListener> listeners;
245
246 bool selected = false;
247 bool selectable = false;
248 bool invalidated = true;
249
250 Bounds bounds{{0, 0}, {0, 0}};
251
252 // Drawing big solid chunks (such as backgrounds) takes a lot of time: do
253 // not redraw if not necessary
254 bool wasSelected = false;
255 mxgui::Color lastBgColor = colBg;
256};
257
258} // namespace Boardcore
#define TRACE(...)
Definition Debug.h:58
Base class for anything that can be drawn on the screen and interacted with.
Definition View.h:126
virtual void draw(mxgui::DrawingContext &dc)
Draw the view in its bounds.
Definition View.h:171
virtual std::vector< View * > getChilds()
Returns the list of childs.
Definition View.h:190
virtual void setBackgroundColor(mxgui::Color color)
Definition View.h:196
std::function< void(View *, Interaction)> OnInteractionListener
Definition View.h:128
void addOnInteractionListener(OnInteractionListener listener)
Definition View.h:225
bool isSelected()
Definition View.h:223
virtual ~View()
Definition View.h:131
bool isSelectable()
Definition View.h:215
void invalidate()
Signal that what has been previously drawn is now invalid and has to be redrawn.
Definition View.h:148
Bounds getBounds()
Definition View.h:142
void setSelectable(bool selectable)
Definition View.h:213
virtual void performInteraction(Interaction action)
Definition View.h:230
void setSelected(bool selected)
Definition View.h:217
bool isInvalidated()
Definition View.h:238
virtual mxgui::Color getBackgroundColor()
Definition View.h:205
void setSelectedColor(mxgui::Color color)
Definition View.h:207
void invalidateTree()
Invalidate the view tree which has this view as a root.
Definition View.h:157
virtual void setBounds(Bounds bounds)
Sets the bounds in which the view will be drawn.
Definition View.h:136
This file includes all the types the logdecoder script will decode.
HorizAlignment
Definition View.h:107
VertAlignment
Definition View.h:100
Interaction
Definition View.h:114
Position bottomRight()
Definition View.h:82
Position pos
Definition View.h:76
Position topRight()
Definition View.h:80
Position topLeft()
Definition View.h:79
Position bottomLeft()
Definition View.h:87
void print()
Definition View.h:92
void operator+=(const Position &other)
Definition View.h:59
Position operator-(const Position &other)
Definition View.h:54
Position operator+(const Position &other)
Definition View.h:49
short int x
Definition View.h:44
short int y
Definition View.h:45
void operator-=(const Position &other)
Definition View.h:65
short int height
Definition View.h:39
short int width
Definition View.h:38