Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CGUIScrollBarVertical.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 "CGUIScrollBarVertical.h"
25 #include "ps/CLogger.h"
26 
27 
29 {
30 }
31 
33 {
34 }
35 
37 {
38  if (!GetStyle())
39  return;
40 
41  /**
42  * Calculate the position for the top of the item being scrolled
43  */
45 }
46 
48 {
49  if (!GetStyle())
50  {
51  LOGWARNING(L"Attempt to draw scrollbar without a style.");
52  return;
53  }
54 
55  // Only draw the scrollbar if the GUI exists and there is something to scroll.
56  if (GetGUI() && GetMaxPos() != 1)
57  {
58  CRect outline = GetOuterRect();
59 
60  // Draw background
61  GetGUI()->DrawSprite(GetStyle()->m_SpriteBackVertical,
62  0,
63  m_Z+0.1f,
64  CRect(outline.left,
65  outline.top+(m_UseEdgeButtons?GetStyle()->m_Width:0),
66  outline.right,
67  outline.bottom-(m_UseEdgeButtons?GetStyle()->m_Width:0))
68  );
69 
70  if (m_UseEdgeButtons)
71  {
72  // Get Appropriate sprites
73  const CGUISpriteInstance *button_top, *button_bottom;
74 
75  // figure out what sprite to use for top button
77  {
79  button_top = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonTopPressed, GetStyle()->m_SpriteButtonTop);
80  else
81  button_top = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonTopOver, GetStyle()->m_SpriteButtonTop);
82  }
83  else button_top = &GetStyle()->m_SpriteButtonTop;
84 
85  // figure out what sprite to use for bottom button
87  {
89  button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomPressed, GetStyle()->m_SpriteButtonBottom);
90  else
91  button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomOver, GetStyle()->m_SpriteButtonBottom);
92  }
93  else button_bottom = &GetStyle()->m_SpriteButtonBottom;
94 
95  // Draw top button
96  GetGUI()->DrawSprite(*button_top,
97  0,
98  m_Z+0.2f,
99  CRect(outline.left,
100  outline.top,
101  outline.right,
102  outline.top+GetStyle()->m_Width)
103  );
104 
105  // Draw bottom button
106  GetGUI()->DrawSprite(*button_bottom,
107  0,
108  m_Z+0.2f,
109  CRect(outline.left,
110  outline.bottom-GetStyle()->m_Width,
111  outline.right,
112  outline.bottom)
113  );
114  }
115 
116  // Draw bar
117  GetGUI()->DrawSprite(GetStyle()->m_SpriteBarVertical,
118  0,
119  m_Z + 0.2f,
120  GetBarRect());
121  }
122 }
123 
125 {
127 }
128 
130 {
131  CRect ret;
132  if (!GetStyle())
133  return ret;
134 
135  // Get from where the scroll area begins to where it ends
136  float from = m_Y;
137  float to = m_Y + m_Length - m_BarSize;
138 
139  if (m_UseEdgeButtons)
140  {
141  from += GetStyle()->m_Width;
142  to -= GetStyle()->m_Width;
143  }
144 
145  // Setup rectangle
146  ret.top = from + (to - from) * (m_Pos / GetMaxPos());
147  ret.bottom = ret.top + m_BarSize;
148  ret.right = m_X + ((m_RightAligned)?(0.f):(GetStyle()->m_Width));
149  ret.left = ret.right - GetStyle()->m_Width;
150 
151  return ret;
152 }
153 
155 {
156  CRect ret;
157  if (!GetStyle())
158  return ret;
159 
160  ret.top = m_Y;
161  ret.bottom = m_Y+m_Length;
162  ret.right = m_X + ((m_RightAligned)?(0):(GetStyle()->m_Width));
163  ret.left = ret.right - GetStyle()->m_Width;
164 
165  return ret;
166 }
167 
169 {
170  if (!GetStyle())
171  return false;
172 
173  float StartX = (m_RightAligned)?(m_X-GetStyle()->m_Width):(m_X);
174 
175  return (mouse.x >= StartX &&
176  mouse.x <= StartX + GetStyle()->m_Width &&
177  mouse.y >= m_Y &&
178  mouse.y <= m_Y + GetStyle()->m_Width);
179 }
180 
182 {
183  if (!GetStyle())
184  return false;
185 
186  float StartX = (m_RightAligned)?(m_X-GetStyle()->m_Width):(m_X);
187 
188  return (mouse.x > StartX &&
189  mouse.x < StartX + GetStyle()->m_Width &&
190  mouse.y > m_Y + m_Length - GetStyle()->m_Width &&
191  mouse.y < m_Y + m_Length);
192 }
void DrawSprite(const CGUISpriteInstance &Sprite, int CellID, const float &Z, const CRect &Rect, const CRect &Clipping=CRect())
Draw GUI Sprite.
Definition: CGUI.cpp:467
float top
Definition: Overlay.h:159
float m_X
Absolute X Position.
float m_Width
Width of bar, also both sides of the edge buttons.
Definition: IGUIScrollBar.h:67
float left
Returning CPos representing each corner.
Definition: Overlay.h:159
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 ...
virtual void HandleMessage(SGUIMessage &Message)
If an object that contains a scrollbar has got messages, send them to the scroll-bar and it will see ...
virtual CRect GetOuterRect() const
Get the rectangle of the outline of the scrollbar, every component of the scroll-bar should be inside...
static const CGUISpriteInstance & FallBackSprite(const CGUISpriteInstance &prim, const CGUISpriteInstance &sec)
This will return the value of the first sprite if it&#39;s not null, if it is null, it will return the va...
Definition: GUIutil.h:200
bool m_ButtonMinusHovered
Scroll buttons hovered.
CGUISpriteInstance m_SpriteButtonTop
#define LOGWARNING
Definition: CLogger.h:34
virtual void SetPosFromMousePos(const CPos &mouse)
Set m_Pos with g_mouse_x/y input, i.e.
CPos m_BarPressedAtPos
Mouse position when bar was pressed.
virtual void Draw()
Draw the scroll-bar.
float m_PosWhenPressed
Position from 0.f to 1.f it had when the bar was pressed.
bool m_ButtonPlusHovered
float m_Z
Absolute Z Position.
float m_Y
Absolute Y Position.
bool m_ButtonPlusPressed
CGUI * GetGUI() const
Get GUI pointer.
virtual CRect GetBarRect() const
Get the rectangle of the actual BAR.
Made to represent screen positions and delta values.
Definition: Overlay.h:167
virtual bool HoveringButtonMinus(const CPos &mouse)
float m_Pos
Position of scroll bar, 0 means scrolled all the way to one side.
float right
Definition: Overlay.h:159
bool m_ButtonMinusPressed
Scroll buttons pressed.
float y
Definition: Overlay.h:195
bool m_UseEdgeButtons
True if you want edge buttons, i.e.
const SGUIScrollBarStyle * GetStyle() const
Get style used by the scrollbar.
virtual bool HoveringButtonPlus(const CPos &mouse)
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:106
CGUISpriteInstance m_SpriteButtonBottom
float bottom
Definition: Overlay.h:159
float x
Position.
Definition: Overlay.h:195
float GetMaxPos() const
Get the value of m_Pos that corresponds to the bottom of the scrollable region.
bool m_RightAligned
Should the scroll bar proceed to the left or to the right of the m_X value.
float m_BarSize
Use input from the scroll-wheel? True or false.
float m_Width
Width of the scroll bar.
Rectangle class used for screen rectangles.
Definition: Overlay.h:71
float m_Length
Total length of scrollbar, including edge buttons.