LCOV - code coverage report
Current view: top level - source/gui - CGUIScrollBarVertical.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 86 0.0 %
Date: 2022-06-14 00:41:00 Functions: 0 10 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 "CGUIScrollBarVertical.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "ps/CLogger.h"
      24             : 
      25           0 : CGUIScrollBarVertical::CGUIScrollBarVertical(CGUI& pGUI)
      26           0 :  : IGUIScrollBar(pGUI)
      27             : {
      28           0 : }
      29             : 
      30           0 : CGUIScrollBarVertical::~CGUIScrollBarVertical()
      31             : {
      32           0 : }
      33           0 : 
      34             : void CGUIScrollBarVertical::SetPosFromMousePos(const CVector2D& mouse)
      35           0 : {
      36           0 :     if (!GetStyle())
      37             :         return;
      38           0 : 
      39             :     /**
      40           0 :      * Calculate the position for the top of the item being scrolled
      41             :      */
      42           0 :     float emptyBackground = m_Length - m_BarSize;
      43             : 
      44             :     if (GetStyle()->m_UseEdgeButtons)
      45             :         emptyBackground -= GetStyle()->m_Width * 2;
      46             : 
      47             :     m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.Y - m_BarPressedAtPos.Y) / emptyBackground;
      48           0 : }
      49             : 
      50           0 : void CGUIScrollBarVertical::Draw(CCanvas2D& canvas)
      51           0 : {
      52             :     if (!GetStyle())
      53           0 :     {
      54             :         LOGWARNING("Attempt to draw scrollbar without a style.");
      55             :         return;
      56           0 :     }
      57             : 
      58           0 :     if (IsVisible())
      59             :     {
      60           0 :         CRect outline = GetOuterRect();
      61           0 : 
      62             :         m_pGUI.DrawSprite(
      63             :             GetStyle()->m_SpriteBackVertical,
      64           0 :             canvas,
      65             :             CRect(
      66           0 :                 outline.left,
      67             :                 outline.top + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0),
      68           0 :                 outline.right,
      69           0 :                 outline.bottom - (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0)
      70             :             )
      71           0 :         );
      72             : 
      73           0 :         if (GetStyle()->m_UseEdgeButtons)
      74             :         {
      75           0 :             const CGUISpriteInstance* button_top;
      76             :             const CGUISpriteInstance* button_bottom;
      77             : 
      78             :             if (m_ButtonMinusHovered)
      79           0 :             {
      80             :                 if (m_ButtonMinusPressed)
      81           0 :                     button_top = &(GetStyle()->m_SpriteButtonTopPressed ? GetStyle()->m_SpriteButtonTopPressed : GetStyle()->m_SpriteButtonTop);
      82           0 :                 else
      83             :                     button_top = &(GetStyle()->m_SpriteButtonTopOver ? GetStyle()->m_SpriteButtonTopOver : GetStyle()->m_SpriteButtonTop);
      84           0 :             }
      85             :             else
      86           0 :                 button_top = &GetStyle()->m_SpriteButtonTop;
      87           0 : 
      88             :             if (m_ButtonPlusHovered)
      89           0 :             {
      90             :                 if (m_ButtonPlusPressed)
      91             :                     button_bottom = &(GetStyle()->m_SpriteButtonBottomPressed ? GetStyle()->m_SpriteButtonBottomPressed : GetStyle()->m_SpriteButtonBottom);
      92           0 :                 else
      93             :                     button_bottom = &(GetStyle()->m_SpriteButtonBottomOver ? GetStyle()->m_SpriteButtonBottomOver : GetStyle()->m_SpriteButtonBottom);
      94           0 :             }
      95             :             else
      96           0 :                 button_bottom = &GetStyle()->m_SpriteButtonBottom;
      97           0 : 
      98             :             m_pGUI.DrawSprite(
      99           0 :                 *button_top,
     100             :                 canvas,
     101             :                 CRect(
     102           0 :                     outline.left,
     103             :                     outline.top,
     104           0 :                     outline.right,
     105             :                     outline.top+GetStyle()->m_Width
     106             :                 )
     107           0 :             );
     108             : 
     109             :             m_pGUI.DrawSprite(
     110             :                 *button_bottom,
     111           0 :                 canvas,
     112             :                 CRect(
     113             :                     outline.left,
     114             :                     outline.bottom-GetStyle()->m_Width,
     115           0 :                     outline.right,
     116             :                     outline.bottom
     117             :                 )
     118           0 :             );
     119             :         }
     120           0 : 
     121             :         m_pGUI.DrawSprite(
     122             :             GetStyle()->m_SpriteBarVertical,
     123             :             canvas,
     124             :             GetBarRect()
     125             :         );
     126             :     }
     127           0 : }
     128           0 : 
     129             : void CGUIScrollBarVertical::HandleMessage(SGUIMessage& Message)
     130           0 : {
     131             :     IGUIScrollBar::HandleMessage(Message);
     132             : }
     133             : 
     134             : CRect CGUIScrollBarVertical::GetBarRect() const
     135           0 : {
     136             :     CRect ret;
     137           0 :     if (!GetStyle())
     138           0 :         return ret;
     139             : 
     140           0 :     // Get from where the scroll area begins to where it ends
     141             :     float from = m_Y;
     142           0 :     float to = m_Y + m_Length - m_BarSize;
     143           0 : 
     144             :     if (GetStyle()->m_UseEdgeButtons)
     145             :     {
     146             :         from += GetStyle()->m_Width;
     147           0 :         to -= GetStyle()->m_Width;
     148           0 :     }
     149             : 
     150           0 :     ret.top = from + (to - from) * m_Pos / GetMaxPos();
     151             :     ret.bottom = ret.top + m_BarSize;
     152           0 :     ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width);
     153           0 :     ret.left = ret.right - GetStyle()->m_Width;
     154             : 
     155             :     return ret;
     156           0 : }
     157           0 : 
     158           0 : CRect CGUIScrollBarVertical::GetOuterRect() const
     159           0 : {
     160             :     CRect ret;
     161           0 :     if (!GetStyle())
     162             :         return ret;
     163             : 
     164           0 :     ret.top = m_Y;
     165             :     ret.bottom = m_Y+m_Length;
     166           0 :     ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width);
     167           0 :     ret.left = ret.right - GetStyle()->m_Width;
     168             : 
     169             :     return ret;
     170           0 : }
     171           0 : 
     172           0 : bool CGUIScrollBarVertical::HoveringButtonMinus(const CVector2D& mouse)
     173           0 : {
     174             :     if (!GetStyle())
     175           0 :         return false;
     176             : 
     177             :     float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X;
     178           0 : 
     179             :     return mouse.X >= StartX &&
     180           0 :            mouse.X <= StartX + GetStyle()->m_Width &&
     181             :            mouse.Y >= m_Y &&
     182             :            mouse.Y <= m_Y + GetStyle()->m_Width;
     183           0 : }
     184             : 
     185           0 : bool CGUIScrollBarVertical::HoveringButtonPlus(const CVector2D& mouse)
     186           0 : {
     187           0 :     if (!GetStyle())
     188           0 :         return false;
     189             : 
     190             :     float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X;
     191           0 : 
     192             :     return mouse.X > StartX &&
     193           0 :            mouse.X < StartX + GetStyle()->m_Width &&
     194             :            mouse.Y > m_Y + m_Length - GetStyle()->m_Width &&
     195             :            mouse.Y < m_Y + m_Length;
     196           0 : }

Generated by: LCOV version 1.13