2021-05-03 18:07:26 +02:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2009-04-18 19:00:33 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2019-08-29 11:07:29 +02:00
|
|
|
#include "CGUISetting.h"
|
2005-02-05 08:25:16 +01:00
|
|
|
|
2019-09-18 22:51:45 +02:00
|
|
|
#include "gui/CGUI.h"
|
2021-05-12 15:48:55 +02:00
|
|
|
#include "gui/ObjectBases/IGUIObject.h"
|
2020-11-18 12:35:36 +01:00
|
|
|
#include "ps/CLogger.h"
|
2021-05-14 12:18:03 +02:00
|
|
|
#include "scriptinterface/ScriptConversions.h"
|
2003-11-06 01:21:45 +01:00
|
|
|
|
2021-05-06 10:22:37 +02:00
|
|
|
IGUISetting::IGUISetting(const CStr& name, IGUIObject* owner) : m_pObject(*owner)
|
|
|
|
{
|
|
|
|
m_pObject.RegisterSetting(name, this);
|
|
|
|
}
|
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 14:49:59 +02:00
|
|
|
|
2021-05-06 10:22:37 +02:00
|
|
|
IGUISetting::IGUISetting(IGUISetting&& o) : m_pObject(o.m_pObject)
|
2019-08-04 04:20:08 +02:00
|
|
|
{
|
2021-05-06 10:22:37 +02:00
|
|
|
m_pObject.ReregisterSetting(o.GetName(), this);
|
2019-08-04 04:20:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 10:22:37 +02:00
|
|
|
bool IGUISetting::FromString(const CStrW& value, const bool sendMessage)
|
2019-08-04 04:20:08 +02:00
|
|
|
{
|
2021-05-06 10:22:37 +02:00
|
|
|
if (!DoFromString(value))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
OnSettingChange(GetName(), sendMessage);
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-04 04:20:08 +02:00
|
|
|
|
2021-05-06 10:22:37 +02:00
|
|
|
/**
|
|
|
|
* Parses the given JS::Value using ScriptInterface::FromJSVal and assigns it to the setting data.
|
|
|
|
*/
|
|
|
|
bool IGUISetting::FromJSVal(const ScriptRequest& rq, JS::HandleValue value, const bool sendMessage)
|
|
|
|
{
|
|
|
|
if (!DoFromJSVal(rq, value))
|
2019-08-04 04:20:08 +02:00
|
|
|
return false;
|
|
|
|
|
2021-05-06 10:22:37 +02:00
|
|
|
OnSettingChange(GetName(), sendMessage);
|
2019-08-04 04:20:08 +02:00
|
|
|
return true;
|
2021-05-06 10:22:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IGUISetting::OnSettingChange(const CStr& setting, bool sendMessage)
|
|
|
|
{
|
|
|
|
m_pObject.SettingChanged(setting, sendMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool CGUISimpleSetting<T>::DoFromString(const CStrW& value)
|
|
|
|
{
|
|
|
|
return CGUI::ParseString<T>(&m_pObject.GetGUI(), value, m_Setting);
|
2019-08-04 04:20:08 +02:00
|
|
|
};
|
|
|
|
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
template<>
|
2021-05-06 10:22:37 +02:00
|
|
|
bool CGUISimpleSetting<CGUIColor>::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value)
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
{
|
2021-05-06 10:22:37 +02:00
|
|
|
if (value.isString())
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
{
|
|
|
|
CStr name;
|
2021-05-13 11:43:33 +02:00
|
|
|
if (!Script::FromJSVal(rq, value, name))
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
return false;
|
|
|
|
|
2021-05-06 10:22:37 +02:00
|
|
|
if (!m_Setting.ParseString(m_pObject.GetGUI(), name))
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
LOGERROR("Invalid color '%s'", name.c_str());
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-07 13:34:20 +02:00
|
|
|
return true;
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
}
|
2021-05-13 11:43:33 +02:00
|
|
|
return Script::FromJSVal<CColor>(rq, value, m_Setting);
|
Delete wrongful proxy CGUIManager::GetPreDefinedColor from f0d9806b3f.
It is wrong because the predefined colors should be loaded from the GUI
page that requests to have the color parsed, which may be different from
the topmost page.
Similar to FindObjectByName removed in f9b529f2fb.
Achieve this by implementing the CGUISetting<CGUIColor>::FromJSVal
specialization, moved from ScriptInterface::FromJSVal<CGUIColor>,
instead of adding a CGUI pointer to CGUIColor.
Mark ScriptInterface::FromJSVal<GUIColor> and inherited
CColor::ParseString explicitly as deleted, so that people get a compile
error if they forget to check for predefined colors when parsing a
color.
Refs #5387, D1746 > D1684 > this > f9b529f2fb, 9be8a560a9, 415939b59b,
2c47fbd66a, 85a622b13a.
Differential Revision: https://code.wildfiregames.com/D2108
Tested on: clang 8, VS2015
This was SVN commit r22663.
2019-08-13 20:00:41 +02:00
|
|
|
};
|
|
|
|
|
2019-08-04 04:20:08 +02:00
|
|
|
template<typename T>
|
2021-05-06 10:22:37 +02:00
|
|
|
bool CGUISimpleSetting<T>::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value)
|
2019-08-04 04:20:08 +02:00
|
|
|
{
|
2021-05-13 11:43:33 +02:00
|
|
|
return Script::FromJSVal<T>(rq, value, m_Setting);
|
2019-08-04 04:20:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2021-05-06 10:22:37 +02:00
|
|
|
void CGUISimpleSetting<T>::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value)
|
2019-08-04 04:20:08 +02:00
|
|
|
{
|
2021-05-13 11:43:33 +02:00
|
|
|
Script::ToJSVal<T>(rq, value, m_Setting);
|
2019-08-04 04:20:08 +02:00
|
|
|
};
|
|
|
|
|
2021-05-06 10:22:37 +02:00
|
|
|
/**
|
|
|
|
* Explicitly instantiate CGUISimpleSetting for the basic types.
|
|
|
|
*/
|
2004-12-17 01:05:37 +01:00
|
|
|
#define TYPE(T) \
|
2021-05-06 10:22:37 +02:00
|
|
|
template class CGUISimpleSetting<T>;
|
|
|
|
|
|
|
|
TYPE(bool)
|
|
|
|
TYPE(i32)
|
|
|
|
TYPE(u32)
|
|
|
|
TYPE(float)
|
|
|
|
TYPE(CVector2D)
|
|
|
|
#include "ps/CStr.h"
|
|
|
|
TYPE(CStr)
|
|
|
|
TYPE(CStrW)
|
|
|
|
// TODO: make these inherit from CGUISimpleSetting directly.
|
|
|
|
#include "gui/SettingTypes/CGUISize.h"
|
|
|
|
TYPE(CGUISize)
|
|
|
|
TYPE(CGUIColor)
|
|
|
|
#include "gui/CGUISprite.h"
|
|
|
|
TYPE(CGUISpriteInstance)
|
|
|
|
#include "gui/SettingTypes/CGUIString.h"
|
|
|
|
TYPE(CGUIString)
|
|
|
|
#include "gui/SettingTypes/EAlign.h"
|
|
|
|
TYPE(EAlign)
|
|
|
|
TYPE(EVAlign)
|
|
|
|
#include "gui/SettingTypes/CGUIList.h"
|
|
|
|
TYPE(CGUIList)
|
|
|
|
#include "gui/SettingTypes/CGUISeries.h"
|
|
|
|
TYPE(CGUISeries)
|
2019-08-04 04:20:08 +02:00
|
|
|
|
2010-02-18 01:06:50 +01:00
|
|
|
#undef TYPE
|