Changeset View
Changeset View
Standalone View
Standalone View
source/gui/CGUISetting.cpp
/* Copyright (C) 2019 Wildfire Games. | /* Copyright (C) 2021 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * 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/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "CGUISetting.h" | #include "CGUISetting.h" | ||||
#include "gui/CGUI.h" | #include "gui/CGUI.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
template<typename T> | IGUISetting::IGUISetting(const CStr& name, IGUIObject* owner) : m_pObject(*owner) | ||||
{ | |||||
m_pObject.RegisterSetting(name, this); | |||||
} | |||||
CGUISetting<T>::CGUISetting(IGUIObject& pObject, const CStr& Name, T& Value) | IGUISetting::IGUISetting(IGUISetting&& o) : m_pObject(o.m_pObject) | ||||
: m_pSetting(Value), m_Name(Name), m_pObject(pObject) | |||||
{ | { | ||||
m_pObject.ReregisterSetting(o.GetName(), this); | |||||
} | } | ||||
template<typename T> | bool IGUISetting::FromString(const CStrW& value, const bool sendMessage) | ||||
bool CGUISetting<T>::FromString(const CStrW& Value, const bool SendMessage) | { | ||||
m_pObject.PreSettingChange(GetName()); | |||||
if (!DoFromString(value)) | |||||
return false; | |||||
OnSettingChange(GetName(), sendMessage); | |||||
return true; | |||||
} | |||||
/** | |||||
* 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) | |||||
{ | { | ||||
T settingValue; | m_pObject.PreSettingChange(GetName()); | ||||
if (!CGUI::ParseString<T>(&m_pObject.GetGUI(), Value, settingValue)) | if (!DoFromJSVal(rq, value)) | ||||
return false; | return false; | ||||
m_pObject.SetSetting<T>(m_Name, settingValue, SendMessage); | OnSettingChange(GetName(), sendMessage); | ||||
return true; | return true; | ||||
} | |||||
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); | |||||
}; | }; | ||||
template<> | template<> | ||||
bool CGUISetting<CGUIColor>::FromJSVal(const ScriptRequest& rq, JS::HandleValue Value, const bool SendMessage) | bool CGUISimpleSetting<CGUIColor>::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) | ||||
{ | { | ||||
CGUIColor settingValue; | if (value.isString()) | ||||
if (Value.isString()) | |||||
{ | { | ||||
CStr name; | CStr name; | ||||
if (!ScriptInterface::FromJSVal(rq, Value, name)) | if (!ScriptInterface::FromJSVal(rq, value, name)) | ||||
return false; | return false; | ||||
if (!settingValue.ParseString(m_pObject.GetGUI(), name)) | if (!m_Setting.ParseString(m_pObject.GetGUI(), name)) | ||||
{ | { | ||||
LOGERROR("Invalid color '%s'", name.c_str()); | LOGERROR("Invalid color '%s'", name.c_str()); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
else if (!ScriptInterface::FromJSVal<CColor>(rq, Value, settingValue)) | |||||
return false; | |||||
m_pObject.SetSetting<CGUIColor>(m_Name, settingValue, SendMessage); | return ScriptInterface::FromJSVal<CColor>(rq, value, m_Setting); | ||||
return true; | |||||
}; | }; | ||||
template<typename T> | template<typename T> | ||||
bool CGUISetting<T>::FromJSVal(const ScriptRequest& rq, JS::HandleValue Value, const bool SendMessage) | bool CGUISimpleSetting<T>::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value) | ||||
{ | { | ||||
T settingValue; | return ScriptInterface::FromJSVal<T>(rq, value, m_Setting); | ||||
if (!ScriptInterface::FromJSVal<T>(rq, Value, settingValue)) | |||||
return false; | |||||
m_pObject.SetSetting<T>(m_Name, settingValue, SendMessage); | |||||
return true; | |||||
}; | }; | ||||
template<typename T> | template<typename T> | ||||
void CGUISetting<T>::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue Value) | void CGUISimpleSetting<T>::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue value) | ||||
{ | { | ||||
ScriptInterface::ToJSVal<T>(rq, Value, m_pSetting); | ScriptInterface::ToJSVal<T>(rq, value, m_Setting); | ||||
}; | }; | ||||
#define TYPE(T) \ | #define TYPE(T) \ | ||||
template class CGUISetting<T>; \ | template class CGUISimpleSetting<T>; \ | ||||
#include "gui/GUISettingTypes.h" | #include "gui/GUISettingTypes.h" | ||||
#undef TYPE | #undef TYPE |
Wildfire Games · Phabricator