BuGUI
BUtton Grid User Interface
Loading...
Searching...
No Matches
widget.hpp
1#pragma once
2#include "deleter.hpp"
3#include "base_widget.hpp"
4#include <bugui/concepts/widget.hpp>
5
6namespace bugui
7{
10template <typename T, bool can_delete = true>
11struct widget : deleter<T, base_widget, can_delete>
12{
15 void update() override final;
16
24 void handle_painter(painter& painter) const override final;
25
35 bool handle_presser(presser& presser) override final;
36
37protected:
38 using deleter<T, base_widget, can_delete>::deleter;
39};
40
41template <typename T, bool can_delete>
43{
44 if constexpr (has_set<T>)
45 static_cast<T*>(this)->set();
46}
47
48template <typename T, bool can_delete>
50{
51 if constexpr (has_paint<T>)
52 {
53 auto x_offset{this->parent->get_x() + this->get_x()};
54 auto y_offset{this->parent->get_y() + this->get_y()};
55
56 painter.ofset(x_offset, y_offset);
57 static_cast<const T*>(this)->paint(painter);
58 painter.ofset(-x_offset, -y_offset);
59 }
60}
61
62template <typename T, bool can_delete>
64{
65 auto p_x{this->parent->get_x()};
66 auto p_y{this->parent->get_y()};
67
68 presser.ofset(p_x, p_y);
69
70 if (!this->overlap(presser))
71 {
72 presser.ofset(-p_x, -p_y);
73 return false;
74 }
75
76 auto x{this->get_x()};
77 auto y{this->get_y()};
78 auto x_offset{x + p_x};
79 auto y_offset{y + p_y};
80
81 presser.ofset(x, y);
82
83 if constexpr (!has_any_press<T>)
84 {
85 presser.ofset(-x_offset, -y_offset);
86 return true;
87 }
88
89 if constexpr (has_double_press<T>)
91 {
92 static_cast<T*>(this)->on_double_press(presser);
93 presser.ofset(-x_offset, -y_offset);
94 return true;
95 }
96
97 if constexpr (has_drag<T>)
99 {
100 this->parent->remove_held(this);
101 static_cast<T*>(this)->on_drag(presser);
102 presser.ofset(-x_offset, -y_offset);
103 return true;
104 }
105
106 if constexpr (has_drop<T>)
108 {
109 this->parent->remove_held(this);
110 static_cast<T*>(this)->on_drop(presser);
111 presser.ofset(-x_offset, -y_offset);
112 return true;
113 }
114
116 presser.get_bool() ? this->parent->add_held(this)
117 : this->parent->remove_held(this);
118
119 if constexpr (has_press<T>)
120 static_cast<T*>(this)->on_press(presser);
121
122 presser.ofset(-x_offset, -y_offset);
123 return true;
124}
125
126} // namespace bugui
Definition widget.hpp:45
Definition widget.hpp:27
Definition widget.hpp:33
Definition widget.hpp:39
Definition widget.hpp:15
Definition widget.hpp:21
Definition widget.hpp:9
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.
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
void update() override final
Calls the set() function of the derived class if defined.
Definition widget.hpp:42
bool handle_presser(presser &presser) override final
Depending on the presser::states, calls either on_double_press(presser&), on_drag(presser&) ,...
Definition widget.hpp:63
void handle_painter(painter &painter) const override final
Calls the paint(painter&) function of the derived class if defined. The position of the painter is of...
Definition widget.hpp:49