Line data Source code
1 : /* Copyright (C) 2021 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 : #include "precompiled.h"
19 :
20 : #include "CGUIScrollBarVertical.h"
21 :
22 : #include "gui/CGUI.h"
23 : #include "ps/CLogger.h"
24 :
25 0 : CGUIScrollBarVertical::CGUIScrollBarVertical(CGUI& pGUI)
26 0 : : IGUIScrollBar(pGUI)
27 : {
28 0 : }
29 :
30 0 : CGUIScrollBarVertical::~CGUIScrollBarVertical()
31 : {
32 0 : }
33 :
34 0 : void CGUIScrollBarVertical::SetPosFromMousePos(const CVector2D& mouse)
35 : {
36 0 : if (!GetStyle())
37 0 : return;
38 :
39 : /**
40 : * Calculate the position for the top of the item being scrolled
41 : */
42 0 : float emptyBackground = m_Length - m_BarSize;
43 :
44 0 : if (GetStyle()->m_UseEdgeButtons)
45 0 : emptyBackground -= GetStyle()->m_Width * 2;
46 :
47 0 : m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.Y - m_BarPressedAtPos.Y) / emptyBackground;
48 : }
49 :
50 0 : void CGUIScrollBarVertical::Draw(CCanvas2D& canvas)
51 : {
52 0 : if (!GetStyle())
53 : {
54 0 : LOGWARNING("Attempt to draw scrollbar without a style.");
55 0 : return;
56 : }
57 :
58 0 : if (IsVisible())
59 : {
60 0 : CRect outline = GetOuterRect();
61 :
62 0 : m_pGUI.DrawSprite(
63 0 : GetStyle()->m_SpriteBackVertical,
64 : canvas,
65 0 : CRect(
66 : outline.left,
67 0 : outline.top + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0),
68 : outline.right,
69 0 : outline.bottom - (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0)
70 : )
71 : );
72 :
73 0 : if (GetStyle()->m_UseEdgeButtons)
74 : {
75 : const CGUISpriteInstance* button_top;
76 : const CGUISpriteInstance* button_bottom;
77 :
78 0 : if (m_ButtonMinusHovered)
79 : {
80 0 : if (m_ButtonMinusPressed)
81 0 : button_top = &(GetStyle()->m_SpriteButtonTopPressed ? GetStyle()->m_SpriteButtonTopPressed : GetStyle()->m_SpriteButtonTop);
82 : else
83 0 : button_top = &(GetStyle()->m_SpriteButtonTopOver ? GetStyle()->m_SpriteButtonTopOver : GetStyle()->m_SpriteButtonTop);
84 : }
85 : else
86 0 : button_top = &GetStyle()->m_SpriteButtonTop;
87 :
88 0 : if (m_ButtonPlusHovered)
89 : {
90 0 : if (m_ButtonPlusPressed)
91 0 : button_bottom = &(GetStyle()->m_SpriteButtonBottomPressed ? GetStyle()->m_SpriteButtonBottomPressed : GetStyle()->m_SpriteButtonBottom);
92 : else
93 0 : button_bottom = &(GetStyle()->m_SpriteButtonBottomOver ? GetStyle()->m_SpriteButtonBottomOver : GetStyle()->m_SpriteButtonBottom);
94 : }
95 : else
96 0 : button_bottom = &GetStyle()->m_SpriteButtonBottom;
97 :
98 0 : m_pGUI.DrawSprite(
99 : *button_top,
100 : canvas,
101 0 : CRect(
102 : outline.left,
103 : outline.top,
104 : outline.right,
105 0 : outline.top+GetStyle()->m_Width
106 : )
107 : );
108 :
109 0 : m_pGUI.DrawSprite(
110 : *button_bottom,
111 : canvas,
112 0 : CRect(
113 : outline.left,
114 0 : outline.bottom-GetStyle()->m_Width,
115 : outline.right,
116 : outline.bottom
117 : )
118 : );
119 : }
120 :
121 0 : m_pGUI.DrawSprite(
122 0 : GetStyle()->m_SpriteBarVertical,
123 : canvas,
124 0 : GetBarRect()
125 : );
126 : }
127 : }
128 :
129 0 : void CGUIScrollBarVertical::HandleMessage(SGUIMessage& Message)
130 : {
131 0 : IGUIScrollBar::HandleMessage(Message);
132 0 : }
133 :
134 0 : CRect CGUIScrollBarVertical::GetBarRect() const
135 : {
136 0 : CRect ret;
137 0 : if (!GetStyle())
138 0 : return ret;
139 :
140 : // Get from where the scroll area begins to where it ends
141 0 : float from = m_Y;
142 0 : float to = m_Y + m_Length - m_BarSize;
143 :
144 0 : if (GetStyle()->m_UseEdgeButtons)
145 : {
146 0 : from += GetStyle()->m_Width;
147 0 : to -= GetStyle()->m_Width;
148 : }
149 :
150 0 : ret.top = from + (to - from) * m_Pos / GetMaxPos();
151 0 : ret.bottom = ret.top + m_BarSize;
152 0 : ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width);
153 0 : ret.left = ret.right - GetStyle()->m_Width;
154 :
155 0 : return ret;
156 : }
157 :
158 0 : CRect CGUIScrollBarVertical::GetOuterRect() const
159 : {
160 0 : CRect ret;
161 0 : if (!GetStyle())
162 0 : return ret;
163 :
164 0 : ret.top = m_Y;
165 0 : ret.bottom = m_Y+m_Length;
166 0 : ret.right = m_X + (m_RightAligned ? 0 : GetStyle()->m_Width);
167 0 : ret.left = ret.right - GetStyle()->m_Width;
168 :
169 0 : return ret;
170 : }
171 :
172 0 : bool CGUIScrollBarVertical::HoveringButtonMinus(const CVector2D& mouse)
173 : {
174 0 : if (!GetStyle())
175 0 : return false;
176 :
177 0 : float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X;
178 :
179 0 : return mouse.X >= StartX &&
180 0 : mouse.X <= StartX + GetStyle()->m_Width &&
181 0 : mouse.Y >= m_Y &&
182 0 : mouse.Y <= m_Y + GetStyle()->m_Width;
183 : }
184 :
185 0 : bool CGUIScrollBarVertical::HoveringButtonPlus(const CVector2D& mouse)
186 : {
187 0 : if (!GetStyle())
188 0 : return false;
189 :
190 0 : float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X;
191 :
192 0 : return mouse.X > StartX &&
193 0 : mouse.X < StartX + GetStyle()->m_Width &&
194 0 : mouse.Y > m_Y + m_Length - GetStyle()->m_Width &&
195 0 : mouse.Y < m_Y + m_Length;
196 : }
|