From 189736c1a96a1ad0e571ad69f01039e96455011a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 22 Feb 2025 13:31:51 +0100 Subject: Add option to take a screenshot (default hotkey: alt+f1) --- src/gui/Button.cpp | 67 +++++--- src/gui/DropdownButton.cpp | 9 + src/gui/GlobalSettingsPage.cpp | 126 ++++++-------- src/gui/GsrPage.cpp | 13 +- src/gui/ScreenshotSettingsPage.cpp | 339 +++++++++++++++++++++++++++++++++++++ src/gui/SettingsPage.cpp | 26 +-- 6 files changed, 472 insertions(+), 108 deletions(-) create mode 100644 src/gui/ScreenshotSettingsPage.cpp (limited to 'src/gui') diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index 54d1854..476e679 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -15,8 +15,8 @@ namespace gsr { // These are relative to the button size static const float padding_top_icon_scale = 0.25f; static const float padding_bottom_icon_scale = 0.25f; - static const float padding_left_icon_scale = 0.25f; - static const float padding_right_icon_scale = 0.25f; + //static const float padding_left_icon_scale = 0.25f; + static const float padding_right_icon_scale = 0.15f; Button::Button(mgl::Font *font, const char *text, mgl::vec2f size, mgl::Color bg_color) : size(size), bg_color(bg_color), bg_hover_color(bg_color), text(text, *font) @@ -53,13 +53,21 @@ namespace gsr { background.set_color(mouse_inside ? bg_hover_color : bg_color); window.draw(background); - text.set_position((draw_pos + item_size * 0.5f - text.get_bounds().size * 0.5f).floor()); - window.draw(text); - if(sprite.get_texture() && sprite.get_texture()->is_valid()) { scale_sprite_to_button_size(); - sprite.set_position((background.get_position() + background.get_size() * 0.5f - sprite.get_size() * 0.5f).floor()); + const int padding_left = padding_left_scale * get_theme().window_height; + if(text.get_string().empty()) // Center + sprite.set_position((background.get_position() + background.get_size() * 0.5f - sprite.get_size() * 0.5f).floor()); + else // Left + sprite.set_position((draw_pos + mgl::vec2f(padding_left, background.get_size().y * 0.5f - sprite.get_size().y * 0.5f)).floor()); window.draw(sprite); + + const int padding_icon_right = padding_right_icon_scale * get_button_height(); + text.set_position((sprite.get_position() + mgl::vec2f(sprite.get_size().x + padding_icon_right, sprite.get_size().y * 0.5f - text.get_bounds().size.y * 0.5f)).floor()); + window.draw(text); + } else { + text.set_position((draw_pos + item_size * 0.5f - text.get_bounds().size * 0.5f).floor()); + window.draw(text); } if(mouse_inside) { @@ -72,18 +80,25 @@ namespace gsr { if(!visible) return {0.0f, 0.0f}; - const int padding_top = padding_top_scale * get_theme().window_height; - const int padding_bottom = padding_bottom_scale * get_theme().window_height; const int padding_left = padding_left_scale * get_theme().window_height; const int padding_right = padding_right_scale * get_theme().window_height; const mgl::vec2f text_bounds = text.get_bounds().size; - mgl::vec2f s = size; - if(s.x < 0.0001f) - s.x = padding_left + text_bounds.x + padding_right; - if(s.y < 0.0001f) - s.y = padding_top + text_bounds.y + padding_bottom; - return s; + mgl::vec2f widget_size = size; + + if(widget_size.y < 0.0001f) + widget_size.y = get_button_height(); + + if(widget_size.x < 0.0001f) { + widget_size.x = padding_left + text_bounds.x + padding_right; + if(sprite.get_texture() && sprite.get_texture()->is_valid()) { + scale_sprite_to_button_size(); + const int padding_icon_right = text_bounds.x > 0.001f ? padding_right_icon_scale * widget_size.y : 0.0f; + widget_size.x += sprite.get_size().x + padding_icon_right; + } + } + + return widget_size; } void Button::set_border_scale(float scale) { @@ -110,13 +125,23 @@ namespace gsr { if(!sprite.get_texture() || !sprite.get_texture()->is_valid()) return; - const mgl::vec2f button_size = get_size(); - const int padding_icon_top = padding_top_icon_scale * button_size.y; - const int padding_icon_bottom = padding_bottom_icon_scale * button_size.y; - const int padding_icon_left = padding_left_icon_scale * button_size.y; - const int padding_icon_right = padding_right_icon_scale * button_size.y; + const float widget_height = get_button_height(); + + const int padding_icon_top = padding_top_icon_scale * widget_height; + const int padding_icon_bottom = padding_bottom_icon_scale * widget_height; + + const float desired_height = widget_height - (padding_icon_top + padding_icon_bottom); + sprite.set_height((int)desired_height); + } + + float Button::get_button_height() { + const int padding_top = padding_top_scale * get_theme().window_height; + const int padding_bottom = padding_bottom_scale * get_theme().window_height; + + float widget_height = size.y; + if(widget_height < 0.0001f) + widget_height = padding_top + text.get_bounds().size.y + padding_bottom; - const mgl::vec2f desired_size = button_size - mgl::vec2f(padding_icon_left + padding_icon_right, padding_icon_top + padding_icon_bottom); - sprite.set_size(scale_keep_aspect_ratio(sprite.get_texture()->get_size().to_vec2f(), desired_size).floor()); + return widget_height; } } \ No newline at end of file diff --git a/src/gui/DropdownButton.cpp b/src/gui/DropdownButton.cpp index 81bc015..bdc4027 100644 --- a/src/gui/DropdownButton.cpp +++ b/src/gui/DropdownButton.cpp @@ -201,6 +201,15 @@ namespace gsr { } } + void DropdownButton::set_item_description(const std::string &id, const std::string &new_description) { + for(auto &item : items) { + if(item.id == id) { + item.description_text.set_string(new_description); + return; + } + } + } + void DropdownButton::set_description(std::string description_text) { description.set_string(std::move(description_text)); } diff --git a/src/gui/GlobalSettingsPage.cpp b/src/gui/GlobalSettingsPage.cpp index d00ad49..a65cf8f 100644 --- a/src/gui/GlobalSettingsPage.cpp +++ b/src/gui/GlobalSettingsPage.cpp @@ -67,46 +67,6 @@ namespace gsr { return 0; } - static std::vector hotkey_modifiers_to_mgl_keys(uint32_t modifiers) { - std::vector result; - if(modifiers & HOTKEY_MOD_LCTRL) - result.push_back(mgl::Keyboard::LControl); - if(modifiers & HOTKEY_MOD_LSHIFT) - result.push_back(mgl::Keyboard::LShift); - if(modifiers & HOTKEY_MOD_LALT) - result.push_back(mgl::Keyboard::LAlt); - if(modifiers & HOTKEY_MOD_LSUPER) - result.push_back(mgl::Keyboard::LSystem); - if(modifiers & HOTKEY_MOD_RCTRL) - result.push_back(mgl::Keyboard::RControl); - if(modifiers & HOTKEY_MOD_RSHIFT) - result.push_back(mgl::Keyboard::RShift); - if(modifiers & HOTKEY_MOD_RALT) - result.push_back(mgl::Keyboard::RAlt); - if(modifiers & HOTKEY_MOD_RSUPER) - result.push_back(mgl::Keyboard::RSystem); - return result; - } - - static std::string config_hotkey_to_string(ConfigHotkey config_hotkey) { - std::string result; - - const std::vector modifier_keys = hotkey_modifiers_to_mgl_keys(config_hotkey.modifiers); - for(const mgl::Keyboard::Key modifier_key : modifier_keys) { - if(!result.empty()) - result += " + "; - result += mgl::Keyboard::key_to_string(modifier_key); - } - - if(config_hotkey.key != 0) { - if(!result.empty()) - result += " + "; - result += mgl::Keyboard::key_to_string((mgl::Keyboard::Key)config_hotkey.key); - } - - return result; - } - GlobalSettingsPage::GlobalSettingsPage(Overlay *overlay, const GsrInfo *gsr_info, Config &config, PageStack *page_stack) : StaticPage(mgl::vec2f(get_theme().window_width, get_theme().window_height).floor()), overlay(overlay), @@ -114,7 +74,7 @@ namespace gsr { gsr_info(gsr_info), page_stack(page_stack) { - auto content_page = std::make_unique(); + auto content_page = std::make_unique("Global", "Settings"); content_page->add_button("Back", "back", get_color_theme().page_bg_color); content_page->on_click = [page_stack](const std::string &id) { if(id == "back") @@ -322,30 +282,41 @@ namespace gsr { return list; } + std::unique_ptr GlobalSettingsPage::create_screenshot_hotkey_options() { + auto list = std::make_unique(List::Orientation::HORIZONTAL, List::Alignment::CENTER); + + list->add_widget(std::make_unique