Fix tooltips behaving incorrectly on Tree nodes

This commit is contained in:
Michael Alexsander 2023-09-24 02:18:17 -03:00
parent 4c3dc26367
commit 9ee82ebe1d
No known key found for this signature in database
GPG Key ID: A9C91EE110F4EABA
2 changed files with 14 additions and 13 deletions

View File

@ -1414,6 +1414,8 @@ void Viewport::_gui_sort_roots() {
void Viewport::_gui_cancel_tooltip() { void Viewport::_gui_cancel_tooltip() {
gui.tooltip_control = nullptr; gui.tooltip_control = nullptr;
gui.tooltip_text = "";
if (gui.tooltip_timer.is_valid()) { if (gui.tooltip_timer.is_valid()) {
gui.tooltip_timer->release_connections(); gui.tooltip_timer->release_connections();
gui.tooltip_timer = Ref<SceneTreeTimer>(); gui.tooltip_timer = Ref<SceneTreeTimer>();
@ -1470,18 +1472,20 @@ void Viewport::_gui_show_tooltip() {
// Get the Control under cursor and the relevant tooltip text, if any. // Get the Control under cursor and the relevant tooltip text, if any.
Control *tooltip_owner = nullptr; Control *tooltip_owner = nullptr;
String tooltip_text = _gui_get_tooltip( gui.tooltip_text = _gui_get_tooltip(
gui.tooltip_control, gui.tooltip_control,
gui.tooltip_control->get_global_transform().xform_inv(gui.last_mouse_pos), gui.tooltip_control->get_global_transform().xform_inv(gui.last_mouse_pos),
&tooltip_owner); &tooltip_owner);
tooltip_text = tooltip_text.strip_edges(); gui.tooltip_text = gui.tooltip_text.strip_edges();
if (tooltip_text.is_empty()) {
if (gui.tooltip_text.is_empty()) {
return; // Nothing to show. return; // Nothing to show.
} }
// Remove previous popup if we change something. // Remove previous popup if we change something.
if (gui.tooltip_popup) { if (gui.tooltip_popup) {
memdelete(gui.tooltip_popup); memdelete(gui.tooltip_popup);
gui.tooltip_popup = nullptr;
} }
if (!tooltip_owner) { if (!tooltip_owner) {
@ -1497,14 +1501,14 @@ void Viewport::_gui_show_tooltip() {
// Controls can implement `make_custom_tooltip` to provide their own tooltip. // Controls can implement `make_custom_tooltip` to provide their own tooltip.
// This should be a Control node which will be added as child to a TooltipPanel. // This should be a Control node which will be added as child to a TooltipPanel.
Control *base_tooltip = tooltip_owner->make_custom_tooltip(tooltip_text); Control *base_tooltip = tooltip_owner->make_custom_tooltip(gui.tooltip_text);
// If no custom tooltip is given, use a default implementation. // If no custom tooltip is given, use a default implementation.
if (!base_tooltip) { if (!base_tooltip) {
gui.tooltip_label = memnew(Label); gui.tooltip_label = memnew(Label);
gui.tooltip_label->set_theme_type_variation(SNAME("TooltipLabel")); gui.tooltip_label->set_theme_type_variation(SNAME("TooltipLabel"));
gui.tooltip_label->set_auto_translate(gui.tooltip_control->is_auto_translating()); gui.tooltip_label->set_auto_translate(gui.tooltip_control->is_auto_translating());
gui.tooltip_label->set_text(tooltip_text); gui.tooltip_label->set_text(gui.tooltip_text);
base_tooltip = gui.tooltip_label; base_tooltip = gui.tooltip_label;
panel->connect("mouse_entered", callable_mp(this, &Viewport::_gui_cancel_tooltip)); panel->connect("mouse_entered", callable_mp(this, &Viewport::_gui_cancel_tooltip));
} }
@ -1935,23 +1939,19 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
mm->set_velocity(velocity); mm->set_velocity(velocity);
mm->set_relative(rel); mm->set_relative(rel);
// Nothing pressed.
if (mm->get_button_mask().is_empty()) { if (mm->get_button_mask().is_empty()) {
// Nothing pressed.
bool is_tooltip_shown = false; bool is_tooltip_shown = false;
if (gui.tooltip_popup) { if (gui.tooltip_popup) {
if (gui.tooltip_control) { if (gui.tooltip_control) {
String tooltip = _gui_get_tooltip(over, gui.tooltip_control->get_global_transform().xform_inv(mpos)); String tooltip = _gui_get_tooltip(over, gui.tooltip_control->get_global_transform().xform_inv(mpos));
tooltip = tooltip.strip_edges(); tooltip = tooltip.strip_edges();
if (tooltip.length() == 0) {
if (tooltip.is_empty() || tooltip != gui.tooltip_text) {
_gui_cancel_tooltip(); _gui_cancel_tooltip();
} else if (gui.tooltip_label) {
if (tooltip == gui.tooltip_label->get_text()) {
is_tooltip_shown = true;
}
} else { } else {
is_tooltip_shown = true; // Nothing to compare against, likely using custom control, so if it changes there is nothing we can do. is_tooltip_shown = true;
} }
} else { } else {
_gui_cancel_tooltip(); _gui_cancel_tooltip();

View File

@ -368,6 +368,7 @@ private:
Control *tooltip_control = nullptr; Control *tooltip_control = nullptr;
Window *tooltip_popup = nullptr; Window *tooltip_popup = nullptr;
Label *tooltip_label = nullptr; Label *tooltip_label = nullptr;
String tooltip_text;
Point2 tooltip_pos; Point2 tooltip_pos;
Point2 last_mouse_pos; Point2 last_mouse_pos;
Point2 drag_accum; Point2 drag_accum;