LCOV - code coverage report
Current view: top level - source/gui - GUITooltip.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 20 99 20.2 %
Date: 2026-12-03 01:19:39 Functions: 3 6 50.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "GUITooltip.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/ObjectBases/IGUIObject.h"
      24             : #include "gui/ObjectTypes/CTooltip.h"
      25             : #include "lib/debug.h"
      26             : #include "lib/timer.h"
      27             : #include "ps/CLogger.h"
      28             : 
      29             : #include <string>
      30             : 
      31             : /*
      32             :     Tooltips:
      33             :     When holding the mouse stationary over an object for some amount of time,
      34             :     the tooltip is displayed. If the mouse moves off that object, the tooltip
      35             :     disappears. If the mouse re-enters an object within a short time, the new
      36             :     tooltip is displayed immediately. (This lets you run the mouse across a
      37             :     series of buttons, without waiting ages for the text to pop up every time.)
      38             : 
      39             :     See Visual Studio's toolbar buttons for an example.
      40             : 
      41             : 
      42             :     Implemented as a state machine:
      43             : 
      44             :     (where "*" lines are checked constantly, and "<" lines are handled
      45             :     on entry to that state)
      46             : 
      47             :     IN MOTION
      48             :     * If the mouse stops, check whether it should have a tooltip and move to
      49             :       'STATIONARY, NO TOOLTIP' or 'STATIONARY, TOOLIP'
      50             :     * If the mouse enters an object with a tooltip delay of 0, switch to 'SHOWING'
      51             : 
      52             :     STATIONARY, NO TOOLTIP
      53             :     * If the mouse moves, switch to 'IN MOTION'
      54             : 
      55             :     STATIONARY, TOOLTIP
      56             :     < Set target time = now + tooltip time
      57             :     * If the mouse moves, switch to 'IN MOTION'
      58             :     * If now > target time, switch to 'SHOWING'
      59             : 
      60             :     SHOWING
      61             :     < Start displaying the tooltip
      62             :     * If the mouse leaves the object, check whether the new object has a tooltip
      63             :       and switch to 'SHOWING' or 'COOLING'
      64             : 
      65             :     COOLING  (since I can't think of a better name)
      66             :     < Stop displaying the tooltip
      67             :     < Set target time = now + cooldown time
      68             :     * If the mouse has moved and is over a tooltipped object, switch to 'SHOWING'
      69             :     * If now > target time, switch to 'STATIONARY, NO TOOLTIP'
      70             : */
      71             : 
      72             : enum
      73             : {
      74             :     ST_IN_MOTION,
      75             :     ST_STATIONARY_NO_TOOLTIP,
      76             :     ST_STATIONARY_TOOLTIP,
      77             :     ST_SHOWING,
      78             :     ST_COOLING
      79             : };
      80             : 
      81          26 : GUITooltip::GUITooltip()
      82          26 : : m_State(ST_IN_MOTION), m_PreviousObject(nullptr), m_PreviousTooltipName()
      83             : {
      84          26 : }
      85             : 
      86             : const double CooldownTime = 0.25; // TODO: Don't hard-code this value
      87             : 
      88          14 : bool GUITooltip::GetTooltip(IGUIObject* obj, CStr& style)
      89             : {
      90          14 :     if (!obj)
      91             :         return false;
      92             : 
      93           0 :     if (obj->GetTooltipText().empty())
      94             :         return false;
      95             : 
      96           0 :     style = obj->GetTooltipStyle();
      97           0 :     if (style.empty())
      98           0 :         style = "default";
      99             :     return true;
     100             : }
     101             : 
     102           0 : void GUITooltip::ShowTooltip(IGUIObject* obj, const CVector2D& pos, const CStr& style, CGUI& pGUI)
     103             : {
     104           0 :     ENSURE(obj);
     105             : 
     106           0 :     if (style.empty())
     107             :         return;
     108             : 
     109             :     // Objects in __tooltip_ are guaranteed to be CTooltip* by the engine.
     110           0 :     CTooltip* tooltipobj = static_cast<CTooltip*>(pGUI.FindObjectByName("__tooltip_" + style));
     111           0 :     if (!tooltipobj || !tooltipobj->SettingExists("use_object"))
     112             :     {
     113           0 :         LOGERROR("Cannot find tooltip named '{}'", style);
     114           0 :         return;
     115             :     }
     116             : 
     117           0 :     IGUIObject* usedobj; // object actually used to display the tooltip in.
     118             : 
     119           0 :     const CStr& usedObjectName = tooltipobj->GetUsedObject();
     120           0 :     if (usedObjectName.empty())
     121             :     {
     122           0 :         usedobj = tooltipobj;
     123           0 :         tooltipobj->SetMousePos(pos);
     124             :     }
     125             :     else
     126             :     {
     127           0 :         usedobj = pGUI.FindObjectByName(usedObjectName);
     128           0 :         if (!usedobj)
     129             :         {
     130           0 :             LOGERROR("Cannot find object named '{}' used by tooltip '{}'", usedObjectName,
     131             :                 style);
     132           0 :             return;
     133             :         }
     134             :     }
     135             : 
     136           0 :     if (usedobj->SettingExists("caption"))
     137             :     {
     138           0 :         const CStrW& text = obj->GetTooltipText();
     139           0 :         usedobj->SetSettingFromString("caption", text, true);
     140             :     }
     141             :     else
     142             :     {
     143           0 :         LOGERROR("Object '{}' used by tooltip '{}' must have a caption setting!",
     144             :             usedobj->GetPresentableName(), style);
     145           0 :         return;
     146             :     }
     147             : 
     148           0 :     usedobj->SetHidden(false);
     149             : }
     150             : 
     151           0 : void GUITooltip::HideTooltip(const CStr& style, CGUI& pGUI)
     152             : {
     153           0 :     if (style.empty())
     154             :         return;
     155             : 
     156             :     // Objects in __tooltip_ are guaranteed to be CTooltip* by the engine.
     157           0 :     CTooltip* tooltipobj = static_cast<CTooltip*>(pGUI.FindObjectByName("__tooltip_" + style));
     158           0 :     if (!tooltipobj || !tooltipobj->SettingExists("use_object") || !tooltipobj->SettingExists("hide_object"))
     159             :     {
     160           0 :         LOGERROR("Cannot find tooltip named '{}' or it is not a tooltip", style);
     161           0 :         return;
     162             :     }
     163           0 :     const CStr& usedObjectName = tooltipobj->GetUsedObject();
     164           0 :     if (!usedObjectName.empty())
     165             :     {
     166           0 :         IGUIObject* usedobj = pGUI.FindObjectByName(usedObjectName);
     167           0 :         if (usedobj && usedobj->SettingExists("caption"))
     168             :         {
     169           0 :             usedobj->SetSettingFromString("caption", L"", true);
     170             :         }
     171             :         else
     172             :         {
     173           0 :             LOGERROR("Object named '{}' used by tooltip '{}' does not exist or does not have a "
     174             :                 "caption setting!", usedObjectName, style);
     175           0 :             return;
     176             :         }
     177             : 
     178           0 :         if (tooltipobj->ShouldHideObject())
     179           0 :             usedobj->SetHidden(true);
     180             :     }
     181             :     else
     182           0 :         tooltipobj->SetHidden(true);
     183             : }
     184             : 
     185           0 : static std::int32_t GetTooltipDelay(const CStr& style, CGUI& pGUI)
     186             : {
     187             :     // Objects in __tooltip_ are guaranteed to be CTooltip* by the engine.
     188           0 :     CTooltip* tooltipobj = static_cast<CTooltip*>(pGUI.FindObjectByName("__tooltip_" + style));
     189             : 
     190           0 :     if (!tooltipobj)
     191             :     {
     192           0 :         LOGERROR("Cannot find tooltip object named '{}'", style);
     193           0 :         return 500;
     194             :     }
     195             : 
     196           0 :     return tooltipobj->GetTooltipDelay();
     197             : }
     198             : 
     199          18 : void GUITooltip::Update(IGUIObject* Nearest, const CVector2D& MousePos, CGUI& GUI)
     200             : {
     201             :     // Called once per frame, so efficiency isn't vital
     202          18 :     double now = timer_Time();
     203             : 
     204          18 :     CStr style;
     205          18 :     int nextstate = -1;
     206             : 
     207          18 :     switch (m_State)
     208             :     {
     209          14 :     case ST_IN_MOTION:
     210          14 :         if (MousePos == m_PreviousMousePos)
     211             :         {
     212          14 :             if (GetTooltip(Nearest, style))
     213             :                 nextstate = ST_STATIONARY_TOOLTIP;
     214             :             else
     215             :                 nextstate = ST_STATIONARY_NO_TOOLTIP;
     216             :         }
     217             :         else
     218             :         {
     219             :             // Check for movement onto a zero-delayed tooltip
     220           0 :             if (GetTooltip(Nearest, style) && GetTooltipDelay(style, GUI)==0)
     221             :             {
     222             :                 // Reset any previous tooltips completely
     223             :                 //m_Time = now + (double)GetTooltipDelay(style, GUI) / 1000.;
     224           0 :                 HideTooltip(m_PreviousTooltipName, GUI);
     225             : 
     226             :                 nextstate = ST_SHOWING;
     227             :             }
     228             :         }
     229             :         break;
     230             : 
     231           4 :     case ST_STATIONARY_NO_TOOLTIP:
     232           4 :         if (MousePos != m_PreviousMousePos)
     233             :             nextstate = ST_IN_MOTION;
     234             :         break;
     235             : 
     236           0 :     case ST_STATIONARY_TOOLTIP:
     237           0 :         if (MousePos != m_PreviousMousePos)
     238             :             nextstate = ST_IN_MOTION;
     239           0 :         else if (now >= m_Time)
     240             :         {
     241             :             // Make sure the tooltip still exists
     242           0 :             if (GetTooltip(Nearest, style))
     243             :                 nextstate = ST_SHOWING;
     244             :             else
     245             :             {
     246             :                 // Failed to retrieve style - the object has probably been
     247             :                 // altered, so just restart the process
     248             :                 nextstate = ST_IN_MOTION;
     249             :             }
     250             :         }
     251             :         break;
     252             : 
     253           0 :     case ST_SHOWING:
     254             :         // Handle sub-object tooltips.
     255           0 :         if (Nearest == m_PreviousObject)
     256             :         {
     257             :             // Still showing the same object's tooltip, but the text might have changed
     258           0 :             if (GetTooltip(Nearest, style))
     259           0 :                 ShowTooltip(Nearest, MousePos, style, GUI);
     260             :             else
     261             :                 nextstate = ST_COOLING;
     262             :         }
     263             :         else
     264             :         {
     265             :             // Mouse moved onto a new object
     266           0 :             if (GetTooltip(Nearest, style))
     267             :             {
     268           0 :                 CStr style_old;
     269             : 
     270             :                 // If we're displaying a tooltip with no delay, then we want to
     271             :                 //  reset so that other object that should have delay can't
     272             :                 //  "ride this tail", it have to wait.
     273             :                 // Notice that this doesn't apply to when you go from one delay=0
     274             :                 //  to another delay=0
     275           0 :                 if (GetTooltip(m_PreviousObject, style_old) && GetTooltipDelay(style_old, GUI) == 0 &&
     276           0 :                     GetTooltipDelay(style, GUI) != 0)
     277             :                 {
     278           0 :                     HideTooltip(m_PreviousTooltipName, GUI);
     279             :                     nextstate = ST_IN_MOTION;
     280             :                 }
     281             :                 else
     282             :                 {
     283             :                     // Hide old scrollbar
     284           0 :                     HideTooltip(m_PreviousTooltipName, GUI);
     285             :                     nextstate = ST_SHOWING;
     286             :                 }
     287           0 :             }
     288             :             else
     289             :                 nextstate = ST_COOLING;
     290             :         }
     291             :         break;
     292             : 
     293           0 :     case ST_COOLING:
     294           0 :         if (GetTooltip(Nearest, style))
     295             :             nextstate = ST_SHOWING;
     296           0 :         else if (now >= m_Time)
     297             :             nextstate = ST_IN_MOTION;
     298             :         break;
     299             :     }
     300             : 
     301             :     // Handle state-entry code:
     302           0 :     if (nextstate != -1)
     303             :     {
     304          14 :         switch (nextstate)
     305             :         {
     306           0 :         case ST_STATIONARY_TOOLTIP:
     307           0 :             m_Time = now + (double)GetTooltipDelay(style, GUI) / 1000.;
     308           0 :             break;
     309             : 
     310           0 :         case ST_SHOWING:
     311           0 :             ShowTooltip(Nearest, MousePos, style, GUI);
     312           0 :             m_PreviousTooltipName = style;
     313             :             break;
     314             : 
     315           0 :         case ST_COOLING:
     316           0 :             HideTooltip(m_PreviousTooltipName, GUI);
     317           0 :             m_Time = now + CooldownTime;
     318           0 :             break;
     319             :         }
     320             : 
     321          14 :         m_State = nextstate;
     322             :     }
     323             : 
     324          18 :     m_PreviousMousePos = MousePos;
     325          18 :     m_PreviousObject = Nearest;
     326          18 : }

Generated by: LCOV version 1.16