MyRP2_ui_core
Loading...
Searching...
No Matches
ui_core.cpp
1#include "ui_core.h"
2
3UIDisplayDevice::UIDisplayDevice(size_t width, size_t height, FramebufferFormat format, StructFramebufferText txt_cnf)
4 : Framebuffer(width, height, format, txt_cnf)
5{
6}
7
11
12UIModelObject::UIModelObject()
13{
14}
15
19
21{
22 return this->change_flag;
23}
24
25void UIModelObject::clear_change_flag()
26{
27 this->change_flag = false;
28}
29
31{
32 if (this->current_controller != _new_controller) // to avoid deadlock with recursive callback
33 {
34 this->current_controller = _new_controller;
35 this->current_controller->update_current_controlled_object(this);
36 }
37}
38
40{
41 if (this->status != _new_status)
42 {
43 this->status = _new_status;
45 }
46}
47
49{
50 return this->status;
51}
52
54{
55 this->change_flag = true;
56}
57
58UIControlledIncrementalValue::UIControlledIncrementalValue(int _min_value, int _max_value, bool _is_wrapable, int _increment)
60{
61 this->value = 0;
62 this->min_value = _min_value;
63 this->max_value = _max_value;
64 this->is_wrappable = _is_wrapable;
65 this->increment = _increment;
66}
67
71
72void UIControlledIncrementalValue::increment_value()
73{
74 int previous_value = value;
75 value += increment;
76 if (value > max_value)
77 value = (is_wrappable) ? min_value : max_value;
78 if (value != previous_value)
80}
81
82void UIControlledIncrementalValue::decrement_value()
83{
84 int previous_value = value;
85 value -= increment;
86 if (value < min_value)
87 value = (is_wrappable) ? max_value : min_value;
88 if (value != previous_value)
90}
91
93{
94 int previous_value = value;
95 value = std::min(max_value, std::max(min_value, _new_value));
96 if (value != previous_value)
98}
99
101{
102 return this->value;
103}
104
106{
107 return min_value;
108}
109
111{
112 return max_value;
113}
114
115UIObjectManager::UIObjectManager()
116 : UIControlledIncrementalValue(0, 0, true, 1)
117{
118 update_status(ControlledObjectStatus::IS_ACTIVE);
119 current_active_model = this;
120}
121
125
127{
128 this->managed_models.push_back(_new_model);
129 this->max_value = managed_models.size() - 1;
130}
131
132void UIObjectManager::increment_focus()
133{
134 int previous_value = value;
135 this->increment_value();
136 if (value != previous_value) // action takes place only when the value changes
137 {
138 this->managed_models[previous_value]->update_status(ControlledObjectStatus::IS_WAITING);
139 this->managed_models[this->value]->update_status(ControlledObjectStatus::HAS_FOCUS);
140 }
141}
142
143void UIObjectManager::decrement_focus()
144{
145 int previous_value = value;
146 this->decrement_value();
147 if (value != previous_value) // action takes place only when the value changes
148 {
149 this->managed_models[previous_value]->update_status(ControlledObjectStatus::IS_WAITING);
150 this->managed_models[this->value]->update_status(ControlledObjectStatus::HAS_FOCUS);
151 }
152}
153
154void UIObjectManager::make_managed_object_active()
155{
156 this->current_active_model = this->managed_models[this->value];
157 this->current_active_model->update_status(ControlledObjectStatus::IS_ACTIVE);
158 update_status(ControlledObjectStatus::IS_WAITING);
159}
160
161void UIObjectManager::make_manager_active()
162{
163 if (managed_models.size() != 0)
164 {
165 current_active_model->update_status(ControlledObjectStatus::HAS_FOCUS);
166 for (auto &&model : managed_models)
167 model->set_change_flag();
168 }
169 current_active_model = this;
170 update_status(ControlledObjectStatus::IS_ACTIVE);
171}
172
173UIController::UIController()
174{
175}
176
180
182{
183 if (this->current_controlled_object != _new_controlled_object)
184 {
185 this->current_controlled_object = _new_controlled_object;
186 this->current_controlled_object->update_current_controller(this);
187 }
188}
189
190void UIWidget::draw_border()
191{
192 rect(0, 0, frame_width, frame_height);
193}
194
195FramebufferColor UIWidget::blinking_us(uint32_t _blink_period)
196{
197 return ((time_us_32() / _blink_period) % 2) ? FramebufferColor::WHITE : FramebufferColor::BLACK;
198}
199
200UIWidget::UIWidget(UIDisplayDevice *_display_screen, size_t _frame_width, size_t _frame_height, uint8_t _widget_anchor_x, uint8_t _widget_anchor_y, bool _widget_with_border,
201 uint8_t _widget_border_width, FramebufferFormat _framebuffer_format, StructFramebufferText _framebuffer_txt_cnf)
202 : Framebuffer(_frame_width, _frame_height, _framebuffer_format, _framebuffer_txt_cnf)
203{
204 this->display_screen = _display_screen;
205 this->widget_anchor_x = _widget_anchor_x;
206 this->widget_anchor_y = _widget_anchor_y;
207 this->widget_with_border = _widget_with_border;
208 this->widget_border_width = (widget_with_border) ? _widget_border_width : 0;
209
210 widget_start_x = widget_border_width;
211 widget_start_y = widget_border_width;
212 widget_width = frame_width - 2 * widget_border_width;
213 widget_height = frame_height - 2 * widget_border_width;
214}
215
219
221{
222 this->displayed_model = _new_displayed_model;
223}
224
226{
227 this->display_screen = _new_display_device;
228}
229
231{
232 this->widgets.push_back(_sub_widget);
233}
234
236{
241 if (widgets.size() != 0)
242 {
243 for (auto &&w : widgets)
244 w->refresh();
245 }
251 if ((this->displayed_model != nullptr) and (this->displayed_model->has_changed()))
252 {
253 draw();
254 if (widget_with_border)
255 draw_border();
256 this->display_screen->show(this, this->widget_anchor_x, this->widget_anchor_y);
257 this->displayed_model->clear_change_flag();
258 }
259}
~UIControlledIncrementalValue()
Destroy the UIControlledIncrementalValue object.
Definition ui_core.cpp:68
int get_max_value()
Get the max value object.
Definition ui_core.cpp:110
UIControlledIncrementalValue(int _min_value=0, int _max_value=10, bool _is_wrapable=false, int increment=1)
Construct a new UIControlledIncrementalValue object.
Definition ui_core.cpp:58
int get_min_value()
Get the min value object.
Definition ui_core.cpp:105
void set_clipped_value(int _new_value)
Set the clipped value object.
Definition ui_core.cpp:92
int get_value()
Get the value object.
Definition ui_core.cpp:100
void update_current_controlled_object(UIModelObject *_new_controlled_object)
Definition ui_core.cpp:181
~UIController()
Destroy the UIController object.
Definition ui_core.cpp:177
This is the abstract class to handle all generic behavior of physical display devices (e....
Definition ui_core.h:73
UIDisplayDevice(size_t width, size_t height, FramebufferFormat format=FramebufferFormat::MONO_VLSB, StructFramebufferText txt_cnf={.font=font_8x8})
Construct a new UIDisplayDevice object.
Definition ui_core.cpp:3
virtual ~UIDisplayDevice()
Destroy the UIDisplayDevice object.
Definition ui_core.cpp:8
virtual void show()=0
This is an pure virtual member function that all final derived class must implement....
void update_status(ControlledObjectStatus _new_status)
Definition ui_core.cpp:39
~UIModelObject()
Destroy the UIModelObject object.
Definition ui_core.cpp:16
void set_change_flag()
Set the change flag object.
Definition ui_core.cpp:53
ControlledObjectStatus get_status()
Get the status object.
Definition ui_core.cpp:48
void update_current_controller(UIController *_new_controller)
Definition ui_core.cpp:30
bool has_changed()
Definition ui_core.cpp:20
~UIObjectManager()
Destroy the UIObjectManager object.
Definition ui_core.cpp:122
void add_managed_model(UIModelObject *_new_model)
Definition ui_core.cpp:126
virtual void set_displayed_model(UIModelObject *_new_displayed_model)
Set the displayed model object.
Definition ui_core.cpp:220
void add_widget(UIWidget *_sub_widget)
Definition ui_core.cpp:230
~UIWidget()
Destroy the UIWidget object.
Definition ui_core.cpp:216
static FramebufferColor blinking_us(uint32_t _blink_period)
Definition ui_core.cpp:195
virtual void refresh()
WARNING : this function can be redefined. When several widget display one Model, only le last one mus...
Definition ui_core.cpp:235
void set_display_screen(UIDisplayDevice *_new_display_device)
Set the display screen object.
Definition ui_core.cpp:225
UIWidget(UIDisplayDevice *_display_screen, size_t _frame_width, size_t _frame_height, uint8_t _widget_anchor_x, uint8_t _widget_anchor_y, bool _widget_with_border, uint8_t _widget_border_width=1, FramebufferFormat _framebuffer_format=FramebufferFormat::MONO_VLSB, StructFramebufferText _framebuffer_txt_cnf={.font=font_8x8})
Construct a new UIWidget object.
Definition ui_core.cpp:200
ControlledObjectStatus
The list of status that a model can have.
Definition ui_core.h:43