Index: ps/trunk/source/gui/CGUI.h =================================================================== --- ps/trunk/source/gui/CGUI.h +++ ps/trunk/source/gui/CGUI.h @@ -292,7 +292,21 @@ */ void SetFocusedObject(IGUIObject* pObject); + /** + * Reads a string value and modifies the given value of type T if successful. + * Does not change the value upon conversion failure. + * + * @param pGUI The GUI page which may contain data relevant to the parsing + * (for example predefined colors). + * @param Value The value in string form, like "0 0 100% 100%" + * @param tOutput Parsed value of type T + * @return True at success. + */ + template + static bool ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput); + private: + //-------------------------------------------------------- /** @name XML Reading Xeromyces specific subroutines * Index: ps/trunk/source/gui/CGUI.cpp =================================================================== --- ps/trunk/source/gui/CGUI.cpp +++ ps/trunk/source/gui/CGUI.cpp @@ -286,7 +286,7 @@ GuiScriptingInit(*m_ScriptInterface); m_ScriptInterface->LoadGlobalScripts(); - m_BaseObject = new CGUIDummyObject(*this); + m_BaseObject = new CGUIDummyObject(*this); } CGUI::~CGUI() @@ -1031,7 +1031,7 @@ else if (attr_name == "size") { CClientArea ca; - if (!GUI::ParseString(this, attr_value, ca)) + if (!ParseString(this, attr_value, ca)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_Size = ca; @@ -1039,7 +1039,7 @@ else if (attr_name == "texture_size") { CClientArea ca; - if (!GUI::ParseString(this, attr_value, ca)) + if (!ParseString(this, attr_value, ca)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_TextureSize = ca; @@ -1047,7 +1047,7 @@ else if (attr_name == "real_texture_placement") { CRect rect; - if (!GUI::ParseString(this, attr_value, rect)) + if (!ParseString(this, attr_value, rect)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_TexturePlacementInFile = rect; @@ -1055,7 +1055,7 @@ else if (attr_name == "cell_size") { CSize size; - if (!GUI::ParseString(this, attr_value, size)) + if (!ParseString(this, attr_value, size)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_CellSize = size; @@ -1063,7 +1063,7 @@ else if (attr_name == "fixed_h_aspect_ratio") { float val; - if (!GUI::ParseString(this, attr_value, val)) + if (!ParseString(this, attr_value, val)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_FixedHAspectRatio = val; @@ -1071,7 +1071,7 @@ else if (attr_name == "round_coordinates") { bool b; - if (!GUI::ParseString(this, attr_value, b)) + if (!ParseString(this, attr_value, b)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_RoundCoordinates = b; @@ -1090,25 +1090,25 @@ else if (attr_name == "z_level") { float z_level; - if (!GUI::ParseString(this, attr_value, z_level)) + if (!ParseString(this, attr_value, z_level)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_DeltaZ = z_level/100.f; } else if (attr_name == "backcolor") { - if (!GUI::ParseString(this, attr_value, Image->m_BackColor)) + if (!ParseString(this, attr_value, Image->m_BackColor)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); } else if (attr_name == "bordercolor") { - if (!GUI::ParseString(this, attr_value, Image->m_BorderColor)) + if (!ParseString(this, attr_value, Image->m_BorderColor)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); } else if (attr_name == "border") { bool b; - if (!GUI::ParseString(this, attr_value, b)) + if (!ParseString(this, attr_value, b)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); else Image->m_Border = b; @@ -1201,7 +1201,7 @@ else if (attr_name == "show_edge_buttons") { bool b; - if (!GUI::ParseString(this, attr_value.FromUTF8(), b)) + if (!ParseString(this, attr_value.FromUTF8(), b)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); else scrollbar.m_UseEdgeButtons = b; @@ -1209,7 +1209,7 @@ else if (attr_name == "width") { float f; - if (!GUI::ParseString(this, attr_value.FromUTF8(), f)) + if (!ParseString(this, attr_value.FromUTF8(), f)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); else scrollbar.m_Width = f; @@ -1217,7 +1217,7 @@ else if (attr_name == "minimum_bar_size") { float f; - if (!GUI::ParseString(this, attr_value.FromUTF8(), f)) + if (!ParseString(this, attr_value.FromUTF8(), f)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); else scrollbar.m_MinimumBarSize = f; @@ -1225,7 +1225,7 @@ else if (attr_name == "maximum_bar_size") { float f; - if (!GUI::ParseString(this, attr_value.FromUTF8(), f)) + if (!ParseString(this, attr_value.FromUTF8(), f)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); else scrollbar.m_MaximumBarSize = f; @@ -1280,7 +1280,7 @@ else if (attr_name == "size") { CSize size; - if (!GUI::ParseString(this, attr_value.FromUTF8(), size)) + if (!ParseString(this, attr_value.FromUTF8(), size)) LOGERROR("Error parsing '%s' (\"%s\") inside .", attr_name, attr_value); else icon.m_Size = size; @@ -1288,7 +1288,7 @@ else if (attr_name == "cell_id") { int cell_id; - if (!GUI::ParseString(this, attr_value.FromUTF8(), cell_id)) + if (!ParseString(this, attr_value.FromUTF8(), cell_id)) LOGERROR("GUI: Error parsing '%s' (\"%s\") inside .", attr_name, attr_value); else icon.m_CellID = cell_id; Index: ps/trunk/source/gui/CGUISetting.h =================================================================== --- ps/trunk/source/gui/CGUISetting.h +++ ps/trunk/source/gui/CGUISetting.h @@ -0,0 +1,96 @@ +/* Copyright (C) 2019 Wildfire Games. + * 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 . + */ + +#ifndef INCLUDED_CGUISETTINGS +#define INCLUDED_CGUISETTINGS + +#include "gui/IGUIObject.h" + +/** + * This setting interface allows GUI objects to call setting function functions without having to know the setting type. + * This is fact is used for setting the value from a JS value or XML value (string) and when deleting the setting, + * when the type of the setting value is not known in advance. + */ +class IGUISetting +{ +public: + NONCOPYABLE(IGUISetting); + + IGUISetting() = default; + virtual ~IGUISetting() = default; + + /** + * Parses the given string and assigns to the setting value. Used for parsing XML attributes. + */ + virtual bool FromString(const CStrW& Value, const bool SendMessage) = 0; + + /** + * Parses the given JS::Value using ScriptInterface::FromJSVal and assigns it to the setting data. + */ + virtual bool FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) = 0; + + /** + * Converts the setting data to a JS::Value using ScriptInterface::ToJSVal. + */ + virtual void ToJSVal(JSContext* cx, JS::MutableHandleValue Value) = 0; +}; + +template +class CGUISetting : public IGUISetting +{ +public: + NONCOPYABLE(CGUISetting); + + CGUISetting(IGUIObject& pObject, const CStr& Name); + + /** + * Parses the given string and assigns to the setting value. Used for parsing XML attributes. + */ + bool FromString(const CStrW& Value, const bool SendMessage) override; + + /** + * Parses the given JS::Value using ScriptInterface::FromJSVal and assigns it to the setting data. + */ + bool FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) override; + + /** + * Converts the setting data to a JS::Value using ScriptInterface::ToJSVal. + */ + void ToJSVal(JSContext* cx, JS::MutableHandleValue Value) override; + + /** + * These members are public because they are either unmodifiable or free to be modified. + * In particular it avoids the need for setter templates specialized depending on copiability. + */ + + /** + * The object that stores this setting. + */ + IGUIObject& m_pObject; + + /** + * Property name identifying the setting. + */ + const CStr m_Name; + + /** + * Holds the value of the setting. + */ + T m_pSetting; +}; + +#endif // INCLUDED_CGUISETTINGS Index: ps/trunk/source/gui/CGUISetting.cpp =================================================================== --- ps/trunk/source/gui/CGUISetting.cpp +++ ps/trunk/source/gui/CGUISetting.cpp @@ -0,0 +1,86 @@ +/* Copyright (C) 2019 Wildfire Games. + * 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 . + */ + +#include "precompiled.h" + +#include "CGUISetting.h" + +#include "gui/GUI.h" + +template +CGUISetting::CGUISetting(IGUIObject& pObject, const CStr& Name) + : m_pSetting(T()), m_Name(Name), m_pObject(pObject) +{ +} + +template +bool CGUISetting::FromString(const CStrW& Value, const bool SendMessage) +{ + T settingValue; + + if (!CGUI::ParseString(&m_pObject.GetGUI(), Value, settingValue)) + return false; + + m_pObject.SetSetting(m_Name, settingValue, SendMessage); + return true; +}; + +template<> +bool CGUISetting::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) +{ + 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(cx, Value, settingValue)) + return false; + + m_pObject.SetSetting(m_Name, settingValue, SendMessage); + return true; +}; + +template +bool CGUISetting::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) +{ + T settingValue; + if (!ScriptInterface::FromJSVal(cx, Value, settingValue)) + return false; + + m_pObject.SetSetting(m_Name, settingValue, SendMessage); + return true; +}; + +template +void CGUISetting::ToJSVal(JSContext* cx, JS::MutableHandleValue Value) +{ + ScriptInterface::ToJSVal(cx, Value, m_pSetting); +}; + +#define TYPE(T) \ + template class CGUISetting; \ + +#include "GUItypes.h" +#undef TYPE Index: ps/trunk/source/gui/CGUIString.cpp =================================================================== --- ps/trunk/source/gui/CGUIString.cpp +++ ps/trunk/source/gui/CGUIString.cpp @@ -146,7 +146,7 @@ // Displace the sprite CSize displacement; // Parse the value - if (!GUI::ParseString(&pGUI, tagAttrib.value, displacement)) + if (!CGUI::ParseString(&pGUI, tagAttrib.value, displacement)) LOGERROR("Error parsing 'displace' value for tag [ICON]"); else SpriteCall.m_Area += displacement; @@ -192,7 +192,7 @@ case TextChunk::Tag::TAG_COLOR: TextCall.m_UseCustomColor = true; - if (!GUI::ParseString(&pGUI, tag.m_TagValue, TextCall.m_Color) && pObject) + if (!CGUI::ParseString(&pGUI, tag.m_TagValue, TextCall.m_Color) && pObject) LOGERROR("Error parsing the value of a [color]-tag in GUI text when reading object \"%s\".", pObject->GetPresentableName().c_str()); break; case TextChunk::Tag::TAG_FONT: Index: ps/trunk/source/gui/CGUIText.h =================================================================== --- ps/trunk/source/gui/CGUIText.h +++ ps/trunk/source/gui/CGUIText.h @@ -20,7 +20,6 @@ #include "gui/CGUIColor.h" #include "gui/CGUISprite.h" -#include "gui/GUIutil.h" #include "graphics/TextRenderer.h" #include "ps/CStrIntern.h" #include "ps/Shapes.h" @@ -29,6 +28,7 @@ #include #include +class CGUI; class CGUIString; struct SGenerateTextImage; using SGenerateTextImages = std::array, 2>; Index: ps/trunk/source/gui/COList.cpp =================================================================== --- ps/trunk/source/gui/COList.cpp +++ ps/trunk/source/gui/COList.cpp @@ -208,7 +208,7 @@ if (attr_name == "color") { - if (!GUI::ParseString(&m_pGUI, attr_value.FromUTF8(), column.m_TextColor)) + if (!CGUI::ParseString(&m_pGUI, attr_value.FromUTF8(), column.m_TextColor)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name.c_str(), attr_value.c_str()); } else if (attr_name == "id") @@ -217,13 +217,13 @@ } else if (attr_name == "hidden") { - if (!GUI::ParseString(&m_pGUI, attr_value.FromUTF8(), hidden)) + if (!CGUI::ParseString(&m_pGUI, attr_value.FromUTF8(), hidden)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name.c_str(), attr_value.c_str()); } else if (attr_name == "width") { float width; - if (!GUI::ParseString(&m_pGUI, attr_value.FromUTF8(), width)) + if (!CGUI::ParseString(&m_pGUI, attr_value.FromUTF8(), width)) LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name.c_str(), attr_value.c_str()); else { Index: ps/trunk/source/gui/GUI.h =================================================================== --- ps/trunk/source/gui/GUI.h +++ ps/trunk/source/gui/GUI.h @@ -29,7 +29,6 @@ #include "CGUISeries.h" #include "CGUIText.h" #include "GUIbase.h" -#include "GUIutil.h" #include "IGUIButtonBehavior.h" #include "IGUIObject.h" #include "IGUIScrollBarOwner.h" // Required by IGUIScrollBar Index: ps/trunk/source/gui/GUIRenderer.cpp =================================================================== --- ps/trunk/source/gui/GUIRenderer.cpp +++ ps/trunk/source/gui/GUIRenderer.cpp @@ -21,9 +21,9 @@ #include "graphics/ShaderManager.h" #include "graphics/TextureManager.h" +#include "gui/CGUI.h" #include "gui/CGUIColor.h" #include "gui/CGUISprite.h" -#include "gui/GUIutil.h" #include "gui/GUIMatrix.h" #include "i18n/L10n.h" #include "lib/ogl.h" @@ -159,7 +159,7 @@ color = &Image->m_BackColor; // Check color is valid - if (!GUI::ParseString(&pGUI, value, *color)) + if (!CGUI::ParseString(&pGUI, value, *color)) { LOGERROR("GUI: Error parsing sprite 'color' (\"%s\")", utf8_from_wstring(value)); return; Index: ps/trunk/source/gui/GUIStringConversions.cpp =================================================================== --- ps/trunk/source/gui/GUIStringConversions.cpp +++ ps/trunk/source/gui/GUIStringConversions.cpp @@ -17,8 +17,7 @@ #include "precompiled.h" -#include "GUIutil.h" - +#include "gui/CGUI.h" #include "gui/CGUIString.h" #include "ps/CLogger.h" @@ -26,7 +25,7 @@ class CGUISeries; template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, bool& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, bool& Output) { if (Value == L"true") Output = true; @@ -39,28 +38,28 @@ } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, int& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, int& Output) { Output = Value.ToInt(); return true; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, u32& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, u32& Output) { Output = Value.ToUInt(); return true; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, float& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, float& Output) { Output = Value.ToFloat(); return true; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CRect& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CRect& Output) { const unsigned int NUM_COORDS = 4; float coords[NUM_COORDS]; @@ -95,19 +94,19 @@ } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CClientArea& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CClientArea& Output) { return Output.SetClientArea(Value.ToUTF8()); } template <> -bool GUI::ParseString(const CGUI* pGUI, const CStrW& Value, CGUIColor& Output) +bool CGUI::ParseString(const CGUI* pGUI, const CStrW& Value, CGUIColor& Output) { return Output.ParseString(*pGUI, Value.ToUTF8()); } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CSize& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CSize& Output) { const unsigned int NUM_COORDS = 2; float coords[NUM_COORDS]; @@ -142,7 +141,7 @@ } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& Output) { const unsigned int NUM_COORDS = 2; float coords[NUM_COORDS]; @@ -177,7 +176,7 @@ } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EAlign& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EAlign& Output) { if (Value == L"left") Output = EAlign_Left; @@ -192,7 +191,7 @@ } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EVAlign& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, EVAlign& Output) { if (Value == L"top") Output = EVAlign_Top; @@ -207,41 +206,41 @@ } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUIString& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUIString& Output) { Output.SetValue(Value); return true; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStr& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStr& Output) { Output = Value.ToUTF8(); return true; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStrW& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CStrW& Output) { Output = Value; return true; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISpriteInstance& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISpriteInstance& Output) { Output = CGUISpriteInstance(Value.ToUTF8()); return true; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUISeries& UNUSED(Output)) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUISeries& UNUSED(Output)) { return false; } template <> -bool GUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUIList& UNUSED(Output)) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUIList& UNUSED(Output)) { return false; } Index: ps/trunk/source/gui/GUIutil.h =================================================================== --- ps/trunk/source/gui/GUIutil.h +++ ps/trunk/source/gui/GUIutil.h @@ -1,113 +0,0 @@ -/* Copyright (C) 2019 Wildfire Games. - * 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 . - */ - -#ifndef INCLUDED_GUIUTIL -#define INCLUDED_GUIUTIL - -#include "gui/IGUIObject.h" - -class CGUI; -template class GUI; - -class IGUISetting -{ -public: - NONCOPYABLE(IGUISetting); - - IGUISetting() = default; - virtual ~IGUISetting() = default; - - /** - * Parses the given string and assigns to the setting value. Used for parsing XML attributes. - */ - virtual bool FromString(const CStrW& Value, const bool SendMessage) = 0; - - /** - * Parses the given JS::Value using ScriptInterface::FromJSVal and assigns it to the setting data. - */ - virtual bool FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) = 0; - - /** - * Converts the setting data to a JS::Value using ScriptInterface::ToJSVal. - */ - virtual void ToJSVal(JSContext* cx, JS::MutableHandleValue Value) = 0; -}; - -template -class CGUISetting : public IGUISetting -{ - friend class GUI; - -public: - NONCOPYABLE(CGUISetting); - - CGUISetting(IGUIObject& pObject, const CStr& Name); - - /** - * Parses the given string and assigns to the setting value. Used for parsing XML attributes. - */ - bool FromString(const CStrW& Value, const bool SendMessage) override; - - /** - * Parses the given JS::Value using ScriptInterface::FromJSVal and assigns it to the setting data. - */ - bool FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) override; - - /** - * Converts the setting data to a JS::Value using ScriptInterface::ToJSVal. - */ - void ToJSVal(JSContext* cx, JS::MutableHandleValue Value) override; - - /** - * These members are public because they are either unmodifiable or free to be modified. - * In particular it avoids the need for setter templates specialized depending on copiability. - */ - - /** - * The object that stores this setting. - */ - IGUIObject& m_pObject; - - /** - * Property name identifying the setting. - */ - const CStr m_Name; - - /** - * Holds the value of the setting. - */ - T m_pSetting; -}; - -template -class GUI -{ -public: - NONCOPYABLE(GUI); - - /** - * Sets a value by setting and object name using a real - * datatype as input. - * - * @param Value The value in string form, like "0 0 100% 100%" - * @param tOutput Parsed value of type T - * @return True at success. - */ - static bool ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput); -}; - -#endif // INCLUDED_GUIUTIL Index: ps/trunk/source/gui/GUIutil.cpp =================================================================== --- ps/trunk/source/gui/GUIutil.cpp +++ ps/trunk/source/gui/GUIutil.cpp @@ -1,86 +0,0 @@ -/* Copyright (C) 2019 Wildfire Games. - * 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 . - */ - -#include "precompiled.h" - -#include "GUIutil.h" - -#include "gui/GUI.h" - -template -CGUISetting::CGUISetting(IGUIObject& pObject, const CStr& Name) - : m_pSetting(T()), m_Name(Name), m_pObject(pObject) -{ -} - -template -bool CGUISetting::FromString(const CStrW& Value, const bool SendMessage) -{ - T settingValue; - - if (!GUI::ParseString(&m_pObject.GetGUI(), Value, settingValue)) - return false; - - m_pObject.SetSetting(m_Name, settingValue, SendMessage); - return true; -}; - -template<> -bool CGUISetting::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) -{ - 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(cx, Value, settingValue)) - return false; - - m_pObject.SetSetting(m_Name, settingValue, SendMessage); - return true; -}; - -template -bool CGUISetting::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage) -{ - T settingValue; - if (!ScriptInterface::FromJSVal(cx, Value, settingValue)) - return false; - - m_pObject.SetSetting(m_Name, settingValue, SendMessage); - return true; -}; - -template -void CGUISetting::ToJSVal(JSContext* cx, JS::MutableHandleValue Value) -{ - ScriptInterface::ToJSVal(cx, Value, m_pSetting); -}; - -#define TYPE(T) \ - template class CGUISetting; \ - -#include "GUItypes.h" -#undef TYPE Index: ps/trunk/source/gui/IGUIButtonBehavior.h =================================================================== --- ps/trunk/source/gui/IGUIButtonBehavior.h +++ ps/trunk/source/gui/IGUIButtonBehavior.h @@ -36,6 +36,7 @@ #define INCLUDED_IGUIBUTTONBEHAVIOR #include "gui/GUI.h" +#include "gui/IGUIObject.h" class CGUISpriteInstance; Index: ps/trunk/source/gui/IGUIObject.cpp =================================================================== --- ps/trunk/source/gui/IGUIObject.cpp +++ ps/trunk/source/gui/IGUIObject.cpp @@ -19,6 +19,7 @@ #include "GUI.h" +#include "gui/CGUISetting.h" #include "gui/scripting/JSInterface_GUITypes.h" #include "gui/scripting/JSInterface_IGUIObject.h" #include "ps/GameSetup/Config.h" Index: ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp +++ ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -21,6 +21,7 @@ #include "gui/CGUI.h" #include "gui/CGUIColor.h" +#include "gui/CGUISetting.h" #include "gui/CList.h" #include "gui/GUIManager.h" #include "gui/IGUIObject.h" Index: ps/trunk/source/gui/tests/test_ParseString.h =================================================================== --- ps/trunk/source/gui/tests/test_ParseString.h +++ ps/trunk/source/gui/tests/test_ParseString.h @@ -18,7 +18,7 @@ #include "lib/self_test.h" #include "gui/GUIbase.h" -#include "gui/GUIutil.h" +#include "gui/CGUI.h" #include "ps/CLogger.h" class TestGuiParseString : public CxxTest::TestSuite @@ -68,12 +68,12 @@ TestLogger nolog; CRect test; - TS_ASSERT(GUI::ParseString(nullptr, CStrW(L"0.0 10.0 20.0 30.0"), test)); + TS_ASSERT(CGUI::ParseString(nullptr, CStrW(L"0.0 10.0 20.0 30.0"), test)); TS_ASSERT_EQUALS(CRect(0.0, 10.0, 20.0, 30.0), test); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20 30 40"), test)); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0,0 10,0 20,0 30,0"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0 10 20 30 40"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0,0 10,0 20,0 30,0"), test)); } void test_size() @@ -81,12 +81,12 @@ TestLogger nolog; CSize test; - TS_ASSERT(GUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); + TS_ASSERT(CGUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); TS_ASSERT_EQUALS(CSize(0.0, 10.0), test); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0"), test)); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); } void test_pos() @@ -94,11 +94,11 @@ TestLogger nolog; CPos test; - TS_ASSERT(GUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); + TS_ASSERT(CGUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); TS_ASSERT_EQUALS(CPos(0.0, 10.0), test); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0"), test)); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); - TS_ASSERT(!GUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); } };