Skyward boardcore
Loading...
Searching...
No Matches
GridLayout.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/misc_inst.h>
26
27#include <map>
28
29#include "View.h"
30
31namespace Boardcore
32{
33
37class GridLayout : public View
38{
39public:
40 using GridPosition = std::pair<uint8_t, uint8_t>;
41
49 GridLayout(uint8_t numRows, uint8_t numCols, short int spacing = 0)
50 : View(), numRows(numRows), numCols(numCols), spacing(spacing)
51 {
52 }
53
54 virtual ~GridLayout() {}
55
56 void setCell(View* child, unsigned int position)
57 {
58 uint8_t col = position % numCols;
59 uint8_t row = (position - col) / numCols;
60
61 setCell(child, row, col);
62 }
63
64 void setCell(View* child, uint8_t row, uint8_t col)
65 {
66 if (row >= numRows || col >= numCols)
67 return;
68
69 mapChilds[GridPosition(row, col)] = child;
70
71 updateChildBounds();
72 invalidate();
73 }
74
75 View* getCell(uint8_t row, uint8_t col)
76 {
77 GridPosition pos(row, col);
78
79 if (mapChilds.count(pos) > 0)
80 return mapChilds[pos];
81 else
82 return nullptr;
83 }
84
85 View* getCell(unsigned int position)
86 {
87 uint8_t col = position % numCols;
88 uint8_t row = position - col / numRows;
89
90 return getCell(row, col);
91 }
92
93 void clearCell(View* child)
94 {
95 for (auto it = mapChilds.begin(); it != mapChilds.end(); it++)
96 {
97 if (it->second == child)
98 {
99 mapChilds.erase(it);
100 break;
101 }
102 }
103
104 invalidate();
105 }
106
113 void setDrawBorder(bool drawBorder, mxgui::Color color = mxgui::white)
114 {
115 this->drawBorder = drawBorder;
116 borderColor = color;
117
118 invalidate();
119 }
120
121 void setBounds(Bounds bounds) override
122 {
123 View::setBounds(bounds);
124
125 updateChildBounds();
126 }
127
128 virtual void draw(mxgui::DrawingContext& context) override
129 {
130 View::draw(context);
131
132 for (uint8_t row = 0; row < numRows; ++row)
133 {
134 for (uint8_t col = 0; col < numCols; ++col)
135 {
136 GridPosition pos(row, col);
137
138 bool childSelected = false;
139 if (mapChilds.count(pos) > 0)
140 {
141 mapChilds[pos]->draw(context);
142 childSelected = mapChilds[pos]->isSelected();
143 }
144
145 if (drawBorder && !childSelected)
146 {
147 context.drawRectangle(mapChildBounds[pos].topLeft(),
148 mapChildBounds[pos].bottomRight(),
149 borderColor);
150 }
151 }
152 }
153 }
154
155 uint8_t getRows() { return numRows; }
156
157 uint8_t getCols() { return numCols; }
158
159 std::vector<View*> getChilds() override
160 {
161 std::vector<View*> out;
162 for (auto it = mapChilds.begin(); it != mapChilds.end(); it++)
163 out.push_back(it->second);
164 return out;
165 }
166
167private:
168 void updateChildBounds()
169 {
170 Bounds bounds = getBounds();
171 Bounds childBounds;
172
173 childBounds.size.width = std::max(
174 0, (bounds.size.width - (numCols + 1) * spacing) / numCols);
175 childBounds.size.height = std::max(
176 0, (bounds.size.height - (numRows + 1) * spacing) / numRows);
177
178 for (uint8_t row = 0; row < numRows; ++row)
179 {
180 for (uint8_t col = 0; col < numCols; ++col)
181 {
182 GridPosition pos(row, col);
183
184 childBounds.pos.x = bounds.pos.x + (col + 1) * spacing +
185 col * childBounds.size.width;
186 childBounds.pos.y = bounds.pos.y + (row + 1) * spacing +
187 row * childBounds.size.height;
188
189 mapChildBounds[pos] = childBounds;
190 if (mapChilds.count(pos) > 0)
191 mapChilds[pos]->setBounds(childBounds);
192 }
193 }
194 }
195
196 uint8_t numRows;
197 uint8_t numCols;
198 short int spacing;
199
200 bool drawBorder = false;
201 mxgui::Color borderColor = mxgui::white;
202
203 std::map<GridPosition, View*> mapChilds;
204 std::map<GridPosition, Bounds> mapChildBounds;
205};
206
207} // namespace Boardcore
Displays childs in a numRows*numCols grid.
Definition GridLayout.h:38
void setCell(View *child, uint8_t row, uint8_t col)
Definition GridLayout.h:64
GridLayout(uint8_t numRows, uint8_t numCols, short int spacing=0)
Creates a new GridLayout.
Definition GridLayout.h:49
void setCell(View *child, unsigned int position)
Definition GridLayout.h:56
std::pair< uint8_t, uint8_t > GridPosition
Definition GridLayout.h:40
void setBounds(Bounds bounds) override
Sets the bounds in which the view will be drawn.
Definition GridLayout.h:121
std::vector< View * > getChilds() override
Returns the list of childs.
Definition GridLayout.h:159
View * getCell(uint8_t row, uint8_t col)
Definition GridLayout.h:75
virtual void draw(mxgui::DrawingContext &context) override
Draw the view in its bounds.
Definition GridLayout.h:128
View * getCell(unsigned int position)
Definition GridLayout.h:85
void setDrawBorder(bool drawBorder, mxgui::Color color=mxgui::white)
Wether to draw the borders of each cell or not.
Definition GridLayout.h:113
void clearCell(View *child)
Definition GridLayout.h:93
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
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
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.
Position pos
Definition View.h:76
short int x
Definition View.h:44
short int y
Definition View.h:45
short int height
Definition View.h:39
short int width
Definition View.h:38