Skyward boardcore
Loading...
Searching...
No Matches
OptionView.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 <map>
26#include <string>
27
28#include "GridLayout.h"
29#include "TextView.h"
30
31namespace Boardcore
32{
33
38class OptionView : public View
39{
40public:
41 using OnOptionChosenListener = std::function<void(unsigned int id)>;
42
43 OptionView(std::string name, std::map<uint8_t, std::string> options,
44 uint8_t defaultOption = 0, uint8_t numCols = 4)
45 : tvTitle(new TextView(name))
46 {
47 uint8_t numRows = options.size() / numCols;
48 if (options.size() % numCols != 0)
49 ++numRows;
50
51 gridOptions = new GridLayout(numRows, numCols);
52
53 unsigned int gridPos = 0;
54 unsigned int i = 0;
55 for (auto it = options.begin(); it != options.end(); it++)
56 {
57 using namespace std::placeholders;
58
59 TextView* tvOpt = new TextView(it->second);
60 tvOpt->setSelectable(true);
61 tvOpt->addOnInteractionListener(
62 std::bind(&OptionView::onOptionClick, this, _1, _2));
64
65 mapOptions[tvOpt] = it->first;
66
67 gridOptions->setCell(tvOpt, gridPos++);
68
69 if (i == defaultOption)
70 selectOption(tvOpt);
71
72 ++i;
73 }
74 }
75
76 virtual ~OptionView()
77 {
78 delete gridOptions;
79 delete tvTitle;
80 for (auto it = mapOptions.begin(); it != mapOptions.end(); it++)
81 delete it->first;
82 }
83
84 void setBounds(Bounds bounds) override
85 {
86 View::setBounds(bounds);
87
88 Bounds titleBounds = getBounds();
89 titleBounds.size.height = tvTitle->getFont().getHeight();
90 tvTitle->setBounds(titleBounds);
91
92 Bounds gridBounds = getBounds();
93 gridBounds.pos.y = getBounds().pos.y + titleBounds.size.height + 1;
94 gridBounds.size.height =
95 getBounds().size.height - titleBounds.size.height;
96
97 gridOptions->setBounds(gridBounds);
98 }
99
105 {
106 optListeners.push_back(listener);
107 }
108
109 std::vector<View*> getChilds() override
110 {
111 std::vector<View*> out;
112 out.push_back(tvTitle);
113 out.push_back(gridOptions);
114
115 return out;
116 }
117
118 void draw(mxgui::DrawingContext& dc) override
119 {
120 View::draw(dc);
121 tvTitle->draw(dc);
122 gridOptions->draw(dc);
123 }
124
125private:
126 void selectOption(TextView* opt)
127 {
128 if (selectedOption != nullptr)
129 selectedOption->setBackgroundColor(colBgNormal);
130
131 selectedOption = opt;
132
133 selectedOption->setBackgroundColor(colBgHighlight);
134 }
135
136 void onOptionClick(View* option, Interaction action)
137 {
138 if (action == Interaction::CLICK)
139 {
140 TextView* textview = static_cast<TextView*>(option);
141
142 selectOption(textview);
143
144 for (auto l : optListeners)
145 if (l != nullptr)
146 l(mapOptions[textview]);
147 }
148 }
149
150 TextView* tvTitle;
151 mxgui::Color colBgNormal = mxgui::black;
152 mxgui::Color colBgHighlight = mxgui::blue;
153
154 GridLayout* gridOptions;
155 std::map<TextView*, uint8_t> mapOptions;
156
157 std::vector<OnOptionChosenListener> optListeners;
158
159 TextView* selectedOption = nullptr;
160};
161
162} // namespace Boardcore
if(canDrivers[canDev]) canDrivers[canDev] -> handleRXInterrupt(fifo)
Displays childs in a numRows*numCols grid.
Definition GridLayout.h:38
void setCell(View *child, unsigned int position)
Definition GridLayout.h:56
void setBounds(Bounds bounds) override
Sets the bounds in which the view will be drawn.
Definition GridLayout.h:121
virtual void draw(mxgui::DrawingContext &context) override
Draw the view in its bounds.
Definition GridLayout.h:128
View used to display an option list, so the user can select one by clicking on it.
Definition OptionView.h:39
OptionView(std::string name, std::map< uint8_t, std::string > options, uint8_t defaultOption=0, uint8_t numCols=4)
Definition OptionView.h:43
void addOnOptionChosenListener(OnOptionChosenListener listener)
Adds a callback to be called each time an option is selected by the user.
Definition OptionView.h:104
std::function< void(unsigned int id)> OnOptionChosenListener
Definition OptionView.h:41
std::vector< View * > getChilds() override
Returns the list of childs.
Definition OptionView.h:109
void setBounds(Bounds bounds) override
Sets the bounds in which the view will be drawn.
Definition OptionView.h:84
void draw(mxgui::DrawingContext &dc) override
Draw the view in its bounds.
Definition OptionView.h:118
Simple view to display text on screen.
Definition TextView.h:37
virtual void draw(mxgui::DrawingContext &dc) override
Draw the view in its bounds.
Definition TextView.h:92
mxgui::Font getFont()
Definition TextView.h:151
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 void setBackgroundColor(mxgui::Color color)
Definition View.h:196
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.
Interaction
Definition View.h:114
Position pos
Definition View.h:76
short int y
Definition View.h:45
short int height
Definition View.h:39