1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#include "../../include/gui/ComboBox.hpp"
#include "../../include/gui/Utils.hpp"
#include "../../include/Theme.hpp"
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/graphics/Font.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <assert.h>
namespace gsr {
static const float padding_top_scale = 0.004629f;
static const float padding_bottom_scale = 0.004629f;
static const float padding_left_scale = 0.007f;
static const float padding_right_scale = 0.007f;
static const float border_scale = 0.0015f;
ComboBox::ComboBox(mgl::Font *font) : font(font), dropdown_arrow(&get_theme().combobox_arrow_texture) {
assert(font);
}
bool ComboBox::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) {
if(!visible)
return true;
if(items.empty())
return true;
const int padding_top = padding_top_scale * get_theme().window_height;
const int padding_bottom = padding_bottom_scale * get_theme().window_height;
const int padding_left = padding_left_scale * get_theme().window_height;
if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) {
const mgl::vec2f draw_pos = position + offset;
const mgl::vec2f mouse_pos = { (float)event.mouse_button.x, (float)event.mouse_button.y };
const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom);
if(show_dropdown && !items.empty()) {
mgl::vec2f pos = draw_pos + mgl::vec2f(padding_left, padding_top);
pos.y += items[selected_item].text.get_bounds().size.y + padding_top + padding_bottom;
for(size_t i = 0; i < items.size(); ++i) {
Item &item = items[i];
const mgl::FloatRect text_bounds = item.text.get_bounds();
if(mgl::FloatRect(pos - mgl::vec2f(padding_left, padding_top), item_size).contains(mouse_pos)) {
const size_t prev_selected_item = selected_item;
selected_item = i;
show_dropdown = false;
remove_widget_as_selected_in_parent();
if(selected_item != prev_selected_item && on_selection_changed)
on_selection_changed(item.text.get_string(), item.id);
return false;
}
pos.y += text_bounds.size.y + padding_top + padding_bottom;
}
}
if(mgl::FloatRect(draw_pos, item_size).contains(mouse_pos)) {
show_dropdown = !show_dropdown;
if(show_dropdown)
set_widget_as_selected_in_parent();
else
remove_widget_as_selected_in_parent();
} else {
show_dropdown = false;
remove_widget_as_selected_in_parent();
}
}
return true;
}
void ComboBox::draw(mgl::Window &window, mgl::vec2f offset) {
if(!visible)
return;
update_if_dirty();
const int padding_top = padding_top_scale * get_theme().window_height;
const int padding_bottom = padding_bottom_scale * get_theme().window_height;
const int padding_left = padding_left_scale * get_theme().window_height;
const int padding_right = padding_right_scale * get_theme().window_height;
const mgl::vec2f draw_pos = (position + offset).floor();
const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom);
const mgl::vec2i mouse_pos = window.get_mouse_position();
bool inside = false;
mgl::Rectangle background(draw_pos, item_size.floor());
if(show_dropdown) {
background.set_size(max_size.floor());
background.set_color(mgl::Color(0, 0, 0));
} else {
background.set_color(mgl::Color(0, 0, 0, 120));
}
window.draw(background);
if(!show_dropdown) {
dropdown_arrow.set_height(get_dropdown_arrow_height());
dropdown_arrow.set_position(draw_pos + mgl::vec2f(item_size.x - dropdown_arrow.get_size().x - padding_right, item_size.y * 0.5f - dropdown_arrow.get_size().y * 0.5f).floor());
dropdown_arrow.set_color(mgl::Color(255, 255, 255, 30));
window.draw(dropdown_arrow);
}
const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f()) && !has_parent_with_selected_child_widget();
mgl::vec2f pos = draw_pos + mgl::vec2f(padding_left, padding_top);
if(selected_item < items.size()) {
Item &selected_item_widget = items[selected_item];
selected_item_widget.text.set_position(pos.floor());
if(show_dropdown || mouse_inside) {
const int border_size = std::max(1.0f, border_scale * get_theme().window_height);
const mgl::Color border_color = get_theme().tint_color;
draw_rectangle_outline(window, pos - mgl::vec2f(padding_left, padding_top), item_size.floor(), border_color, border_size);
}
window.draw(selected_item_widget.text);
pos.y += selected_item_widget.text.get_bounds().size.y + padding_top + padding_bottom;
}
for(size_t i = 0; i < items.size(); ++i) {
Item &item = items[i];
item.text.set_position(pos.floor());
const mgl::FloatRect text_bounds = item.text.get_bounds();
if(show_dropdown) {
if(!inside) {
inside = mgl::FloatRect(text_bounds.position - mgl::vec2f(padding_left, padding_top), item_size).contains({ (float)mouse_pos.x, (float)mouse_pos.y });
if(inside) {
mgl::Rectangle item_background((text_bounds.position - mgl::vec2f(padding_left, padding_top)).floor(), item_size.floor());
item_background.set_color(get_theme().tint_color);
window.draw(item_background);
} else {
/*const int border_size = 3;
const mgl::Color border_color(150, 150, 150);
draw_rectangle_outline(window, text_bounds.position, item_size, border_color, border_size);*/
}
}
window.draw(item.text);
}
pos.y += text_bounds.size.y + padding_top + padding_bottom;
}
}
void ComboBox::add_item(const std::string &text, const std::string &id) {
items.push_back({mgl::Text(text, *font), id});
dirty = true;
}
void ComboBox::set_selected_item(const std::string &id, bool trigger_event, bool trigger_event_even_if_selection_not_changed) {
for(size_t i = 0; i < items.size(); ++i) {
auto &item = items[i];
if(item.id == id) {
const size_t prev_selected_item = selected_item;
selected_item = i;
if(trigger_event && (trigger_event_even_if_selection_not_changed || selected_item != prev_selected_item) && on_selection_changed)
on_selection_changed(item.text.get_string(), item.id);
break;
}
}
}
const std::string& ComboBox::get_selected_id() const {
if(items.empty()) {
static std::string dummy;
return dummy;
} else {
return items[selected_item].id;
}
}
void ComboBox::update_if_dirty() {
if(!dirty)
return;
const int padding_top = padding_top_scale * get_theme().window_height;
const int padding_bottom = padding_bottom_scale * get_theme().window_height;
const int padding_left = padding_left_scale * get_theme().window_height;
const int padding_right = padding_right_scale * get_theme().window_height;
max_size = { 0.0f, font->get_character_size() + (float)padding_top + (float)padding_bottom };
for(Item &item : items) {
const mgl::vec2f bounds = item.text.get_bounds().size;
max_size.x = std::max(max_size.x, bounds.x + padding_left + padding_right);
max_size.y += bounds.y + padding_top + padding_bottom;
}
if(max_size.x <= 0.001f)
max_size.x = 50.0f;
max_size.x += padding_left + get_dropdown_arrow_height();
dirty = false;
}
mgl::vec2f ComboBox::get_size() {
if(!visible)
return {0.0f, 0.0f};
update_if_dirty();
const int padding_top = padding_top_scale * get_theme().window_height;
const int padding_bottom = padding_bottom_scale * get_theme().window_height;
return { max_size.x, font->get_character_size() + (float)padding_top + (float)padding_bottom };
}
float ComboBox::get_dropdown_arrow_height() const {
const int padding_top = padding_top_scale * get_theme().window_height;
const int padding_bottom = padding_bottom_scale * get_theme().window_height;
return (font->get_character_size() + padding_top + padding_bottom) * 0.4f;
}
}
|