Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
IGUIScrollBar.h
Go to the documentation of this file.
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
34class CCanvas2D;
35class CGUI;
36class CRect;
38struct 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 */
49{
50 // CGUISpriteInstance makes this NONCOPYABLE implicitly, make it explicit
53 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 */
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 */
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 */
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 */
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 */
95
96 /**
97 * True if you want edge buttons, i.e. buttons that can be pressed in order
98 * to scroll.
99 */
101
102 //@}
103 //--------------------------------------------------------
104 /** @name Vertical Sprites */
105 //--------------------------------------------------------
106 //@{
107
112
117
121
123
124 //@}
125 //--------------------------------------------------------
126 /** @name Horizontal Sprites */
127 //--------------------------------------------------------
128 //@{
129
134
139
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 */
163{
164public:
166
167 IGUIScrollBar(CGUI& pGUI);
168 virtual ~IGUIScrollBar();
169
170public:
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 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 virtual bool HoveringButtonPlus(const CVector2D& /*mouse*/) { return false; }
207
208 /**
209 * Get scroll-position
210 */
211 float GetPos() const { return m_Pos; }
212
213 /**
214 * Set scroll-position by hand
215 */
216 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 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 bool IsVisible() const { return GetMaxPos() >= 1.0f; }
229
230 /**
231 * Increase scroll one step
232 */
233 virtual void ScrollPlus() { m_Pos += 30.f; UpdatePosBoundaries(); }
234
235 /**
236 * Decrease scroll one step
237 */
238 virtual void ScrollMinus() { m_Pos -= 30.f; UpdatePosBoundaries(); }
239
240 /**
241 * Increase scroll three steps
242 */
243 virtual void ScrollPlusPlenty() { m_Pos += 90.f; UpdatePosBoundaries(); }
244
245 /**
246 * Decrease scroll three steps
247 */
248 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 */
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 void SetX(float x) { m_X = x; }
267
268 /**
269 * Set Y Position
270 * @param y Position in this axis
271 */
272 void SetY(float y) { m_Y = y; }
273
274 /**
275 * Set Z Position
276 * @param z Position in this axis
277 */
278 void SetZ(float z) { m_Z = z; }
279
280 /**
281 * Set Length of scroll bar
282 * @param length Length
283 */
284 void SetLength(float length) { m_Length = length; }
285
286 /**
287 * Set content length
288 * @param range Maximum scrollable range
289 */
290 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 */
297
298 /**
299 * Set bar pressed
300 * @param b True if bar is pressed
301 */
302 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 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
329protected:
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
340protected:
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 */
376
377 /**
378 * Content that can be viewed at a time, in pixels
379 */
381
382 /**
383 * Use input from the scroll-wheel? True or false.
384 */
386
387 /**
388 * Scroll bar style reference name
389 */
391
392 /**
393 * Pointer to scroll bar style used.
394 */
396
397 /**
398 * Host object, prerequisite!
399 */
401
402 /**
403 * Reference to CGUI object, these cannot work stand-alone
404 */
406
407 /**
408 * Mouse position when bar was pressed
409 */
411
412 //@}
413 //--------------------------------------------------------
414 /** @name States */
415 //--------------------------------------------------------
416 //@{
417
418 /**
419 * If the bar is currently being pressed and dragged.
420 */
422
423 /**
424 * Bar being hovered or not
425 */
427
428 /**
429 * Scroll buttons hovered
430 */
432
433 /**
434 * Scroll buttons pressed
435 */
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 */
449
450 //@}
451};
452
453#endif // INCLUDED_IGUISCROLLBAR
Definition: Canvas2D.h:36
Definition: CGUISprite.h:137
The main object that represents a whole GUI page.
Definition: CGUI.h:80
Rectangle class used for screen rectangles.
Definition: Rect.h:31
Definition: Vector2D.h:32
Base-class this if you want an object to contain one, or several, scroll-bars.
Definition: IGUIScrollBarOwner.h:38
The GUI Scroll-bar, used everywhere there is a scroll-bar in the game.
Definition: IGUIScrollBar.h:163
void SetY(float y)
Set Y Position.
Definition: IGUIScrollBar.h:272
float m_BarSize
Use input from the scroll-wheel? True or false.
Definition: IGUIScrollBar.h:385
virtual CRect GetOuterRect() const =0
Get the rectangle of the outline of the scrollbar, every component of the scroll-bar should be inside...
bool m_ButtonMinusPressed
Scroll buttons pressed.
Definition: IGUIScrollBar.h:436
virtual ~IGUIScrollBar()
Definition: IGUIScrollBar.cpp:44
bool m_BarPressed
If the bar is currently being pressed and dragged.
Definition: IGUIScrollBar.h:421
virtual void Draw(CCanvas2D &canvas)=0
Draw the scroll-bar.
virtual void HandleMessage(SGUIMessage &Message)=0
If an object that contains a scrollbar has got messages, send them to the scroll-bar and it will see ...
Definition: IGUIScrollBar.cpp:88
float m_Y
Absolute Y Position.
Definition: IGUIScrollBar.h:360
bool m_ButtonMinusHovered
Scroll buttons hovered.
Definition: IGUIScrollBar.h:431
void SetLength(float length)
Set Length of scroll bar.
Definition: IGUIScrollBar.h:284
virtual void ScrollMinusPlenty()
Decrease scroll three steps.
Definition: IGUIScrollBar.h:248
SGUIScrollBarStyle * m_pStyle
Pointer to scroll bar style used.
Definition: IGUIScrollBar.h:395
virtual bool HoveringButtonPlus(const CVector2D &)
Hovering the scroll plus button.
Definition: IGUIScrollBar.h:206
void SetBarPressed(bool b)
Set bar pressed.
Definition: IGUIScrollBar.h:302
bool IsVisible() const
Whether to draw the scrollbar.
Definition: IGUIScrollBar.h:228
virtual void ScrollPlus()
Increase scroll one step.
Definition: IGUIScrollBar.h:233
float m_Z
Absolute Z Position.
Definition: IGUIScrollBar.h:365
bool m_ButtonPlusPressed
Definition: IGUIScrollBar.h:436
virtual CRect GetBarRect() const =0
Get the rectangle of the actual BAR.
virtual void SetPosFromMousePos(const CVector2D &mouse)=0
Set m_Pos with g_mouse_x/y input, i.e.
float GetMaxPos() const
Get the value of m_Pos that corresponds to the bottom of the scrollable region.
Definition: IGUIScrollBar.h:221
void UpdatePosBoundaries()
Call every time m_Pos has been updated.
Definition: IGUIScrollBar.cpp:79
NONCOPYABLE(IGUIScrollBar)
void SetScrollRange(float range)
Set content length.
Definition: IGUIScrollBar.h:290
CVector2D m_BarPressedAtPos
Mouse position when bar was pressed.
Definition: IGUIScrollBar.h:410
void SetScrollSpace(float space)
Set space that is visible in the scrollable control.
Definition: IGUIScrollBar.h:296
bool m_ButtonPlusHovered
Definition: IGUIScrollBar.h:431
void SetWidth(float width)
Set Width.
Definition: IGUIScrollBar.h:260
float m_ScrollSpace
Content that can be viewed at a time, in pixels.
Definition: IGUIScrollBar.h:380
CGUI & m_pGUI
Reference to CGUI object, these cannot work stand-alone.
Definition: IGUIScrollBar.h:405
IGUIScrollBarOwner * m_pHostObject
Host object, prerequisite!
Definition: IGUIScrollBar.h:400
float m_Length
Total length of scrollbar, including edge buttons.
Definition: IGUIScrollBar.h:370
float m_Pos
Position of scroll bar, 0 means scrolled all the way to one side.
Definition: IGUIScrollBar.h:443
const SGUIScrollBarStyle * GetStyle() const
Get style used by the scrollbar.
Definition: IGUIScrollBar.cpp:71
CStr m_ScrollBarStyle
Scroll bar style reference name.
Definition: IGUIScrollBar.h:390
virtual void SetPos(float f)
Set scroll-position by hand.
Definition: IGUIScrollBar.h:216
float m_ScrollRange
Content that can be scrolled, in pixels.
Definition: IGUIScrollBar.h:375
bool m_BarHovered
Bar being hovered or not.
Definition: IGUIScrollBar.h:426
IGUIScrollBar(CGUI &pGUI)
Definition: IGUIScrollBar.cpp:28
void SetScrollBarStyle(const CStr &style)
Set Scroll bar style string.
Definition: IGUIScrollBar.h:308
float m_PosWhenPressed
Position from 0.f to 1.f it had when the bar was pressed.
Definition: IGUIScrollBar.h:448
float m_Width
Width of the scroll bar.
Definition: IGUIScrollBar.h:350
float GetPos() const
Get scroll-position.
Definition: IGUIScrollBar.h:211
virtual void ScrollPlusPlenty()
Increase scroll three steps.
Definition: IGUIScrollBar.h:243
virtual bool HoveringButtonMinus(const CVector2D &)
Hovering the scroll minus button.
Definition: IGUIScrollBar.h:198
virtual void ScrollMinus()
Decrease scroll one step.
Definition: IGUIScrollBar.h:238
void SetZ(float z)
Set Z Position.
Definition: IGUIScrollBar.h:278
void SetHostObject(IGUIScrollBarOwner *pOwner)
Set host object, must be done almost at creation of scroll bar.
Definition: IGUIScrollBar.h:254
void SetupBarSize()
Sets up bar size.
Definition: IGUIScrollBar.cpp:48
void SetX(float x)
Set X Position.
Definition: IGUIScrollBar.h:266
virtual void SetScrollPlentyFromMousePos(const CVector2D &mouse)=0
float m_X
Absolute X Position.
Definition: IGUIScrollBar.h:355
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: SGUIMessage.h:73
The GUI Scroll-bar style.
Definition: IGUIScrollBar.h:49
CGUISpriteInstance m_SpriteButtonRight
Definition: IGUIScrollBar.h:135
CGUISpriteInstance m_SpriteSliderHorizontalOver
Definition: IGUIScrollBar.h:142
CGUISpriteInstance m_SpriteButtonTopPressed
Definition: IGUIScrollBar.h:109
CGUISpriteInstance m_SpriteButtonRightOver
Definition: IGUIScrollBar.h:138
bool m_ScrollButtons
Whether or not the edge buttons should appear or not.
Definition: IGUIScrollBar.h:80
CGUISpriteInstance m_SpriteButtonBottomDisabled
Definition: IGUIScrollBar.h:115
bool m_UseEdgeButtons
True if you want edge buttons, i.e.
Definition: IGUIScrollBar.h:100
CGUISpriteInstance m_SpriteButtonTopDisabled
Definition: IGUIScrollBar.h:110
CGUISpriteInstance m_SpriteButtonBottom
Definition: IGUIScrollBar.h:113
CGUISpriteInstance m_SpriteBackVertical
Definition: IGUIScrollBar.h:122
CGUISpriteInstance m_SpriteSliderVertical
Definition: IGUIScrollBar.h:118
MOVABLE(SGUIScrollBarStyle)
float m_MinimumBarSize
Sometimes there is a lot to scroll, but to prevent the scroll "bar" from being almost invisible (or u...
Definition: IGUIScrollBar.h:87
CGUISpriteInstance m_SpriteSliderHorizontal
Definition: IGUIScrollBar.h:141
CGUISpriteInstance m_SpriteSliderHorizontalPressed
Definition: IGUIScrollBar.h:143
CGUISpriteInstance m_SpriteSliderVerticalOver
Definition: IGUIScrollBar.h:119
CGUISpriteInstance m_SpriteSliderVerticalPressed
Definition: IGUIScrollBar.h:120
CGUISpriteInstance m_SpriteButtonLeftOver
Definition: IGUIScrollBar.h:133
CGUISpriteInstance m_SpriteButtonLeftPressed
Definition: IGUIScrollBar.h:131
SGUIScrollBarStyle()=default
float m_MaximumBarSize
Sometimes you would like your scroll bar to have a fixed maximum size so that the texture does not ge...
Definition: IGUIScrollBar.h:94
CGUISpriteInstance m_SpriteButtonLeftDisabled
Definition: IGUIScrollBar.h:132
CGUISpriteInstance m_SpriteBackHorizontal
Definition: IGUIScrollBar.h:140
CGUISpriteInstance m_SpriteButtonRightDisabled
Definition: IGUIScrollBar.h:137
CGUISpriteInstance m_SpriteButtonLeft
Definition: IGUIScrollBar.h:130
CGUISpriteInstance m_SpriteButtonBottomOver
Definition: IGUIScrollBar.h:116
bool m_ScrollWheel
Scrollable with the wheel.
Definition: IGUIScrollBar.h:68
NONCOPYABLE(SGUIScrollBarStyle)
float m_Width
Width of bar, also both sides of the edge buttons.
Definition: IGUIScrollBar.h:63
CGUISpriteInstance m_SpriteButtonBottomPressed
Definition: IGUIScrollBar.h:114
CGUISpriteInstance m_SpriteButtonRightPressed
Definition: IGUIScrollBar.h:136
CGUISpriteInstance m_SpriteButtonTop
Definition: IGUIScrollBar.h:108
float m_ScrollSpeed
How much (in percent, 0.1f = 10%) to scroll each time the wheel is admitted, or the buttons are press...
Definition: IGUIScrollBar.h:74
CGUISpriteInstance m_SpriteButtonTopOver
Definition: IGUIScrollBar.h:111