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