0ad/source/gui/CSlider.cpp
elexis 1a49ccb294 Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.

Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.

Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts

This was SVN commit r22796.
2019-08-28 11:21:11 +00:00

142 lines
3.7 KiB
C++

/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "CSlider.h"
#include "GUI.h"
#include "lib/ogl.h"
#include "ps/CLogger.h"
CSlider::CSlider(CGUI& pGUI)
: IGUIObject(pGUI), m_IsPressed(false), m_ButtonSide(0)
{
AddSetting<float>("value");
AddSetting<float>("min_value");
AddSetting<float>("max_value");
AddSetting<i32>("cell_id");
AddSetting<CGUISpriteInstance>("sprite");
AddSetting<CGUISpriteInstance>("sprite_bar");
AddSetting<float>("button_width");
m_Value = GetSetting<float>("value");
m_MinValue = GetSetting<float>("min_value");
m_MaxValue = GetSetting<float>("max_value");
m_ButtonSide = GetSetting<float>("button_width");
m_Value = Clamp(m_Value, m_MinValue, m_MaxValue);
}
CSlider::~CSlider()
{
}
float CSlider::GetSliderRatio() const
{
return (m_MaxValue - m_MinValue) / (m_CachedActualSize.GetWidth() - m_ButtonSide);
}
void CSlider::IncrementallyChangeValue(const float difference)
{
m_Value = Clamp(m_Value + difference, m_MinValue, m_MaxValue);
UpdateValue();
}
void CSlider::HandleMessage(SGUIMessage& Message)
{
switch (Message.type)
{
case GUIM_SETTINGS_UPDATED:
{
m_Value = GetSetting<float>("value");
m_MinValue = GetSetting<float>("min_value");
m_MaxValue = GetSetting<float>("max_value");
m_ButtonSide = GetSetting<float>("button_width");
m_Value = Clamp(m_Value, m_MinValue, m_MaxValue);
break;
}
case GUIM_MOUSE_WHEEL_DOWN:
{
if (m_IsPressed)
break;
IncrementallyChangeValue(-0.01f);
break;
}
case GUIM_MOUSE_WHEEL_UP:
{
if (m_IsPressed)
break;
IncrementallyChangeValue(0.01f);
break;
}
case GUIM_MOUSE_PRESS_LEFT:
{
m_Mouse = m_pGUI.GetMousePos();
m_IsPressed = true;
IncrementallyChangeValue((m_Mouse.x - GetButtonRect().CenterPoint().x) * GetSliderRatio());
break;
}
case GUIM_MOUSE_RELEASE_LEFT:
{
m_IsPressed = false;
break;
}
case GUIM_MOUSE_MOTION:
{
if (!IsMouseOver())
m_IsPressed = false;
if (m_IsPressed)
{
float difference = float(m_pGUI.GetMousePos().x - m_Mouse.x) * GetSliderRatio();
m_Mouse = m_pGUI.GetMousePos();
IncrementallyChangeValue(difference);
}
break;
}
default:
break;
}
}
void CSlider::Draw()
{
CGUISpriteInstance& sprite = GetSetting<CGUISpriteInstance>("sprite_bar");
CGUISpriteInstance& sprite_button = GetSetting<CGUISpriteInstance>("sprite");
const int cell_id = GetSetting<i32>("cell_id");
CRect slider_line(m_CachedActualSize);
slider_line.left += m_ButtonSide / 2.0f;
slider_line.right -= m_ButtonSide / 2.0f;
float bz = GetBufferedZ();
m_pGUI.DrawSprite(sprite, cell_id, bz, slider_line);
m_pGUI.DrawSprite(sprite_button, cell_id, bz, GetButtonRect());
}
void CSlider::UpdateValue()
{
SetSetting<float>("value", m_Value, true);
ScriptEvent("valuechange");
}
CRect CSlider::GetButtonRect() const
{
float ratio = m_MaxValue > m_MinValue ? (m_Value - m_MinValue) / (m_MaxValue - m_MinValue) : 0.0f;
float x = m_CachedActualSize.left + ratio * (m_CachedActualSize.GetWidth() - m_ButtonSide);
float y = m_CachedActualSize.top + (m_CachedActualSize.GetHeight() - m_ButtonSide) / 2.0;
return CRect(x, y, x + m_ButtonSide, y + m_ButtonSide);
}