From 6a7183119f1eed8871fe73a45880033cfb02f3d9 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Tue, 24 Sep 2024 14:34:20 +0800 Subject: [PATCH] Make _make_custom_tooltip receive raw tooltip for buttons with shortcut enabled --- doc/classes/BaseButton.xml | 1 + scene/gui/base_button.cpp | 34 +++++++++++++++++++++++++--------- scene/gui/base_button.h | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/doc/classes/BaseButton.xml b/doc/classes/BaseButton.xml index 18dccfa5a98..13b23d70b76 100644 --- a/doc/classes/BaseButton.xml +++ b/doc/classes/BaseButton.xml @@ -75,6 +75,7 @@ If [code]true[/code], the button will add information about its shortcut in the tooltip. + [b]Note:[/b] This property does nothing when the tooltip control is customized using [method Control._make_custom_tooltip]. If [code]true[/code], the button is in toggle mode. Makes the button flip state between pressed and unpressed each time its area is clicked. diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index 72299f788d6..34f50954932 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/os/keyboard.h" +#include "scene/gui/label.h" #include "scene/main/window.h" void BaseButton::_unpress_group() { @@ -390,16 +391,31 @@ void BaseButton::shortcut_input(const Ref &p_event) { } } -String BaseButton::get_tooltip(const Point2 &p_pos) const { - String tooltip = Control::get_tooltip(p_pos); - if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) { - String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")"; - if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) { - text += "\n" + atr(tooltip); - } - tooltip = text; +Control *BaseButton::make_custom_tooltip(const String &p_text) const { + Control *control = Control::make_custom_tooltip(p_text); + if (control) { + return control; } - return tooltip; + if (!shortcut_in_tooltip || shortcut.is_null() || !shortcut->has_valid_event()) { + return nullptr; // Use the default tooltip label. + } + + String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")"; + if (!p_text.is_empty() && shortcut->get_name().nocasecmp_to(p_text) != 0) { + text += "\n" + atr(p_text); + } + + // Make a label similar to the default tooltip label. + // Auto translation is disabled because we already did that manually above. + // + // We can't customize the tooltip text by overriding `get_tooltip()` + // because otherwise user-defined `_make_custom_tooltip()` would receive + // the translated and annotated text. + Label *label = memnew(Label(text)); + label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); + label->set_theme_type_variation(SNAME("TooltipLabel")); + + return label; } void BaseButton::set_button_group(const Ref &p_group) { diff --git a/scene/gui/base_button.h b/scene/gui/base_button.h index a8d5cee44ca..8405acb21db 100644 --- a/scene/gui/base_button.h +++ b/scene/gui/base_button.h @@ -134,7 +134,7 @@ public: void set_shortcut(const Ref &p_shortcut); Ref get_shortcut() const; - virtual String get_tooltip(const Point2 &p_pos) const override; + virtual Control *make_custom_tooltip(const String &p_text) const override; void set_button_group(const Ref &p_group); Ref get_button_group() const;