1
0
forked from 0ad/0ad

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.
This commit is contained in:
elexis 2019-08-13 18:00:41 +00:00
parent 02d23d1ff0
commit 6ad90aa1b8
16 changed files with 132 additions and 103 deletions

View File

@ -196,7 +196,7 @@ void CChart::UpdateSeries()
{
CChartData& data = m_Series[i];
if (i < pSeriesColor->m_Items.size() && !data.m_Color.ParseString(pSeriesColor->m_Items[i].GetOriginalString().ToUTF8(), 0))
if (i < pSeriesColor->m_Items.size() && !data.m_Color.ParseString(m_pGUI, pSeriesColor->m_Items[i].GetOriginalString().ToUTF8(), 0))
LOGWARNING("GUI: Error parsing 'series_color' (\"%s\")", utf8_from_wstring(pSeriesColor->m_Items[i].GetOriginalString()));
data.m_Points = pSeries->m_Series[i];

View File

@ -359,7 +359,7 @@ void CGUI::DrawSprite(const CGUISpriteInstance& Sprite, int CellID, const float&
// TODO: Clipping?
Sprite.Draw(Rect, CellID, m_Sprites, Z);
Sprite.Draw(this, Rect, CellID, m_Sprites, Z);
}
void CGUI::Destroy()
@ -1450,7 +1450,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "size")
{
CClientArea ca;
if (!GUI<CClientArea>::ParseString(attr_value, ca))
if (!GUI<CClientArea>::ParseString(this, attr_value, ca))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_Size = ca;
@ -1458,7 +1458,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "texture_size")
{
CClientArea ca;
if (!GUI<CClientArea>::ParseString(attr_value, ca))
if (!GUI<CClientArea>::ParseString(this, attr_value, ca))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_TextureSize = ca;
@ -1466,7 +1466,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "real_texture_placement")
{
CRect rect;
if (!GUI<CRect>::ParseString(attr_value, rect))
if (!GUI<CRect>::ParseString(this, attr_value, rect))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_TexturePlacementInFile = rect;
@ -1474,7 +1474,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "cell_size")
{
CSize size;
if (!GUI<CSize>::ParseString(attr_value, size))
if (!GUI<CSize>::ParseString(this, attr_value, size))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_CellSize = size;
@ -1482,7 +1482,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "fixed_h_aspect_ratio")
{
float val;
if (!GUI<float>::ParseString(attr_value, val))
if (!GUI<float>::ParseString(this, attr_value, val))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_FixedHAspectRatio = val;
@ -1490,7 +1490,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "round_coordinates")
{
bool b;
if (!GUI<bool>::ParseString(attr_value, b))
if (!GUI<bool>::ParseString(this, attr_value, b))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_RoundCoordinates = b;
@ -1509,7 +1509,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "z_level")
{
float z_level;
if (!GUI<float>::ParseString(attr_value, z_level))
if (!GUI<float>::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;
@ -1517,7 +1517,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "backcolor")
{
CGUIColor color;
if (!GUI<CGUIColor>::ParseString(attr_value, color))
if (!GUI<CGUIColor>::ParseString(this, attr_value, color))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_BackColor = color;
@ -1525,7 +1525,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "bordercolor")
{
CGUIColor color;
if (!GUI<CGUIColor>::ParseString(attr_value, color))
if (!GUI<CGUIColor>::ParseString(this, attr_value, color))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_BorderColor = color;
@ -1533,7 +1533,7 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
else if (attr_name == "border")
{
bool b;
if (!GUI<bool>::ParseString(attr_value, b))
if (!GUI<bool>::ParseString(this, attr_value, b))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_Border = b;
@ -1572,7 +1572,7 @@ void CGUI::Xeromyces_ReadEffects(XMBElement Element, CXeromyces* pFile, SGUIImag
if (attr_name == "add_color")
{
CGUIColor color;
if (!color.ParseString(attr.Value, 0))
if (!color.ParseString(this, attr.Value, 0))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr.Value);
else effects.m_AddColor = color;
}
@ -1628,7 +1628,7 @@ void CGUI::Xeromyces_ReadScrollBarStyle(XMBElement Element, CXeromyces* pFile)
else if (attr_name == "show_edge_buttons")
{
bool b;
if (!GUI<bool>::ParseString(attr_value.FromUTF8(), b))
if (!GUI<bool>::ParseString(this, attr_value.FromUTF8(), b))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value);
else
scrollbar.m_UseEdgeButtons = b;
@ -1636,7 +1636,7 @@ void CGUI::Xeromyces_ReadScrollBarStyle(XMBElement Element, CXeromyces* pFile)
else if (attr_name == "width")
{
float f;
if (!GUI<float>::ParseString(attr_value.FromUTF8(), f))
if (!GUI<float>::ParseString(this, attr_value.FromUTF8(), f))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value);
else
scrollbar.m_Width = f;
@ -1644,7 +1644,7 @@ void CGUI::Xeromyces_ReadScrollBarStyle(XMBElement Element, CXeromyces* pFile)
else if (attr_name == "minimum_bar_size")
{
float f;
if (!GUI<float>::ParseString(attr_value.FromUTF8(), f))
if (!GUI<float>::ParseString(this, attr_value.FromUTF8(), f))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value);
else
scrollbar.m_MinimumBarSize = f;
@ -1652,7 +1652,7 @@ void CGUI::Xeromyces_ReadScrollBarStyle(XMBElement Element, CXeromyces* pFile)
else if (attr_name == "maximum_bar_size")
{
float f;
if (!GUI<float>::ParseString(attr_value.FromUTF8(), f))
if (!GUI<float>::ParseString(this, attr_value.FromUTF8(), f))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value);
else
scrollbar.m_MaximumBarSize = f;
@ -1707,7 +1707,7 @@ void CGUI::Xeromyces_ReadIcon(XMBElement Element, CXeromyces* pFile)
else if (attr_name == "size")
{
CSize size;
if (!GUI<CSize>::ParseString(attr_value.FromUTF8(), size))
if (!GUI<CSize>::ParseString(this, attr_value.FromUTF8(), size))
LOGERROR("Error parsing '%s' (\"%s\") inside <icon>.", attr_name, attr_value);
else
icon.m_Size = size;
@ -1715,7 +1715,7 @@ void CGUI::Xeromyces_ReadIcon(XMBElement Element, CXeromyces* pFile)
else if (attr_name == "cell_id")
{
int cell_id;
if (!GUI<int>::ParseString(attr_value.FromUTF8(), cell_id))
if (!GUI<int>::ParseString(this, attr_value.FromUTF8(), cell_id))
LOGERROR("GUI: Error parsing '%s' (\"%s\") inside <icon>.", attr_name, attr_value);
else
icon.m_CellID = cell_id;
@ -1759,7 +1759,7 @@ void CGUI::Xeromyces_ReadColor(XMBElement Element, CXeromyces* pFile)
return;
// Try setting color to value
if (!color.ParseString(value))
if (!color.ParseString(this, value))
{
LOGERROR("GUI: Unable to create custom color '%s'. Invalid color syntax.", name.c_str());
return;

27
source/gui/CGUIColor.cpp Normal file
View File

@ -0,0 +1,27 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "CGUIColor.h"
#include "ps/CStr.h"
#include "gui/CGUI.h"
bool CGUIColor::ParseString(const CGUI* pGUI, const CStr& value, int defaultAlpha)
{
return (pGUI != nullptr && pGUI->GetPreDefinedColor(value, *this)) || CColor::ParseString(value, defaultAlpha);
}

View File

@ -19,7 +19,9 @@
#define INCLUDED_GUICOLOR
#include "graphics/Color.h"
#include "gui/GUIManager.h"
#include "ps/CStr.h"
class CGUI;
/**
* Same as the CColor class, but this one can also parse colors predefined in the GUI page (such as "yellow").
@ -30,10 +32,14 @@ struct CGUIColor : CColor
CGUIColor(float r, float g, float b, float a) : CColor(r, g, b, a) {}
bool ParseString(const CStr& value, int defaultAlpha = 255)
{
return g_GUI->GetPreDefinedColor(value, *this) || CColor::ParseString(value, defaultAlpha);
}
/**
* Load color depending on current GUI page.
*/
bool ParseString(const CGUI* pGUI, const CStr& value, int defaultAlpha = 255);
/**
* Ensure that all users check for predefined colors.
*/
bool ParseString(const CStr& value, int defaultAlpha = 255) = delete;
};
#endif // INCLUDED_GUICOLOR

View File

@ -30,11 +30,11 @@ void CGUISprite::AddImage(SGUIImage* image)
m_Images.push_back(image);
}
void CGUISpriteInstance::Draw(const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites, float Z) const
void CGUISpriteInstance::Draw(const CGUI* pGUI, const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites, float Z) const
{
if (m_CachedSize != Size || m_CachedCellID != CellID)
{
GUIRenderer::UpdateDrawCallCache(m_DrawCallCache, m_SpriteName, Size, CellID, Sprites);
GUIRenderer::UpdateDrawCallCache(pGUI, m_DrawCallCache, m_SpriteName, Size, CellID, Sprites);
m_CachedSize = Size;
m_CachedCellID = CellID;
}

View File

@ -164,7 +164,7 @@ public:
CGUISpriteInstance();
CGUISpriteInstance(const CStr& SpriteName);
void Draw(const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites, float Z) const;
void Draw(const CGUI* pGUI, const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites, float Z) const;
bool IsEmpty() const;
const CStr& GetName() const { return m_SpriteName; }
void SetName(const CStr& SpriteName);

View File

@ -144,7 +144,7 @@ void CGUIString::GenerateTextCall(const CGUI* pGUI, SFeedback& Feedback, CStrInt
// Displace the sprite
CSize displacement;
// Parse the value
if (!GUI<CSize>::ParseString(tagAttrib.value, displacement))
if (!GUI<CSize>::ParseString(pGUI, tagAttrib.value, displacement))
LOGERROR("Error parsing 'displace' value for tag [ICON]");
else
SpriteCall.m_Area += displacement;
@ -190,7 +190,7 @@ void CGUIString::GenerateTextCall(const CGUI* pGUI, SFeedback& Feedback, CStrInt
case TextChunk::Tag::TAG_COLOR:
TextCall.m_UseCustomColor = true;
if (!GUI<CGUIColor>::ParseString(tag.m_TagValue, TextCall.m_Color) && pObject)
if (!GUI<CGUIColor>::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:

View File

@ -231,7 +231,7 @@ bool COList::HandleAdditionalChildren(const XMBElement& child, CXeromyces* pFile
if (attr_name == "color")
{
CGUIColor color;
if (!GUI<CGUIColor>::ParseString(attr_value.FromUTF8(), color))
if (!GUI<CGUIColor>::ParseString(m_pGUI, attr_value.FromUTF8(), color))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name.c_str(), attr_value.c_str());
else
column.m_TextColor = color;
@ -242,13 +242,13 @@ bool COList::HandleAdditionalChildren(const XMBElement& child, CXeromyces* pFile
}
else if (attr_name == "hidden")
{
if (!GUI<bool>::ParseString(attr_value.FromUTF8(), hidden))
if (!GUI<bool>::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<float>::ParseString(attr_value.FromUTF8(), width))
if (!GUI<float>::ParseString(m_pGUI, attr_value.FromUTF8(), width))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name.c_str(), attr_value.c_str());
else
{

View File

@ -350,12 +350,6 @@ InReaction CGUIManager::HandleEvent(const SDL_Event_* ev)
return IN_PASS;
}
bool CGUIManager::GetPreDefinedColor(const CStr& name, CGUIColor& output) const
{
return top()->GetPreDefinedColor(name, output);
}
void CGUIManager::SendEventToAll(const CStr& eventName) const
{
top()->SendEventToAll(eventName);

View File

@ -102,11 +102,6 @@ public:
*/
InReaction HandleEvent(const SDL_Event_* ev);
/**
* See CGUI::GetPreDefinedColor; applies to the currently active page.
*/
bool GetPreDefinedColor(const CStr& name, CGUIColor& output) const;
/**
* See CGUI::SendEventToAll; applies to the currently active page.
*/

View File

@ -59,7 +59,7 @@ DrawCalls& DrawCalls::operator=(const DrawCalls&)
}
void GUIRenderer::UpdateDrawCallCache(DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites)
void GUIRenderer::UpdateDrawCallCache(const CGUI* pGUI, DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites)
{
// This is called only when something has changed (like the size of the
// sprite), so it doesn't need to be particularly efficient.
@ -144,7 +144,7 @@ void GUIRenderer::UpdateDrawCallCache(DrawCalls& Calls, const CStr& SpriteName,
CGUIColor color;
// Check color is valid
if (!GUI<CGUIColor>::ParseString(value, color))
if (!GUI<CGUIColor>::ParseString(pGUI, value, color))
{
LOGERROR("GUI: Error parsing sprite 'color' (\"%s\")", utf8_from_wstring(value));
return;

View File

@ -71,7 +71,7 @@ namespace GUIRenderer
namespace GUIRenderer
{
void UpdateDrawCallCache(DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites);
void UpdateDrawCallCache(const CGUI* pGUI, DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites);
void Draw(DrawCalls& Calls, float Z);
}

View File

@ -34,13 +34,36 @@ bool CGUISetting<T>::FromString(const CStrW& Value, const bool& SkipMessage)
{
T settingValue;
if (!GUI<T>::ParseString(Value, settingValue))
if (!GUI<T>::ParseString(m_pObject.GetGUI(), Value, settingValue))
return false;
GUI<T>::SetSetting(&m_pObject, m_Name, settingValue, SkipMessage);
return true;
};
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;
};
template<typename T>
bool CGUISetting<T>::FromJSVal(JSContext* cx, JS::HandleValue Value)
{
@ -59,7 +82,7 @@ void CGUISetting<T>::ToJSVal(JSContext* cx, JS::MutableHandleValue Value)
};
template <>
bool __ParseString<bool>(const CStrW& Value, bool& Output)
bool __ParseString<bool>(const CGUI* UNUSED(pGUI), const CStrW& Value, bool& Output)
{
if (Value == L"true")
Output = true;
@ -72,28 +95,28 @@ bool __ParseString<bool>(const CStrW& Value, bool& Output)
}
template <>
bool __ParseString<int>(const CStrW& Value, int& Output)
bool __ParseString<int>(const CGUI* UNUSED(pGUI), const CStrW& Value, int& Output)
{
Output = Value.ToInt();
return true;
}
template <>
bool __ParseString<u32>(const CStrW& Value, u32& Output)
bool __ParseString<u32>(const CGUI* UNUSED(pGUI), const CStrW& Value, u32& Output)
{
Output = Value.ToUInt();
return true;
}
template <>
bool __ParseString<float>(const CStrW& Value, float& Output)
bool __ParseString<float>(const CGUI* UNUSED(pGUI), const CStrW& Value, float& Output)
{
Output = Value.ToFloat();
return true;
}
template <>
bool __ParseString<CRect>(const CStrW& Value, CRect& Output)
bool __ParseString<CRect>(const CGUI* UNUSED(pGUI), const CStrW& Value, CRect& Output)
{
const unsigned int NUM_COORDS = 4;
float coords[NUM_COORDS];
@ -128,19 +151,19 @@ bool __ParseString<CRect>(const CStrW& Value, CRect& Output)
}
template <>
bool __ParseString<CClientArea>(const CStrW& Value, CClientArea& Output)
bool __ParseString<CClientArea>(const CGUI* UNUSED(pGUI), const CStrW& Value, CClientArea& Output)
{
return Output.SetClientArea(Value.ToUTF8());
}
template <>
bool __ParseString<CGUIColor>(const CStrW& Value, CGUIColor& Output)
bool __ParseString<CGUIColor>(const CGUI* pGUI, const CStrW& Value, CGUIColor& Output)
{
return Output.ParseString(Value.ToUTF8());
return Output.ParseString(pGUI, Value.ToUTF8());
}
template <>
bool __ParseString<CSize>(const CStrW& Value, CSize& Output)
bool __ParseString<CSize>(const CGUI* UNUSED(pGUI), const CStrW& Value, CSize& Output)
{
const unsigned int NUM_COORDS = 2;
float coords[NUM_COORDS];
@ -175,7 +198,7 @@ bool __ParseString<CSize>(const CStrW& Value, CSize& Output)
}
template <>
bool __ParseString<CPos>(const CStrW& Value, CPos& Output)
bool __ParseString<CPos>(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& Output)
{
const unsigned int NUM_COORDS = 2;
float coords[NUM_COORDS];
@ -210,7 +233,7 @@ bool __ParseString<CPos>(const CStrW& Value, CPos& Output)
}
template <>
bool __ParseString<EAlign>(const CStrW& Value, EAlign& Output)
bool __ParseString<EAlign>(const CGUI* UNUSED(pGUI), const CStrW& Value, EAlign& Output)
{
if (Value == L"left")
Output = EAlign_Left;
@ -225,7 +248,7 @@ bool __ParseString<EAlign>(const CStrW& Value, EAlign& Output)
}
template <>
bool __ParseString<EVAlign>(const CStrW& Value, EVAlign& Output)
bool __ParseString<EVAlign>(const CGUI* UNUSED(pGUI), const CStrW& Value, EVAlign& Output)
{
if (Value == L"top")
Output = EVAlign_Top;
@ -240,42 +263,41 @@ bool __ParseString<EVAlign>(const CStrW& Value, EVAlign& Output)
}
template <>
bool __ParseString<CGUIString>(const CStrW& Value, CGUIString& Output)
bool __ParseString<CGUIString>(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUIString& Output)
{
Output.SetValue(Value);
return true;
}
template <>
bool __ParseString<CStr>(const CStrW& Value, CStr& Output)
bool __ParseString<CStr>(const CGUI* UNUSED(pGUI), const CStrW& Value, CStr& Output)
{
// Do very little.
Output = Value.ToUTF8();
return true;
}
template <>
bool __ParseString<CStrW>(const CStrW& Value, CStrW& Output)
bool __ParseString<CStrW>(const CGUI* UNUSED(pGUI), const CStrW& Value, CStrW& Output)
{
Output = Value;
return true;
}
template <>
bool __ParseString<CGUISpriteInstance>(const CStrW& Value, CGUISpriteInstance& Output)
bool __ParseString<CGUISpriteInstance>(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISpriteInstance& Output)
{
Output = CGUISpriteInstance(Value.ToUTF8());
return true;
}
template <>
bool __ParseString<CGUIList>(const CStrW& UNUSED(Value), CGUIList& UNUSED(Output))
bool __ParseString<CGUIList>(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUIList& UNUSED(Output))
{
return false;
}
template <>
bool __ParseString<CGUISeries>(const CStrW& UNUSED(Value), CGUISeries& UNUSED(Output))
bool __ParseString<CGUISeries>(const CGUI* UNUSED(pGUI), const CStrW& UNUSED(Value), CGUISeries& UNUSED(Output))
{
return false;
}

View File

@ -111,7 +111,7 @@ private:
};
template <typename T>
bool __ParseString(const CStrW& Value, T& tOutput);
bool __ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput);
struct SGUIMessage;
@ -206,9 +206,9 @@ public:
*
* @see __ParseString()
*/
static bool ParseString(const CStrW& Value, T& tOutput)
static bool ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput)
{
return __ParseString<T>(Value, tOutput);
return __ParseString<T>(pGUI, Value, tOutput);
}
private:

View File

@ -150,25 +150,10 @@ template<> void ScriptInterface::ToJSVal<CGUIColor>(JSContext* cx, JS::MutableHa
ToJSVal<CColor>(cx, ret, val);
}
template<> bool ScriptInterface::FromJSVal<CGUIColor>(JSContext* cx, JS::HandleValue v, CGUIColor& out)
{
if (v.isString())
{
CStr name;
if (!FromJSVal(cx, v, name))
return false;
if (!out.ParseString(name))
{
JS_ReportError(cx, "Invalid color '%s'", name.c_str());
return false;
}
return true;
}
// Parse as object
return FromJSVal<CColor>(cx, v, out);
}
/**
* The color depends on the predefined color database stored in the current GUI page.
*/
template<> bool ScriptInterface::FromJSVal<CGUIColor>(JSContext* cx, JS::HandleValue v, CGUIColor& out) = delete;
template<> void ScriptInterface::ToJSVal<CSize>(JSContext* cx, JS::MutableHandleValue ret, const CSize& val)
{

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games.
/* 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
@ -68,12 +68,12 @@ public:
TestLogger nolog;
CRect test;
TS_ASSERT(__ParseString(CStrW(L"0.0 10.0 20.0 30.0"), test));
TS_ASSERT(__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(!__ParseString(CStrW(L"0 10 20"), test));
TS_ASSERT(!__ParseString(CStrW(L"0 10 20 30 40"), test));
TS_ASSERT(!__ParseString(CStrW(L"0,0 10,0 20,0 30,0"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20 30 40"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0,0 10,0 20,0 30,0"), test));
}
void test_size()
@ -81,12 +81,12 @@ public:
TestLogger nolog;
CSize test;
TS_ASSERT(__ParseString(CStrW(L"0.0 10.0"), test));
TS_ASSERT(__ParseString(nullptr, CStrW(L"0.0 10.0"), test));
TS_ASSERT_EQUALS(CSize(0.0, 10.0), test);
TS_ASSERT(!__ParseString(CStrW(L"0"), test));
TS_ASSERT(!__ParseString(CStrW(L"0 10 20"), test));
TS_ASSERT(!__ParseString(CStrW(L"0,0 10,0"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0,0 10,0"), test));
}
void test_pos()
@ -94,11 +94,11 @@ public:
TestLogger nolog;
CPos test;
TS_ASSERT(__ParseString(CStrW(L"0.0 10.0"), test));
TS_ASSERT(__ParseString(nullptr, CStrW(L"0.0 10.0"), test));
TS_ASSERT_EQUALS(CPos(0.0, 10.0), test);
TS_ASSERT(!__ParseString(CStrW(L"0"), test));
TS_ASSERT(!__ParseString(CStrW(L"0 10 20"), test));
TS_ASSERT(!__ParseString(CStrW(L"0,0 10,0"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0 10 20"), test));
TS_ASSERT(!__ParseString(nullptr, CStrW(L"0,0 10,0"), test));
}
};