BuGUI
BUtton Grid User Interface
Loading...
Searching...
No Matches
container_widget.hpp
1#pragma once
2#include "deleter.hpp"
3#include "base_container_widget.hpp"
4#include <bugui/concepts/widget.hpp>
5
6namespace bugui
7{
11template <typename T, bool can_delete = true>
13 : deleter<T, base_container_widget, can_delete>
14{
18 void update() override final;
19
26 void add_held(base_widget* widget) override final;
27
33 void remove_held(base_widget* widget) override final;
34
41 void handle_painter(painter& painter) const override final;
42
49 bool handle_presser(presser& presser) override final;
50
53 bool handle_child_Delete() override final;
54
55protected:
56 using deleter<T, base_container_widget, can_delete>::deleter;
57};
58
59template <typename T, bool can_delete>
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, bool can_delete>
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, bool can_delete>
79{
80 if (this->held.empty()) return;
81
82 const auto it{std::find(this->held.begin()
83 , this->held.end(), 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, bool can_delete>
93{
94 auto x_offset{this->parent->get_x()};
95 auto y_offset{this->parent->get_y()};
96
97 if constexpr (has_paint<T>)
98 {
99 auto x{this->get_x()};
100 auto y{this->get_y()};
101
102 painter.ofset(x_offset + x, y_offset + y);
103 static_cast<const T*>(this)->paint(painter);
104 painter.ofset(-x, -y);
105 }
106 else
107 painter.ofset(x_offset, y_offset);
108
109 for (const auto& c : this->children) c->handle_painter(painter);
110
111 painter.ofset(-x_offset, -y_offset);
112}
113
114template <typename T, bool can_delete>
116{
117 auto p_x{this->parent->get_x()};
118 auto p_y{this->parent->get_y()};
119
120 presser.ofset(p_x, p_y);
121
122 if (!this->overlap(presser))
123 {
124 presser.ofset(-p_x, -p_y);
125 return false;
126 }
127
128 for (const auto& c : this->children)
129 if (c->handle_presser(presser))
130 {
131 switch (presser.get_state())
132 {
133 case presser::single :
134 presser.get_bool() ? this->parent->add_held(this)
135 : this->parent->remove_held(this);
136 break;
137 case presser::dragged :
138 case presser::dropped :
139 this->parent->remove_held(this);
140 break;
141 default: break;
142 }
143
144 presser.ofset(-p_x, -p_y);
145 return true;
146 }
147
148 auto x{this->get_x()};
149 auto y{this->get_y()};
150 auto x_offset{x + p_x};
151 auto y_offset{y + p_y};
152
153 presser.ofset(x, y);
154
155 if constexpr (!has_any_press<T>)
156 {
157 presser.ofset(-p_x, -p_y);
158 return true;
159 }
160
161 presser.ofset(x, y);
162
163 if constexpr (has_double_press<T>)
165 {
166 static_cast<T*>(this)->on_double_press(presser);
167 presser.ofset(-x_offset, -y_offset);
168 return true;
169 }
170
171 if constexpr (has_drag<T>)
173 {
174 static_cast<T*>(this)->on_drag(presser);
175 presser.ofset(-x_offset, -y_offset);
176 return true;
177 }
178
179 if constexpr (has_drop<T>)
181 {
182 this->parent->remove_held(this);
183 static_cast<T*>(this)->on_drop(presser);
184 presser.ofset(-x_offset, -y_offset);
185 return true;
186 }
187
189 presser.get_bool() ? this->parent->add_held(this)
190 : this->parent->remove_held(this);
191
192 if constexpr (has_press<T>)
193 static_cast<T*>(this)->on_press(presser);
194
195 presser.ofset(-x_offset, -y_offset);
196 return true;
197}
198
199template <typename T, bool can_delete>
201{
202 if constexpr (has_child_Delete<T>)
203 {
204 static_cast<T*>(this)->on_child_Delete();
205 return true;
206 }
207 else return false;
208}
209
210} // namespace bugui
Definition widget.hpp:45
Definition widget.hpp:75
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
Definition widget.hpp:9
Provides functionalities for 2D widget that can hold other widgets as children.
Definition base_container_widget.hpp:14
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 get_bool()
Converts the currently stored value to boolean if necesary, and returns the result.
To be inherited to create custom wigets whcih can hold other widgets as children. Derived classes mus...
Definition container_widget.hpp:14
void update() override final
Calls the set function of the derived class if defined. update will then be called on every child wid...
Definition container_widget.hpp:60
bool handle_presser(presser &presser) override final
Ofsets the presser's corordinates to match the relative position of the widget. If a paint funciton i...
Definition container_widget.hpp:115
bool handle_child_Delete() override final
Calls on_child_Delete if defined.
Definition container_widget.hpp:200
void remove_held(base_widget *widget) override final
Called when a child widget is released. The asociated pointer stored in the parent is deleted....
Definition container_widget.hpp:78
void add_held(base_widget *widget) override final
Called when a child widget is held down. Pointers to held widgets are stored in their parent....
Definition container_widget.hpp:69
void handle_painter(painter &painter) const override final
Ofsets the painter's corordinates to match the relative position of the widget. If a paint funciton i...
Definition container_widget.hpp:92
Allows derived class to call a user defined on_Delete function.
Definition deleter.hpp:14
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