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"
24 #include "ps/CLogger.h"
27 CGUIScrollBarVertical::CGUIScrollBarVertical()
31 CGUIScrollBarVertical::~CGUIScrollBarVertical()
35 void CGUIScrollBarVertical::SetPosFromMousePos(const CPos
& mouse
)
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()
55 LOGWARNING("Attempt to draw scrollbar without a style.");
59 if (GetGUI() && IsVisible())
61 CRect outline
= GetOuterRect();
64 GetStyle()->m_SpriteBackVertical
,
69 outline
.top
+ (GetStyle()->m_UseEdgeButtons
? GetStyle()->m_Width
: 0),
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
);
85 button_top
= &GUI
<>::FallBackSprite(GetStyle()->m_SpriteButtonTopOver
, GetStyle()->m_SpriteButtonTop
);
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
);
95 button_bottom
= &GUI
<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomOver
, GetStyle()->m_SpriteButtonBottom
);
98 button_bottom
= &GetStyle()->m_SpriteButtonBottom
;
100 GetGUI()->DrawSprite(
108 outline
.top
+GetStyle()->m_Width
112 GetGUI()->DrawSprite(
118 outline
.bottom
-GetStyle()->m_Width
,
125 GetGUI()->DrawSprite(
126 GetStyle()->m_SpriteBarVertical
,
134 void CGUIScrollBarVertical::HandleMessage(SGUIMessage
& Message
)
136 IGUIScrollBar::HandleMessage(Message
);
139 CRect
CGUIScrollBarVertical::GetBarRect() const
145 // Get from where the scroll area begins to where it ends
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
;
163 CRect
CGUIScrollBarVertical::GetOuterRect() const
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
;
177 bool CGUIScrollBarVertical::HoveringButtonMinus(const CPos
& mouse
)
182 float StartX
= m_RightAligned
? m_X
-GetStyle()->m_Width
: m_X
;
184 return mouse
.x
>= StartX
&&
185 mouse
.x
<= StartX
+ GetStyle()->m_Width
&&
187 mouse
.y
<= m_Y
+ GetStyle()->m_Width
;
190 bool CGUIScrollBarVertical::HoveringButtonPlus(const CPos
& mouse
)
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
;