[Windows] Automated build.
[0ad.git] / source / gui / CGUIScrollBarVertical.cpp
blob2a3ac5e93d0d672599846c31a228e65f6eaba023
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"
22 #include "gui/CGUI.h"
23 #include "ps/CLogger.h"
25 CGUIScrollBarVertical::CGUIScrollBarVertical(CGUI& pGUI)
26 : IGUIScrollBar(pGUI)
30 CGUIScrollBarVertical::~CGUIScrollBarVertical()
34 void CGUIScrollBarVertical::SetPosFromMousePos(const CVector2D& mouse)
36 if (!GetStyle())
37 return;
39 /**
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)
52 if (!GetStyle())
54 LOGWARNING("Attempt to draw scrollbar without a style.");
55 return;
58 if (IsVisible())
60 CRect outline = GetOuterRect();
62 m_pGUI.DrawSprite(
63 GetStyle()->m_SpriteBackVertical,
64 canvas,
65 CRect(
66 outline.left,
67 outline.top + (GetStyle()->m_UseEdgeButtons ? GetStyle()->m_Width : 0),
68 outline.right,
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);
82 else
83 button_top = &(GetStyle()->m_SpriteButtonTopOver ? GetStyle()->m_SpriteButtonTopOver : GetStyle()->m_SpriteButtonTop);
85 else
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);
92 else
93 button_bottom = &(GetStyle()->m_SpriteButtonBottomOver ? GetStyle()->m_SpriteButtonBottomOver : GetStyle()->m_SpriteButtonBottom);
95 else
96 button_bottom = &GetStyle()->m_SpriteButtonBottom;
98 m_pGUI.DrawSprite(
99 *button_top,
100 canvas,
101 CRect(
102 outline.left,
103 outline.top,
104 outline.right,
105 outline.top+GetStyle()->m_Width
109 m_pGUI.DrawSprite(
110 *button_bottom,
111 canvas,
112 CRect(
113 outline.left,
114 outline.bottom-GetStyle()->m_Width,
115 outline.right,
116 outline.bottom
121 m_pGUI.DrawSprite(
122 GetStyle()->m_SpriteBarVertical,
123 canvas,
124 GetBarRect()
129 void CGUIScrollBarVertical::HandleMessage(SGUIMessage& Message)
131 IGUIScrollBar::HandleMessage(Message);
134 CRect CGUIScrollBarVertical::GetBarRect() const
136 CRect ret;
137 if (!GetStyle())
138 return ret;
140 // Get from where the scroll area begins to where it ends
141 float from = m_Y;
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;
155 return ret;
158 CRect CGUIScrollBarVertical::GetOuterRect() const
160 CRect ret;
161 if (!GetStyle())
162 return ret;
164 ret.top = m_Y;
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;
169 return ret;
172 bool CGUIScrollBarVertical::HoveringButtonMinus(const CVector2D& mouse)
174 if (!GetStyle())
175 return false;
177 float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X;
179 return mouse.X >= StartX &&
180 mouse.X <= StartX + GetStyle()->m_Width &&
181 mouse.Y >= m_Y &&
182 mouse.Y <= m_Y + GetStyle()->m_Width;
185 bool CGUIScrollBarVertical::HoveringButtonPlus(const CVector2D& mouse)
187 if (!GetStyle())
188 return false;
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;