LCOV - code coverage report
Current view: top level - source/gui/ObjectTypes - CTooltip.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 62 0.0 %
Date: 2023-01-19 00:18:29 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2021 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 "CTooltip.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/SettingTypes/CGUIString.h"
      24             : #include "gui/CGUIText.h"
      25             : 
      26             : #include <algorithm>
      27             : 
      28           0 : CTooltip::CTooltip(CGUI& pGUI)
      29             :     : IGUIObject(pGUI),
      30             :       IGUITextOwner(*static_cast<IGUIObject*>(this)),
      31             :       m_BufferZone(this, "buffer_zone"),
      32             :       m_Caption(this, "caption"),
      33             :       m_Font(this, "font"),
      34             :       m_Sprite(this, "sprite"),
      35             :       m_Delay(this, "delay", 500), // in milliseconds
      36             :       m_TextColor(this, "textcolor"),
      37             :       m_MaxWidth(this, "maxwidth"),
      38             :       m_Offset(this, "offset"),
      39             :       m_Anchor(this, "anchor", EVAlign::BOTTOM),
      40             :       // This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
      41             :       m_Independent(this, "independent"),
      42             :       // Private settings:
      43             :       // This is set by GUITooltip
      44             :       m_MousePos(this, "_mousepos"),
      45             :       // If the tooltip is just a reference to another object:
      46             :       m_UseObject(this, "use_object"),
      47           0 :       m_HideObject(this, "hide_object")
      48             : {
      49             :     // Set up a blank piece of text, to be replaced with a more
      50             :     // interesting message later
      51           0 :     AddText();
      52           0 : }
      53             : 
      54           0 : CTooltip::~CTooltip()
      55             : {
      56           0 : }
      57             : 
      58           0 : void CTooltip::SetupText()
      59             : {
      60           0 :     ENSURE(m_GeneratedTexts.size() == 1);
      61             : 
      62           0 :     m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_MaxWidth, m_BufferZone, m_TextAlign, this);
      63             : 
      64             :     // Position the tooltip relative to the mouse:
      65             : 
      66           0 :     const CVector2D& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos;
      67             : 
      68           0 :     float textwidth = m_GeneratedTexts[0].GetSize().Width;
      69           0 :     float textheight = m_GeneratedTexts[0].GetSize().Height;
      70             : 
      71           0 :     CGUISize size;
      72           0 :     size.pixel.left = mousepos.X + m_Offset->X;
      73           0 :     size.pixel.right = size.pixel.left + textwidth;
      74             : 
      75           0 :     switch (m_Anchor)
      76             :     {
      77           0 :     case EVAlign::TOP:
      78           0 :         size.pixel.top = mousepos.Y + m_Offset->Y;
      79           0 :         size.pixel.bottom = size.pixel.top + textheight;
      80           0 :         break;
      81           0 :     case EVAlign::BOTTOM:
      82           0 :         size.pixel.bottom = mousepos.Y + m_Offset->Y;
      83           0 :         size.pixel.top = size.pixel.bottom - textheight;
      84           0 :         break;
      85           0 :     case EVAlign::CENTER:
      86           0 :         size.pixel.top = mousepos.Y + m_Offset->Y - textheight/2.f;
      87           0 :         size.pixel.bottom = size.pixel.top + textwidth;
      88           0 :         break;
      89           0 :     default:
      90           0 :         debug_warn(L"Invalid EVAlign!");
      91             :     }
      92             : 
      93             : 
      94             :     // Reposition the tooltip if it's falling off in the GUI window.
      95           0 :     const CSize2D windowSize = m_pGUI.GetWindowSize();
      96             : 
      97           0 :     if (size.pixel.top < 0.f)
      98             :     {
      99           0 :         size.pixel.bottom -= size.pixel.top;
     100           0 :         size.pixel.top = 0.f;
     101             :     }
     102           0 :     else if (size.pixel.bottom > windowSize.Height)
     103             :     {
     104           0 :         size.pixel.top -= size.pixel.bottom - windowSize.Height;
     105           0 :         size.pixel.bottom = windowSize.Height;
     106             :     }
     107             : 
     108           0 :     if (size.pixel.left < 0.f)
     109             :     {
     110           0 :         size.pixel.right -= size.pixel.left;
     111           0 :         size.pixel.left = 0.f;
     112             :     }
     113           0 :     else if (size.pixel.right > windowSize.Width)
     114             :     {
     115           0 :         size.pixel.left -= size.pixel.right - windowSize.Width;
     116           0 :         size.pixel.right = windowSize.Width;
     117             :     }
     118             : 
     119           0 :     m_Size.Set(size, true);
     120           0 : }
     121             : 
     122           0 : void CTooltip::UpdateCachedSize()
     123             : {
     124           0 :     IGUIObject::UpdateCachedSize();
     125           0 :     IGUITextOwner::UpdateCachedSize();
     126           0 : }
     127             : 
     128           0 : void CTooltip::HandleMessage(SGUIMessage& Message)
     129             : {
     130           0 :     IGUIObject::HandleMessage(Message);
     131           0 :     IGUITextOwner::HandleMessage(Message);
     132           0 : }
     133             : 
     134           0 : void CTooltip::Draw(CCanvas2D& canvas)
     135             : {
     136             :     // Normally IGUITextOwner will handle this updating but since SetupText can modify the position
     137             :     // we need to call it now *before* we do the rest of the drawing
     138           0 :     if (!m_GeneratedTextsValid)
     139             :     {
     140           0 :         SetupText();
     141           0 :         m_GeneratedTextsValid = true;
     142             :     }
     143             : 
     144           0 :     m_pGUI.DrawSprite(m_Sprite, canvas, m_CachedActualSize);
     145           0 :     DrawText(canvas, 0, m_TextColor, m_CachedActualSize.TopLeft());
     146           0 : }
     147             : 
     148           0 : float CTooltip::GetBufferedZ() const
     149             : {
     150             :     // TODO: Find a nicer way of putting the tooltip on top of everything else.
     151           0 :     return 900.f;
     152             : }

Generated by: LCOV version 1.13