Line data Source code
1 : /* Copyright (C) 2022 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 "CSlider.h"
21 :
22 : #include "gui/CGUI.h"
23 : #include "maths/MathUtil.h"
24 :
25 1 : const CStr CSlider::EventNameValueChange = "ValueChange";
26 :
27 0 : CSlider::CSlider(CGUI& pGUI)
28 : : IGUIObject(pGUI),
29 : IGUIButtonBehavior(*static_cast<IGUIObject*>(this)),
30 : m_ButtonSide(this, "button_width"),
31 : m_MaxValue(this, "max_value"),
32 : m_MinValue(this, "min_value"),
33 : m_Sprite(this, "sprite"),
34 : m_SpriteDisabled(this, "sprite_disabled"),
35 : m_SpriteBar(this, "sprite_bar"),
36 : m_SpriteBarDisabled(this, "sprite_bar_disabled"),
37 0 : m_Value(this, "value")
38 : {
39 0 : m_Value.Set(Clamp<float>(m_Value, m_MinValue, m_MaxValue), false);
40 0 : }
41 :
42 0 : CSlider::~CSlider()
43 : {
44 0 : }
45 :
46 0 : void CSlider::ResetStates()
47 : {
48 0 : IGUIObject::ResetStates();
49 0 : IGUIButtonBehavior::ResetStates();
50 0 : }
51 :
52 0 : float CSlider::GetSliderRatio() const
53 : {
54 0 : return (m_MaxValue - m_MinValue) / (m_CachedActualSize.GetWidth() - m_ButtonSide);
55 : }
56 :
57 0 : void CSlider::IncrementallyChangeValue(const float difference)
58 : {
59 0 : m_Value.Set(Clamp<float>(m_Value + difference, m_MinValue, m_MaxValue), true);
60 0 : UpdateValue();
61 0 : }
62 :
63 0 : void CSlider::HandleMessage(SGUIMessage& Message)
64 : {
65 0 : IGUIObject::HandleMessage(Message);
66 0 : IGUIButtonBehavior::HandleMessage(Message);
67 :
68 0 : switch (Message.type)
69 : {
70 : /*
71 : case GUIM_SETTINGS_UPDATED:
72 : {
73 : m_Value.Set(Clamp<float>(m_Value, m_MinValue, m_MaxValue), true);
74 : break;
75 : }
76 : */
77 0 : case GUIM_MOUSE_WHEEL_DOWN:
78 : {
79 0 : if (m_Pressed)
80 0 : break;
81 0 : IncrementallyChangeValue(-0.01f);
82 0 : break;
83 : }
84 0 : case GUIM_MOUSE_WHEEL_UP:
85 : {
86 0 : if (m_Pressed)
87 0 : break;
88 0 : IncrementallyChangeValue(0.01f);
89 0 : break;
90 : }
91 0 : case GUIM_MOUSE_PRESS_LEFT:
92 : FALLTHROUGH;
93 : case GUIM_MOUSE_MOTION:
94 : {
95 0 : if (m_Pressed)
96 : {
97 0 : m_Mouse = m_pGUI.GetMousePos();
98 0 : IncrementallyChangeValue((m_Mouse.X - GetButtonRect().CenterPoint().X) * GetSliderRatio());
99 : }
100 0 : break;
101 : }
102 0 : default:
103 0 : break;
104 : }
105 0 : }
106 :
107 0 : void CSlider::Draw(CCanvas2D& canvas)
108 : {
109 0 : CRect sliderLine(m_CachedActualSize);
110 0 : sliderLine.left += m_ButtonSide / 2.0f;
111 0 : sliderLine.right -= m_ButtonSide / 2.0f;
112 0 : m_pGUI.DrawSprite(IsEnabled() ? m_SpriteBar : m_SpriteBarDisabled, canvas, sliderLine);
113 0 : m_pGUI.DrawSprite(IsEnabled() ? m_Sprite : m_SpriteDisabled, canvas, GetButtonRect());
114 0 : }
115 :
116 0 : void CSlider::UpdateValue()
117 : {
118 0 : ScriptEvent(EventNameValueChange);
119 0 : }
120 :
121 0 : CRect CSlider::GetButtonRect() const
122 : {
123 : // Even if the value is incorrect it doesn't make sense to draw it outside
124 : // of the element bounds. Because that value might be set intentionally in the
125 : // config for debug purposes.
126 0 : const float value = Clamp<float>(m_Value, m_MinValue, m_MaxValue);
127 0 : float ratio = m_MaxValue > m_MinValue ? (value - m_MinValue) / (m_MaxValue - m_MinValue) : 0.0f;
128 0 : float x = m_CachedActualSize.left + ratio * (m_CachedActualSize.GetWidth() - m_ButtonSide);
129 0 : float y = m_CachedActualSize.top + (m_CachedActualSize.GetHeight() - m_ButtonSide) / 2.0;
130 0 : return CRect(x, y, x + m_ButtonSide, y + m_ButtonSide);
131 3 : }
|