Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IGUIScrollBar.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009 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 IGUIScrollBar
20 */
21 
22 #include "precompiled.h"
23 #include "GUI.h"
24 #include "maths/MathUtil.h"
25 #include "ps/CLogger.h"
26 
27 //-------------------------------------------------------------------
28 // IGUIScrollBar
29 //-------------------------------------------------------------------
30 IGUIScrollBar::IGUIScrollBar() : m_pStyle(NULL), m_pGUI(NULL),
31  m_X(300.f), m_Y(300.f),
32  m_ScrollRange(1.f), m_ScrollSpace(0.f), // MaxPos: not 0, due to division.
33  m_Length(200.f), m_Width(20.f),
34  m_BarSize(0.f), m_Pos(0.f),
35  m_UseEdgeButtons(true),
36  m_ButtonPlusPressed(false),
37  m_ButtonMinusPressed(false),
38  m_ButtonPlusHovered(false),
39  m_ButtonMinusHovered(false),
40  m_BarHovered(false),
41  m_BarPressed(false)
42 {
43 }
44 
46 {
47 }
48 
50 {
51  float min = GetStyle()->m_MinimumBarSize;
52  float max = GetStyle()->m_MaximumBarSize;
53  float length = m_Length;
54 
55  // Check for edge buttons
56  if (m_UseEdgeButtons)
57  length -= GetStyle()->m_Width * 2.f;
58 
59  // Check min and max are valid
60  if (min > length)
61  {
62  LOGWARNING(L"Minimum scrollbar size of %g is larger than the total scrollbar size of %g", min, length);
63  min = 0.f;
64  }
65  if (max < min)
66  max = length;
67 
68  // Clamp size to not exceed a minimum or maximum.
69  m_BarSize = clamp(length * std::min((float)m_ScrollSpace / (float)m_ScrollRange, 1.f), min, max);
70 }
71 
73 {
74  if (!m_pHostObject)
75  return NULL;
76 
78 }
79 
81 {
82  if (!m_pHostObject)
83  return NULL;
84 
85  return m_pHostObject->GetGUI();
86 }
87 
89 {
90  if (m_Pos < 0.f ||
91  m_ScrollRange < m_ScrollSpace) // <= scrolling not applicable
92  m_Pos = 0.f;
93  else
94  if (m_Pos > GetMaxPos())
95  m_Pos = GetMaxPos();
96 }
97 
99 {
100  switch (Message.type)
101  {
102  case GUIM_MOUSE_MOTION:
103  {
104  // TODO Gee: Optimizations needed!
105 
106  CPos mouse = m_pHostObject->GetMousePos();
107 
108  // If bar is being dragged
109  if (m_BarPressed)
110  {
111  SetPosFromMousePos(mouse);
113  }
114 
115  // check if components are being hovered
119 
121  m_ButtonMinusPressed = false;
122 
123  if (!m_ButtonPlusHovered)
124  m_ButtonPlusPressed = false;
125  } break;
126 
128  {
129  if (!m_pHostObject)
130  break;
131 
132  CPos mouse = m_pHostObject->GetMousePos();
133 
134  // if bar is pressed
135  if (GetBarRect().PointInside(mouse))
136  {
137  m_BarPressed = true;
138  m_BarPressedAtPos = mouse;
140  }
141  else
142  // if button-minus is pressed
144  {
145  m_ButtonMinusPressed = true;
146  ScrollMinus();
147  }
148  else
149  // if button-plus is pressed
151  {
152  m_ButtonPlusPressed = true;
153  ScrollPlus();
154  }
155  else
156  // Pressing the background of the bar, to scroll
157  // notice the if-sentence alone does not admit that,
158  // it must be after the above if/elses
159  {
160  if (GetOuterRect().PointInside(mouse))
161  {
162  // Scroll plus or minus a lot, this might change, it doesn't
163  // have to be fancy though.
164  if (mouse.y < GetBarRect().top)
166  else
168 
169  // Simulate mouse movement to see if bar now is hovered
171  HandleMessage(msg);
172  }
173  }
174  } break;
175 
177  m_ButtonMinusPressed = false;
178  m_ButtonPlusPressed = false;
179  break;
180 
181  default:
182  break;
183  }
184 }
virtual CRect GetBarRect() const =0
Get the rectangle of the actual BAR.
float m_MinimumBarSize
Sometimes there is a lot to scroll, but to prevent the scroll &quot;bar&quot; from being almost invisible (or u...
Definition: IGUIScrollBar.h:91
virtual void SetPosFromMousePos(const CPos &mouse)=0
Set m_Pos with g_mouse_x/y input, i.e.
virtual const SGUIScrollBarStyle * GetScrollBarStyle(const CStr &style) const
Interface for the m_ScrollBar to use.
float m_Width
Width of bar, also both sides of the edge buttons.
Definition: IGUIScrollBar.h:67
The GUI Scroll-bar style.
Definition: IGUIScrollBar.h:57
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 ...
bool m_ButtonMinusHovered
Scroll buttons hovered.
The main object that represents a whole GUI page.
Definition: CGUI.h:97
bool m_BarHovered
Bar being hovered or not.
IGUIScrollBarOwner * m_pHostObject
Host object, prerequisite!
virtual bool HoveringButtonMinus(const CPos &mouse)
Hovering the scroll minus button.
#define LOGWARNING
Definition: CLogger.h:34
CPos m_BarPressedAtPos
Mouse position when bar was pressed.
virtual void ScrollPlus()
Increase scroll one step.
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:98
float m_PosWhenPressed
Position from 0.f to 1.f it had when the bar was pressed.
bool m_ButtonPlusHovered
virtual void ScrollMinus()
Decrease scroll one step.
void SetupBarSize()
Sets up bar size.
void UpdatePosBoundaries()
Call every time m_Pos has been updated.
virtual bool HoveringButtonPlus(const CPos &mouse)
Hovering the scroll plus button.
bool m_ButtonPlusPressed
CGUI * GetGUI() const
Get GUI pointer.
float m_ScrollRange
Content that can be scrolled, in pixels.
float m_ScrollSpace
Content that can be viewed at a time, in pixels.
Made to represent screen positions and delta values.
Definition: Overlay.h:167
bool m_BarPressed
If the bar is currently being pressed and dragged.
float m_Pos
Position of scroll bar, 0 means scrolled all the way to one side.
CPos GetMousePos() const
Get Mouse from CGUI.
Definition: IGUIObject.cpp:207
bool m_ButtonMinusPressed
Scroll buttons pressed.
CGUI * GetGUI()
Definition: IGUIObject.h:388
virtual void ScrollMinusPlenty()
Decrease scroll three steps.
virtual ~IGUIScrollBar()
float y
Definition: Overlay.h:195
bool m_UseEdgeButtons
True if you want edge buttons, i.e.
virtual CRect GetOuterRect() const =0
Get the rectangle of the outline of the scrollbar, every component of the scroll-bar should be inside...
const SGUIScrollBarStyle * GetStyle() const
Get style used by the scrollbar.
virtual void ScrollPlusPlenty()
Increase scroll three steps.
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:106
EGUIMessageType type
Describes what the message regards.
Definition: GUIbase.h:122
float GetMaxPos() const
Get the value of m_Pos that corresponds to the bottom of the scrollable region.
bool PointInside(const CPos &point) const
Evalutates if point is within the rectangle.
Definition: Overlay.cpp:272
float m_BarSize
Use input from the scroll-wheel? True or false.
T clamp(T value, T min, T max)
Definition: MathUtil.h:32
CStr m_ScrollBarStyle
Scroll bar style reference name.
float m_Length
Total length of scrollbar, including edge buttons.