diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-08-06 08:22:42 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-08-06 08:22:42 +0200 |
commit | 520c61ec64c001d2c9c9d953051aa58715831731 (patch) | |
tree | 2c0e10995d1a85374f628e78a53321f727013bf3 | |
parent | 9b6033e2b97f6f572cb69a393d4d1d1ea41b5d72 (diff) |
Add checkbox, scale more sizes by window scale
-rw-r--r-- | include/gui/Button.hpp | 2 | ||||
-rw-r--r-- | include/gui/CheckBox.hpp | 28 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/gui/Button.cpp | 12 | ||||
-rw-r--r-- | src/gui/CheckBox.cpp | 71 | ||||
-rw-r--r-- | src/gui/ComboBox.cpp | 6 | ||||
-rw-r--r-- | src/gui/DropdownButton.cpp | 5 | ||||
-rw-r--r-- | src/gui/Entry.cpp | 3 | ||||
-rw-r--r-- | src/main.cpp | 7 |
9 files changed, 125 insertions, 10 deletions
diff --git a/include/gui/Button.hpp b/include/gui/Button.hpp index 4f8b404..f4c44ef 100644 --- a/include/gui/Button.hpp +++ b/include/gui/Button.hpp @@ -19,11 +19,13 @@ namespace gsr { void draw(mgl::Window &window, mgl::vec2f offset) override; mgl::vec2f get_size() override; + void set_border_scale(float scale); std::function<void()> on_click; private: mgl::vec2f size; mgl::Color bg_color; mgl::Text text; + float border_scale = 0.0015f; }; }
\ No newline at end of file diff --git a/include/gui/CheckBox.hpp b/include/gui/CheckBox.hpp new file mode 100644 index 0000000..4459976 --- /dev/null +++ b/include/gui/CheckBox.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "Widget.hpp" +#include <functional> + +#include <mglpp/graphics/Color.hpp> +#include <mglpp/graphics/Text.hpp> + +namespace gsr { + class CheckBox : public Widget { + public: + CheckBox(mgl::Font *font, const char *text); + CheckBox(const CheckBox&) = delete; + CheckBox& operator=(const CheckBox&) = delete; + + bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override; + void draw(mgl::Window &window, mgl::vec2f offset) override; + + mgl::vec2f get_size() override; + + std::function<void()> on_click; + private: + mgl::vec2f get_checkbox_size(); + private: + mgl::Text text; + bool checked = false; + }; +}
\ No newline at end of file diff --git a/meson.build b/meson.build index 60b3bd4..f856e97 100644 --- a/meson.build +++ b/meson.build @@ -11,6 +11,7 @@ src = [ 'src/gui/ScrollablePage.cpp', 'src/gui/Button.cpp', 'src/gui/Entry.cpp', + 'src/gui/CheckBox.cpp', 'src/gui/ComboBox.cpp', 'src/gui/Page.cpp', 'src/gui/StaticPage.cpp', 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 <mglpp/graphics/Rectangle.hpp> +#include <mglpp/window/Window.hpp> +#include <mglpp/window/Event.hpp> +#include <mglpp/system/FloatRect.hpp> + +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; diff --git a/src/main.cpp b/src/main.cpp index 4ec211b..939866a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "../include/gui/DropdownButton.hpp" #include "../include/gui/Button.hpp" #include "../include/gui/Entry.hpp" +#include "../include/gui/CheckBox.hpp" #include "../include/gui/ComboBox.hpp" #include "../include/gui/Label.hpp" #include "../include/gui/List.hpp" @@ -208,6 +209,7 @@ static const mgl_monitor* find_monitor_by_cursor_position(mgl::Window &window) { static void add_widgets_to_settings_page(mgl::vec2i window_size, mgl::vec2f settings_page_position, mgl::vec2f settings_page_size, gsr::Page *settings_page, gsr::Page *settings_content_page, const gsr::GsrInfo &gsr_info, const std::vector<gsr::AudioDevice> &audio_devices, std::function<void()> settings_back_button_callback) { auto back_button = std::make_unique<gsr::Button>(&gsr::get_theme().title_font, "Back", mgl::vec2f(window_size.x / 10, window_size.y / 15), gsr::get_theme().scrollable_page_bg_color); back_button->set_position(settings_page_position + mgl::vec2f(settings_page_size.x + window_size.x / 50, 0.0f).floor()); + back_button->set_border_scale(0.003f); back_button->on_click = settings_back_button_callback; settings_page->add_widget(std::move(back_button)); @@ -386,6 +388,11 @@ static void add_widgets_to_settings_page(mgl::vec2i window_size, mgl::vec2f sett file_list->add_widget(std::move(container_list)); } settings_list->add_widget(std::move(file_list)); + + settings_list->add_widget(std::make_unique<gsr::CheckBox>(&gsr::get_theme().body_font, "Record cursor")); + settings_list->add_widget(std::make_unique<gsr::CheckBox>(&gsr::get_theme().body_font, "Show recording started notification")); + //settings_list->add_widget(std::make_unique<gsr::CheckBox>(&gsr::get_theme().body_font, "Show recording stopped notification")); + settings_list->add_widget(std::make_unique<gsr::CheckBox>(&gsr::get_theme().body_font, "Show video saved notification")); } settings_content_page->add_widget(std::move(settings_list)); } |