1
0
forked from 0ad/0ad

Rename CClientArea to CGUISize, GUIbase.h to CGUISize.h, avoid temporary instances and default value and error duplication.

The name is in accordance with its JS classname, member names and
setting names.

Differential Revision: https://code.wildfiregames.com/D2341
Tested on: clang 8.0.1, Jenkins/vs2015

This was SVN commit r23023.
This commit is contained in:
elexis 2019-10-01 15:06:13 +00:00
parent 07cc1ba34c
commit 32d6d07eda
12 changed files with 94 additions and 111 deletions

View File

@ -1028,10 +1028,7 @@ void CGUI::Xeromyces_ReadSprite(XMBElement Element, CXeromyces* pFile)
void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite& parent)
{
SGUIImage* Image = new SGUIImage;
Image->m_TextureSize = CClientArea(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100));
Image->m_Size = CClientArea(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100));
SGUIImage* Image = new SGUIImage();
// TODO Gee: Setup defaults here (or maybe they are in the SGUIImage ctor)
@ -1046,19 +1043,11 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite
}
else if (attr_name == "size")
{
CClientArea ca;
if (!ParseString<CClientArea>(this, attr_value, ca))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_Size = ca;
Image->m_Size.FromString(attr.Value);
}
else if (attr_name == "texture_size")
{
CClientArea ca;
if (!ParseString<CClientArea>(this, attr_value, ca))
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value));
else
Image->m_TextureSize = ca;
Image->m_TextureSize.FromString(attr.Value);
}
else if (attr_name == "real_texture_placement")
{

View File

@ -17,29 +17,30 @@
#include "precompiled.h"
#include "GUIbase.h"
#include "CGUISize.h"
#include "gui/scripting/JSInterface_GUISize.h"
#include "ps/CLogger.h"
CClientArea::CClientArea() : pixel(0.f,0.f,0.f,0.f), percent(0.f,0.f,0.f,0.f)
CGUISize::CGUISize()
: pixel(), percent()
{
}
CClientArea::CClientArea(const CStr& Value)
{
SetClientArea(Value);
}
CClientArea::CClientArea(const CRect& pixel, const CRect& percent)
CGUISize::CGUISize(const CRect& pixel, const CRect& percent)
: pixel(pixel), percent(percent)
{
}
CRect CClientArea::GetClientArea(const CRect& parent) const
CGUISize CGUISize::Full()
{
return CGUISize(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100));
}
CRect CGUISize::GetSize(const CRect& parent) const
{
// If it's a 0 0 100% 100% we need no calculations
if (percent == CRect(0.f,0.f,100.f,100.f) && pixel == CRect(0.f,0.f,0.f,0.f))
if (percent == CRect(0.f, 0.f, 100.f, 100.f) && pixel == CRect())
return parent;
CRect client;
@ -53,9 +54,10 @@ CRect CClientArea::GetClientArea(const CRect& parent) const
return client;
}
bool CClientArea::SetClientArea(const CStr& Value)
bool CGUISize::FromString(const CStr& Value)
{
/* ClientAreas contain a left, top, right, and bottom
/*
* GUISizes contain a left, top, right, and bottom
* for example: "50%-150 10%+9 50%+150 10%+25" means
* the left edge is at 50% minus 150 pixels, the top
* edge is at 10% plus 9 pixels, the right edge is at
@ -107,19 +109,19 @@ bool CClientArea::SetClientArea(const CStr& Value)
++coord;
break;
default:
LOGWARNING("ClientArea definitions may only contain numerics. Your input: '%s'", Value.c_str());
LOGERROR("CGUISize definition may only include numbers. Your input: '%s'", Value.c_str());
return false;
}
if (coord > 3)
{
LOGWARNING("Too many CClientArea parameters (4 max). Your input: '%s'", Value.c_str());
LOGERROR("Too many CGUISize parameters (4 max). Your input: '%s'", Value.c_str());
return false;
}
}
if (coord < 3)
{
LOGWARNING("Too few CClientArea parameters (4 min). Your input: '%s'", Value.c_str());
LOGERROR("Too few CGUISize parameters (4 min). Your input: '%s'", Value.c_str());
return false;
}
@ -138,7 +140,7 @@ bool CClientArea::SetClientArea(const CStr& Value)
return true;
}
void CClientArea::ToJSVal(JSContext* cx, JS::MutableHandleValue ret) const
void CGUISize::ToJSVal(JSContext* cx, JS::MutableHandleValue ret) const
{
JSAutoRequest rq(cx);
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
@ -146,14 +148,14 @@ void CClientArea::ToJSVal(JSContext* cx, JS::MutableHandleValue ret) const
if (!ret.isObject())
{
JS_ReportError(cx, "CClientArea value is not an Object");
JS_ReportError(cx, "CGUISize value is not an Object");
return;
}
JS::RootedObject obj(cx, &ret.toObject());
if (!JS_InstanceOf(cx, obj, &JSI_GUISize::JSI_class, nullptr))
{
JS_ReportError(cx, "CClientArea value is not a CClientArea class instance");
JS_ReportError(cx, "CGUISize value is not a CGUISize class instance");
return;
}
@ -174,7 +176,7 @@ void CClientArea::ToJSVal(JSContext* cx, JS::MutableHandleValue ret) const
#undef P
}
bool CClientArea::FromJSVal(JSContext* cx, JS::HandleValue v)
bool CGUISize::FromJSVal(JSContext* cx, JS::HandleValue v)
{
JSAutoRequest rq(cx);
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
@ -184,13 +186,13 @@ bool CClientArea::FromJSVal(JSContext* cx, JS::HandleValue v)
CStrW str;
if (!ScriptInterface::FromJSVal(cx, v, str))
{
JS_ReportError(cx, "Could not read CClientArea string");
JS_ReportError(cx, "CGUISize could not read JS string");
return false;
}
if (!SetClientArea(str.ToUTF8()))
if (!FromString(str.ToUTF8()))
{
JS_ReportError(cx, "Could not set SetClientArea");
JS_ReportError(cx, "CGUISize could not parse JS string");
return false;
}
return true;
@ -198,21 +200,21 @@ bool CClientArea::FromJSVal(JSContext* cx, JS::HandleValue v)
if (!v.isObject())
{
JS_ReportError(cx, "CClientArea value is not an String, nor Object");
JS_ReportError(cx, "CGUISize value is not an String, nor Object");
return false;
}
JS::RootedObject obj(cx, &v.toObject());
if (!JS_InstanceOf(cx, obj, &JSI_GUISize::JSI_class, nullptr))
{
JS_ReportError(cx, "CClientArea value is not a CClientArea class instance");
JS_ReportError(cx, "CGUISize value is not a CGUISize class instance");
return false;
}
#define P(x, y, z) \
if (!pScriptInterface->GetProperty(v, #z, x.y))\
{\
JS_ReportError(cx, "CClientArea could not get object property '%s'", #z);\
JS_ReportError(cx, "CGUISize could not get object property '%s'", #z);\
return false;\
}

View File

@ -15,8 +15,8 @@
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_GUIBASE
#define INCLUDED_GUIBASE
#ifndef INCLUDED_CGUISIZE
#define INCLUDED_CGUISIZE
#include "ps/CStr.h"
#include "ps/Errors.h"
@ -24,19 +24,18 @@
#include "scriptinterface/ScriptInterface.h"
/**
* Client Area is a rectangle relative to a parent rectangle
*
* You can input the whole value of the Client Area by
* string. Like used in the GUI.
* This class represents a rectangle relative to a parent rectangle
* The value can be initialized from a string or JS object.
*/
class CClientArea
class CGUISize
{
public:
// COPYABLE, since there are only primitives involved, making move and copy identical,
// and since some temporaries cannot be avoided.
CClientArea();
CClientArea(const CStr& Value);
CClientArea(const CRect& pixel, const CRect& percent);
CGUISize();
CGUISize(const CRect& pixel, const CRect& percent);
static CGUISize Full();
/// Pixel modifiers
CRect pixel;
@ -47,10 +46,10 @@ public:
/**
* Get client area rectangle when the parent is given
*/
CRect GetClientArea(const CRect& parent) const;
CRect GetSize(const CRect& parent) const;
/**
* The ClientArea can be set from a string looking like:
* The value can be set from a string looking like:
*
* "0 0 100% 100%"
* "50%-10 50%-10 50%+10 50%+10"
@ -60,12 +59,11 @@ public:
* though that the percent modifier must always be the first when
* both modifiers are inputted.
*
* @return true if success, false if failure. If false then the client area
* will be unchanged.
* @return true if success, otherwise size will remain unchanged.
*/
bool SetClientArea(const CStr& Value);
bool FromString(const CStr& Value);
bool operator==(const CClientArea& other) const
bool operator==(const CGUISize& other) const
{
return pixel == other.pixel && percent == other.percent;
}
@ -82,4 +80,4 @@ ERROR_TYPE(GUI, OperationNeedsGUIObject);
ERROR_TYPE(GUI, NameAmbiguity);
ERROR_TYPE(GUI, ObjectNeedsName);
#endif // INCLUDED_GUIBASE
#endif // INCLUDED_CGUISIZE

View File

@ -23,7 +23,7 @@
#ifndef INCLUDED_CGUISPRITE
#define INCLUDED_CGUISPRITE
#include "gui/GUIbase.h"
#include "gui/CGUISize.h"
#include "gui/GUIRenderer.h"
#include <map>
@ -47,8 +47,14 @@ struct SGUIImage
NONCOPYABLE(SGUIImage);
public:
SGUIImage() :
m_FixedHAspectRatio(0.f), m_RoundCoordinates(true), m_WrapMode(GL_REPEAT),
m_Effects(), m_Border(false), m_DeltaZ(0.f)
m_FixedHAspectRatio(0.f),
m_RoundCoordinates(true),
m_WrapMode(GL_REPEAT),
m_Effects(),
m_Border(false),
m_DeltaZ(0.f),
m_Size(CGUISize::Full()),
m_TextureSize(CGUISize::Full())
{
}
@ -56,10 +62,10 @@ public:
VfsPath m_TextureName;
// Image placement (relative to object)
CClientArea m_Size;
CGUISize m_Size;
// Texture placement (relative to image placement)
CClientArea m_TextureSize;
CGUISize m_TextureSize;
// Because OpenGL wants textures in squares with a power of 2 (64x64, 256x256)
// it's sometimes tedious to adjust this. So this value simulates which area

View File

@ -90,7 +90,7 @@ void CTooltip::SetupText()
float textwidth = m_GeneratedTexts[0].GetSize().cx;
float textheight = m_GeneratedTexts[0].GetSize().cy;
CClientArea size;
CGUISize size;
size.pixel.left = mousepos.x + m_Offset.x;
size.pixel.right = size.pixel.left + textwidth;
@ -130,7 +130,7 @@ void CTooltip::SetupText()
else if (size.pixel.right > screenw)
size.pixel.left -= (size.pixel.right-screenw), size.pixel.right = screenw;
SetSetting<CClientArea>("size", size, true);
SetSetting<CGUISize>("size", size, true);
}
void CTooltip::UpdateCachedSize()

View File

@ -99,7 +99,7 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
if (SpriteName.Find("stretched:") != -1)
{
// TODO: Should check (nicely) that this is a valid file?
SGUIImage* Image = new SGUIImage;
SGUIImage* Image = new SGUIImage();
Image->m_TextureName = TextureName;
// Allow grayscale images for disabled portraits
@ -109,10 +109,6 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
Image->m_Effects->m_Greyscale = true;
}
CClientArea ca(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100));
Image->m_Size = ca;
Image->m_TextureSize = ca;
Sprite->AddImage(Image);
Sprites[SpriteName] = Sprite;
@ -120,19 +116,15 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
else if (SpriteName.Find("cropped:") != -1)
{
// TODO: Should check (nicely) that this is a valid file?
SGUIImage* Image = new SGUIImage;
SGUIImage* Image = new SGUIImage();
CStr info = SpriteName.AfterLast("cropped:").BeforeFirst(":");
double xRatio = info.BeforeFirst(",").ToDouble();
double yRatio = info.AfterLast(",").ToDouble();
Image->m_TextureSize = CGUISize(CRect(0, 0, 0, 0), CRect(0, 0, 100/xRatio, 100/yRatio));
Image->m_TextureName = TextureName;
CClientArea ca(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100));
CClientArea cb(CRect(0, 0, 0, 0), CRect(0, 0, 100/xRatio, 100/yRatio));
Image->m_Size = ca;
Image->m_TextureSize = cb;
Sprite->AddImage(Image);
Sprites[SpriteName] = Sprite;
@ -141,7 +133,7 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
{
CStrW value = wstring_from_utf8(SpriteName.AfterLast("color:").BeforeFirst(":"));
SGUIImage* Image = new SGUIImage;
SGUIImage* Image = new SGUIImage();
CGUIColor* color;
// If we are using a mask, this is an effect.
@ -163,10 +155,6 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
return;
}
CClientArea ca(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100));
Image->m_Size = ca;
Image->m_TextureSize = ca;
Sprite->AddImage(Image);
Sprites[SpriteName] = Sprite;
@ -191,7 +179,7 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
{
SDrawCall Call(*cit); // pointers are safe since we never modify sprites/images after startup
CRect ObjectSize = (*cit)->m_Size.GetClientArea(Size);
CRect ObjectSize = (*cit)->m_Size.GetSize(Size);
if (ObjectSize.GetWidth() == 0.0 || ObjectSize.GetHeight() == 0.0)
{
@ -286,7 +274,7 @@ CRect SDrawCall::ComputeTexCoords() const
// the screen. The texture is positioned to make those blocks line up.
// Get the screen's position/size for the block
CRect BlockScreen = m_Image->m_TextureSize.GetClientArea(m_ObjectSize);
CRect BlockScreen = m_Image->m_TextureSize.GetSize(m_ObjectSize);
if (m_Image->m_FixedHAspectRatio)
BlockScreen.right = BlockScreen.left + BlockScreen.GetHeight() * m_Image->m_FixedHAspectRatio;

View File

@ -94,9 +94,9 @@ bool CGUI::ParseString<CRect>(const CGUI* UNUSED(pGUI), const CStrW& Value, CRec
}
template <>
bool CGUI::ParseString<CClientArea>(const CGUI* UNUSED(pGUI), const CStrW& Value, CClientArea& Output)
bool CGUI::ParseString<CGUISize>(const CGUI* UNUSED(pGUI), const CStrW& Value, CGUISize& Output)
{
return Output.SetClientArea(Value.ToUTF8());
return Output.FromString(Value.ToUTF8());
}
template <>

View File

@ -39,7 +39,7 @@ TYPE(CPos)
#ifndef GUITYPE_IGNORE_NONCOPYABLE
#include "gui/CGUIList.h"
#include "gui/CGUISeries.h"
TYPE(CClientArea)
TYPE(CGUISize)
TYPE(CGUIColor)
TYPE(CGUIList)
TYPE(CGUISeries)

View File

@ -284,9 +284,9 @@ void IGUIObject::UpdateCachedSize()
// use its cached size instead of the screen. Notice
// it must have just been cached for it to work.
if (!m_Absolute && m_pParent && !IsRootObject())
m_CachedActualSize = m_Size.GetClientArea(m_pParent->m_CachedActualSize);
m_CachedActualSize = m_Size.GetSize(m_pParent->m_CachedActualSize);
else
m_CachedActualSize = m_Size.GetClientArea(CRect(0.f, 0.f, g_xres / g_GuiScale, g_yres / g_GuiScale));
m_CachedActualSize = m_Size.GetSize(CRect(0.f, 0.f, g_xres / g_GuiScale, g_yres / g_GuiScale));
// In a few cases, GUI objects have to resize to fill the screen
// but maintain a constant aspect ratio.

View File

@ -25,7 +25,7 @@
#ifndef INCLUDED_IGUIOBJECT
#define INCLUDED_IGUIOBJECT
#include "gui/GUIbase.h"
#include "gui/CGUISize.h"
#include "gui/scripting/JSInterface_IGUIObject.h"
#include "gui/SGUIMessage.h"
#include "lib/input.h" // just for IN_PASS
@ -522,7 +522,7 @@ protected:
// Cache references to settings for performance
bool m_Enabled;
bool m_Hidden;
CClientArea m_Size;
CGUISize m_Size;
CStr m_Style;
CStr m_Hotkey;
float m_Z;

View File

@ -20,7 +20,7 @@
#include "gui/CGUIColor.h"
#include "gui/CGUIList.h"
#include "gui/CGUISeries.h"
#include "gui/GUIbase.h"
#include "gui/CGUISize.h"
#include "gui/IGUIObject.h"
#include "lib/external_libraries/libsdl.h"
#include "maths/Vector2D.h"
@ -230,12 +230,12 @@ template<> void ScriptInterface::ToJSVal<CRect>(JSContext* cx, JS::MutableHandle
"bottom", val.bottom);
}
template<> void ScriptInterface::ToJSVal<CClientArea>(JSContext* cx, JS::MutableHandleValue ret, const CClientArea& val)
template<> void ScriptInterface::ToJSVal<CGUISize>(JSContext* cx, JS::MutableHandleValue ret, const CGUISize& val)
{
val.ToJSVal(cx, ret);
}
template<> bool ScriptInterface::FromJSVal<CClientArea>(JSContext* cx, JS::HandleValue v, CClientArea& out)
template<> bool ScriptInterface::FromJSVal<CGUISize>(JSContext* cx, JS::HandleValue v, CGUISize& out)
{
return out.FromJSVal(cx, v);
}

View File

@ -17,50 +17,50 @@
#include "lib/self_test.h"
#include "gui/GUIbase.h"
#include "gui/CGUISize.h"
#include "gui/CGUI.h"
#include "ps/CLogger.h"
class TestGuiParseString : public CxxTest::TestSuite
{
public:
void test_clientarea()
void test_guisize()
{
TestLogger nolog;
CClientArea ca;
CGUISize size;
// Test only pixels
TS_ASSERT(ca.SetClientArea("0.0 -10 20.0 -30"));
TS_ASSERT_EQUALS(ca, CClientArea(CRect(0, -10, 20, -30), CRect(0, 0, 0, 0)));
TS_ASSERT(size.FromString("0.0 -10 20.0 -30"));
TS_ASSERT_EQUALS(size, CGUISize(CRect(0, -10, 20, -30), CRect(0, 0, 0, 0)));
// Test only pixels, but with math
TS_ASSERT(ca.SetClientArea("0 -100-10+100 20+200-200 -30"));
TS_ASSERT_EQUALS(ca, CClientArea(CRect(0, -10, 20, -30), CRect(0, 0, 0, 0)));
TS_ASSERT(size.FromString("0 -100-10+100 20+200-200 -30"));
TS_ASSERT_EQUALS(size, CGUISize(CRect(0, -10, 20, -30), CRect(0, 0, 0, 0)));
// Test only percent
TS_ASSERT(ca.SetClientArea("-5% 10.0% -20% 30.0%"));
TS_ASSERT_EQUALS(ca, CClientArea(CRect(0, 0, 0, 0), CRect(-5, 10, -20, 30)));
TS_ASSERT(size.FromString("-5% 10.0% -20% 30.0%"));
TS_ASSERT_EQUALS(size, CGUISize(CRect(0, 0, 0, 0), CRect(-5, 10, -20, 30)));
// Test only percent, but with math
TS_ASSERT(ca.SetClientArea("15%-5%-15% 10% -20% 30%+500%-500%"));
TS_ASSERT_EQUALS(ca, CClientArea(CRect(0, 0, 0, 0), CRect(-5, 10, -20, 30)));
TS_ASSERT(size.FromString("15%-5%-15% 10% -20% 30%+500%-500%"));
TS_ASSERT_EQUALS(size, CGUISize(CRect(0, 0, 0, 0), CRect(-5, 10, -20, 30)));
// Test mixed
TS_ASSERT(ca.SetClientArea("5% -10 -20% 30"));
TS_ASSERT_EQUALS(ca, CClientArea(CRect(0, -10, 0, 30), CRect(5, 0, -20, 0)));
TS_ASSERT(size.FromString("5% -10 -20% 30"));
TS_ASSERT_EQUALS(size, CGUISize(CRect(0, -10, 0, 30), CRect(5, 0, -20, 0)));
// Test mixed with math
TS_ASSERT(ca.SetClientArea("5%+10%-10% 30%-10-30% 50-20%-50 30-100+100"));
TS_ASSERT_EQUALS(ca, CClientArea(CRect(0, -10, 0, 30), CRect(5, 0, -20, 0)));
TS_ASSERT(size.FromString("5%+10%-10% 30%-10-30% 50-20%-50 30-100+100"));
TS_ASSERT_EQUALS(size, CGUISize(CRect(0, -10, 0, 30), CRect(5, 0, -20, 0)));
// Test for fail with too many/few parameters
TS_ASSERT(!ca.SetClientArea("10 20 30 40 50"));
TS_ASSERT(!ca.SetClientArea("10 20 30"));
TS_ASSERT(!size.FromString("10 20 30 40 50"));
TS_ASSERT(!size.FromString("10 20 30"));
// Test for fail with garbage data
TS_ASSERT(!ca.SetClientArea("Hello world!"));
TS_ASSERT(!ca.SetClientArea("abc 123 xyz 789"));
TS_ASSERT(!ca.SetClientArea("300 wide, 400 high"));
TS_ASSERT(!size.FromString("Hello world!"));
TS_ASSERT(!size.FromString("abc 123 xyz 789"));
TS_ASSERT(!size.FromString("300 wide, 400 high"));
}
void test_rect()