LCOV - code coverage report
Current view: top level - source/gui - CGUIScrollBarHorizontal.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 107 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 11 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 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 "CGUIScrollBarHorizontal.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/CGUISprite.h"
      24             : #include "gui/IGUIScrollBar.h"
      25             : #include "gui/SGUIMessage.h"
      26             : #include "maths/Rect.h"
      27             : #include "maths/Vector2D.h"
      28             : #include "ps/CLogger.h"
      29             : 
      30           0 : CGUIScrollBarHorizontal::CGUIScrollBarHorizontal(CGUI& pGUI)
      31           0 :     : IGUIScrollBar(pGUI)
      32             : {
      33           0 : }
      34             : 
      35           0 : CGUIScrollBarHorizontal::~CGUIScrollBarHorizontal()
      36             : {
      37           0 : }
      38             : 
      39           0 : void CGUIScrollBarHorizontal::SetPosFromMousePos(const CVector2D& mouse)
      40             : {
      41           0 :     if (!GetStyle())
      42             :         return;
      43             : 
      44             :     // Calculate the position for the top of the item being scrolled
      45           0 :     float emptyBackground = m_Length - m_BarSize;
      46             : 
      47           0 :     if (GetStyle()->m_UseEdgeButtons)
      48           0 :         emptyBackground -= GetStyle()->m_Width * 2;
      49             : 
      50           0 :     m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.X - m_BarPressedAtPos.X) / emptyBackground;
      51             : }
      52             : 
      53           0 : void CGUIScrollBarHorizontal::Draw(CCanvas2D& canvas)
      54             : {
      55           0 :     if (!IsVisible())
      56           0 :         return;
      57             : 
      58           0 :     if (!GetStyle())
      59             :     {
      60           0 :         LOGWARNING("Attempt to draw scrollbar without a style.");
      61           0 :         return;
      62             :     }
      63             : 
      64           0 :     CRect outline = GetOuterRect();
      65             : 
      66           0 :     m_pGUI.DrawSprite(
      67           0 :         GetStyle()->m_SpriteBackHorizontal,
      68             :         canvas,
      69           0 :         CRect(
      70           0 :             outline.left + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0),
      71             :             outline.top,
      72           0 :             outline.right - (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0),
      73             :             outline.bottom));
      74             : 
      75           0 :     if (GetStyle()->m_UseEdgeButtons)
      76             :     {
      77           0 :         const CGUISpriteInstance* button_left;
      78           0 :         const CGUISpriteInstance* button_right;
      79             : 
      80           0 :         if (m_ButtonMinusHovered)
      81             :         {
      82           0 :             if (m_ButtonMinusPressed)
      83           0 :                 button_left = &(GetStyle()->m_SpriteButtonLeftPressed ? GetStyle()->m_SpriteButtonLeftPressed : GetStyle()->m_SpriteButtonLeft);
      84             :             else
      85           0 :                 button_left = &(GetStyle()->m_SpriteButtonLeftOver ? GetStyle()->m_SpriteButtonLeftOver : GetStyle()->m_SpriteButtonLeft);
      86             :         }
      87             :         else
      88           0 :             button_left = &GetStyle()->m_SpriteButtonLeft;
      89             : 
      90           0 :         if (m_ButtonPlusHovered)
      91             :         {
      92           0 :             if (m_ButtonPlusPressed)
      93           0 :                 button_right = &(GetStyle()->m_SpriteButtonRightPressed ? GetStyle()->m_SpriteButtonRightPressed : GetStyle()->m_SpriteButtonRight);
      94             :             else
      95           0 :                 button_right = &(GetStyle()->m_SpriteButtonRightOver ? GetStyle()->m_SpriteButtonRightOver : GetStyle()->m_SpriteButtonRight);
      96             :         }
      97             :         else
      98           0 :             button_right = &GetStyle()->m_SpriteButtonRight;
      99             : 
     100           0 :         m_pGUI.DrawSprite(
     101             :             *button_left,
     102             :             canvas,
     103           0 :             CRect(
     104             :                 outline.left,
     105             :                 outline.top,
     106           0 :                 outline.left + GetStyle()->m_Width,
     107             :                 outline.bottom));
     108             : 
     109           0 :         m_pGUI.DrawSprite(
     110             :             *button_right,
     111             :             canvas,
     112           0 :             CRect(
     113           0 :                 outline.right - GetStyle()->m_Width,
     114             :                 outline.top,
     115             :                 outline.right,
     116             :                 outline.bottom));
     117             :     }
     118             : 
     119           0 :     m_pGUI.DrawSprite(
     120           0 :         GetStyle()->m_SpriteSliderHorizontal,
     121             :         canvas,
     122           0 :         GetBarRect()
     123             :     );
     124             : }
     125             : 
     126           0 : void CGUIScrollBarHorizontal::HandleMessage(SGUIMessage& Message)
     127             : {
     128           0 :     switch (Message.type)
     129             :     {
     130           0 :     case GUIM_MOUSE_WHEEL_LEFT:
     131           0 :     {
     132           0 :         ScrollMinus();
     133             :         // Since the scroll was changed, let's simulate a mouse movement
     134             :         //  to check if scrollbar now is hovered
     135           0 :         SGUIMessage msg(GUIM_MOUSE_MOTION);
     136           0 :         HandleMessage(msg);
     137           0 :         Message.Skip(false);
     138           0 :         break;
     139           0 :     }
     140             : 
     141           0 :     case GUIM_MOUSE_WHEEL_RIGHT:
     142           0 :     {
     143           0 :         ScrollPlus();
     144             :         // Since the scroll was changed, let's simulate a mouse movement
     145             :         //  to check if scrollbar now is hovered
     146           0 :         SGUIMessage msg(GUIM_MOUSE_MOTION);
     147           0 :         HandleMessage(msg);
     148           0 :         Message.Skip(false);
     149           0 :         break;
     150           0 :     }
     151             : 
     152           0 :     default:
     153           0 :         IGUIScrollBar::HandleMessage(Message);
     154           0 :         break;
     155             :     }
     156           0 : }
     157             : 
     158           0 : CRect CGUIScrollBarHorizontal::GetBarRect() const
     159             : {
     160           0 :     CRect ret;
     161           0 :     if (!GetStyle())
     162             :         return ret;
     163             : 
     164             :     // Get from where the scroll area begins to where it ends
     165           0 :     float from = m_X;
     166           0 :     float to = m_X + m_Length - m_BarSize;
     167             : 
     168           0 :     if (GetStyle()->m_UseEdgeButtons)
     169             :     {
     170           0 :         from += GetStyle()->m_Width;
     171           0 :         to -= GetStyle()->m_Width;
     172             :     }
     173             : 
     174           0 :     ret.left = from + (to - from) * m_Pos / GetMaxPos();
     175           0 :     ret.right = ret.left + m_BarSize;
     176           0 :     ret.bottom = m_Y + (m_BottomAligned ? 0 : GetStyle()->m_Width);
     177           0 :     ret.top = ret.bottom - GetStyle()->m_Width;
     178             : 
     179           0 :     return ret;
     180             : }
     181             : 
     182           0 : CRect CGUIScrollBarHorizontal::GetOuterRect() const
     183             : {
     184           0 :     CRect ret;
     185           0 :     if (!GetStyle())
     186             :         return ret;
     187             : 
     188           0 :     ret.left = m_X;
     189           0 :     ret.right = m_X + m_Length;
     190           0 :     ret.bottom = m_Y + (m_BottomAligned ? 0 : GetStyle()->m_Width);
     191           0 :     ret.top = ret.bottom - GetStyle()->m_Width;
     192             : 
     193           0 :     return ret;
     194             : }
     195             : 
     196           0 : bool CGUIScrollBarHorizontal::HoveringButtonMinus(const CVector2D& mouse)
     197             : {
     198           0 :     if (!GetStyle())
     199             :         return false;
     200             : 
     201           0 :     float StartY = m_BottomAligned ? m_Y - GetStyle()->m_Width : m_Y;
     202             : 
     203           0 :     return mouse.Y >= StartY &&
     204           0 :         mouse.Y <= StartY + GetStyle()->m_Width &&
     205           0 :         mouse.X >= m_X &&
     206           0 :         mouse.X <= m_X + GetStyle()->m_Width;
     207             : }
     208             : 
     209           0 : bool CGUIScrollBarHorizontal::HoveringButtonPlus(const CVector2D& mouse)
     210             : {
     211           0 :     if (!GetStyle())
     212             :         return false;
     213             : 
     214           0 :     float StartY = m_BottomAligned ? m_Y - GetStyle()->m_Width : m_Y;
     215             : 
     216           0 :     return mouse.Y > StartY &&
     217           0 :         mouse.Y < StartY + GetStyle()->m_Width &&
     218           0 :         mouse.X > m_X + m_Length - GetStyle()->m_Width &&
     219           0 :         mouse.X < m_X + m_Length;
     220             : }
     221             : 
     222           0 : void CGUIScrollBarHorizontal::SetScrollPlentyFromMousePos(const CVector2D& mouse)
     223             : {
     224             :     // Scroll plus or minus a lot, this might change, it doesn't
     225             :     //  have to be fancy though.
     226           0 :     if (mouse.X < GetBarRect().left)
     227           0 :         ScrollMinusPlenty();
     228             :     else
     229           0 :         ScrollPlusPlenty();
     230           0 : }

Generated by: LCOV version 1.16