LCOV - code coverage report
Current view: top level - source/gui - IGUIScrollBar.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 21 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 10 0.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             : /*
      19             :     A GUI Scrollbar, this class doesn't present all functionality
      20             :     to the scrollbar, it just controls the drawing and a wrapper
      21             :     for interaction with it.
      22             : */
      23             : 
      24             : #ifndef INCLUDED_IGUISCROLLBAR
      25             : #define INCLUDED_IGUISCROLLBAR
      26             : 
      27             : #include "gui/CGUISprite.h"
      28             : #include "lib/code_annotation.h"
      29             : #include "maths/Vector2D.h"
      30             : #include "ps/CStr.h"
      31             : 
      32             : #include <algorithm>
      33             : 
      34             : class CCanvas2D;
      35             : class CGUI;
      36             : class CRect;
      37             : class IGUIScrollBarOwner;
      38             : struct SGUIMessage;
      39             : 
      40             : /**
      41             :  * The GUI Scroll-bar style. Tells us how scroll-bars look and feel.
      42             :  *
      43             :  * A scroll-bar style can choose whether to support horizontal, vertical
      44             :  * or both.
      45             :  *
      46             :  * @see IGUIScrollBar
      47             :  */
      48             : struct SGUIScrollBarStyle
      49             : {
      50             :     // CGUISpriteInstance makes this NONCOPYABLE implicitly, make it explicit
      51             :     NONCOPYABLE(SGUIScrollBarStyle);
      52           0 :     MOVABLE(SGUIScrollBarStyle);
      53           0 :     SGUIScrollBarStyle() = default;
      54             : 
      55             :     //--------------------------------------------------------
      56             :     /** @name General Settings */
      57             :     //--------------------------------------------------------
      58             :     //@{
      59             : 
      60             :     /**
      61             :      * Width of bar, also both sides of the edge buttons.
      62             :      */
      63             :     float m_Width;
      64             : 
      65             :     /**
      66             :      * Scrollable with the wheel.
      67             :      */
      68             :     bool m_ScrollWheel;
      69             : 
      70             :     /**
      71             :      * How much (in percent, 0.1f = 10%) to scroll each time
      72             :      * the wheel is admitted, or the buttons are pressed.
      73             :      */
      74             :     float m_ScrollSpeed;
      75             : 
      76             :     /**
      77             :      * Whether or not the edge buttons should appear or not. Sometimes
      78             :      * you actually don't want them, like perhaps in a combo box.
      79             :      */
      80             :     bool m_ScrollButtons;
      81             : 
      82             :     /**
      83             :      * Sometimes there is *a lot* to scroll, but to prevent the scroll "bar"
      84             :      * from being almost invisible (or ugly), you can set a minimum in pixel
      85             :      * size.
      86             :      */
      87             :     float m_MinimumBarSize;
      88             : 
      89             :     /**
      90             :      * Sometimes you would like your scroll bar to have a fixed maximum size
      91             :      * so that the texture does not get too stretched, you can set a maximum
      92             :      * in pixels.
      93             :      */
      94             :     float m_MaximumBarSize;
      95             : 
      96             :     /**
      97             :      * True if you want edge buttons, i.e. buttons that can be pressed in order
      98             :      * to scroll.
      99             :      */
     100             :     bool m_UseEdgeButtons;
     101             : 
     102             :     //@}
     103             :     //--------------------------------------------------------
     104             :     /** @name Vertical Sprites */
     105             :     //--------------------------------------------------------
     106             :     //@{
     107             : 
     108             :     CGUISpriteInstance m_SpriteButtonTop;
     109             :     CGUISpriteInstance m_SpriteButtonTopPressed;
     110             :     CGUISpriteInstance m_SpriteButtonTopDisabled;
     111             :     CGUISpriteInstance m_SpriteButtonTopOver;
     112             : 
     113             :     CGUISpriteInstance m_SpriteButtonBottom;
     114             :     CGUISpriteInstance m_SpriteButtonBottomPressed;
     115             :     CGUISpriteInstance m_SpriteButtonBottomDisabled;
     116             :     CGUISpriteInstance m_SpriteButtonBottomOver;
     117             : 
     118             :     CGUISpriteInstance m_SpriteSliderVertical;
     119             :     CGUISpriteInstance m_SpriteSliderVerticalOver;
     120             :     CGUISpriteInstance m_SpriteSliderVerticalPressed;
     121             : 
     122             :     CGUISpriteInstance m_SpriteBackVertical;
     123             : 
     124             :     //@}
     125             :     //--------------------------------------------------------
     126             :     /** @name Horizontal Sprites */
     127             :     //--------------------------------------------------------
     128             :     //@{
     129             : 
     130             :     CGUISpriteInstance m_SpriteButtonLeft;
     131             :     CGUISpriteInstance m_SpriteButtonLeftPressed;
     132             :     CGUISpriteInstance m_SpriteButtonLeftDisabled;
     133             :     CGUISpriteInstance m_SpriteButtonLeftOver;
     134             : 
     135             :     CGUISpriteInstance m_SpriteButtonRight;
     136             :     CGUISpriteInstance m_SpriteButtonRightPressed;
     137             :     CGUISpriteInstance m_SpriteButtonRightDisabled;
     138             :     CGUISpriteInstance m_SpriteButtonRightOver;
     139             : 
     140             :     CGUISpriteInstance m_SpriteBackHorizontal;
     141             :     CGUISpriteInstance m_SpriteSliderHorizontal;
     142             :     CGUISpriteInstance m_SpriteSliderHorizontalOver;
     143             :     CGUISpriteInstance m_SpriteSliderHorizontalPressed;
     144             : 
     145             :     //@}
     146             : };
     147             : 
     148             : 
     149             : /**
     150             :  * The GUI Scroll-bar, used everywhere there is a scroll-bar in the game.
     151             :  *
     152             :  * To include a scroll-bar to an object, inherent the object from
     153             :  * IGUIScrollBarOwner and call AddScrollBar() to add the scroll-bars.
     154             :  *
     155             :  * It's also important that the scrollbar is located within the parent
     156             :  * object's mouse over area. Otherwise the input won't be sent to the
     157             :  * scroll-bar.
     158             :  *
     159             :  * The class does not provide all functionality to the scroll-bar, many
     160             :  * things the parent of the scroll-bar, must provide. Like a combo-box.
     161             :  */
     162             : class IGUIScrollBar
     163             : {
     164             : public:
     165             :     NONCOPYABLE(IGUIScrollBar);
     166             : 
     167             :     IGUIScrollBar(CGUI& pGUI);
     168             :     virtual ~IGUIScrollBar();
     169             : 
     170             : public:
     171             :     /**
     172             :      * Draw the scroll-bar
     173             :      */
     174             :     virtual void Draw(CCanvas2D& canvas) = 0;
     175             : 
     176             :     /**
     177             :      * If an object that contains a scrollbar has got messages, send
     178             :      * them to the scroll-bar and it will see if the message regarded
     179             :      * itself.
     180             :      *
     181             :      * @see IGUIObject#HandleMessage()
     182             :      */
     183             :     virtual void HandleMessage(SGUIMessage& Message) = 0;
     184             : 
     185             :     /**
     186             :      * Set m_Pos with g_mouse_x/y input, i.e. when draggin.
     187             :      */
     188             :     virtual void SetPosFromMousePos(const CVector2D& mouse) = 0;
     189             : 
     190             :     virtual void SetScrollPlentyFromMousePos(const CVector2D& mouse) = 0;
     191             : 
     192             :     /**
     193             :      * Hovering the scroll minus button
     194             :      *
     195             :      * @param mouse current mouse position
     196             :      * @return True if mouse positions are hovering the button
     197             :      */
     198           0 :     virtual bool HoveringButtonMinus(const CVector2D& /*mouse*/) { return false; }
     199             : 
     200             :     /**
     201             :      * Hovering the scroll plus button
     202             :      *
     203             :      * @param mouse current mouse position
     204             :      * @return True if mouse positions are hovering the button
     205             :      */
     206           0 :     virtual bool HoveringButtonPlus(const CVector2D& /*mouse*/) { return false; }
     207             : 
     208             :     /**
     209             :      * Get scroll-position
     210             :      */
     211           0 :     float GetPos() const { return m_Pos; }
     212             : 
     213             :     /**
     214             :      * Set scroll-position by hand
     215             :      */
     216           0 :     virtual void SetPos(float f) { m_Pos = f; UpdatePosBoundaries(); }
     217             : 
     218             :     /**
     219             :      * Get the value of m_Pos that corresponds to the bottom of the scrollable region
     220             :      */
     221           0 :     float GetMaxPos() const { return std::max(0.f, m_ScrollRange - m_ScrollSpace); }
     222             : 
     223             :     /**
     224             :      * Whether to draw the scrollbar.
     225             :      * If the max pos is less than 1px, scrolling might not actually displace the content if the renderer rounds
     226             :      * positions. Only show it if scrolling is guaranteed to have a visible effect.
     227             :      */
     228           0 :     bool IsVisible() const { return GetMaxPos() >= 1.0f; }
     229             : 
     230             :     /**
     231             :      * Increase scroll one step
     232             :      */
     233           0 :     virtual void ScrollPlus() { m_Pos += 30.f; UpdatePosBoundaries(); }
     234             : 
     235             :     /**
     236             :      * Decrease scroll one step
     237             :      */
     238           0 :     virtual void ScrollMinus() { m_Pos -= 30.f; UpdatePosBoundaries(); }
     239             : 
     240             :     /**
     241             :      * Increase scroll three steps
     242             :      */
     243           0 :     virtual void ScrollPlusPlenty() { m_Pos += 90.f; UpdatePosBoundaries(); }
     244             : 
     245             :     /**
     246             :      * Decrease scroll three steps
     247             :      */
     248           0 :     virtual void ScrollMinusPlenty() { m_Pos -= 90.f; UpdatePosBoundaries(); }
     249             : 
     250             :     /**
     251             :      * Set host object, must be done almost at creation of scroll bar.
     252             :      * @param pOwner Pointer to host object.
     253             :      */
     254           0 :     void SetHostObject(IGUIScrollBarOwner* pOwner) { m_pHostObject = pOwner; }
     255             : 
     256             :     /**
     257             :      * Set Width
     258             :      * @param width Width
     259             :      */
     260             :     void SetWidth(float width) { m_Width = width; }
     261             : 
     262             :     /**
     263             :      * Set X Position
     264             :      * @param x Position in this axis
     265             :      */
     266           0 :     void SetX(float x) { m_X = x; }
     267             : 
     268             :     /**
     269             :      * Set Y Position
     270             :      * @param y Position in this axis
     271             :      */
     272           0 :     void SetY(float y) { m_Y = y; }
     273             : 
     274             :     /**
     275             :      * Set Z Position
     276             :      * @param z Position in this axis
     277             :      */
     278           0 :     void SetZ(float z) { m_Z = z; }
     279             : 
     280             :     /**
     281             :      * Set Length of scroll bar
     282             :      * @param length Length
     283             :      */
     284           0 :     void SetLength(float length) { m_Length = length; }
     285             : 
     286             :     /**
     287             :      * Set content length
     288             :      * @param range Maximum scrollable range
     289             :      */
     290           0 :     void SetScrollRange(float range) { m_ScrollRange = std::max(range, 1.f); SetupBarSize(); UpdatePosBoundaries(); }
     291             : 
     292             :     /**
     293             :      * Set space that is visible in the scrollable control.
     294             :      * @param space Visible area in the scrollable control.
     295             :      */
     296           0 :     void SetScrollSpace(float space) { m_ScrollSpace = space; SetupBarSize(); UpdatePosBoundaries(); }
     297             : 
     298             :     /**
     299             :      * Set bar pressed
     300             :      * @param b True if bar is pressed
     301             :      */
     302           0 :     void SetBarPressed(bool b) { m_BarPressed = b; }
     303             : 
     304             :     /**
     305             :      * Set Scroll bar style string
     306             :      * @param style String with scroll bar style reference name
     307             :      */
     308           0 :     void SetScrollBarStyle(const CStr& style) { m_ScrollBarStyle = style; }
     309             : 
     310             :     /**
     311             :      * Get style used by the scrollbar
     312             :      * @return Scroll bar style struct.
     313             :      */
     314             :     const SGUIScrollBarStyle* GetStyle() const;
     315             : 
     316             :     /**
     317             :      * Get the rectangle of the actual BAR. not the whole scroll-bar.
     318             :      * @return Rectangle, CRect
     319             :      */
     320             :     virtual CRect GetBarRect() const = 0;
     321             : 
     322             :     /**
     323             :      * Get the rectangle of the outline of the scrollbar, every component of the
     324             :      * scroll-bar should be inside this area.
     325             :      * @return Rectangle, CRect
     326             :      */
     327             :     virtual CRect GetOuterRect() const = 0;
     328             : 
     329             : protected:
     330             :     /**
     331             :      * Sets up bar size
     332             :      */
     333             :     void SetupBarSize();
     334             : 
     335             :     /**
     336             :      * Call every time m_Pos has been updated.
     337             :      */
     338             :     void UpdatePosBoundaries();
     339             : 
     340             : protected:
     341             :     //@}
     342             :     //--------------------------------------------------------
     343             :     /** @name Settings */
     344             :     //--------------------------------------------------------
     345             :     //@{
     346             : 
     347             :     /**
     348             :      * Width of the scroll bar
     349             :      */
     350             :     float m_Width;
     351             : 
     352             :     /**
     353             :      * Absolute X Position
     354             :      */
     355             :     float m_X;
     356             : 
     357             :     /**
     358             :      * Absolute Y Position
     359             :      */
     360             :     float m_Y;
     361             : 
     362             :     /**
     363             :      * Absolute Z Position
     364             :      */
     365             :     float m_Z;
     366             : 
     367             :     /**
     368             :      * Total length of scrollbar, including edge buttons.
     369             :      */
     370             :     float m_Length;
     371             : 
     372             :     /**
     373             :      * Content that can be scrolled, in pixels
     374             :      */
     375             :     float m_ScrollRange;
     376             : 
     377             :     /**
     378             :      * Content that can be viewed at a time, in pixels
     379             :      */
     380             :     float m_ScrollSpace;
     381             : 
     382             :     /**
     383             :      * Use input from the scroll-wheel? True or false.
     384             :      */
     385             :     float m_BarSize;
     386             : 
     387             :     /**
     388             :      * Scroll bar style reference name
     389             :      */
     390             :     CStr m_ScrollBarStyle;
     391             : 
     392             :     /**
     393             :      * Pointer to scroll bar style used.
     394             :      */
     395             :     SGUIScrollBarStyle* m_pStyle;
     396             : 
     397             :     /**
     398             :      * Host object, prerequisite!
     399             :      */
     400             :     IGUIScrollBarOwner* m_pHostObject;
     401             : 
     402             :     /**
     403             :      * Reference to CGUI object, these cannot work stand-alone
     404             :      */
     405             :     CGUI& m_pGUI;
     406             : 
     407             :     /**
     408             :      * Mouse position when bar was pressed
     409             :      */
     410             :     CVector2D m_BarPressedAtPos;
     411             : 
     412             :     //@}
     413             :     //--------------------------------------------------------
     414             :     /** @name States */
     415             :     //--------------------------------------------------------
     416             :     //@{
     417             : 
     418             :     /**
     419             :      * If the bar is currently being pressed and dragged.
     420             :      */
     421             :     bool m_BarPressed;
     422             : 
     423             :     /**
     424             :      * Bar being hovered or not
     425             :      */
     426             :     bool m_BarHovered;
     427             : 
     428             :     /**
     429             :      * Scroll buttons hovered
     430             :      */
     431             :     bool m_ButtonMinusHovered, m_ButtonPlusHovered;
     432             : 
     433             :     /**
     434             :      * Scroll buttons pressed
     435             :      */
     436             :     bool m_ButtonMinusPressed, m_ButtonPlusPressed;
     437             : 
     438             :     /**
     439             :      * Position of scroll bar, 0 means scrolled all the way to one side.
     440             :      * It is measured in pixels, it is up to the host to make it actually
     441             :      * apply in pixels.
     442             :      */
     443             :     float m_Pos;
     444             : 
     445             :     /**
     446             :      * Position from 0.f to 1.f it had when the bar was pressed.
     447             :      */
     448             :     float m_PosWhenPressed;
     449             : 
     450             :     //@}
     451             : };
     452             : 
     453             : #endif // INCLUDED_IGUISCROLLBAR

Generated by: LCOV version 1.16