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