2019-07-26 11:57:28 -07:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2009-04-18 10:00:33 -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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-06-03 11:38:14 -07:00
|
|
|
#include "precompiled.h"
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
#include "GUIutil.h"
|
2005-02-04 23:25:16 -08:00
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
#include "gui/GUI.h"
|
2009-09-27 08:04:46 -07:00
|
|
|
#include "ps/CLogger.h"
|
2003-11-05 16:21:45 -08:00
|
|
|
|
2019-08-03 19:20:08 -07:00
|
|
|
template<typename T>
|
|
|
|
|
CGUISetting<T>::CGUISetting(IGUIObject& pObject, const CStr& Name)
|
|
|
|
|
: m_pSetting(T()), m_Name(Name), m_pObject(pObject)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool CGUISetting<T>::FromString(const CStrW& Value, const bool& SkipMessage)
|
|
|
|
|
{
|
|
|
|
|
T settingValue;
|
|
|
|
|
|
2019-08-21 03:12:33 -07:00
|
|
|
if (!GUI<T>::ParseString(&m_pObject.GetGUI(), Value, settingValue))
|
2019-08-03 19:20:08 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
GUI<T>::SetSetting(&m_pObject, m_Name, settingValue, SkipMessage);
|
|
|
|
|
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 11:00:41 -07:00
|
|
|
template<>
|
|
|
|
|
bool CGUISetting<CGUIColor>::FromJSVal(JSContext* cx, JS::HandleValue Value)
|
|
|
|
|
{
|
|
|
|
|
CGUIColor settingValue;
|
|
|
|
|
if (Value.isString())
|
|
|
|
|
{
|
|
|
|
|
CStr name;
|
|
|
|
|
if (!ScriptInterface::FromJSVal(cx, Value, name))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!settingValue.ParseString(m_pObject.GetGUI(), name))
|
|
|
|
|
{
|
|
|
|
|
JS_ReportError(cx, "Invalid color '%s'", name.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!ScriptInterface::FromJSVal<CColor>(cx, Value, settingValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
GUI<CGUIColor>::SetSetting(&m_pObject, m_Name, settingValue);
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-03 19:20:08 -07:00
|
|
|
template<typename T>
|
|
|
|
|
bool CGUISetting<T>::FromJSVal(JSContext* cx, JS::HandleValue Value)
|
|
|
|
|
{
|
|
|
|
|
T settingValue;
|
|
|
|
|
if (!ScriptInterface::FromJSVal<T>(cx, Value, settingValue))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
GUI<T>::SetSetting(&m_pObject, m_Name, settingValue);
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void CGUISetting<T>::ToJSVal(JSContext* cx, JS::MutableHandleValue Value)
|
|
|
|
|
{
|
|
|
|
|
ScriptInterface::ToJSVal<T>(cx, Value, m_pSetting);
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-28 15:40:58 -07:00
|
|
|
template <typename T>
|
|
|
|
|
PSRETURN GUI<T>::SetSetting(IGUIObject* pObject, const CStr& Setting, T& Value, const bool& SkipMessage)
|
|
|
|
|
{
|
2019-08-20 03:51:29 -07:00
|
|
|
return SetSettingWrap(pObject, Setting, SkipMessage,
|
2019-07-28 15:40:58 -07:00
|
|
|
[&pObject, &Setting, &Value]() {
|
2019-08-03 19:20:08 -07:00
|
|
|
static_cast<CGUISetting<T>* >(pObject->m_Settings[Setting])->m_pSetting = std::move(Value);
|
2019-07-28 15:40:58 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
PSRETURN GUI<T>::SetSetting(IGUIObject* pObject, const CStr& Setting, const T& Value, const bool& SkipMessage)
|
|
|
|
|
{
|
2019-08-20 03:51:29 -07:00
|
|
|
return SetSettingWrap(pObject, Setting, SkipMessage,
|
2019-07-28 15:40:58 -07:00
|
|
|
[&pObject, &Setting, &Value]() {
|
2019-08-03 19:20:08 -07:00
|
|
|
static_cast<CGUISetting<T>* >(pObject->m_Settings[Setting])->m_pSetting = Value;
|
2019-07-28 15:40:58 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-16 16:05:37 -08:00
|
|
|
template <typename T>
|
2019-08-20 03:51:29 -07:00
|
|
|
PSRETURN GUI<T>::SetSettingWrap(IGUIObject* pObject, const CStr& Setting, const bool& SkipMessage, const std::function<void()>& valueSet)
|
2004-12-16 16:05:37 -08:00
|
|
|
{
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(pObject != NULL);
|
2004-12-16 16:05:37 -08:00
|
|
|
|
|
|
|
|
if (!pObject->SettingExists(Setting))
|
2009-09-27 08:04:46 -07:00
|
|
|
{
|
2015-08-21 10:08:41 -07:00
|
|
|
LOGWARNING("setting %s was not found on object %s",
|
2009-09-27 08:04:46 -07:00
|
|
|
Setting.c_str(),
|
|
|
|
|
pObject->GetPresentableName().c_str());
|
|
|
|
|
return PSRETURN_GUI_InvalidSetting;
|
|
|
|
|
}
|
2004-12-16 16:05:37 -08:00
|
|
|
|
2019-07-28 15:40:58 -07:00
|
|
|
valueSet();
|
2004-12-16 16:05:37 -08:00
|
|
|
|
|
|
|
|
// Some settings needs special attention at change
|
|
|
|
|
|
|
|
|
|
// If setting was "size", we need to re-cache itself and all children
|
2004-12-19 04:20:04 -08:00
|
|
|
if (Setting == "size")
|
2004-12-16 16:05:37 -08:00
|
|
|
{
|
2019-08-21 06:22:25 -07:00
|
|
|
pObject->RecurseObject(nullptr, &IGUIObject::UpdateCachedSize);
|
2004-12-16 16:05:37 -08:00
|
|
|
}
|
2015-08-21 10:08:41 -07:00
|
|
|
else if (Setting == "hidden")
|
2004-12-19 04:20:04 -08:00
|
|
|
{
|
|
|
|
|
// Hiding an object requires us to reset it and all children
|
2019-08-26 05:25:07 -07:00
|
|
|
if (pObject->GetSetting<bool>(Setting))
|
2019-08-22 17:42:00 -07:00
|
|
|
pObject->RecurseObject(nullptr, &IGUIObject::ResetStates);
|
2004-12-19 04:20:04 -08:00
|
|
|
}
|
2004-12-16 16:05:37 -08:00
|
|
|
|
2005-02-04 23:25:16 -08:00
|
|
|
if (!SkipMessage)
|
2011-04-28 13:42:11 -07:00
|
|
|
{
|
|
|
|
|
SGUIMessage msg(GUIM_SETTINGS_UPDATED, Setting);
|
2019-07-27 19:39:52 -07:00
|
|
|
pObject->HandleMessage(msg);
|
2011-04-28 13:42:11 -07:00
|
|
|
}
|
2004-12-16 16:05:37 -08:00
|
|
|
|
2009-09-27 08:04:46 -07:00
|
|
|
return PSRETURN_OK;
|
2004-12-16 16:05:37 -08:00
|
|
|
}
|
|
|
|
|
|
2004-12-17 08:20:08 -08:00
|
|
|
// Instantiate templated functions:
|
2019-08-25 01:57:36 -07:00
|
|
|
// These functions avoid copies by working with a reference and move semantics.
|
2004-12-16 16:05:37 -08:00
|
|
|
#define TYPE(T) \
|
2019-07-28 15:40:58 -07:00
|
|
|
template PSRETURN GUI<T>::SetSetting(IGUIObject* pObject, const CStr& Setting, T& Value, const bool& SkipMessage); \
|
2019-08-03 19:20:08 -07:00
|
|
|
template class CGUISetting<T>; \
|
|
|
|
|
|
2004-12-16 16:05:37 -08:00
|
|
|
#include "GUItypes.h"
|
2010-02-17 16:06:50 -08:00
|
|
|
#undef TYPE
|
2004-12-16 16:05:37 -08:00
|
|
|
|
Use NONCOPYABLE for most GUI classes and structs to have the compiler indicate unintended copies, refs 3028551b91 / D2163.
That is CChartData, CGUIList, CGUISeries, COListColumn, GUITooltip,
SGUIMessage, SSpriteCall, STextCall, SFeedback, IGUISetting,
CGUISetting, GUI, IGUIObject, IGUIScrollBar.
Drop copying GetSetting and SetSetting template functions for CGUIList,
CGUISeries, CClientArea, CGUIString.
Stop copying COListColumn.
Drop copying GUI<CClientArea>::GetSetting call in
IGUIObject::UpdateCachedSize() and four copying
GUI<CGUIString>::GetSetting calls in SetupText() functions.
Delete unused GUIRenderer IGLState class from 849f50a500 obsolete since
1f5b8f1c9a.
Differential Revision: https://code.wildfiregames.com/D2164
This was SVN commit r22638.
2019-08-09 17:04:17 -07:00
|
|
|
// Copying functions - discouraged except for primitives.
|
|
|
|
|
#define TYPE(T) \
|
|
|
|
|
template PSRETURN GUI<T>::SetSetting(IGUIObject* pObject, const CStr& Setting, const T& Value, const bool& SkipMessage); \
|
|
|
|
|
|
|
|
|
|
#define GUITYPE_IGNORE_NONCOPYABLE
|
|
|
|
|
#include "GUItypes.h"
|
|
|
|
|
#undef GUITYPE_IGNORE_NONCOPYABLE
|
|
|
|
|
#undef TYPE
|