From c26247464feabe0fbb9d6a8527b242d667faa066 Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Sep 2024 23:26:41 +0100 Subject: Allow tooltips to be as tall as the window + shift them to stay inside it --- .../src/openvic-extension/classes/GUILabel.cpp | 16 +++++++++++ .../src/openvic-extension/classes/GUILabel.hpp | 3 ++ game/src/Game/GameSession/Tooltip.gd | 33 +++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/extension/src/openvic-extension/classes/GUILabel.cpp b/extension/src/openvic-extension/classes/GUILabel.cpp index 4b6b1c1..7e93f01 100644 --- a/extension/src/openvic-extension/classes/GUILabel.cpp +++ b/extension/src/openvic-extension/classes/GUILabel.cpp @@ -47,6 +47,8 @@ void GUILabel::_bind_methods() { OV_BIND_METHOD(GUILabel::clear); OV_BIND_METHOD(GUILabel::get_gui_text_name); + OV_BIND_METHOD(GUILabel::force_update_lines); + OV_BIND_METHOD(GUILabel::get_text); OV_BIND_METHOD(GUILabel::set_text, { "new_text" }); @@ -57,6 +59,7 @@ void GUILabel::_bind_methods() { OV_BIND_METHOD(GUILabel::get_horizontal_alignment); OV_BIND_METHOD(GUILabel::set_horizontal_alignment, { "new_horizontal_alignment" }); + OV_BIND_METHOD(GUILabel::get_base_max_size); OV_BIND_METHOD(GUILabel::get_max_size); OV_BIND_METHOD(GUILabel::set_max_size, { "new_max_size" }); OV_BIND_METHOD(GUILabel::get_border_size); @@ -293,6 +296,11 @@ Error GUILabel::set_gui_text(GUI::Text const* new_gui_text, GFX::Font::colour_co return err; } +void GUILabel::force_update_lines() { + line_update_queued = true; + _update_lines(); +} + void GUILabel::set_text(String const& new_text) { if (text != new_text) { text = new_text; @@ -331,6 +339,10 @@ void GUILabel::set_horizontal_alignment(HorizontalAlignment new_horizontal_align } } +Size2 GUILabel::get_base_max_size() const { + return gui_text != nullptr ? Utilities::to_godot_fvec2(gui_text->get_max_size()) : Size2 {}; +} + void GUILabel::set_max_size(Size2 new_max_size) { if (max_size != new_max_size) { max_size = new_max_size; @@ -462,6 +474,10 @@ void GUILabel::_queue_line_update() { } void GUILabel::_update_lines() { + if (!line_update_queued) { + return; + } + line_update_queued = false; lines.clear(); diff --git a/extension/src/openvic-extension/classes/GUILabel.hpp b/extension/src/openvic-extension/classes/GUILabel.hpp index 67981d7..f04e3ab 100644 --- a/extension/src/openvic-extension/classes/GUILabel.hpp +++ b/extension/src/openvic-extension/classes/GUILabel.hpp @@ -74,6 +74,8 @@ namespace OpenVic { GUI::Text const* new_gui_text, GFX::Font::colour_codes_t const* override_colour_codes = nullptr ); + void force_update_lines(); + void set_text(godot::String const& new_text); void add_substitution(godot::String const& key, godot::String const& value); @@ -81,6 +83,7 @@ namespace OpenVic { void clear_substitutions(); void set_horizontal_alignment(godot::HorizontalAlignment new_horizontal_alignment); + godot::Size2 get_base_max_size() const; void set_max_size(godot::Size2 new_max_size); void set_border_size(godot::Size2 new_border_size); void set_auto_adjust_to_content_size(bool new_auto_adjust_to_content_size); diff --git a/game/src/Game/GameSession/Tooltip.gd b/game/src/Game/GameSession/Tooltip.gd index c110e2e..f079e4c 100644 --- a/game/src/Game/GameSession/Tooltip.gd +++ b/game/src/Game/GameSession/Tooltip.gd @@ -7,17 +7,48 @@ func _ready() -> void: _tooltip_label = get_gui_label_from_nodepath(^"./ToolTip") if _tooltip_label: + _update_tooltip_max_size() _tooltip_label.set_auto_adjust_to_content_size(true) MenuSingleton.update_tooltip.connect(update_tooltip) hide() +func _notification(what : int) -> void: + match what: + NOTIFICATION_RESIZED: + _update_tooltip_max_size() + +func _update_tooltip_max_size() -> void: + if _tooltip_label: + var max_size : Vector2 = _tooltip_label.get_base_max_size() + var window_size : Vector2 = get_size() + _tooltip_label.set_max_size(Vector2(min(max_size.x, window_size.x), window_size.y)) + func update_tooltip(text : String, substitution_dict : Dictionary, position : Vector2) -> void: - if text: + if text and _tooltip_label: _tooltip_label.set_text(text) _tooltip_label.set_substitution_dict(substitution_dict) + _tooltip_label.force_update_lines() + + var adjusted_rect : Rect2 = _tooltip_label.get_adjusted_rect() + + # Shift position so that the tooltip doesn't go past the bottom or right sides of the window + var bottom_right : Vector2 = position + adjusted_rect.position + adjusted_rect.size - get_size() + if bottom_right.x > 0: + position.x -= bottom_right.x + if bottom_right.y > 0: + position.y -= bottom_right.y + + # Shift position so that the tooltip doesn't go past the top or left sides of the window + var top_left : Vector2 = position + adjusted_rect.position + if top_left.x < 0: + position.x -= top_left.x + if top_left.y < 0: + position.y -= top_left.y + _tooltip_label.set_position(position) + show() else: hide() -- cgit v1.2.3-56-ga3b1