diff options
author | hop311 <hop3114@gmail.com> | 2024-08-23 01:03:02 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-08-23 01:03:02 +0200 |
commit | 95a6b95a5df63fa1bcb1d4680b3fdd7091a9c38e (patch) | |
tree | 7a487c2283eac8159ea0e25f93abdefa6b3ca0d1 /extension/src/openvic-extension/classes/GUITextLabel.hpp | |
parent | 7c85ab11e840c281a2499dcc6dd3219c33e7d37f (diff) |
Rework GUITextLabel to use custom text handling instead of RichTextLabel
Diffstat (limited to 'extension/src/openvic-extension/classes/GUITextLabel.hpp')
-rw-r--r-- | extension/src/openvic-extension/classes/GUITextLabel.hpp | 71 |
1 files changed, 55 insertions, 16 deletions
diff --git a/extension/src/openvic-extension/classes/GUITextLabel.hpp b/extension/src/openvic-extension/classes/GUITextLabel.hpp index b29870e..aadb76f 100644 --- a/extension/src/openvic-extension/classes/GUITextLabel.hpp +++ b/extension/src/openvic-extension/classes/GUITextLabel.hpp @@ -1,12 +1,16 @@ #pragma once -#include <godot_cpp/classes/rich_text_label.hpp> +#include <godot_cpp/classes/control.hpp> +#include <godot_cpp/classes/font_file.hpp> +#include <godot_cpp/classes/style_box_texture.hpp> #include <openvic-simulation/interface/GUI.hpp> +#include "openvic-extension/classes/GFXSpriteTexture.hpp" + namespace OpenVic { - class GUITextLabel : public godot::RichTextLabel { - GDCLASS(GUITextLabel, godot::RichTextLabel) + class GUITextLabel : public godot::Control { + GDCLASS(GUITextLabel, godot::Control) using colour_instructions_t = std::vector<std::pair<int64_t, char>>; @@ -15,11 +19,30 @@ namespace OpenVic { godot::String PROPERTY(text); godot::Dictionary PROPERTY(substitution_dict); godot::HorizontalAlignment PROPERTY(alignment); - real_t font_height; + godot::Vector2 border_size; + + godot::Ref<godot::FontFile> font; + godot::Color default_colour; GFX::Font::colour_codes_t const* colour_codes; - int32_t PROPERTY(max_lines); + godot::Ref<GFXSpriteTexture> currency_texture; + + godot::Ref<godot::StyleBoxTexture> PROPERTY(background); + + struct string_segment_t { + godot::String text; + godot::Color colour; + real_t width; + }; + using currency_segment_t = std::monostate; + using segment_t = std::variant<string_segment_t, currency_segment_t>; + struct line_t { + std::vector<segment_t> segments; + real_t width {}; + }; - bool update_queued; + std::vector<line_t> lines; + + bool line_update_queued; protected: static void _bind_methods(); @@ -33,7 +56,9 @@ namespace OpenVic { void clear(); /* Set the GUI::Text. */ - godot::Error set_gui_text(GUI::Text const* new_gui_text); + godot::Error set_gui_text( + GUI::Text const* new_gui_text, GFX::Font::colour_codes_t const* override_colour_codes = nullptr + ); /* Return the name of the GUI::Text, or an empty String if it's null. */ godot::String get_gui_text_name() const; @@ -43,15 +68,29 @@ namespace OpenVic { void set_substitution_dict(godot::Dictionary const& new_substitution_dict); void clear_substitutions(); - /* Any text going over this number of lines will be trimmed and replaced with an ellipsis. - * Values less than 1 indicate no limit. Default value: 1. */ - void set_max_lines(int32_t new_max_lines); - private: - godot::Error _update_font(); - - void _queue_update(); - void _update_text(); - void _generate_text(godot::String const& display_text, colour_instructions_t const& colour_instructions); + godot::Vector2 get_content_max_size() const; + + godot::Error _update_font(GFX::Font::colour_codes_t const* override_colour_codes); + real_t get_string_width(godot::String const& string) const; + real_t get_segment_width(segment_t const& segment) const; + + void _queue_line_update(); + void _update_lines(); + + godot::String generate_substituted_text(godot::String const& base_text) const; + std::pair<godot::String, colour_instructions_t> generate_display_text_and_colour_instructions( + godot::String const& substituted_text + ) const; + std::vector<line_t> generate_lines_and_segments( + godot::String const& display_text, colour_instructions_t const& colour_instructions + ) const; + void separate_lines( + godot::String const& string, godot::Color const& colour, std::vector<line_t>& lines + ) const; + void separate_currency_segments( + godot::String const& string, godot::Color const& colour, line_t& line + ) const; + std::vector<line_t> wrap_lines(std::vector<line_t>& unwrapped_lines) const; }; } |