diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-08-01 18:38:06 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-08-01 18:38:06 +0200 |
commit | 6624db873c91087bc1805b9d018c92c455b85190 (patch) | |
tree | 169010942015f1ff209cd56600f4de433c8792ef /src/gui | |
parent | 5d40409fc6e54af4c4dccdab11f03bce21c5a9a2 (diff) |
Move dropdown button text and icon code to dropdown button class
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/Button.cpp | 3 | ||||
-rw-r--r-- | src/gui/ComboBox.cpp | 44 | ||||
-rw-r--r-- | src/gui/DropdownButton.cpp | 181 | ||||
-rw-r--r-- | src/gui/Utils.cpp | 43 | ||||
-rw-r--r-- | src/gui/WidgetContainer.cpp | 5 |
5 files changed, 235 insertions, 41 deletions
diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index b9d6d7c..3cad31f 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -1,4 +1,5 @@ #include "../../include/gui/Button.hpp" +#include "../../include/Theme.hpp" #include <mglpp/graphics/Rectangle.hpp> #include <mglpp/window/Window.hpp> #include <mglpp/window/Event.hpp> @@ -40,7 +41,7 @@ namespace gsr { } const int border_size = 5; - const mgl::Color border_color(118, 185, 0); + const mgl::Color border_color = gsr::get_theme().tint_color; // Green line at top { diff --git a/src/gui/ComboBox.cpp b/src/gui/ComboBox.cpp index 00e8bc3..dd683e1 100644 --- a/src/gui/ComboBox.cpp +++ b/src/gui/ComboBox.cpp @@ -1,4 +1,6 @@ #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> @@ -46,40 +48,6 @@ namespace gsr { 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(); @@ -102,10 +70,10 @@ namespace gsr { mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top); Item &item = items[selected_item]; - item.text.set_position(pos); + item.text.set_position(pos.floor()); if(show_dropdown) { const int border_size = 3; - const mgl::Color border_color(118, 185, 0); + const mgl::Color border_color = gsr::get_theme().tint_color; draw_rectangle_outline(window, pos - mgl::vec2f(padding_left, padding_top), item_size, border_color, border_size); } window.draw(item.text); @@ -113,7 +81,7 @@ namespace gsr { for(size_t i = 0; i < items.size(); ++i) { Item &item = items[i]; - item.text.set_position(pos); + item.text.set_position(pos.floor()); const mgl::FloatRect text_bounds = item.text.get_bounds(); if(show_dropdown) { @@ -121,7 +89,7 @@ namespace gsr { 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)); + item_background.set_color(gsr::get_theme().tint_color); window.draw(item_background); } else { /*const int border_size = 3; diff --git a/src/gui/DropdownButton.cpp b/src/gui/DropdownButton.cpp new file mode 100644 index 0000000..9610e26 --- /dev/null +++ b/src/gui/DropdownButton.cpp @@ -0,0 +1,181 @@ +#include "../../include/gui/DropdownButton.hpp" +#include "../../include/gui/Utils.hpp" +#include "../../include/Theme.hpp" +#include <mglpp/graphics/Rectangle.hpp> +#include <mglpp/graphics/Texture.hpp> +#include <mglpp/window/Window.hpp> +#include <mglpp/window/Event.hpp> +#include <mglpp/system/FloatRect.hpp> + +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; + static const int border_size = 5; + + DropdownButton::DropdownButton(mgl::Font *title_font, mgl::Font *description_font, const char *title, const char *description_activated, const char *description_deactivated, mgl::Texture *icon_texture, mgl::vec2f size) : + title_font(title_font), description_font(description_font), size(size), title(title, *title_font), description(description_deactivated, *description_font), + description_activated(description_activated), description_deactivated(description_deactivated) + { + if(icon_texture && icon_texture->is_valid()) { + icon_sprite.set_texture(icon_texture); + icon_sprite.set_height((int)(size.y * 0.5f)); + } + this->description.set_color(mgl::Color(150, 150, 150)); + } + + bool DropdownButton::on_event(mgl::Event &event, mgl::Window&) { + if(event.type == mgl::Event::MouseMoved) { + const mgl::vec2f collision_margin(1.0f, 1.0f); // Makes sure that multiple buttons that are next to each other wont activate at the same time when the cursor is right between them + const bool inside = mgl::FloatRect(position + collision_margin, size - collision_margin).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); + if(mouse_inside && !inside) { + mouse_inside = false; + } else if(!mouse_inside && inside) { + mouse_inside = true; + } + } else if(event.type == mgl::Event::MouseButtonPressed) { + const bool clicked_inside = mouse_inside; + show_dropdown = clicked_inside; + if(on_click && mouse_inside_item >= 0 && mouse_inside_item < (int)items.size()) + on_click(items[mouse_inside_item].id); + } + return true; + } + + void DropdownButton::draw(mgl::Window &window) { + update_if_dirty(); + + if(show_dropdown) { + // Background + { + mgl::Rectangle rect(size); + rect.set_position(position); + rect.set_color(mgl::Color(0, 0, 0, 255)); + window.draw(rect); + } + + const mgl::Color border_color = gsr::get_theme().tint_color; + + // Green line at top + { + mgl::Rectangle rect({ size.x, border_size }); + rect.set_position(position); + rect.set_color(border_color); + window.draw(rect); + } + } else if(mouse_inside) { + // Background + { + mgl::Rectangle rect(size); + rect.set_position(position); + rect.set_color(mgl::Color(0, 0, 0, 255)); + window.draw(rect); + } + + const mgl::Color border_color = gsr::get_theme().tint_color; + draw_rectangle_outline(window, position, size, border_color, border_size); + } else { + // Background + mgl::Rectangle rect(size); + rect.set_position(position); + rect.set_color(mgl::Color(0, 0, 0, 220)); + window.draw(rect); + } + + const int text_margin = size.y * 0.085; + + const auto title_bounds = title.get_bounds(); + title.set_position((position + mgl::vec2f(size.x * 0.5f - title_bounds.size.x * 0.5f, text_margin)).floor()); + window.draw(title); + + const auto description_bounds = description.get_bounds(); + description.set_position((position + mgl::vec2f(size.x * 0.5f - description_bounds.size.x * 0.5f, size.y - description_bounds.size.y - text_margin)).floor()); + window.draw(description); + + if(icon_sprite.get_texture()->is_valid()) { + icon_sprite.set_position((position + size * 0.5f - icon_sprite.get_size() * 0.5f).floor()); + window.draw(icon_sprite); + } + + mouse_inside_item = -1; + if(show_dropdown) { + const mgl::vec2i mouse_pos = window.get_mouse_position(); + + mgl::Rectangle dropdown_bg(max_size); + dropdown_bg.set_position(position + mgl::vec2f(0.0f, size.y)); + dropdown_bg.set_color(mgl::Color(0, 0, 0)); + window.draw(dropdown_bg); + + mgl::vec2f item_position = dropdown_bg.get_position(); + for(size_t i = 0; i < items.size(); ++i) { + auto &item = items[i]; + const auto text_bounds = item.text.get_bounds(); + const float item_height = padding_top + text_bounds.size.y + padding_bottom; + + if(mouse_inside_item == -1) { + const mgl::vec2f item_size(max_size.x, item_height); + const bool inside = mgl::FloatRect(item_position, item_size).contains({ (float)mouse_pos.x, (float)mouse_pos.y }); + if(inside) { + draw_rectangle_outline(window, item_position, item_size, gsr::get_theme().tint_color, 5); + mouse_inside_item = i; + } + } + + item.text.set_position((item_position + mgl::vec2f(padding_left, item_height * 0.5f - text_bounds.size.y * 0.5f)).floor()); + window.draw(item.text); + item_position.y += item_height; + } + } + } + + void DropdownButton::add_item(const std::string &text, const std::string &id) { + items.push_back({mgl::Text(text, *title_font), id}); + dirty = true; + } + + void DropdownButton::set_item_label(const std::string &id, const std::string &new_label) { + for(auto &item : items) { + if(item.id == id) { + item.text.set_string(new_label); + dirty = true; + return; + } + } + } + + void DropdownButton::set_activated(bool activated) { + if(this->activated == activated) + return; + + this->activated = activated; + + if(activated) { + description = mgl::Text(description_activated, *description_font); + description.set_color(get_theme().tint_color); + icon_sprite.set_color(get_theme().tint_color); + } else { + description = mgl::Text(description_deactivated, *description_font); + description.set_color(mgl::Color(150, 150, 150)); + icon_sprite.set_color(mgl::Color(255, 255, 255)); + } + } + + void DropdownButton::update_if_dirty() { + if(!dirty) + return; + + max_size = { size.x, 0.0f }; + 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 DropdownButton::get_size() { + update_if_dirty(); + return size; + } +}
\ No newline at end of file diff --git a/src/gui/Utils.cpp b/src/gui/Utils.cpp new file mode 100644 index 0000000..20c7b73 --- /dev/null +++ b/src/gui/Utils.cpp @@ -0,0 +1,43 @@ +#include "../../include/gui/Utils.hpp" +#include <mglpp/window/Window.hpp> +#include <mglpp/graphics/Rectangle.hpp> + +namespace gsr { + // TODO: Use vertices to make it one draw call + void draw_rectangle_outline(mgl::Window &window, mgl::vec2f pos, mgl::vec2f size, mgl::Color color, float border_size) { + pos = pos.floor(); + size = size.floor(); + border_size = (int)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); + } + } +}
\ No newline at end of file diff --git a/src/gui/WidgetContainer.cpp b/src/gui/WidgetContainer.cpp index ab97b5b..8824d1a 100644 --- a/src/gui/WidgetContainer.cpp +++ b/src/gui/WidgetContainer.cpp @@ -42,10 +42,11 @@ namespace gsr { Widget *widget = *it; if(widget->move_to_top) { widget->move_to_top = false; - if(widgets.back() != widget) { + std::swap(*it, widgets.back()); + /*if(widgets.back() != widget) { widgets.erase(it); widgets.push_back(widget); - } + }*/ } } |