瀏覽代碼

Make _make_custom_tooltip receive raw tooltip for buttons with shortcut enabled

Haoyu Qiu 10 月之前
父節點
當前提交
6a7183119f
共有 3 個文件被更改,包括 27 次插入10 次删除
  1. 1 0
      doc/classes/BaseButton.xml
  2. 25 9
      scene/gui/base_button.cpp
  3. 1 1
      scene/gui/base_button.h

+ 1 - 0
doc/classes/BaseButton.xml

@@ -75,6 +75,7 @@
 		</member>
 		<member name="shortcut_in_tooltip" type="bool" setter="set_shortcut_in_tooltip" getter="is_shortcut_in_tooltip_enabled" default="true">
 			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].
 		</member>
 		<member name="toggle_mode" type="bool" setter="set_toggle_mode" getter="is_toggle_mode" default="false">
 			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.

+ 25 - 9
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<InputEvent> &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;
+	}
+	if (!shortcut_in_tooltip || shortcut.is_null() || !shortcut->has_valid_event()) {
+		return nullptr; // Use the default tooltip label.
 	}
-	return tooltip;
+
+	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<ButtonGroup> &p_group) {

+ 1 - 1
scene/gui/base_button.h

@@ -134,7 +134,7 @@ public:
 	void set_shortcut(const Ref<Shortcut> &p_shortcut);
 	Ref<Shortcut> 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<ButtonGroup> &p_group);
 	Ref<ButtonGroup> get_button_group() const;