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
|
#include "../../include/gui/List.hpp"
#include "../../include/Theme.hpp"
static float floor(float f) {
return (int)f;
}
namespace gsr {
// TODO: Add homogeneous option, using a specified max size of this list.
static const mgl::vec2f spacing_scale(0.009f, 0.009f);
List::List(Orientation orientation, Alignment content_alignment) : orientation(orientation), content_alignment(content_alignment) {}
bool List::on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f) {
// We want to store the selected child widget since it can change in the event loop below
Widget *selected_widget = selected_child_widget;
if(selected_widget) {
if(!selected_widget->on_event(event, window, mgl::vec2f(0.0f, 0.0f)))
return false;
}
// Process widgets by visibility (backwards)
for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) {
// Ignore offset because widgets are positioned with offset in ::draw, this solution is simpler
if(it->get() != selected_widget) {
if(!(*it)->on_event(event, window, mgl::vec2f(0.0f, 0.0f)))
return false;
}
}
return true;
}
// TODO: Maybe call this from main on all widgets instead of from draw
void List::update() {
//widgets.insert(widgets.back(), std::make_move_iterator(add_queue.begin()), std::make_move_iterator(add_queue.end()));
std::move(add_queue.begin(), add_queue.end(), std::back_inserter(widgets));
add_queue.clear();
for(Widget *widget_to_remove : remove_queue) {
remove_widget_immediate(widget_to_remove);
}
remove_queue.clear();
}
void List::remove_widget_immediate(Widget *widget) {
for(auto it = widgets.begin(), end = widgets.end(); it != end; ++it) {
if(it->get() == widget) {
widgets.erase(it);
return;
}
}
}
void List::draw(mgl::Window &window, mgl::vec2f offset) {
update();
mgl::vec2f draw_pos = position + offset;
offset = {0.0f, 0.0f};
Widget *selected_widget = selected_child_widget;
// TODO: Handle start/end alignment
const mgl::vec2f size = get_size();
const mgl::vec2f spacing = (spacing_scale * get_theme().window_height).floor();
switch(orientation) {
case Orientation::VERTICAL: {
for(auto &widget : widgets) {
if(content_alignment == Alignment::CENTER)
offset.x = floor(size.x * 0.5f - widget->get_size().x * 0.5f);
widget->set_position(draw_pos + offset);
if(widget.get() != selected_widget)
widget->draw(window, mgl::vec2f(0.0f, 0.0f));
draw_pos.y += widget->get_size().y + spacing.y;
}
break;
}
case Orientation::HORIZONTAL: {
for(auto &widget : widgets) {
if(content_alignment == Alignment::CENTER)
offset.y = floor(size.y * 0.5f - widget->get_size().y * 0.5f);
widget->set_position(draw_pos + offset);
if(widget.get() != selected_widget)
widget->draw(window, mgl::vec2f(0.0f, 0.0f));
draw_pos.x += widget->get_size().x + spacing.x;
}
break;
}
}
if(selected_widget)
selected_widget->draw(window, mgl::vec2f(0.0f, 0.0f));
}
// void List::remove_child_widget(Widget *widget) {
// for(auto it = widgets.begin(), end = widgets.end(); it != end; ++it) {
// if(it->get() == widget) {
// widgets.erase(it);
// return;
// }
// }
// }
void List::add_widget(std::unique_ptr<Widget> widget) {
widget->parent_widget = this;
// TODO: Maybe only do this if this is called inside an event handler
//widgets.push_back(std::move(widget));
add_queue.push_back(std::move(widget));
}
void List::remove_widget(Widget *widget) {
for(auto it = add_queue.begin(), end = add_queue.end(); it != end; ++it) {
if(it->get() == widget) {
add_queue.erase(it);
return;
}
}
// TODO: Maybe only do this if this is called inside an event handler
remove_queue.push_back(widget);
}
// TODO: Cache result
mgl::vec2f List::get_size() {
mgl::vec2f size;
const mgl::vec2f spacing = (spacing_scale * get_theme().window_height).floor();
switch(orientation) {
case Orientation::VERTICAL: {
for(auto &widget : widgets) {
const auto widget_size = widget->get_size();
size.x = std::max(size.x, widget_size.x);
size.y += widget_size.y + spacing.y;
}
break;
}
case Orientation::HORIZONTAL: {
for(auto &widget : widgets) {
const auto widget_size = widget->get_size();
size.x += widget_size.x + spacing.x;
size.y = std::max(size.y, widget_size.y);
}
break;
}
}
return size;
}
}
|