From 78801f61da872d3df03d21b36626af6cfb7f2c00 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Tue, 24 Sep 2024 19:11:49 +0800 Subject: [PATCH] Add auto translate mode for tooltips --- doc/classes/Control.xml | 4 ++++ scene/gui/control.cpp | 17 +++++++++++++---- scene/gui/control.h | 5 ++++- scene/main/viewport.cpp | 3 ++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/doc/classes/Control.xml b/doc/classes/Control.xml index 927ab9ae0e3..9d36bc657b8 100644 --- a/doc/classes/Control.xml +++ b/doc/classes/Control.xml @@ -1057,6 +1057,10 @@ [b]Note:[/b] To look up [Control]'s own items use various [code]get_theme_*[/code] methods without specifying [code]theme_type[/code]. [b]Note:[/b] Theme items are looked for in the tree order, from branch to root, where each [Control] node is checked for its [member theme] property. The earliest match against any type/class name is returned. The project-level Theme and the default Theme are checked last. + + Defines if tooltip text should automatically change to its translated version depending on the current locale. Uses the same auto translate mode as this control when set to [constant Node.AUTO_TRANSLATE_MODE_INHERIT]. + [b]Note:[/b] When the tooltip is customized using [method _make_custom_tooltip], this auto translate mode is applied automatically to the returned control. + The default tooltip text. The tooltip appears when the user's mouse cursor stays idle over this control for a few moments, provided that the [member mouse_filter] property is not [constant MOUSE_FILTER_IGNORE]. The time required for the tooltip to appear can be changed with the [member ProjectSettings.gui/timers/tooltip_delay_sec] option. See also [method get_tooltip]. The tooltip popup will use either a default implementation, or a custom one that you can provide by overriding [method _make_custom_tooltip]. The default tooltip includes a [PopupPanel] and [Label] whose theme properties can be customized using [Theme] methods with the [code]"TooltipPanel"[/code] and [code]"TooltipLabel"[/code] respectively. For example: diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index cecddebe882..c1d197ea9b2 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -33,12 +33,8 @@ #include "container.h" #include "core/config/project_settings.h" #include "core/math/geometry_2d.h" -#include "core/os/keyboard.h" #include "core/os/os.h" -#include "core/string/print_string.h" #include "core/string/translation_server.h" -#include "scene/gui/label.h" -#include "scene/gui/panel.h" #include "scene/main/canvas_layer.h" #include "scene/main/window.h" #include "scene/theme/theme_db.h" @@ -3161,6 +3157,16 @@ bool Control::is_auto_translating() const { } #endif +void Control::set_tooltip_auto_translate_mode(AutoTranslateMode p_mode) { + ERR_MAIN_THREAD_GUARD; + data.tooltip_auto_translate_mode = p_mode; +} + +Node::AutoTranslateMode Control::get_tooltip_auto_translate_mode() const { + ERR_READ_THREAD_GUARD_V(AUTO_TRANSLATE_MODE_INHERIT); + return data.tooltip_auto_translate_mode; +} + // Extra properties. void Control::set_tooltip_text(const String &p_hint) { @@ -3510,6 +3516,8 @@ void Control::_bind_methods() { ClassDB::bind_method(D_METHOD("set_v_grow_direction", "direction"), &Control::set_v_grow_direction); ClassDB::bind_method(D_METHOD("get_v_grow_direction"), &Control::get_v_grow_direction); + ClassDB::bind_method(D_METHOD("set_tooltip_auto_translate_mode", "mode"), &Control::set_tooltip_auto_translate_mode); + ClassDB::bind_method(D_METHOD("get_tooltip_auto_translate_mode"), &Control::get_tooltip_auto_translate_mode); ClassDB::bind_method(D_METHOD("set_tooltip_text", "hint"), &Control::set_tooltip_text); ClassDB::bind_method(D_METHOD("get_tooltip_text"), &Control::get_tooltip_text); ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2())); @@ -3617,6 +3625,7 @@ void Control::_bind_methods() { ADD_GROUP("Tooltip", "tooltip_"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "tooltip_text", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip_text", "get_tooltip_text"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "tooltip_auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_tooltip_auto_translate_mode", "get_tooltip_auto_translate_mode"); ADD_GROUP("Focus", "focus_"); ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_left", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_LEFT); diff --git a/scene/gui/control.h b/scene/gui/control.h index 2655b14562b..6e1621ce580 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -33,7 +33,6 @@ #include "core/math/transform_2d.h" #include "core/object/gdvirtual.gen.inc" -#include "core/templates/rid.h" #include "scene/main/canvas_item.h" #include "scene/main/timer.h" #include "scene/resources/theme.h" @@ -262,6 +261,7 @@ private: // Extra properties. String tooltip; + AutoTranslateMode tooltip_auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT; } data; @@ -634,6 +634,9 @@ public: bool is_auto_translating() const; #endif + void set_tooltip_auto_translate_mode(AutoTranslateMode p_mode); + AutoTranslateMode get_tooltip_auto_translate_mode() const; + // Extra properties. void set_tooltip_text(const String &text); diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index ba69f8cc45d..169a5dcb014 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -1400,7 +1400,7 @@ String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Cont String tooltip; while (p_control) { - tooltip = p_control->atr(p_control->get_tooltip(pos)); + tooltip = p_control->get_tooltip(pos); // Temporary solution for PopupMenus. PopupMenu *menu = Object::cast_to(this); @@ -1483,6 +1483,7 @@ void Viewport::_gui_show_tooltip() { } base_tooltip->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); + base_tooltip->set_auto_translate_mode(tooltip_owner->get_tooltip_auto_translate_mode()); panel->set_transient(true); panel->set_flag(Window::FLAG_NO_FOCUS, true);