Skyward boardcore
Loading...
Searching...
No Matches
VerticalLayout.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 "View.h"
26
27namespace Boardcore
28{
29
34class VerticalLayout : public View
35{
36public:
42 VerticalLayout(short int spacing = 0) : View(), spacing(spacing) {}
43
50 void addView(View* view, float weight)
51 {
52 if (weight < 0)
53 weight = 0;
54 childs.push_back({weight, view});
55 weigthSum += weight;
56
57 // Recalculated child bounds
58 updateChildBounds();
59
60 // Redraw
61 invalidate();
62 }
63
64 void setBounds(Bounds bounds) override
65 {
66 View::setBounds(bounds);
67
68 updateChildBounds();
69 }
70
71 void draw(mxgui::DrawingContext& dc) override
72 {
73 View::draw(dc);
74
75 for (auto view : childs)
76 view.drawable->draw(dc);
77 }
78
79 virtual std::vector<View*> getChilds() override
80 {
81 std::vector<View*> out;
82 out.reserve(childs.size());
83 for (auto v : childs)
84 out.push_back(v.drawable);
85 return out;
86 }
87
88private:
89 void updateChildBounds()
90 {
91 if (weigthSum > 0)
92 {
93 short int avalHeight =
94 getBounds().size.height - (childs.size() - 1) * spacing;
95
96 short int y = 0;
97
98 for (auto view : childs)
99 {
100 Bounds viewBounds{{0, y}, {getBounds().size.width, 0}};
101
102 viewBounds.size.height =
103 avalHeight / weigthSum * view.vertWeight;
104 viewBounds.pos += getBounds().pos;
105
106 view.drawable->setBounds(viewBounds);
107
108 y += viewBounds.size.height + spacing;
109 }
110 }
111 }
112
113 struct Child
114 {
115 float vertWeight;
116 View* drawable;
117 };
118
119 float weigthSum = 0;
120 short int spacing;
121 std::vector<Child> childs;
122};
123
124} // namespace Boardcore
Positions the childs in a vertical grid. The height of each child is dictated by its weight parameter...
VerticalLayout(short int spacing=0)
Creates a new VerticalLayout.
void draw(mxgui::DrawingContext &dc) override
Draw the view in its bounds.
void setBounds(Bounds bounds) override
Sets the bounds in which the view will be drawn.
virtual std::vector< View * > getChilds() override
Returns the list of childs.
void addView(View *view, float weight)
Adds a view to the layout.
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 height
Definition View.h:39
short int width
Definition View.h:38