LCOV - code coverage report
Current view: top level - source/gui - IGUIScrollBar.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 84 0.0 %
Date: 2022-06-14 00:41:00 Functions: 0 7 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 "IGUIScrollBar.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/SGUIMessage.h"
      24             : #include "gui/ObjectBases/IGUIScrollBarOwner.h"
      25             : #include "maths/MathUtil.h"
      26             : 
      27           0 : IGUIScrollBar::IGUIScrollBar(CGUI& pGUI)
      28             :     : m_pGUI(pGUI),
      29             :      m_pStyle(nullptr),
      30             :      m_X(300.f), m_Y(300.f),
      31             :      m_ScrollRange(1.f), m_ScrollSpace(0.f), // MaxPos: not 0, due to division.
      32             :      m_Length(200.f), m_Width(20.f),
      33             :      m_BarSize(0.f), m_Pos(0.f),
      34             :      m_ButtonPlusPressed(false),
      35             :      m_ButtonMinusPressed(false),
      36             :      m_ButtonPlusHovered(false),
      37             :      m_ButtonMinusHovered(false),
      38             :      m_BarHovered(false),
      39           0 :      m_BarPressed(false)
      40             : {
      41           0 : }
      42             : 
      43           0 : IGUIScrollBar::~IGUIScrollBar()
      44             : {
      45           0 : }
      46           0 : 
      47             : void IGUIScrollBar::SetupBarSize()
      48           0 : {
      49           0 :     if (!GetStyle())
      50             :         return;
      51           0 : 
      52             :     float min = GetStyle()->m_MinimumBarSize;
      53           0 :     float max = GetStyle()->m_MaximumBarSize;
      54             :     float length = m_Length;
      55           0 : 
      56           0 :     // Check for edge buttons
      57             :     if (GetStyle()->m_UseEdgeButtons)
      58           0 :         length -= GetStyle()->m_Width * 2.f;
      59           0 : 
      60           0 :     // Check min and max are valid
      61             :     if (min > length)
      62             :         min = 0.f;
      63           0 :     if (max < min)
      64           0 :         max = length;
      65             : 
      66             :     // Clamp size to not exceed a minimum or maximum.
      67           0 :     m_BarSize = Clamp(length * std::min(m_ScrollSpace / m_ScrollRange, 1.f), min, max);
      68           0 : }
      69           0 : 
      70           0 : const SGUIScrollBarStyle* IGUIScrollBar::GetStyle() const
      71             : {
      72             :     if (!m_pHostObject)
      73           0 :         return nullptr;
      74             : 
      75             :     return m_pHostObject->GetScrollBarStyle(m_ScrollBarStyle);
      76           0 : }
      77             : 
      78           0 : void IGUIScrollBar::UpdatePosBoundaries()
      79             : {
      80             :     if (m_Pos < 0.f ||
      81           0 :         m_ScrollRange < m_ScrollSpace) // <= scrolling not applicable
      82             :         m_Pos = 0.f;
      83             :     else if (m_Pos > GetMaxPos())
      84           0 :         m_Pos = GetMaxPos();
      85             : }
      86           0 : 
      87           0 : void IGUIScrollBar::HandleMessage(SGUIMessage& Message)
      88           0 : {
      89           0 :     switch (Message.type)
      90           0 :     {
      91           0 :     case GUIM_MOUSE_MOTION:
      92             :     {
      93           0 :         // TODO Gee: Optimizations needed!
      94             : 
      95           0 :         const CVector2D& mouse = m_pGUI.GetMousePos();
      96             : 
      97           0 :         // If bar is being dragged
      98           0 :         if (m_BarPressed)
      99             :         {
     100             :             SetPosFromMousePos(mouse);
     101           0 :             UpdatePosBoundaries();
     102             :         }
     103             : 
     104           0 :         // check if components are being hovered
     105             :         m_BarHovered = GetBarRect().PointInside(mouse);
     106           0 :         m_ButtonMinusHovered = HoveringButtonMinus(mouse);
     107           0 :         m_ButtonPlusHovered =  HoveringButtonPlus(mouse);
     108             : 
     109             :         if (!m_ButtonMinusHovered)
     110             :             m_ButtonMinusPressed = false;
     111           0 : 
     112           0 :         if (!m_ButtonPlusHovered)
     113           0 :             m_ButtonPlusPressed = false;
     114             :         break;
     115           0 :     }
     116           0 : 
     117             :     case GUIM_MOUSE_PRESS_LEFT:
     118           0 :     {
     119           0 :         if (!m_pHostObject)
     120             :             break;
     121             : 
     122             :         const CVector2D& mouse = m_pGUI.GetMousePos();
     123           0 : 
     124           0 :         // if bar is pressed
     125           0 :         if (GetBarRect().PointInside(mouse))
     126             :         {
     127             :             m_BarPressed = true;
     128           0 :             m_BarPressedAtPos = mouse;
     129             :             m_PosWhenPressed = m_Pos;
     130             :         }
     131           0 :         // if button-minus is pressed
     132             :         else if (m_ButtonMinusHovered)
     133           0 :         {
     134           0 :             m_ButtonMinusPressed = true;
     135           0 :             ScrollMinus();
     136             :         }
     137             :         // if button-plus is pressed
     138           0 :         else if (m_ButtonPlusHovered)
     139             :         {
     140           0 :             m_ButtonPlusPressed = true;
     141           0 :             ScrollPlus();
     142             :         }
     143             :         // Pressing the background of the bar, to scroll
     144           0 :         //  notice the if-sentence alone does not admit that,
     145             :         //  it must be after the above if/elses
     146           0 :         else
     147           0 :         {
     148             :             if (GetOuterRect().PointInside(mouse))
     149             :             {
     150             :                 // Scroll plus or minus a lot, this might change, it doesn't
     151             :                 //  have to be fancy though.
     152             :                 if (mouse.Y < GetBarRect().top)
     153             :                     ScrollMinusPlenty();
     154           0 :                 else
     155             :                     ScrollPlusPlenty();
     156             :                 // Simulate mouse movement to see if bar now is hovered
     157             :                 SGUIMessage msg(GUIM_MOUSE_MOTION);
     158           0 :                 HandleMessage(msg);
     159           0 :             }
     160             :         }
     161           0 :         break;
     162             :     }
     163           0 : 
     164           0 :     case GUIM_MOUSE_RELEASE_LEFT:
     165             :         m_ButtonMinusPressed = false;
     166             :         m_ButtonPlusPressed = false;
     167             :         break;
     168             : 
     169             :     case GUIM_MOUSE_WHEEL_UP:
     170           0 :     {
     171           0 :         ScrollMinus();
     172           0 :         // Since the scroll was changed, let's simulate a mouse movement
     173           0 :         //  to check if scrollbar now is hovered
     174             :         SGUIMessage msg(GUIM_MOUSE_MOTION);
     175           0 :         HandleMessage(msg);
     176           0 :         break;
     177           0 :     }
     178             : 
     179             :     case GUIM_MOUSE_WHEEL_DOWN:
     180           0 :     {
     181           0 :         ScrollPlus();
     182           0 :         // Since the scroll was changed, let's simulate a mouse movement
     183             :         //  to check if scrollbar now is hovered
     184             :         SGUIMessage msg(GUIM_MOUSE_MOTION);
     185           0 :         HandleMessage(msg);
     186           0 :         break;
     187           0 :     }
     188             : 
     189             :     default:
     190           0 :         break;
     191           0 :     }
     192           0 : }

Generated by: LCOV version 1.13