Don't crash while in the lobby when receiving an error IQ stanza without an error...
[0ad.git] / source / gui / IGUIScrollBar.cpp
blobb3fdcdc83cf705113de559bdac6d6a88b2afa347
1 /* Copyright (C) 2015 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
20 #include "GUI.h"
21 #include "maths/MathUtil.h"
23 IGUIScrollBar::IGUIScrollBar() : m_pStyle(NULL), m_pGUI(NULL),
24 m_X(300.f), m_Y(300.f),
25 m_ScrollRange(1.f), m_ScrollSpace(0.f), // MaxPos: not 0, due to division.
26 m_Length(200.f), m_Width(20.f),
27 m_BarSize(0.f), m_Pos(0.f),
28 m_ButtonPlusPressed(false),
29 m_ButtonMinusPressed(false),
30 m_ButtonPlusHovered(false),
31 m_ButtonMinusHovered(false),
32 m_BarHovered(false),
33 m_BarPressed(false)
37 IGUIScrollBar::~IGUIScrollBar()
41 void IGUIScrollBar::SetupBarSize()
43 if (!GetStyle())
44 return;
46 float min = GetStyle()->m_MinimumBarSize;
47 float max = GetStyle()->m_MaximumBarSize;
48 float length = m_Length;
50 // Check for edge buttons
51 if (GetStyle()->m_UseEdgeButtons)
52 length -= GetStyle()->m_Width * 2.f;
54 // Check min and max are valid
55 if (min > length)
56 min = 0.f;
57 if (max < min)
58 max = length;
60 // Clamp size to not exceed a minimum or maximum.
61 m_BarSize = clamp(length * std::min((float)m_ScrollSpace / (float)m_ScrollRange, 1.f), min, max);
64 const SGUIScrollBarStyle* IGUIScrollBar::GetStyle() const
66 if (!m_pHostObject)
67 return NULL;
69 return m_pHostObject->GetScrollBarStyle(m_ScrollBarStyle);
72 CGUI* IGUIScrollBar::GetGUI() const
74 if (!m_pHostObject)
75 return NULL;
77 return m_pHostObject->GetGUI();
80 void IGUIScrollBar::UpdatePosBoundaries()
82 if (m_Pos < 0.f ||
83 m_ScrollRange < m_ScrollSpace) // <= scrolling not applicable
84 m_Pos = 0.f;
85 else if (m_Pos > GetMaxPos())
86 m_Pos = GetMaxPos();
89 void IGUIScrollBar::HandleMessage(SGUIMessage& Message)
91 switch (Message.type)
93 case GUIM_MOUSE_MOTION:
95 // TODO Gee: Optimizations needed!
97 CPos mouse = m_pHostObject->GetMousePos();
99 // If bar is being dragged
100 if (m_BarPressed)
102 SetPosFromMousePos(mouse);
103 UpdatePosBoundaries();
106 // check if components are being hovered
107 m_BarHovered = GetBarRect().PointInside(mouse);
108 m_ButtonMinusHovered = HoveringButtonMinus(mouse);
109 m_ButtonPlusHovered = HoveringButtonPlus(mouse);
111 if (!m_ButtonMinusHovered)
112 m_ButtonMinusPressed = false;
114 if (!m_ButtonPlusHovered)
115 m_ButtonPlusPressed = false;
116 break;
119 case GUIM_MOUSE_PRESS_LEFT:
121 if (!m_pHostObject)
122 break;
124 CPos mouse = m_pHostObject->GetMousePos();
126 // if bar is pressed
127 if (GetBarRect().PointInside(mouse))
129 m_BarPressed = true;
130 m_BarPressedAtPos = mouse;
131 m_PosWhenPressed = m_Pos;
133 // if button-minus is pressed
134 else if (m_ButtonMinusHovered)
136 m_ButtonMinusPressed = true;
137 ScrollMinus();
139 // if button-plus is pressed
140 else if (m_ButtonPlusHovered)
142 m_ButtonPlusPressed = true;
143 ScrollPlus();
145 // Pressing the background of the bar, to scroll
146 // notice the if-sentence alone does not admit that,
147 // it must be after the above if/elses
148 else
150 if (GetOuterRect().PointInside(mouse))
152 // Scroll plus or minus a lot, this might change, it doesn't
153 // have to be fancy though.
154 if (mouse.y < GetBarRect().top)
155 ScrollMinusPlenty();
156 else
157 ScrollPlusPlenty();
158 // Simulate mouse movement to see if bar now is hovered
159 SGUIMessage msg(GUIM_MOUSE_MOTION);
160 HandleMessage(msg);
163 break;
166 case GUIM_MOUSE_RELEASE_LEFT:
167 m_ButtonMinusPressed = false;
168 m_ButtonPlusPressed = false;
169 break;
171 case GUIM_MOUSE_WHEEL_UP:
173 ScrollMinus();
174 // Since the scroll was changed, let's simulate a mouse movement
175 // to check if scrollbar now is hovered
176 SGUIMessage msg(GUIM_MOUSE_MOTION);
177 HandleMessage(msg);
178 break;
181 case GUIM_MOUSE_WHEEL_DOWN:
183 ScrollPlus();
184 // Since the scroll was changed, let's simulate a mouse movement
185 // to check if scrollbar now is hovered
186 SGUIMessage msg(GUIM_MOUSE_MOTION);
187 HandleMessage(msg);
188 break;
191 default:
192 break;