aboutsummaryrefslogtreecommitdiff
path: root/src/gui/List.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-08-03 23:36:11 +0200
committerdec05eba <dec05eba@protonmail.com>2024-08-03 23:36:11 +0200
commit3a20c417cbf7d5db1d9d26abfbda388e58f96c18 (patch)
treea7552bff0a5164446eaade0dde570cd6111e474d /src/gui/List.cpp
parentc080342fcd358561af7edc64cea2222880923b93 (diff)
Align list items, dont process selected item twice if changed in event loop, mouse button events should only occur when pressing left mouse button
Diffstat (limited to 'src/gui/List.cpp')
-rw-r--r--src/gui/List.cpp36
1 files changed, 25 insertions, 11 deletions
diff --git a/src/gui/List.cpp b/src/gui/List.cpp
index 10be76f..4a74f62 100644
--- a/src/gui/List.cpp
+++ b/src/gui/List.cpp
@@ -1,22 +1,28 @@
#include "../../include/gui/List.hpp"
+static float floor(float f) {
+ return (int)f;
+}
+
namespace gsr {
// TODO: Make this modifiable, multiple by window size.
// TODO: Add homogeneous option, using a specified max size of this list.
static const mgl::vec2f spacing(30.0f, 10.0f);
- List::List(Orientation orientation) : orientation(orientation) {}
+ 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) {
- if(selected_child_widget) {
- if(!selected_child_widget->on_event(event, window, mgl::vec2f(0.0f, 0.0f)))
+ // 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_child_widget) {
+ if(it->get() != selected_widget) {
if(!(*it)->on_event(event, window, mgl::vec2f(0.0f, 0.0f)))
return false;
}
@@ -27,13 +33,19 @@ namespace gsr {
void List::draw(mgl::Window &window, mgl::vec2f offset) {
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();
- // TODO: Do same vertical/horizontal alignment for items
switch(orientation) {
case Orientation::VERTICAL: {
for(auto &widget : widgets) {
- widget->set_position(draw_pos);
- if(widget.get() != selected_child_widget)
+ 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;
}
@@ -41,8 +53,10 @@ namespace gsr {
}
case Orientation::HORIZONTAL: {
for(auto &widget : widgets) {
- widget->set_position(draw_pos);
- if(widget.get() != selected_child_widget)
+ 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;
}
@@ -50,8 +64,8 @@ namespace gsr {
}
}
- if(selected_child_widget)
- selected_child_widget->draw(window, mgl::vec2f(0.0f, 0.0f));
+ if(selected_widget)
+ selected_widget->draw(window, mgl::vec2f(0.0f, 0.0f));
}
void List::add_widget(std::unique_ptr<Widget> widget) {