BuGUI
BUtton Grid User Interface
Loading...
Searching...
No Matches
controller.hpp
1#pragma once
2#include "ctrlr_shifter.hpp"
3#include "ctrlr_deleter.hpp"
4#include <bugui/io/painter.hpp>
5#include <bugui/devices/all.hpp>
6#include <bugui/io/button_painter.hpp>
7#include <bugui/widgets/container_widget.hpp>
8
9#include <string_view>
10
11namespace bugui
12{
13struct base_hardware;
17template <typename T>
18struct controller
19 : ctrlr_shifter<T>
20 , ctrlr_deleter<T>
21{
22 // FIXME : a lot of duplicates from container_widget
23 void update() override final;
24 void add_held(base_widget* widget) override final;
25 void remove_held(base_widget* widget) override final;
26 void handle_painter(painter& painter) const override final;
27 bool handle_presser(presser& presser) override final;
28
29 stdx::error set_midi_in_port
30 (const libremidi::observer& observer
31 , std::string_view port_name)
32 { return this->hdw->set_midi_in_port(observer, port_name); };
33
34 stdx::error set_midi_out_port
35 (const libremidi::observer& observer
36 , std::string_view port_name)
37 {
38 auto ret{this->hdw->set_midi_out_port(observer, port_name)};
39 repaint();
40 return ret;
41 };
42
43protected:
44 explicit controller(std::string_view device_name)
45 // FIXME : Passing list of device Macro as variadic
46 // template, is this a good idea ?
49 ::get(device_name, static_cast<T*>(this))}
50 { }
51
52 void repaint() override final;
53
56 bool handle_child_Delete() override final { return true; };
57};
58
59template<typename T>
60inline void controller<T>::update()
61{
62 if constexpr (has_set<T>)
63 static_cast<T*>(this)->set();
64
65 for (auto& c : this->children) c->update();
66}
67
68template<typename T>
69inline void controller<T>::add_held(base_widget* widget)
70{
71 this->held.push_back(widget);
72
73 if constexpr (has_held_added<T>)
74 static_cast<T*>(this)->on_held_added();
75}
76
77template<typename T>
78inline void controller<T>::remove_held(base_widget* widget)
79{
80 if (this->held.empty()) return;
81
82 const auto it{std::find(this->held.begin(), this->held.end()
83 , widget)};
84 if (it == this->held.end()) return;
85
86 this->held.erase(it);
87 if constexpr (has_held_removed<T>)
88 static_cast<T*>(this)->on_held_removed();
89}
90
91template<typename T>
92inline void controller<T>::handle_painter(painter& painter) const
93{
94 if constexpr (has_paint<T>)
95 {
96 auto x{this->get_x()};
97 auto y{this->get_y()};
98
99 painter.ofset(x, y);
100 static_cast<const T*>(this)->paint(painter);
101 painter.ofset(-x, -y);
102 }
103
104 for (const auto& c : this->children) c->handle_painter(painter);
105}
106
107template<typename T>
108inline bool controller<T>::handle_presser(presser& presser)
109{
110 for (const auto& c : this->children)
111 if (c->handle_presser(presser))
112 return true;
113
114 presser.ofset(-this->get_x(), -this->get_y());
115
116 if (!this->overlap(presser))
117 return false;
118
119 if constexpr (has_double_press<T>)
121 static_cast<T*>(this)->on_double_press(presser);
122
123 if constexpr (has_drag<T>)
125 static_cast<T*>(this)->on_drag(presser);
126
127 if constexpr (has_drop<T>)
129 static_cast<T*>(this)->on_drop(presser);
130
131 if constexpr (has_press<T>)
133 static_cast<T*>(this)->on_press(presser);
134
135 return true;
136}
137
138template<typename T>
140{
141 this->paint.set(this->get_x(), this->get_y());
142 this->paint.bound_at_offset(this);
143
144 if constexpr (has_paint<T>)
145 static_cast<T*>(this)->paint(this->paint);
146
147 this->paint.reset();
148 for (const auto& c : this->children)
149 c->handle_painter(this->paint);
150
151 this->hdw->set_grid(this->paint);
152}
153
154} // namespace bugui
155
156
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:12
Provides the minimum required 2D widget functionalities.
Definition base_widget.hpp:12
@ single
Single press.
Definition button_presser.hpp:22
@ dragged
Dragged.
Definition button_presser.hpp:24
@ doubled
Doulbe press.
Definition button_presser.hpp:23
@ dropped
Dropped.
Definition button_presser.hpp:25
states get_state() const
Retieves the curent state.
Definition button_presser.hpp:29
bool handle_child_Delete() override final
garanties that ctrlr_deleter::recursive_child_Delete returns
Definition controller.hpp:56
void repaint() override final
Calls for this widget to be repainted.
Definition controller.hpp:139
Ensures that buttons labeled Delete triggers the delete funcitonlaity on the widget tree,...
Definition ctrlr_deleter.hpp:12
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
Adds 2D information on top of a base_painter. The painter is used in user defined paint funcions to d...
Definition painter.hpp:17
void ofset(int x, int y)
Increment current displacement values by the given coordinates.
Adds 2D information on top of a button_presser. Meant to traverse the entire widget tree,...
Definition presser.hpp:16
void ofset(int x, int y)
Displaces the underlying rectangle by the given arguments.
To be inherited for creating custom wigets. Derived classes must be passeed as the template type.
Definition widget.hpp:12