From 520c61ec64c001d2c9c9d953051aa58715831731 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 6 Aug 2024 08:22:42 +0200 Subject: Add checkbox, scale more sizes by window scale --- src/gui/Button.cpp | 12 ++++---- src/gui/CheckBox.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++ src/gui/ComboBox.cpp | 6 ++-- src/gui/DropdownButton.cpp | 5 ++-- src/gui/Entry.cpp | 3 +- 5 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 src/gui/CheckBox.cpp (limited to 'src/gui') diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index 9662c34..a0dd8e9 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -28,6 +28,7 @@ namespace gsr { void Button::draw(mgl::Window &window, mgl::vec2f offset) { const mgl::vec2f draw_pos = position + offset; + const mgl::vec2f item_size = get_size().floor(); mgl::Rectangle background(item_size); background.set_position(draw_pos.floor()); @@ -38,11 +39,8 @@ namespace gsr { window.draw(text); const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f()); - if(mouse_inside) { - const int border_size = 5; - const mgl::Color border_color = gsr::get_theme().tint_color; - draw_rectangle_outline(window, position, item_size, border_color, border_size); - } + if(mouse_inside) + draw_rectangle_outline(window, draw_pos, item_size, gsr::get_theme().tint_color, border_scale * gsr::get_theme().window_height); } mgl::vec2f Button::get_size() { @@ -54,4 +52,8 @@ namespace gsr { s.y = padding_top + text_bounds.y + padding_bottom; return s; } + + void Button::set_border_scale(float scale) { + border_scale = scale; + } } \ No newline at end of file diff --git a/src/gui/CheckBox.cpp b/src/gui/CheckBox.cpp new file mode 100644 index 0000000..db2764d --- /dev/null +++ b/src/gui/CheckBox.cpp @@ -0,0 +1,71 @@ +#include "../../include/gui/CheckBox.hpp" +#include "../../include/gui/Utils.hpp" +#include "../../include/Theme.hpp" +#include +#include +#include +#include + +namespace gsr { + static const float spacing_scale = 0.005f; + static const float checked_margin_scale = 0.003f; + static const float border_scale = 0.001f; + + CheckBox::CheckBox(mgl::Font *font, const char *text) : text(text, *font) { + + } + + bool CheckBox::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) { + if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) { + const bool clicked_inside = mgl::FloatRect(position + offset, get_size()).contains({ (float)event.mouse_button.x, (float)event.mouse_button.y }); + if(clicked_inside) { + checked = !checked; + if(on_click) + on_click(); + } + } + return true; + } + + void CheckBox::draw(mgl::Window &window, mgl::vec2f offset) { + const mgl::vec2f draw_pos = position + offset; + + const mgl::vec2f checkbox_size = get_checkbox_size(); + mgl::Rectangle background(get_checkbox_size()); + background.set_position(draw_pos.floor()); + background.set_color(mgl::Color(0, 0, 0, 120)); + window.draw(background); + + if(checked) { + const float side_margin = checked_margin_scale * get_theme().window_height; + mgl::Rectangle background(get_checkbox_size() - mgl::vec2f(side_margin, side_margin).floor() * 2.0f); + background.set_position(draw_pos.floor() + mgl::vec2f(side_margin, side_margin).floor()); + background.set_color(gsr::get_theme().tint_color); + window.draw(background); + } + + const mgl::vec2f text_bounds = text.get_bounds().size; + text.set_position((draw_pos + mgl::vec2f(checkbox_size.x + spacing_scale * get_theme().window_height, checkbox_size.y * 0.5f - text_bounds.y * 0.5f)).floor()); + window.draw(text); + + const bool mouse_inside = mgl::FloatRect(draw_pos, get_size()).contains(window.get_mouse_position().to_vec2f()); + if(mouse_inside) { + const int border_size = border_scale * gsr::get_theme().window_height; + const mgl::Color border_color = gsr::get_theme().tint_color; + draw_rectangle_outline(window, draw_pos, checkbox_size, border_color, border_size); + } + } + + mgl::vec2f CheckBox::get_size() { + mgl::vec2f size = text.get_bounds().size; + const mgl::vec2f checkbox_size = get_checkbox_size(); + size.x += checkbox_size.x + spacing_scale * get_theme().window_height; + size.y = std::max(size.y, checkbox_size.y); + return size; + } + + mgl::vec2f CheckBox::get_checkbox_size() { + const mgl::vec2f text_bounds = text.get_bounds().size; + return mgl::vec2f(text_bounds.y, text_bounds.y).floor(); + } +} \ No newline at end of file diff --git a/src/gui/ComboBox.cpp b/src/gui/ComboBox.cpp index 851e3da..058254f 100644 --- a/src/gui/ComboBox.cpp +++ b/src/gui/ComboBox.cpp @@ -12,6 +12,7 @@ namespace gsr { static const float padding_bottom = 10.0f; static const float padding_left = 10.0f; static const float padding_right = 10.0f; + static const float border_scale = 0.0015f; ComboBox::ComboBox(mgl::Font *font) : font(font), dropdown_arrow(&get_theme().combobox_arrow) { assert(font); @@ -82,12 +83,13 @@ namespace gsr { window.draw(dropdown_arrow); } + const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f()); mgl::vec2f pos = draw_pos + mgl::vec2f(padding_left, padding_top); Item &item = items[selected_item]; item.text.set_position(pos.floor()); - if(show_dropdown) { - const int border_size = 3; + if(show_dropdown || mouse_inside) { + const int border_size = border_scale * gsr::get_theme().window_height; const mgl::Color border_color = gsr::get_theme().tint_color; draw_rectangle_outline(window, pos - mgl::vec2f(padding_left, padding_top), item_size.floor(), border_color, border_size); } diff --git a/src/gui/DropdownButton.cpp b/src/gui/DropdownButton.cpp index 9fb4803..826d889 100644 --- a/src/gui/DropdownButton.cpp +++ b/src/gui/DropdownButton.cpp @@ -12,7 +12,7 @@ namespace gsr { static const float padding_bottom = 15.0f; static const float padding_left = 20.0f; static const float padding_right = 20.0f; - static const int border_size = 5; + static const float border_scale = 0.003f; 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), @@ -59,6 +59,7 @@ namespace gsr { update_if_dirty(); const mgl::vec2f draw_pos = position + offset; + const int border_size = border_scale * gsr::get_theme().window_height; if(show_dropdown) { // Background @@ -73,7 +74,7 @@ namespace gsr { // Green line at top { - mgl::Rectangle rect({ size.x, border_size }); + mgl::Rectangle rect({ size.x, (float)border_size }); rect.set_position(draw_pos); rect.set_color(border_color); window.draw(rect); diff --git a/src/gui/Entry.cpp b/src/gui/Entry.cpp index bf0f1ad..a8a521b 100644 --- a/src/gui/Entry.cpp +++ b/src/gui/Entry.cpp @@ -13,6 +13,7 @@ namespace gsr { static const float padding_bottom = 10.0f; static const float padding_left = 10.0f; static const float padding_right = 10.0f; + static const float border_scale = 0.0015f; Entry::Entry(mgl::Font *font, const char *text, float max_width) : text("", *font), max_width(max_width) { this->text.set_color(get_theme().text_color); @@ -46,7 +47,7 @@ namespace gsr { window.draw(background); if(selected) { - const int border_size = 3; + const int border_size = border_scale * gsr::get_theme().window_height; draw_rectangle_outline(window, draw_pos.floor(), get_size().floor(), get_theme().tint_color, border_size); const int caret_width = 2; -- cgit v1.2.3