| [26213] | 1 | /* Copyright (C) 2022 Wildfire Games.
|
|---|
| [27965] | 2 | * This file is part of 0 A.D.
|
|---|
| [19479] | 3 | *
|
|---|
| [27965] | 4 | * 0 A.D. is free software: you can redistribute it and/or modify
|
|---|
| [19479] | 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 | *
|
|---|
| [27965] | 9 | * 0 A.D. is distributed in the hope that it will be useful,
|
|---|
| [19479] | 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
|
|---|
| [27965] | 15 | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| [19479] | 16 | */
|
|---|
| 17 |
|
|---|
| 18 | #include "precompiled.h"
|
|---|
| [22931] | 19 |
|
|---|
| [19479] | 20 | #include "CSlider.h"
|
|---|
| [22931] | 21 |
|
|---|
| 22 | #include "gui/CGUI.h"
|
|---|
| [23002] | 23 | #include "maths/MathUtil.h"
|
|---|
| [19479] | 24 |
|
|---|
| [23403] | 25 | const CStr CSlider::EventNameValueChange = "ValueChange";
|
|---|
| 26 |
|
|---|
| [22741] | 27 | CSlider::CSlider(CGUI& pGUI)
|
|---|
| [23005] | 28 | : IGUIObject(pGUI),
|
|---|
| [23435] | 29 | IGUIButtonBehavior(*static_cast<IGUIObject*>(this)),
|
|---|
| [25392] | 30 | m_ButtonSide(this, "button_width"),
|
|---|
| 31 | m_MaxValue(this, "max_value"),
|
|---|
| 32 | m_MinValue(this, "min_value"),
|
|---|
| 33 | m_Sprite(this, "sprite"),
|
|---|
| [26019] | 34 | m_SpriteDisabled(this, "sprite_disabled"),
|
|---|
| [25392] | 35 | m_SpriteBar(this, "sprite_bar"),
|
|---|
| [26019] | 36 | m_SpriteBarDisabled(this, "sprite_bar_disabled"),
|
|---|
| [25392] | 37 | m_Value(this, "value")
|
|---|
| [19479] | 38 | {
|
|---|
| [25392] | 39 | m_Value.Set(Clamp<float>(m_Value, m_MinValue, m_MaxValue), false);
|
|---|
| [19479] | 40 | }
|
|---|
| 41 |
|
|---|
| 42 | CSlider::~CSlider()
|
|---|
| 43 | {
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| [23435] | 46 | void CSlider::ResetStates()
|
|---|
| 47 | {
|
|---|
| 48 | IGUIObject::ResetStates();
|
|---|
| 49 | IGUIButtonBehavior::ResetStates();
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| [22164] | 52 | float CSlider::GetSliderRatio() const
|
|---|
| 53 | {
|
|---|
| 54 | return (m_MaxValue - m_MinValue) / (m_CachedActualSize.GetWidth() - m_ButtonSide);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void CSlider::IncrementallyChangeValue(const float difference)
|
|---|
| 58 | {
|
|---|
| [25392] | 59 | m_Value.Set(Clamp<float>(m_Value + difference, m_MinValue, m_MaxValue), true);
|
|---|
| [22164] | 60 | UpdateValue();
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| [19479] | 63 | void CSlider::HandleMessage(SGUIMessage& Message)
|
|---|
| 64 | {
|
|---|
| [23020] | 65 | IGUIObject::HandleMessage(Message);
|
|---|
| [23435] | 66 | IGUIButtonBehavior::HandleMessage(Message);
|
|---|
| [23020] | 67 |
|
|---|
| [19479] | 68 | switch (Message.type)
|
|---|
| 69 | {
|
|---|
| [25392] | 70 | /*
|
|---|
| [19479] | 71 | case GUIM_SETTINGS_UPDATED:
|
|---|
| 72 | {
|
|---|
| [25392] | 73 | m_Value.Set(Clamp<float>(m_Value, m_MinValue, m_MaxValue), true);
|
|---|
| [19479] | 74 | break;
|
|---|
| 75 | }
|
|---|
| [25392] | 76 | */
|
|---|
| [19479] | 77 | case GUIM_MOUSE_WHEEL_DOWN:
|
|---|
| 78 | {
|
|---|
| [23435] | 79 | if (m_Pressed)
|
|---|
| [19479] | 80 | break;
|
|---|
| [22164] | 81 | IncrementallyChangeValue(-0.01f);
|
|---|
| [19479] | 82 | break;
|
|---|
| 83 | }
|
|---|
| 84 | case GUIM_MOUSE_WHEEL_UP:
|
|---|
| 85 | {
|
|---|
| [23435] | 86 | if (m_Pressed)
|
|---|
| [19479] | 87 | break;
|
|---|
| [22164] | 88 | IncrementallyChangeValue(0.01f);
|
|---|
| [19479] | 89 | break;
|
|---|
| 90 | }
|
|---|
| 91 | case GUIM_MOUSE_PRESS_LEFT:
|
|---|
| [23435] | 92 | FALLTHROUGH;
|
|---|
| [19479] | 93 | case GUIM_MOUSE_MOTION:
|
|---|
| 94 | {
|
|---|
| [23435] | 95 | if (m_Pressed)
|
|---|
| [19479] | 96 | {
|
|---|
| [22741] | 97 | m_Mouse = m_pGUI.GetMousePos();
|
|---|
| [25152] | 98 | IncrementallyChangeValue((m_Mouse.X - GetButtonRect().CenterPoint().X) * GetSliderRatio());
|
|---|
| [19479] | 99 | }
|
|---|
| 100 | break;
|
|---|
| 101 | }
|
|---|
| 102 | default:
|
|---|
| 103 | break;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| [25591] | 107 | void CSlider::Draw(CCanvas2D& canvas)
|
|---|
| [19479] | 108 | {
|
|---|
| [26019] | 109 | CRect sliderLine(m_CachedActualSize);
|
|---|
| 110 | sliderLine.left += m_ButtonSide / 2.0f;
|
|---|
| 111 | sliderLine.right -= m_ButtonSide / 2.0f;
|
|---|
| 112 | m_pGUI.DrawSprite(IsEnabled() ? m_SpriteBar : m_SpriteBarDisabled, canvas, sliderLine);
|
|---|
| 113 | m_pGUI.DrawSprite(IsEnabled() ? m_Sprite : m_SpriteDisabled, canvas, GetButtonRect());
|
|---|
| [19479] | 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void CSlider::UpdateValue()
|
|---|
| [22164] | 117 | {
|
|---|
| [23403] | 118 | ScriptEvent(EventNameValueChange);
|
|---|
| [19479] | 119 | }
|
|---|
| 120 |
|
|---|
| [22757] | 121 | CRect CSlider::GetButtonRect() const
|
|---|
| [19479] | 122 | {
|
|---|
| [26213] | 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 | const float value = Clamp<float>(m_Value, m_MinValue, m_MaxValue);
|
|---|
| 127 | float ratio = m_MaxValue > m_MinValue ? (value - m_MinValue) / (m_MaxValue - m_MinValue) : 0.0f;
|
|---|
| [19479] | 128 | float x = m_CachedActualSize.left + ratio * (m_CachedActualSize.GetWidth() - m_ButtonSide);
|
|---|
| 129 | float y = m_CachedActualSize.top + (m_CachedActualSize.GetHeight() - m_ButtonSide) / 2.0;
|
|---|
| 130 | return CRect(x, y, x + m_ButtonSide, y + m_ButtonSide);
|
|---|
| 131 | }
|
|---|