1 /* Copyright (C) 2021 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"
23 #include "ps/CLogger.h"
25 CGUIScrollBarVertical::CGUIScrollBarVertical(CGUI
& pGUI
)
30 CGUIScrollBarVertical::~CGUIScrollBarVertical()
34 void CGUIScrollBarVertical::SetPosFromMousePos(const CVector2D
& mouse
)
40 * Calculate the position for the top of the item being scrolled
42 float emptyBackground
= m_Length
- m_BarSize
;
44 if (GetStyle()->m_UseEdgeButtons
)
45 emptyBackground
-= GetStyle()->m_Width
* 2;
47 m_Pos
= m_PosWhenPressed
+ GetMaxPos() * (mouse
.Y
- m_BarPressedAtPos
.Y
) / emptyBackground
;
50 void CGUIScrollBarVertical::Draw(CCanvas2D
& canvas
)
54 LOGWARNING("Attempt to draw scrollbar without a style.");
60 CRect outline
= GetOuterRect();
63 GetStyle()->m_SpriteBackVertical
,
67 outline
.top
+ (GetStyle()->m_UseEdgeButtons
? GetStyle()->m_Width
: 0),
69 outline
.bottom
- (GetStyle()->m_UseEdgeButtons
? GetStyle()->m_Width
: 0)
73 if (GetStyle()->m_UseEdgeButtons
)
75 const CGUISpriteInstance
* button_top
;
76 const CGUISpriteInstance
* button_bottom
;
78 if (m_ButtonMinusHovered
)
80 if (m_ButtonMinusPressed
)
81 button_top
= &(GetStyle()->m_SpriteButtonTopPressed
? GetStyle()->m_SpriteButtonTopPressed
: GetStyle()->m_SpriteButtonTop
);
83 button_top
= &(GetStyle()->m_SpriteButtonTopOver
? GetStyle()->m_SpriteButtonTopOver
: GetStyle()->m_SpriteButtonTop
);
86 button_top
= &GetStyle()->m_SpriteButtonTop
;
88 if (m_ButtonPlusHovered
)
90 if (m_ButtonPlusPressed
)
91 button_bottom
= &(GetStyle()->m_SpriteButtonBottomPressed
? GetStyle()->m_SpriteButtonBottomPressed
: GetStyle()->m_SpriteButtonBottom
);
93 button_bottom
= &(GetStyle()->m_SpriteButtonBottomOver
? GetStyle()->m_SpriteButtonBottomOver
: GetStyle()->m_SpriteButtonBottom
);
96 button_bottom
= &GetStyle()->m_SpriteButtonBottom
;
105 outline
.top
+GetStyle()->m_Width
114 outline
.bottom
-GetStyle()->m_Width
,
122 GetStyle()->m_SpriteBarVertical
,
129 void CGUIScrollBarVertical::HandleMessage(SGUIMessage
& Message
)
131 IGUIScrollBar::HandleMessage(Message
);
134 CRect
CGUIScrollBarVertical::GetBarRect() const
140 // Get from where the scroll area begins to where it ends
142 float to
= m_Y
+ m_Length
- m_BarSize
;
144 if (GetStyle()->m_UseEdgeButtons
)
146 from
+= GetStyle()->m_Width
;
147 to
-= GetStyle()->m_Width
;
150 ret
.top
= from
+ (to
- from
) * m_Pos
/ GetMaxPos();
151 ret
.bottom
= ret
.top
+ m_BarSize
;
152 ret
.right
= m_X
+ (m_RightAligned
? 0 : GetStyle()->m_Width
);
153 ret
.left
= ret
.right
- GetStyle()->m_Width
;
158 CRect
CGUIScrollBarVertical::GetOuterRect() const
165 ret
.bottom
= m_Y
+m_Length
;
166 ret
.right
= m_X
+ (m_RightAligned
? 0 : GetStyle()->m_Width
);
167 ret
.left
= ret
.right
- GetStyle()->m_Width
;
172 bool CGUIScrollBarVertical::HoveringButtonMinus(const CVector2D
& mouse
)
177 float StartX
= m_RightAligned
? m_X
-GetStyle()->m_Width
: m_X
;
179 return mouse
.X
>= StartX
&&
180 mouse
.X
<= StartX
+ GetStyle()->m_Width
&&
182 mouse
.Y
<= m_Y
+ GetStyle()->m_Width
;
185 bool CGUIScrollBarVertical::HoveringButtonPlus(const CVector2D
& mouse
)
190 float StartX
= m_RightAligned
? m_X
-GetStyle()->m_Width
: m_X
;
192 return mouse
.X
> StartX
&&
193 mouse
.X
< StartX
+ GetStyle()->m_Width
&&
194 mouse
.Y
> m_Y
+ m_Length
- GetStyle()->m_Width
&&
195 mouse
.Y
< m_Y
+ m_Length
;