2019-04-06 11:47:22 -07:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2017-04-30 09:51:18 -07:00
|
|
|
* 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"
|
2019-09-18 13:51:45 -07:00
|
|
|
|
2017-04-30 09:51:18 -07:00
|
|
|
#include "CSlider.h"
|
2019-09-18 13:51:45 -07:00
|
|
|
|
|
|
|
|
#include "gui/CGUI.h"
|
2019-09-26 14:14:21 -07:00
|
|
|
#include "maths/MathUtil.h"
|
2017-04-30 09:51:18 -07:00
|
|
|
|
2019-08-21 03:12:33 -07:00
|
|
|
CSlider::CSlider(CGUI& pGUI)
|
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
|
|
|
: IGUIObject(pGUI),
|
|
|
|
|
m_IsPressed(),
|
2019-09-27 07:08:00 -07:00
|
|
|
m_ButtonSide(),
|
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
|
|
|
m_CellID(),
|
|
|
|
|
m_MaxValue(),
|
|
|
|
|
m_MinValue(),
|
|
|
|
|
m_Sprite(),
|
|
|
|
|
m_SpriteBar(),
|
2019-09-27 07:08:00 -07:00
|
|
|
m_Value()
|
2017-04-30 09:51:18 -07:00
|
|
|
{
|
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
|
|
|
RegisterSetting("button_width", m_ButtonSide);
|
|
|
|
|
RegisterSetting("cell_id", m_CellID);
|
|
|
|
|
RegisterSetting("max_value", m_MaxValue);
|
|
|
|
|
RegisterSetting("min_value", m_MinValue);
|
|
|
|
|
RegisterSetting("sprite", m_Sprite);
|
|
|
|
|
RegisterSetting("sprite_bar", m_SpriteBar);
|
|
|
|
|
RegisterSetting("value", m_Value);
|
2019-08-23 07:43:10 -07:00
|
|
|
|
2017-04-30 09:51:18 -07:00
|
|
|
m_Value = Clamp(m_Value, m_MinValue, m_MaxValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSlider::~CSlider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-06 11:47:22 -07:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-30 09:51:18 -07:00
|
|
|
void CSlider::HandleMessage(SGUIMessage& Message)
|
|
|
|
|
{
|
|
|
|
|
switch (Message.type)
|
|
|
|
|
{
|
|
|
|
|
case GUIM_SETTINGS_UPDATED:
|
|
|
|
|
{
|
|
|
|
|
m_Value = Clamp(m_Value, m_MinValue, m_MaxValue);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GUIM_MOUSE_WHEEL_DOWN:
|
|
|
|
|
{
|
|
|
|
|
if (m_IsPressed)
|
|
|
|
|
break;
|
2019-04-06 11:47:22 -07:00
|
|
|
IncrementallyChangeValue(-0.01f);
|
2017-04-30 09:51:18 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GUIM_MOUSE_WHEEL_UP:
|
|
|
|
|
{
|
|
|
|
|
if (m_IsPressed)
|
|
|
|
|
break;
|
2019-04-06 11:47:22 -07:00
|
|
|
IncrementallyChangeValue(0.01f);
|
2017-04-30 09:51:18 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GUIM_MOUSE_PRESS_LEFT:
|
|
|
|
|
{
|
2019-08-21 03:12:33 -07:00
|
|
|
m_Mouse = m_pGUI.GetMousePos();
|
2019-04-06 11:47:22 -07:00
|
|
|
m_IsPressed = true;
|
|
|
|
|
|
|
|
|
|
IncrementallyChangeValue((m_Mouse.x - GetButtonRect().CenterPoint().x) * GetSliderRatio());
|
2017-04-30 09:51:18 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GUIM_MOUSE_RELEASE_LEFT:
|
|
|
|
|
{
|
|
|
|
|
m_IsPressed = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GUIM_MOUSE_MOTION:
|
|
|
|
|
{
|
2019-08-22 16:51:10 -07:00
|
|
|
if (!IsMouseOver())
|
2017-04-30 09:51:18 -07:00
|
|
|
m_IsPressed = false;
|
|
|
|
|
if (m_IsPressed)
|
|
|
|
|
{
|
2019-08-21 03:12:33 -07:00
|
|
|
float difference = float(m_pGUI.GetMousePos().x - m_Mouse.x) * GetSliderRatio();
|
|
|
|
|
m_Mouse = m_pGUI.GetMousePos();
|
2019-04-06 11:47:22 -07:00
|
|
|
IncrementallyChangeValue(difference);
|
2017-04-30 09:51:18 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSlider::Draw()
|
|
|
|
|
{
|
|
|
|
|
CRect slider_line(m_CachedActualSize);
|
|
|
|
|
slider_line.left += m_ButtonSide / 2.0f;
|
|
|
|
|
slider_line.right -= m_ButtonSide / 2.0f;
|
|
|
|
|
float bz = GetBufferedZ();
|
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
|
|
|
m_pGUI.DrawSprite(m_SpriteBar, m_CellID, bz, slider_line);
|
|
|
|
|
m_pGUI.DrawSprite(m_Sprite, m_CellID, bz, GetButtonRect());
|
2017-04-30 09:51:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSlider::UpdateValue()
|
2019-04-06 11:47:22 -07:00
|
|
|
{
|
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 04:21:11 -07:00
|
|
|
SetSetting<float>("value", m_Value, true);
|
2017-04-30 09:51:18 -07:00
|
|
|
ScriptEvent("valuechange");
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:51:10 -07:00
|
|
|
CRect CSlider::GetButtonRect() const
|
2017-04-30 09:51:18 -07:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|