diff options
author | dec05eba <dec05eba@protonmail.com> | 2022-09-25 17:29:17 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2022-09-25 17:29:17 +0200 |
commit | 497217a3e09b577f650aaf503356588515067ca8 (patch) | |
tree | 2fd43b747957510c55d66caf81df30732c39c398 /src/gui | |
parent | 663a36df4aaea583e7cd56ad35b26d05f6151746 (diff) |
widgets
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/Button.cpp | 7 | ||||
-rw-r--r-- | src/gui/ComboBox.cpp | 161 | ||||
-rw-r--r-- | src/gui/Widget.cpp | 13 | ||||
-rw-r--r-- | src/gui/WidgetContainer.cpp | 56 |
4 files changed, 234 insertions, 3 deletions
diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index 1436f86..5a71a16 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -9,7 +9,7 @@ namespace gsr { } - void Button::on_event(mgl::Event &event, mgl::Window&) { + bool Button::on_event(mgl::Event &event, mgl::Window&) { if(event.type == mgl::Event::MouseMoved) { const bool inside = mgl::FloatRect(position, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); if(mouse_inside && !inside) { @@ -25,6 +25,7 @@ namespace gsr { if(clicked_inside && on_click) on_click(); } + return true; } void Button::draw(mgl::Window &window) { @@ -33,7 +34,7 @@ namespace gsr { { mgl::Rectangle rect(size); rect.set_position(position); - rect.set_color(mgl::Color(20, 20, 20, 255)); + rect.set_color(mgl::Color(0, 0, 0, 255)); window.draw(rect); } @@ -75,7 +76,7 @@ namespace gsr { // Background mgl::Rectangle rect(size); rect.set_position(position); - rect.set_color(mgl::Color(20, 20, 20, 225)); + rect.set_color(mgl::Color(0, 0, 0, 250)); window.draw(rect); } } diff --git a/src/gui/ComboBox.cpp b/src/gui/ComboBox.cpp new file mode 100644 index 0000000..b96b3ae --- /dev/null +++ b/src/gui/ComboBox.cpp @@ -0,0 +1,161 @@ +#include "../../include/gui/ComboBox.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 = 10.0f; + static const float padding_bottom = 10.0f; + static const float padding_left = 10.0f; + static const float padding_right = 10.0f; + + ComboBox::ComboBox(mgl::Font *font) : font(font) { + assert(font); + } + + bool ComboBox::on_event(mgl::Event &event, mgl::Window&) { + if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) { + 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 = position + 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)) { + selected_item = i; + show_dropdown = false; + return false; + } + pos.y += text_bounds.size.y + padding_top + padding_bottom; + } + } + + if(mgl::FloatRect(position, item_size).contains(mouse_pos)) { + show_dropdown = !show_dropdown; + move_to_top = true; + } else { + show_dropdown = false; + } + } + return true; + } + + static void draw_rectangle_outline(mgl::Window &window, mgl::vec2f pos, mgl::vec2f size, mgl::Color color, float border_size) { + // Green line at top + { + mgl::Rectangle rect({ size.x, border_size }); + rect.set_position(pos); + rect.set_color(color); + window.draw(rect); + } + + // Green line at bottom + { + mgl::Rectangle rect({ size.x, border_size }); + rect.set_position(pos + mgl::vec2f(0.0f, size.y - border_size)); + rect.set_color(color); + window.draw(rect); + } + + // Green line at left + { + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(pos + mgl::vec2f(0, border_size)); + rect.set_color(color); + window.draw(rect); + } + + // Green line at right + { + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(pos + mgl::vec2f(size.x - border_size, border_size)); + rect.set_color(color); + window.draw(rect); + } + } + + void ComboBox::draw(mgl::Window &window) { + update_if_dirty(); + + if(items.empty()) + return; + + 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(position, mgl::vec2f(max_size.x, item_size.y)); + if(show_dropdown) { + background.set_size(max_size); + background.set_color(mgl::Color(0, 0, 0)); + } else { + background.set_color(mgl::Color(0, 0, 0, 250)); + } + window.draw(background); + + mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top); + + Item &item = items[selected_item]; + item.text.set_position(pos); + if(show_dropdown) { + const int border_size = 3; + const mgl::Color border_color(118, 185, 0); + draw_rectangle_outline(window, pos - mgl::vec2f(padding_left, padding_top), item_size, border_color, border_size); + } + window.draw(item.text); + pos.y += item.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); + 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), item_size); + item_background.set_color(mgl::Color(118, 185, 0)); + 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::update_if_dirty() { + if(!dirty) + return; + + max_size = { 0.0f, font->get_character_size() + padding_top + 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; + } + dirty = false; + } + + mgl::vec2f ComboBox::get_size() { + update_if_dirty(); + return { max_size.x, font->get_character_size() + padding_top + padding_bottom }; + } +}
\ No newline at end of file diff --git a/src/gui/Widget.cpp b/src/gui/Widget.cpp index c30c1d1..718fd8d 100644 --- a/src/gui/Widget.cpp +++ b/src/gui/Widget.cpp @@ -1,7 +1,20 @@ #include "../../include/gui/Widget.hpp" +#include "../../include/gui/WidgetContainer.hpp" namespace gsr { + Widget::Widget() { + WidgetContainer::get_instance().add_widget(this); + } + + Widget::~Widget() { + WidgetContainer::get_instance().remove_widget(this); + } + void Widget::set_position(mgl::vec2f position) { this->position = position; } + + mgl::vec2f Widget::get_position() const { + return position; + } }
\ No newline at end of file diff --git a/src/gui/WidgetContainer.cpp b/src/gui/WidgetContainer.cpp new file mode 100644 index 0000000..ab97b5b --- /dev/null +++ b/src/gui/WidgetContainer.cpp @@ -0,0 +1,56 @@ +#include "../../include/gui/WidgetContainer.hpp" +#include "../../include/gui/Widget.hpp" + +namespace gsr { + // static + WidgetContainer& WidgetContainer::get_instance() { + static WidgetContainer instance; + return instance; + } + + void WidgetContainer::add_widget(Widget *widget) { + // TODO: to_be_added, and remove in the draw loop + #ifdef DEBUG + for(Widget *existing_widget : widgets) { + if(existing_widget == widget) + return; + } + #endif + widgets.push_back(widget); + } + + void WidgetContainer::remove_widget(Widget *widget) { + // TODO: to_be_removed, and remove in draw loop + for(auto it = widgets.begin(), end = widgets.end(); it != end; ++it) { + if(*it == widget) { + widgets.erase(it); + return; + } + } + } + + void WidgetContainer::on_event(mgl::Event &event, mgl::Window &window) { + // Process widgets by visibility (backwards) + for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) { + if(!(*it)->on_event(event, window)) + return; + } + } + + void WidgetContainer::draw(mgl::Window &window) { + for(auto it = widgets.begin(); it != widgets.end(); ++it) { + Widget *widget = *it; + if(widget->move_to_top) { + widget->move_to_top = false; + if(widgets.back() != widget) { + widgets.erase(it); + widgets.push_back(widget); + } + } + } + + for(Widget *widget : widgets) { + widget->draw(window); + } + } +}
\ No newline at end of file |