Start on global settings, add tint color setting

This commit is contained in:
dec05eba
2024-12-09 16:29:36 +01:00
parent f3565fdd77
commit f386615937
15 changed files with 237 additions and 28 deletions

BIN
images/settings_small.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -40,6 +40,7 @@ namespace gsr {
struct MainConfig {
int32_t config_file_version = 0;
bool software_encoding_warning_shown = false;
std::string tint_color;
};
struct YoutubeStreamConfig {

View File

@@ -8,6 +8,7 @@
#include <string>
namespace gsr {
struct Config;
struct GsrInfo;
struct Theme {
@@ -26,6 +27,7 @@ namespace gsr {
mgl::Texture combobox_arrow_texture;
mgl::Texture settings_texture;
mgl::Texture settings_small_texture;
mgl::Texture folder_texture;
mgl::Texture up_arrow_texture;
mgl::Texture replay_button_texture;
@@ -56,7 +58,7 @@ namespace gsr {
mgl::Color text_color = mgl::Color(255, 255, 255);
};
bool init_color_theme(const GsrInfo &gsr_info);
bool init_color_theme(const Config &config, const GsrInfo &gsr_info);
void deinit_color_theme();
ColorTheme& get_color_theme();
}

View File

@@ -5,6 +5,7 @@
#include <mglpp/graphics/Color.hpp>
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Sprite.hpp>
namespace gsr {
class Button : public Widget {
@@ -20,15 +21,21 @@ namespace gsr {
mgl::vec2f get_size() override;
void set_border_scale(float scale);
void set_bg_hover_color(mgl::Color color);
void set_icon(mgl::Texture *texture);
const std::string& get_text() const;
void set_text(std::string str);
std::function<void()> on_click;
private:
void scale_sprite_to_button_size();
private:
mgl::vec2f size;
mgl::Color bg_color;
mgl::Color bg_hover_color;
mgl::Text text;
mgl::Sprite sprite;
float border_scale = 0.0015f;
};
}

View File

@@ -0,0 +1,34 @@
#pragma once
#include "StaticPage.hpp"
#include "../GsrInfo.hpp"
#include "../Config.hpp"
namespace gsr {
class GsrPage;
class PageStack;
class ScrollablePage;
class Subsection;
class RadioButton;
class GlobalSettingsPage : public StaticPage {
public:
GlobalSettingsPage(const GsrInfo *gsr_info, Config &config, PageStack *page_stack);
GlobalSettingsPage(const GlobalSettingsPage&) = delete;
GlobalSettingsPage& operator=(const GlobalSettingsPage&) = delete;
void load();
void save();
void on_navigate_away_from_page() override;
private:
std::unique_ptr<Subsection> create_appearance_subsection(ScrollablePage *parent_page);
void add_widgets();
private:
Config &config;
const GsrInfo *gsr_info = nullptr;
GsrPage *content_page_ptr = nullptr;
PageStack *page_stack = nullptr;
RadioButton *tint_color_radio_button_ptr = nullptr;
};
}

View File

@@ -181,7 +181,5 @@ namespace gsr {
RadioButton *turn_on_replay_automatically_mode_ptr = nullptr;
PageStack *page_stack = nullptr;
mgl::Text settings_title_text;
};
}

View File

@@ -15,4 +15,5 @@ namespace gsr {
void draw_rectangle_outline(mgl::Window &window, mgl::vec2f pos, mgl::vec2f size, mgl::Color color, float border_size);
double get_frame_delta_seconds();
void set_frame_delta_seconds(double frame_delta);
mgl::vec2f scale_keep_aspect_ratio(mgl::vec2f from, mgl::vec2f to);
}

View File

@@ -27,6 +27,7 @@ src = [
'src/gui/CustomRendererWidget.cpp',
'src/gui/FileChooser.cpp',
'src/gui/SettingsPage.cpp',
'src/gui/GlobalSettingsPage.cpp',
'src/gui/GsrPage.cpp',
'src/gui/Subsection.cpp',
'src/Utils.cpp',

View File

@@ -49,6 +49,7 @@ namespace gsr {
return {
{"main.config_file_version", &config.main_config.config_file_version},
{"main.software_encoding_warning_shown", &config.main_config.software_encoding_warning_shown},
{"main.tint_color", &config.main_config.tint_color},
{"streaming.record_options.record_area_option", &config.streaming_config.record_options.record_area_option},
{"streaming.record_options.record_area_width", &config.streaming_config.record_options.record_area_width},

View File

@@ -7,8 +7,10 @@
#include "../include/gui/DropdownButton.hpp"
#include "../include/gui/CustomRendererWidget.hpp"
#include "../include/gui/SettingsPage.hpp"
#include "../include/gui/GlobalSettingsPage.hpp"
#include "../include/gui/Utils.hpp"
#include "../include/gui/PageStack.hpp"
#include "../include/gui/GsrPage.hpp"
#include "../include/WindowUtils.hpp"
#include "../include/GlobalHotkeys.hpp"
@@ -420,7 +422,7 @@ namespace gsr {
if(new_config)
config = std::move(new_config.value());
init_color_theme(this->gsr_info);
init_color_theme(config, this->gsr_info);
power_supply_online_filepath = get_power_supply_online_filepath();
@@ -863,6 +865,7 @@ namespace gsr {
const int button_width = button_height;
auto main_buttons_list = std::make_unique<List>(List::Orientation::HORIZONTAL);
List * main_buttons_list_ptr = main_buttons_list.get();
main_buttons_list->set_spacing(0.0f);
{
auto button = std::make_unique<DropdownButton>(&get_theme().title_font, &get_theme().body_font, "Instant Replay", "Off", &get_theme().replay_button_texture,
@@ -928,6 +931,20 @@ namespace gsr {
main_buttons_list->set_position((mgl::vec2f(window_size.x * 0.5f, window_size.y * 0.25f) - main_buttons_list_size * 0.5f).floor());
front_page_ptr->add_widget(std::move(main_buttons_list));
{
const mgl::vec2f main_buttons_size = main_buttons_list_ptr->get_size();
const int settings_button_size = main_buttons_size.y * 0.2f;
auto button = std::make_unique<Button>(&get_theme().title_font, "", mgl::vec2f(settings_button_size, settings_button_size), mgl::Color(0, 0, 0, 180));
button->set_position((main_buttons_list_ptr->get_position() + main_buttons_size - mgl::vec2f(0.0f, settings_button_size) + mgl::vec2f(settings_button_size * 0.333f, 0.0f)).floor());
button->set_bg_hover_color(mgl::Color(0, 0, 0, 255));
button->set_icon(&get_theme().settings_small_texture);
button->on_click = [&]() {
auto settings_page = std::make_unique<GlobalSettingsPage>(&gsr_info, config, &page_stack);
page_stack.push(std::move(settings_page));
};
front_page_ptr->add_widget(std::move(button));
}
close_button_widget.draw_handler = [&](mgl::Window &window, mgl::vec2f pos, mgl::vec2f size) {
const int border_size = std::max(1.0f, 0.0015f * get_theme().window_height);
const float padding_size = std::max(1.0f, 0.003f * get_theme().window_height);

View File

@@ -1,4 +1,5 @@
#include "../include/Theme.hpp"
#include "../include/Config.hpp"
#include "../include/GsrInfo.hpp"
#include <assert.h>
@@ -7,6 +8,27 @@ namespace gsr {
static Theme *theme = nullptr;
static ColorTheme *color_theme = nullptr;
static mgl::Color gpu_vendor_to_color(GpuVendor vendor) {
switch(vendor) {
case GpuVendor::UNKNOWN: return mgl::Color(221, 0, 49);
case GpuVendor::AMD: return mgl::Color(221, 0, 49);
case GpuVendor::INTEL: return mgl::Color(8, 109, 183);
case GpuVendor::NVIDIA: return mgl::Color(118, 185, 0);
}
return mgl::Color(221, 0, 49);
}
static mgl::Color color_name_to_color(const std::string &color_name) {
GpuVendor vendor = GpuVendor::UNKNOWN;
if(color_name == "amd")
vendor = GpuVendor::AMD;
else if(color_name == "intel")
vendor = GpuVendor::INTEL;
else if(color_name == "nvidia")
vendor = GpuVendor::NVIDIA;
return gpu_vendor_to_color(vendor);
}
bool Theme::set_window_size(mgl::vec2i window_size) {
if(std::abs(window_size.x - window_width) < 0.1f && std::abs(window_size.y - window_height) < 0.1f)
return true;
@@ -44,6 +66,9 @@ namespace gsr {
if(!theme->settings_texture.load_from_file((resources_path + "images/settings.png").c_str()))
goto error;
if(!theme->settings_small_texture.load_from_file((resources_path + "images/settings_small.png").c_str()))
goto error;
if(!theme->folder_texture.load_from_file((resources_path + "images/folder.png").c_str()))
goto error;
@@ -102,29 +127,16 @@ namespace gsr {
return *theme;
}
bool init_color_theme(const GsrInfo &gsr_info) {
bool init_color_theme(const Config &config, const GsrInfo &gsr_info) {
if(color_theme)
return true;
color_theme = new ColorTheme();
switch(gsr_info.gpu_info.vendor) {
case GpuVendor::UNKNOWN: {
break;
}
case GpuVendor::AMD: {
color_theme->tint_color = mgl::Color(221, 0, 49);
break;
}
case GpuVendor::INTEL: {
color_theme->tint_color = mgl::Color(8, 109, 183);
break;
}
case GpuVendor::NVIDIA: {
color_theme->tint_color = mgl::Color(118, 185, 0);
break;
}
}
if(config.main_config.tint_color.empty())
color_theme->tint_color = gpu_vendor_to_color(gsr_info.gpu_info.vendor);
else
color_theme->tint_color = color_name_to_color(config.main_config.tint_color);
return true;
}

View File

@@ -12,7 +12,15 @@ namespace gsr {
static const float padding_left_scale = 0.007f;
static const float padding_right_scale = 0.007f;
Button::Button(mgl::Font *font, const char *text, mgl::vec2f size, mgl::Color bg_color) : size(size), bg_color(bg_color), text(text, *font) {
// 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;
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)
{
}
@@ -37,17 +45,23 @@ namespace gsr {
return;
const mgl::vec2f draw_pos = position + offset;
const mgl::vec2f item_size = get_size().floor();
const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f()) && !has_parent_with_selected_child_widget();
mgl::Rectangle background(item_size);
background.set_position(draw_pos.floor());
background.set_color(bg_color);
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);
const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f()) && !has_parent_with_selected_child_widget();
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());
window.draw(sprite);
}
if(mouse_inside) {
const mgl::Color outline_color = (bg_color == get_color_theme().tint_color) ? mgl::Color(255, 255, 255) : get_color_theme().tint_color;
draw_rectangle_outline(window, draw_pos, item_size, outline_color, std::max(1.0f, border_scale * get_theme().window_height));
@@ -76,6 +90,14 @@ namespace gsr {
border_scale = scale;
}
void Button::set_bg_hover_color(mgl::Color color) {
bg_hover_color = color;
}
void Button::set_icon(mgl::Texture *texture) {
sprite.set_texture(texture);
}
const std::string& Button::get_text() const {
return text.get_string();
}
@@ -83,4 +105,18 @@ namespace gsr {
void Button::set_text(std::string str) {
text.set_string(std::move(str));
}
void Button::scale_sprite_to_button_size() {
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 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());
}
}

View File

@@ -0,0 +1,83 @@
#include "../../include/gui/GlobalSettingsPage.hpp"
#include "../../include/Theme.hpp"
#include "../../include/gui/GsrPage.hpp"
#include "../../include/gui/PageStack.hpp"
#include "../../include/gui/ScrollablePage.hpp"
#include "../../include/gui/Subsection.hpp"
#include "../../include/gui/List.hpp"
#include "../../include/gui/Label.hpp"
#include "../../include/gui/RadioButton.hpp"
namespace gsr {
static const char* gpu_vendor_to_color_name(GpuVendor vendor) {
switch(vendor) {
case GpuVendor::UNKNOWN: return "amd";
case GpuVendor::AMD: return "amd";
case GpuVendor::INTEL: return "intel";
case GpuVendor::NVIDIA: return "nvidia";
}
return "amd";
}
GlobalSettingsPage::GlobalSettingsPage(const GsrInfo *gsr_info, Config &config, PageStack *page_stack) :
StaticPage(mgl::vec2f(get_theme().window_width, get_theme().window_height).floor()),
config(config),
gsr_info(gsr_info),
page_stack(page_stack)
{
auto content_page = std::make_unique<GsrPage>();
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")
page_stack->pop();
};
content_page_ptr = content_page.get();
add_widget(std::move(content_page));
add_widgets();
load();
}
std::unique_ptr<Subsection> GlobalSettingsPage::create_appearance_subsection(ScrollablePage *parent_page) {
auto list = std::make_unique<List>(List::Orientation::VERTICAL);
list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Tint color", get_color_theme().text_color));
auto tint_color_radio_button = std::make_unique<RadioButton>(&get_theme().body_font, RadioButton::Orientation::HORIZONTAL);
tint_color_radio_button_ptr = tint_color_radio_button.get();
tint_color_radio_button->add_item("Red", "amd");
tint_color_radio_button->add_item("Green", "nvidia");
tint_color_radio_button->add_item("blue", "intel");
tint_color_radio_button->on_selection_changed = [](const std::string&, const std::string &id) {
if(id == "amd")
get_color_theme().tint_color = mgl::Color(221, 0, 49);
else if(id == "nvidia")
get_color_theme().tint_color = mgl::Color(118, 185, 0);
else if(id == "intel")
get_color_theme().tint_color = mgl::Color(8, 109, 183);
};
list->add_widget(std::move(tint_color_radio_button));
return std::make_unique<Subsection>("Appearance", std::move(list), mgl::vec2f(parent_page->get_inner_size().x, 0.0f));
}
void GlobalSettingsPage::add_widgets() {
auto scrollable_page = std::make_unique<ScrollablePage>(content_page_ptr->get_inner_size());
scrollable_page->add_widget(create_appearance_subsection(scrollable_page.get()));
content_page_ptr->add_widget(std::move(scrollable_page));
}
void GlobalSettingsPage::on_navigate_away_from_page() {
save();
}
void GlobalSettingsPage::load() {
if(config.main_config.tint_color.empty())
tint_color_radio_button_ptr->set_selected_item(gpu_vendor_to_color_name(gsr_info->gpu_info.vendor));
else
tint_color_radio_button_ptr->set_selected_item(config.main_config.tint_color);
}
void GlobalSettingsPage::save() {
config.main_config.tint_color = tint_color_radio_button_ptr->get_selected_id();
save_config(config);
}
}

View File

@@ -27,8 +27,7 @@ namespace gsr {
type(type),
config(config),
gsr_info(gsr_info),
page_stack(page_stack),
settings_title_text("Settings", get_theme().title_font)
page_stack(page_stack)
{
audio_devices = get_audio_devices();
application_audio = get_application_audio();

View File

@@ -50,4 +50,21 @@ namespace gsr {
void set_frame_delta_seconds(double frame_delta) {
frame_delta_seconds = frame_delta;
}
mgl::vec2f scale_keep_aspect_ratio(mgl::vec2f from, mgl::vec2f to) {
if(std::abs(from.x) <= 0.0001f || std::abs(from.y) <= 0.0001f)
return {0.0f, 0.0f};
const double height_to_width_ratio = (double)from.y / (double)from.x;
from.x = to.x;
from.y = from.x * height_to_width_ratio;
if(from.y > to.y) {
const double width_height_ratio = (double)from.x / (double)from.y;
from.y = to.y;
from.x = from.y * width_height_ratio;
}
return from;
}
}