Change text renderer to pango (supports all languages and loads font automatically), start on game detection

This commit is contained in:
dec05eba
2026-04-18 00:37:16 +02:00
parent 72c8c79896
commit 010787854e
39 changed files with 636 additions and 886 deletions

View File

@@ -12,7 +12,7 @@ namespace gsr {
public:
// If width is 0 then the width of the text is used instead (with padding).
// If height is 0 then the height of the text is used instead (with padding).
Button(mgl::Font *font, const char *text, mgl::vec2f size, mgl::Color bg_color);
Button(const char *font_desc, const char *text, mgl::vec2f size, mgl::Color bg_color);
Button(const Button&) = delete;
Button& operator=(const Button&) = delete;
@@ -25,8 +25,8 @@ namespace gsr {
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::string_view get_text() const;
void set_text(std::string_view str);
std::function<void()> on_click;
private:

View File

@@ -11,7 +11,7 @@
namespace gsr {
class CheckBox : public Widget {
public:
CheckBox(mgl::Font *font, const char *text);
CheckBox(const char *font_desc, const char *text);
CheckBox(const CheckBox&) = delete;
CheckBox& operator=(const CheckBox&) = delete;

View File

@@ -11,24 +11,24 @@
namespace gsr {
class ComboBox : public Widget {
public:
ComboBox(mgl::Font *font);
ComboBox(const char *font_desc);
ComboBox(const ComboBox&) = delete;
ComboBox& operator=(const ComboBox&) = delete;
bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override;
void draw(mgl::Window &window, mgl::vec2f offset) override;
void add_item(const std::string &text, const std::string &id, bool allow_duplicate = true);
void add_item(std::string_view text, const std::string &id, bool allow_duplicate = true);
void clear_items();
// The item can only be selected if it's enabled
void set_selected_item(const std::string &id, bool trigger_event = true, bool trigger_event_even_if_selection_not_changed = true);
void set_item_enabled(const std::string &id, bool enabled);
const std::string& get_selected_id() const;
void set_selected_item(std::string_view id, bool trigger_event = true, bool trigger_event_even_if_selection_not_changed = true);
void set_item_enabled(std::string_view id, bool enabled);
std::string_view get_selected_id() const;
mgl::vec2f get_size() override;
std::function<void(const std::string &text, const std::string &id)> on_selection_changed;
std::function<void(std::string_view text, std::string_view id)> on_selection_changed;
private:
void draw_selected(mgl::Window &window, mgl::vec2f draw_pos);
void draw_unselected(mgl::Window &window, mgl::vec2f draw_pos);
@@ -44,7 +44,8 @@ namespace gsr {
};
mgl::vec2f max_size;
mgl::Font *font;
std::string font_desc;
int font_size = 0;
std::vector<Item> items;
mgl::Sprite dropdown_arrow;
bool dirty = true;

View File

@@ -10,7 +10,7 @@
namespace gsr {
class DropdownButton : public Widget {
public:
DropdownButton(mgl::Font *title_font, mgl::Font *description_font, const char *title, const char *description, mgl::Texture *icon_texture, mgl::vec2f size);
DropdownButton(const char *title_font_desc, const char *description_font_desc, const char *title, const char *description, mgl::Texture *icon_texture, mgl::vec2f size);
DropdownButton(const DropdownButton&) = delete;
DropdownButton& operator=(const DropdownButton&) = delete;
@@ -18,10 +18,10 @@ namespace gsr {
void draw(mgl::Window &window, mgl::vec2f offset) override;
void add_item(const std::string &text, const std::string &id, const std::string &description = "");
void set_item_label(const std::string &id, const std::string &new_label);
void set_item_icon(const std::string &id, mgl::Texture *texture);
void set_item_description(const std::string &id, const std::string &new_description);
void set_item_enabled(const std::string &id, bool enabled);
void set_item_label(std::string_view id, const std::string &new_label);
void set_item_icon(std::string_view id, mgl::Texture *texture);
void set_item_description(std::string_view id, const std::string &new_description);
void set_item_enabled(std::string_view id, bool enabled);
void set_description(std::string description_text);
void set_activated(bool activated);
@@ -41,8 +41,8 @@ namespace gsr {
};
std::vector<Item> items;
mgl::Font *title_font;
mgl::Font *description_font;
std::string title_font_desc;
std::string description_font_desc;
mgl::vec2f size;
bool mouse_inside = false;
bool show_dropdown = false;

View File

@@ -4,32 +4,16 @@
#include <functional>
#include <mglpp/graphics/Color.hpp>
#include <mglpp/graphics/Text32.hpp>
#include <mglpp/graphics/TextEdit.hpp>
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Rectangle.hpp>
namespace gsr {
class Entry;
enum class EntryValidateHandlerResult {
DENY,
ALLOW,
REPLACED
};
using EntryValidateHandler = std::function<EntryValidateHandlerResult(Entry &entry, const std::u32string &str)>;
struct CaretIndexPos {
int index;
mgl::vec2f pos;
};
class Entry : public Widget {
public:
enum class Direction {
LEFT,
RIGHT
};
Entry(mgl::Font *font, const char *text, float max_width);
Entry(const char *font_desc, const char *text, float max_width);
Entry(const Entry&) = delete;
Entry& operator=(const Entry&) = delete;
@@ -38,44 +22,18 @@ namespace gsr {
mgl::vec2f get_size() override;
EntryValidateHandlerResult set_text(const std::string &str);
std::string get_text() const;
void set_text(std::string_view str);
std::string_view get_text() const;
void set_masked(bool masked);
bool is_masked() const;
// Return false to specify that the string should not be accepted. This reverts the string back to its previous value.
// The input can be changed by changing the input parameter and returning true.
EntryValidateHandler validate_handler;
void set_number_mode(bool enabled, int min_val, int max_val);
std::function<void(const std::string &text)> on_changed;
std::function<void(std::string_view text)> on_changed;
private:
// Also updates the cursor position
void replace_text(size_t index, size_t size, const std::u32string &replacement);
void move_caret_word(Direction direction, size_t max_codepoints);
EntryValidateHandlerResult set_text_internal(std::u32string str);
void draw_caret(mgl::Window &window, mgl::vec2f draw_pos, mgl::vec2f caret_size);
void draw_caret_selection(mgl::Window &window, mgl::vec2f draw_pos, mgl::vec2f caret_size);
CaretIndexPos find_closest_caret_index_by_position(mgl::vec2f position);
private:
struct Caret {
float offset_x = 0.0f;
int index = 0;
};
mgl::Rectangle background;
mgl::Text32 text;
mgl::Text32 masked_text;
float max_width;
bool selected = false;
bool selecting_text = false;
bool selecting_with_keyboard = false;
bool show_selection = false;
bool masked = false;
Caret caret;
Caret selection_start_caret;
float text_overflow = 0.0f;
mgl::TextEdit text_edit;
float max_width = 0.0f;
};
EntryValidateHandler create_entry_validator_integer_in_range(int min, int max);
}
}

View File

@@ -29,7 +29,7 @@ namespace gsr {
mgl::vec2f get_inner_size() override;
void set_size(mgl::vec2f size);
private:
void set_current_directory(const char *directory);
void set_current_directory(std::string_view directory);
private:
struct Folder {
mgl::Text text;
@@ -48,7 +48,7 @@ namespace gsr {
class FileChooser : public Widget {
public:
FileChooser(const char *start_directory, mgl::vec2f size);
FileChooser(std::string_view start_directory, mgl::vec2f size);
FileChooser(const FileChooser&) = delete;
FileChooser& operator=(const FileChooser&) = delete;
@@ -58,10 +58,10 @@ namespace gsr {
mgl::vec2f get_size() override;
void set_current_directory(const char *directory);
void open_subdirectory(const char *name);
void set_current_directory(std::string_view directory);
void open_subdirectory(std::string_view name);
void open_parent_directory();
const std::string& get_current_directory() const;
std::string_view get_current_directory() const;
private:
struct Folder {
mgl::Text text;

View File

@@ -49,9 +49,9 @@ namespace gsr {
bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override;
std::function<void(bool enable, int exit_status)> on_startup_changed;
std::function<void(const char *reason)> on_click_exit_program_button;
std::function<void(const char *hotkey_option)> on_keyboard_hotkey_changed;
std::function<void(const char *hotkey_option)> on_joystick_hotkey_changed;
std::function<void(std::string_view reason)> on_click_exit_program_button;
std::function<void(std::string_view hotkey_option)> on_keyboard_hotkey_changed;
std::function<void(std::string_view hotkey_option)> on_joystick_hotkey_changed;
std::function<void()> on_page_closed;
private:
void load_hotkeys();

View File

@@ -8,8 +8,7 @@
namespace gsr {
class Label : public Widget {
public:
// TODO: Allow specifying max width, at which either a line-break should occur or elipses should show
Label(mgl::Font *font, const char *text, mgl::Color color);
Label(const char *font_desc, const char *text, mgl::Color color);
Label(const Label&) = delete;
Label& operator=(const Label&) = delete;
@@ -18,8 +17,14 @@ namespace gsr {
mgl::vec2f get_size() override;
void set_text(std::string str);
const std::string& get_text() const;
void set_text(std::string_view str);
std::string_view get_text() const;
// Set to 0 to disable
void set_wrap_width(int width);
// Set to 0 to disable
void set_max_rows(int max_rows);
private:
mgl::Text text;
};

View File

@@ -14,7 +14,7 @@ namespace gsr {
HORIZONTAL
};
RadioButton(mgl::Font *font, Orientation orientation);
RadioButton(const char *font_desc, Orientation orientation);
RadioButton(const RadioButton&) = delete;
RadioButton& operator=(const RadioButton&) = delete;
@@ -22,14 +22,14 @@ namespace gsr {
void draw(mgl::Window &window, mgl::vec2f offset) override;
void add_item(const std::string &text, const std::string &id);
void set_selected_item(const std::string &id, bool trigger_event = true, bool trigger_event_even_if_selection_not_changed = true);
const std::string& get_selected_id() const;
const std::string& get_selected_text() const;
void set_selected_item(std::string_view id, bool trigger_event = true, bool trigger_event_even_if_selection_not_changed = true);
std::string_view get_selected_id() const;
std::string_view get_selected_text() const;
mgl::vec2f get_size() override;
// Return false to revert the change
std::function<bool(const std::string &text, const std::string &id)> on_selection_changed;
std::function<bool(std::string_view text, std::string_view id)> on_selection_changed;
private:
void update_if_dirty();
private:
@@ -38,7 +38,7 @@ namespace gsr {
std::string id;
};
mgl::Font *font;
std::string font_desc;
Orientation orientation;
std::vector<Item> items;
size_t selected_item = 0;

View File

@@ -125,7 +125,7 @@ namespace gsr {
std::unique_ptr<CheckBox> create_save_replay_in_game_folder();
std::unique_ptr<CheckBox> create_restart_replay_on_save();
std::unique_ptr<Label> create_estimated_replay_file_size();
void update_estimated_replay_file_size(const std::string &replay_storage_type);
void update_estimated_replay_file_size(std::string_view replay_storage_type);
void update_replay_time_text();
std::unique_ptr<CheckBox> create_save_recording_in_game_folder();
std::unique_ptr<Label> create_estimated_record_file_size();
@@ -140,6 +140,9 @@ namespace gsr {
std::unique_ptr<ComboBox> create_streaming_service_box();
std::unique_ptr<List> create_streaming_service_section();
std::unique_ptr<List> create_stream_key_section();
std::unique_ptr<List> create_stream_kick_url();
std::unique_ptr<List> create_stream_kick_key();
std::unique_ptr<List> create_stream_kick_section();
std::unique_ptr<List> create_stream_custom_url();
std::unique_ptr<List> create_stream_custom_key();
std::unique_ptr<List> create_stream_custom_section();
@@ -200,6 +203,7 @@ namespace gsr {
ComboBox *streaming_service_box_ptr = nullptr;
List *stream_key_list_ptr = nullptr;
List *custom_stream_list_ptr = nullptr;
List *kick_stream_list_ptr = nullptr;
CheckBox *save_replay_in_game_folder_ptr = nullptr;
CheckBox *restart_replay_on_save = nullptr;
Label *estimated_file_size_ptr = nullptr;

View File

@@ -6,7 +6,7 @@
namespace gsr {
class Tooltip : public Widget {
public:
Tooltip(mgl::Font *font);
Tooltip(const char *font_desc);
Tooltip(const Tooltip&) = delete;
Tooltip& operator=(const Tooltip&) = delete;
@@ -15,7 +15,7 @@ namespace gsr {
mgl::vec2f get_size() override;
void set_text(std::string text);
void set_text(std::string_view text);
private:
mgl::Text label;
};