Don't crash while in the lobby when receiving an error IQ stanza without an error...
[0ad.git] / source / gui / CGUIScrollBarVertical.cpp
blobf10e684f2ae5ccb704d48ffc350c66dd881fee7e
1 /* Copyright (C) 2016 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 "CGUIScrollBarVertical.h"
22 #include "GUI.h"
24 #include "ps/CLogger.h"
27 CGUIScrollBarVertical::CGUIScrollBarVertical()
31 CGUIScrollBarVertical::~CGUIScrollBarVertical()
35 void CGUIScrollBarVertical::SetPosFromMousePos(const CPos& mouse)
37 if (!GetStyle())
38 return;
40 /**
41 * Calculate the position for the top of the item being scrolled
43 float emptyBackground = m_Length - m_BarSize;
45 if (GetStyle()->m_UseEdgeButtons)
46 emptyBackground -= GetStyle()->m_Width * 2;
48 m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.y - m_BarPressedAtPos.y) / emptyBackground;
51 void CGUIScrollBarVertical::Draw()
53 if (!GetStyle())
55 LOGWARNING("Attempt to draw scrollbar without a style.");
56 return;
59 if (GetGUI() && IsVisible())
61 CRect outline = GetOuterRect();
63 GetGUI()->DrawSprite(
64 GetStyle()->m_SpriteBackVertical,
66 m_Z+0.1f,
67 CRect(
68 outline.left,
69 outline.top + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0),
70 outline.right,
71 outline.bottom - (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0)
75 if (GetStyle()->m_UseEdgeButtons)
77 const CGUISpriteInstance* button_top;
78 const CGUISpriteInstance* button_bottom;
80 if (m_ButtonMinusHovered)
82 if (m_ButtonMinusPressed)
83 button_top = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonTopPressed, GetStyle()->m_SpriteButtonTop);
84 else
85 button_top = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonTopOver, GetStyle()->m_SpriteButtonTop);
87 else
88 button_top = &GetStyle()->m_SpriteButtonTop;
90 if (m_ButtonPlusHovered)
92 if (m_ButtonPlusPressed)
93 button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomPressed, GetStyle()->m_SpriteButtonBottom);
94 else
95 button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomOver, GetStyle()->m_SpriteButtonBottom);
97 else
98 button_bottom = &GetStyle()->m_SpriteButtonBottom;
100 GetGUI()->DrawSprite(
101 *button_top,
103 m_Z+0.2f,
104 CRect(
105 outline.left,
106 outline.top,
107 outline.right,
108 outline.top+GetStyle()->m_Width
112 GetGUI()->DrawSprite(
113 *button_bottom,
115 m_Z+0.2f,
116 CRect(
117 outline.left,
118 outline.bottom-GetStyle()->m_Width,
119 outline.right,
120 outline.bottom
125 GetGUI()->DrawSprite(
126 GetStyle()->m_SpriteBarVertical,
128 m_Z + 0.2f,
129 GetBarRect()
134 void CGUIScrollBarVertical::HandleMessage(SGUIMessage& Message)
136 IGUIScrollBar::HandleMessage(Message);
139 CRect CGUIScrollBarVertical::GetBarRect() const
141 CRect ret;
142 if (!GetStyle())
143 return ret;
145 // Get from where the scroll area begins to where it ends
146 float from = m_Y;
147 float to = m_Y + m_Length - m_BarSize;
149 if (GetStyle()->m_UseEdgeButtons)
151 from += GetStyle()->m_Width;
152 to -= GetStyle()->m_Width;
155 ret.top = from + (to - from) * m_Pos / GetMaxPos();
156 ret.bottom = ret.top + m_BarSize;
157 ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width);
158 ret.left = ret.right - GetStyle()->m_Width;
160 return ret;
163 CRect CGUIScrollBarVertical::GetOuterRect() const
165 CRect ret;
166 if (!GetStyle())
167 return ret;
169 ret.top = m_Y;
170 ret.bottom = m_Y+m_Length;
171 ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width);
172 ret.left = ret.right - GetStyle()->m_Width;
174 return ret;
177 bool CGUIScrollBarVertical::HoveringButtonMinus(const CPos& mouse)
179 if (!GetStyle())
180 return false;
182 float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X;
184 return mouse.x >= StartX &&
185 mouse.x <= StartX + GetStyle()->m_Width &&
186 mouse.y >= m_Y &&
187 mouse.y <= m_Y + GetStyle()->m_Width;
190 bool CGUIScrollBarVertical::HoveringButtonPlus(const CPos& mouse)
192 if (!GetStyle())
193 return false;
195 float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X;
197 return mouse.x > StartX &&
198 mouse.x < StartX + GetStyle()->m_Width &&
199 mouse.y > m_Y + m_Length - GetStyle()->m_Width &&
200 mouse.y < m_Y + m_Length;