From 5a8412dd23d1632b4ea7ff90635f3f2ec84f30b0 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Fri, 6 Dec 2024 21:59:51 +0100 Subject: [PATCH] Formspec: Move tooltip above cursor when lacking space (#15470) --- src/gui/guiFormSpecMenu.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp index 2330f7c32..8a69f0429 100644 --- a/src/gui/guiFormSpecMenu.cpp +++ b/src/gui/guiFormSpecMenu.cpp @@ -3610,10 +3610,25 @@ void GUIFormSpecMenu::showTooltip(const std::wstring &text, // Calculate and set the tooltip position s32 tooltip_x = m_pointer.X + tooltip_offset_x; s32 tooltip_y = m_pointer.Y + tooltip_offset_y; - if (tooltip_x + tooltip_width > (s32)screenSize.X) - tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height; - if (tooltip_y + tooltip_height > (s32)screenSize.Y) - tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height; + // Bottom/Left limited positions (if the tooltip is too far out) + s32 tooltip_x_alt = (s32)screenSize.X - tooltip_width - m_btn_height; + s32 tooltip_y_alt = (s32)screenSize.Y - tooltip_height - m_btn_height; + + int collision = (tooltip_x_alt < tooltip_x) + 2 * (tooltip_y_alt < tooltip_y); + switch (collision) { + case 1: // x + tooltip_x = tooltip_x_alt; + break; + case 2: // y + tooltip_y = tooltip_y_alt; + break; + case 3: // both + tooltip_x = tooltip_x_alt; + tooltip_y = (s32)screenSize.Y - 2 * tooltip_height - m_btn_height; + break; + default: // OK + break; + } m_tooltip_element->setRelativePosition( core::rect(