Changeset View
Changeset View
Standalone View
Standalone View
source/gui/CGUISetting.cpp
Show All 17 Lines | |||||
#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" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.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) | |||||
{ | { | ||||
T settingValue; | if (!DoFromString(value)) | ||||
return false; | |||||
OnSettingChange(GetName(), sendMessage); | |||||
return true; | |||||
} | |||||
if (!CGUI::ParseString<T>(&m_pObject.GetGUI(), Value, settingValue)) | /** | ||||
* 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)) | |||||
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); | ||||
}; | }; | ||||
/** | |||||
* Explicitly instantiate CGUISimpleSetting for the basic types. | |||||
*/ | |||||
#define TYPE(T) \ | #define TYPE(T) \ | ||||
template class CGUISetting<T>; \ | 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) | |||||
#include "gui/GUISettingTypes.h" | |||||
#undef TYPE | #undef TYPE |
Wildfire Games · Phabricator