Skyward boardcore
Loading...
Searching...
No Matches
NavController.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
27#include <utils/Debug.h>
28
29#include <vector>
30
31#include "TextView.h"
32#include "View.h"
33
34namespace Boardcore
35{
36
42{
43public:
45
51 void updateViewTree(View* root)
52 {
53 if (selectedIndex < vecSelectable.size())
54 vecSelectable.at(selectedIndex)->setSelected(false);
55
56 vecSelectable.clear();
57 selectedIndex = 0;
58
59 updateSelectableViews(root);
60
61 if (vecSelectable.size() > 0)
62 vecSelectable.at(0)->setSelected(true);
63 }
64
66 {
67 switch (press)
68 {
70 if (selectedIndex < vecSelectable.size())
71 {
72 vecSelectable.at(selectedIndex)
73 ->performInteraction(Interaction::BTN_DOWN);
74 }
75 if (vecSelectable.size() > 0)
76 selectNext();
77 break;
79 if (selectedIndex < vecSelectable.size())
80 {
81 vecSelectable.at(selectedIndex)
82 ->performInteraction(Interaction::BTN_UP);
83 }
84 if (vecSelectable.size() > 0)
85 selectNext();
86 break;
88 if (selectedIndex < vecSelectable.size())
89 {
90 vecSelectable.at(selectedIndex)
91 ->performInteraction(Interaction::CLICK);
92 }
93 if (vecSelectable.size() > 0)
94 selectNext();
95 break;
97 if (selectedIndex < vecSelectable.size())
98 {
99 vecSelectable.at(selectedIndex)
100 ->performInteraction(Interaction::LONG_CLICK);
101 }
102 if (vecSelectable.size() > 0)
103 selectNext();
104 break;
105 default:
106 break;
107 }
108 }
109
110private:
111 void selectNext()
112 {
113 // Deselect old drawble
114 if (selectedIndex < vecSelectable.size())
115 vecSelectable.at(selectedIndex)->setSelected(false);
116
117 if (vecSelectable.size() > 0)
118 {
119 selectedIndex = (selectedIndex + 1) % vecSelectable.size();
120
121 vecSelectable.at(selectedIndex)->setSelected(true);
122
123 TextView* text =
124 dynamic_cast<TextView*>(vecSelectable.at(selectedIndex));
125
126 if (text)
127 LOG_DEBUG(logger, "{}", text->getText().c_str());
128 }
129 }
130
131 void updateSelectableViews(View* root)
132 {
133 std::vector<View*> childs = root->getChilds();
134 for (auto child : childs)
135 {
136 if (child->isSelectable())
137 vecSelectable.push_back(child);
138 updateSelectableViews(child);
139 }
140 }
141
142 unsigned int selectedIndex = 0;
143 std::vector<View*> vecSelectable;
144
145 PrintLogger logger = Logging::getLogger("navcontroller");
146};
147
148} // namespace Boardcore
#define LOG_DEBUG(logger,...)
static PrintLogger getLogger(const string &name)
UI navigation controller: listens for button clicks and dispatches the interactions to the view tree.
void onButtonEvent(ButtonEvent press)
void updateViewTree(View *root)
Simple view to display text on screen.
Definition TextView.h:37
std::string getText()
Definition TextView.h:79
Base class for anything that can be drawn on the screen and interacted with.
Definition View.h:126
This file includes all the types the logdecoder script will decode.
@ PRESSED
The button is pressed.
@ LONG_PRESS
The button is released before VERY_LONG_PRESS_TICKS.
@ SHORT_PRESS
The button is released before LONG_PRESS_TICKS.
@ VERY_LONG_PRESS
The button is released after VERY_LONG_PRESS_TICKS.