1 /* Copyright (C) 2015 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"
21 #include "maths/MathUtil.h"
23 IGUIScrollBar::IGUIScrollBar() : m_pStyle(NULL
), m_pGUI(NULL
),
24 m_X(300.f
), m_Y(300.f
),
25 m_ScrollRange(1.f
), m_ScrollSpace(0.f
), // MaxPos: not 0, due to division.
26 m_Length(200.f
), m_Width(20.f
),
27 m_BarSize(0.f
), m_Pos(0.f
),
28 m_ButtonPlusPressed(false),
29 m_ButtonMinusPressed(false),
30 m_ButtonPlusHovered(false),
31 m_ButtonMinusHovered(false),
37 IGUIScrollBar::~IGUIScrollBar()
41 void IGUIScrollBar::SetupBarSize()
46 float min
= GetStyle()->m_MinimumBarSize
;
47 float max
= GetStyle()->m_MaximumBarSize
;
48 float length
= m_Length
;
50 // Check for edge buttons
51 if (GetStyle()->m_UseEdgeButtons
)
52 length
-= GetStyle()->m_Width
* 2.f
;
54 // Check min and max are valid
60 // Clamp size to not exceed a minimum or maximum.
61 m_BarSize
= clamp(length
* std::min((float)m_ScrollSpace
/ (float)m_ScrollRange
, 1.f
), min
, max
);
64 const SGUIScrollBarStyle
* IGUIScrollBar::GetStyle() const
69 return m_pHostObject
->GetScrollBarStyle(m_ScrollBarStyle
);
72 CGUI
* IGUIScrollBar::GetGUI() const
77 return m_pHostObject
->GetGUI();
80 void IGUIScrollBar::UpdatePosBoundaries()
83 m_ScrollRange
< m_ScrollSpace
) // <= scrolling not applicable
85 else if (m_Pos
> GetMaxPos())
89 void IGUIScrollBar::HandleMessage(SGUIMessage
& Message
)
93 case GUIM_MOUSE_MOTION
:
95 // TODO Gee: Optimizations needed!
97 CPos mouse
= m_pHostObject
->GetMousePos();
99 // If bar is being dragged
102 SetPosFromMousePos(mouse
);
103 UpdatePosBoundaries();
106 // check if components are being hovered
107 m_BarHovered
= GetBarRect().PointInside(mouse
);
108 m_ButtonMinusHovered
= HoveringButtonMinus(mouse
);
109 m_ButtonPlusHovered
= HoveringButtonPlus(mouse
);
111 if (!m_ButtonMinusHovered
)
112 m_ButtonMinusPressed
= false;
114 if (!m_ButtonPlusHovered
)
115 m_ButtonPlusPressed
= false;
119 case GUIM_MOUSE_PRESS_LEFT
:
124 CPos mouse
= m_pHostObject
->GetMousePos();
127 if (GetBarRect().PointInside(mouse
))
130 m_BarPressedAtPos
= mouse
;
131 m_PosWhenPressed
= m_Pos
;
133 // if button-minus is pressed
134 else if (m_ButtonMinusHovered
)
136 m_ButtonMinusPressed
= true;
139 // if button-plus is pressed
140 else if (m_ButtonPlusHovered
)
142 m_ButtonPlusPressed
= true;
145 // Pressing the background of the bar, to scroll
146 // notice the if-sentence alone does not admit that,
147 // it must be after the above if/elses
150 if (GetOuterRect().PointInside(mouse
))
152 // Scroll plus or minus a lot, this might change, it doesn't
153 // have to be fancy though.
154 if (mouse
.y
< GetBarRect().top
)
158 // Simulate mouse movement to see if bar now is hovered
159 SGUIMessage
msg(GUIM_MOUSE_MOTION
);
166 case GUIM_MOUSE_RELEASE_LEFT
:
167 m_ButtonMinusPressed
= false;
168 m_ButtonPlusPressed
= false;
171 case GUIM_MOUSE_WHEEL_UP
:
174 // Since the scroll was changed, let's simulate a mouse movement
175 // to check if scrollbar now is hovered
176 SGUIMessage
msg(GUIM_MOUSE_MOTION
);
181 case GUIM_MOUSE_WHEEL_DOWN
:
184 // Since the scroll was changed, let's simulate a mouse movement
185 // to check if scrollbar now is hovered
186 SGUIMessage
msg(GUIM_MOUSE_MOTION
);