BuGUI
BUtton Grid User Interface
Loading...
Searching...
No Matches
controller.hpp
1#pragma once
2#include "ctrlr_shifter.hpp"
3#include <io/painter.hpp>
4#include <devices/all.hpp>
5#include <io/button_painter.hpp>
6#include <widgets/container_widget.hpp>
7
8#include <string_view>
9
10namespace bugui
11{
12struct base_hardware;
16template <typename T>
17struct controller : ctrlr_shifter<T>
18{
19 // FIXME : a lot of duplicates from container_widget
20 void update() override final;
21 void add_held(base_widget* widget) override final;
22 void remove_held(base_widget* widget) override final;
23 void handle_painter(painter& painter) const override final;
24 bool handle_presser(presser& presser) override final;
25
26protected:
27 explicit controller(std::string_view device_name)
28 // FIXME : Passing list of device Macro as variadic
29 // template, is this a good idea ?
31 ::get(device_name, static_cast<T*>(this))}
32 { }
33
34 void repaint() override final;
35
38 bool handle_child_Delete() override final { return true; };
39};
40
41template<typename T>
42inline void controller<T>::update()
43{
44 if constexpr (has_set<T>)
45 static_cast<T*>(this)->set();
46
47 for (auto& c : this->children) c->update();
48}
49
50template<typename T>
51inline void controller<T>::add_held(base_widget* widget)
52{
53 this->held.push_back(widget);
54
55 if constexpr (has_held_added<T>)
56 static_cast<T*>(this)->on_held_added();
57}
58
59template<typename T>
60inline void controller<T>::remove_held(base_widget* widget)
61{
62 if (this->held.empty()) return;
63
64 const auto it{std::find(this->held.begin(), this->held.end()
65 , widget)};
66 if (it == this->held.end()) return;
67
68 this->held.erase(it);
69 if constexpr (has_held_removed<T>)
70 static_cast<T*>(this)->on_held_removed();
71}
72
73template<typename T>
74inline void controller<T>::handle_painter(painter& painter) const
75{
76 if constexpr (has_paint<T>)
77 {
78 auto x{this->get_x()};
79 auto y{this->get_y()};
80
81 painter.ofset(x, y);
82 static_cast<const T*>(this)->paint(painter);
83 painter.ofset(-x, -y);
84 }
85
86 for (const auto& c : this->children) c->handle_painter(painter);
87}
88
89template<typename T>
90inline bool controller<T>::handle_presser(presser& presser)
91{
92 for (const auto& c : this->children)
93 if (c->handle_presser(presser))
94 return true;
95
96 presser.ofset(-this->get_x(), -this->get_y());
97
98 if (!this->overlap(presser))
99 return false;
100
101 if constexpr (has_double_press<T>)
102 if (presser.get_state() == presser::doubled)
103 static_cast<T*>(this)->on_double_press(presser);
104
105 if constexpr (has_drag<T>)
106 if (presser.get_state() == presser::dragged)
107 static_cast<T*>(this)->on_drag(presser);
108
109 if constexpr (has_drop<T>)
110 if (presser.get_state() == presser::dropped)
111 static_cast<T*>(this)->on_drop(presser);
112
113 if constexpr (has_press<T>)
114 if (presser.get_state() == presser::single)
115 static_cast<T*>(this)->on_press(presser);
116
117 return true;
118}
119
120template<typename T>
122{
123 this->paint.set(this->get_x(), this->get_y());
124 this->paint.bound_at_offset(this);
125
126 if constexpr (has_paint<T>)
127 static_cast<T*>(this)->paint(this->paint);
128
129 this->paint.reset();
130 for (const auto& c : this->children)
131 c->handle_painter(this->paint);
132
133 this->hdw->set_grid(this->paint);
134}
135
136} // namespace bugui
137
138
Definition widget.hpp:27
Definition widget.hpp:33
Definition widget.hpp:39
Definition widget.hpp:51
Definition widget.hpp:57
Definition widget.hpp:15
Definition widget.hpp:21
Provides base functionalities of the hardware object, and enables access from the base_controller.
Definition base_hardware.hpp:11
Provides the minimum required 2D widget functionalities.
Definition base_widget.hpp:12
@ single
Single press.
Definition button_presser.hpp:19
@ dragged
Dragged.
Definition button_presser.hpp:21
@ doubled
Doulbe press.
Definition button_presser.hpp:20
@ dropped
Dropped.
Definition button_presser.hpp:22
bool handle_child_Delete() override final
garanties that ctrlr_deleter::recursive_child_Delete returns
Definition controller.hpp:38
void repaint() override final
Calls for this widget to be repainted.
Definition controller.hpp:121
Ensures that buttons labeled Shift are registered on press and can be queried with Shift_held,...
Definition ctrlr_shifter.hpp:12
Instantiate a hardware object specialised with a specific device.
Definition hardware_factory.hpp:12
The painter class.
Definition painter.hpp:12
Definition presser.hpp:14
To be inherited to create custom wigets. Derived classes must be passeed as the template type.
Definition widget.hpp:12