forked from 0ad/0ad
Introduce a JSInterface_IGUITextOwner to encapsulate JSI_IGUIObject::getTextSize from 9c5062147a
/ D844.
JSI_IGUIObject should not contain functions that work only for some GUI Object types, refs #5442. Deduplicate the shuffled copy of CText::SetupText. Avoid the 80 times slower dynamic_cast mandated by the virtual class inheritance by adding an ugly overridable pointer to the base class pointing at this derived classes, as bargained with Vladislav and proposed by wraitii in D1781 id=8426. This may be improved by refactoring the IGUIObject and JSInterface classes to use templates and / or eliminating its virtual inheritance. Implement and use FromJSVal / ToJSVal CSize specialization. Remove the JS::CallArgsFromVp call. Differential Revision: https://code.wildfiregames.com/D2136 Comments By: wraitii, Vladislav This was SVN commit r22596.
This commit is contained in:
parent
a1963b8ec2
commit
3d07327837
@ -104,7 +104,6 @@ class IGUIObject
|
||||
friend bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp);
|
||||
friend bool JSI_IGUIObject::setProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, bool UNUSED(strict), JS::MutableHandleValue vp);
|
||||
friend bool JSI_IGUIObject::getComputedSize(JSContext* cx, uint argc, JS::Value* vp);
|
||||
friend bool JSI_IGUIObject::getTextSize(JSContext* cx, uint argc, JS::Value* vp);
|
||||
|
||||
public:
|
||||
IGUIObject(CGUI* pGUI);
|
||||
@ -353,6 +352,11 @@ public:
|
||||
*/
|
||||
void SetFocus();
|
||||
|
||||
/**
|
||||
* Workaround to avoid a dynamic_cast which can be 80 times slower than this.
|
||||
*/
|
||||
virtual void* GetTextOwner() { return nullptr; }
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Check if object is focused.
|
||||
|
@ -17,7 +17,10 @@
|
||||
|
||||
#include "precompiled.h"
|
||||
|
||||
#include "IGUITextOwner.h"
|
||||
|
||||
#include "gui/GUI.h"
|
||||
#include "gui/scripting/JSInterface_IGUITextOwner.h"
|
||||
|
||||
IGUITextOwner::IGUITextOwner(CGUI* pGUI)
|
||||
: IGUIObject(pGUI), m_GeneratedTextsValid(false)
|
||||
@ -30,6 +33,14 @@ IGUITextOwner::~IGUITextOwner()
|
||||
delete t;
|
||||
}
|
||||
|
||||
void IGUITextOwner::CreateJSObject()
|
||||
{
|
||||
IGUIObject::CreateJSObject();
|
||||
|
||||
JSI_IGUITextOwner::RegisterScriptFunctions(
|
||||
GetGUI()->GetScriptInterface()->GetContext(), m_JSObject);
|
||||
}
|
||||
|
||||
void IGUITextOwner::AddText(SGUIText* text)
|
||||
{
|
||||
m_GeneratedTexts.push_back(text);
|
||||
@ -110,6 +121,21 @@ void IGUITextOwner::CalculateTextPosition(CRect& ObjSize, CPos& TextPos, SGUITex
|
||||
}
|
||||
}
|
||||
|
||||
CSize IGUITextOwner::CalculateTextSize()
|
||||
{
|
||||
if (!m_GeneratedTextsValid)
|
||||
{
|
||||
SetupText();
|
||||
m_GeneratedTextsValid = true;
|
||||
}
|
||||
|
||||
if (m_GeneratedTexts.empty())
|
||||
return CSize();
|
||||
|
||||
// GUI Object types that use multiple texts may override this function.
|
||||
return m_GeneratedTexts[0]->m_Size;
|
||||
}
|
||||
|
||||
bool IGUITextOwner::MouseOverIcon()
|
||||
{
|
||||
return false;
|
||||
|
@ -36,6 +36,7 @@ GUI Object Base - Text Owner
|
||||
#define INCLUDED_IGUITEXTOWNER
|
||||
|
||||
#include "GUI.h"
|
||||
#include "gui/scripting/JSInterface_IGUITextOwner.h"
|
||||
|
||||
/**
|
||||
* Framework for handling Output text.
|
||||
@ -44,6 +45,8 @@ GUI Object Base - Text Owner
|
||||
*/
|
||||
class IGUITextOwner : virtual public IGUIObject
|
||||
{
|
||||
friend bool JSI_IGUITextOwner::GetTextSize(JSContext* cx, uint argc, JS::Value* vp);
|
||||
|
||||
public:
|
||||
IGUITextOwner(CGUI* pGUI);
|
||||
virtual ~IGUITextOwner();
|
||||
@ -53,6 +56,11 @@ public:
|
||||
*/
|
||||
void AddText(SGUIText* text);
|
||||
|
||||
/**
|
||||
* Subscribe the custom JS methods.
|
||||
*/
|
||||
void CreateJSObject() override;
|
||||
|
||||
/**
|
||||
* @see IGUIObject#HandleMessage()
|
||||
*/
|
||||
@ -80,6 +88,11 @@ public:
|
||||
*/
|
||||
virtual bool MouseOverIcon();
|
||||
|
||||
/**
|
||||
* Workaround to avoid a dynamic_cast which can be 80 times slower than this.
|
||||
*/
|
||||
virtual void* GetTextOwner() override { return this; }
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
@ -101,6 +114,11 @@ protected:
|
||||
* Calculate the position for the text, based on the alignment.
|
||||
*/
|
||||
void CalculateTextPosition(CRect& ObjSize, CPos& TextPos, SGUIText& Text);
|
||||
|
||||
/**
|
||||
* Calculate the size of the first generated text.
|
||||
*/
|
||||
CSize CalculateTextSize();
|
||||
};
|
||||
|
||||
#endif // INCLUDED_IGUITEXTOWNER
|
||||
|
@ -170,6 +170,34 @@ template<> bool ScriptInterface::FromJSVal<CGUIColor>(JSContext* cx, JS::HandleV
|
||||
return FromJSVal<CColor>(cx, v, out);
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<CSize>(JSContext* cx, JS::MutableHandleValue ret, const CSize& val)
|
||||
{
|
||||
ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->CreateObject(ret, "width", val.cx, "height", val.cy);
|
||||
}
|
||||
|
||||
template<> bool ScriptInterface::FromJSVal<CSize>(JSContext* cx, JS::HandleValue v, CSize& out)
|
||||
{
|
||||
if (!v.isObject())
|
||||
{
|
||||
JS_ReportError(cx, "CSize value must be an object!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!FromJSProperty(cx, v, "width", out.cx))
|
||||
{
|
||||
JS_ReportError(cx, "Failed to get CSize.cx property");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!FromJSProperty(cx, v, "height", out.cy))
|
||||
{
|
||||
JS_ReportError(cx, "Failed to get CSize.cy property");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<CPos>(JSContext* cx, JS::MutableHandleValue ret, const CPos& val)
|
||||
{
|
||||
ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->CreateObject(ret, "x", val.x, "y", val.y);
|
||||
|
@ -44,7 +44,6 @@ JSFunctionSpec JSI_IGUIObject::JSI_methods[] =
|
||||
JS_FN("focus", JSI_IGUIObject::focus, 0, 0),
|
||||
JS_FN("blur", JSI_IGUIObject::blur, 0, 0),
|
||||
JS_FN("getComputedSize", JSI_IGUIObject::getComputedSize, 0, 0),
|
||||
JS_FN("getTextSize", JSI_IGUIObject::getTextSize, 0, 0),
|
||||
JS_FS_END
|
||||
};
|
||||
|
||||
@ -66,6 +65,7 @@ bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::Handle
|
||||
return false;
|
||||
|
||||
// Skip registered functions and inherited properties
|
||||
// including JSInterfaces of derived classes
|
||||
if (propName == "constructor" ||
|
||||
propName == "prototype" ||
|
||||
propName == "toString" ||
|
||||
@ -228,78 +228,6 @@ bool JSI_IGUIObject::blur(JSContext* cx, uint UNUSED(argc), JS::Value* vp)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JSI_IGUIObject::getTextSize(JSContext* cx, uint argc, JS::Value* vp)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
|
||||
|
||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
||||
JS::RootedObject thisObj(cx, &args.thisv().toObject());
|
||||
|
||||
IGUIObject* obj = (IGUIObject*)JS_GetInstancePrivate(cx, thisObj, &JSI_IGUIObject::JSI_class, NULL);
|
||||
|
||||
if (!obj || !obj->SettingExists("caption"))
|
||||
return false;
|
||||
|
||||
CStrW font;
|
||||
if (GUI<CStrW>::GetSetting(obj, "font", font) != PSRETURN_OK || font.empty())
|
||||
font = L"default";
|
||||
|
||||
CGUIString caption;
|
||||
EGUISettingType Type;
|
||||
obj->GetSettingType("caption", Type);
|
||||
if (Type == GUIST_CGUIString)
|
||||
// CText, CButton, CCheckBox, CRadioButton
|
||||
GUI<CGUIString>::GetSetting(obj, "caption", caption);
|
||||
else if (Type == GUIST_CStrW)
|
||||
{
|
||||
// CInput
|
||||
CStrW captionStr;
|
||||
GUI<CStrW>::GetSetting(obj, "caption", captionStr);
|
||||
caption.SetValue(captionStr);
|
||||
}
|
||||
else
|
||||
return false;
|
||||
|
||||
obj->UpdateCachedSize();
|
||||
float width = obj->m_CachedActualSize.GetWidth();
|
||||
|
||||
if (obj->SettingExists("scrollbar"))
|
||||
{
|
||||
bool scrollbar;
|
||||
GUI<bool>::GetSetting(obj, "scrollbar", scrollbar);
|
||||
if (scrollbar)
|
||||
{
|
||||
CStr scrollbar_style;
|
||||
GUI<CStr>::GetSetting(obj, "scrollbar_style", scrollbar_style);
|
||||
const SGUIScrollBarStyle* scrollbar_style_object = obj->GetGUI()->GetScrollBarStyle(scrollbar_style);
|
||||
if (scrollbar_style_object)
|
||||
width -= scrollbar_style_object->m_Width;
|
||||
}
|
||||
}
|
||||
|
||||
float buffer_zone = 0.f;
|
||||
GUI<float>::GetSetting(obj, "buffer_zone", buffer_zone);
|
||||
SGUIText text = obj->GetGUI()->GenerateText(caption, font, width, buffer_zone, obj);
|
||||
|
||||
JS::RootedValue objVal(cx);
|
||||
try
|
||||
{
|
||||
ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->CreateObject(
|
||||
&objVal,
|
||||
"width", text.m_Size.cx,
|
||||
"height", text.m_Size.cy);
|
||||
}
|
||||
catch (PSERROR_Scripting_ConversionFailed&)
|
||||
{
|
||||
debug_warn(L"Error creating size object!");
|
||||
return false;
|
||||
}
|
||||
|
||||
rec.rval().set(objVal);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JSI_IGUIObject::getComputedSize(JSContext* cx, uint UNUSED(argc), JS::Value* vp)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
|
59
source/gui/scripting/JSInterface_IGUITextOwner.cpp
Normal file
59
source/gui/scripting/JSInterface_IGUITextOwner.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
/* 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 "JSInterface_IGUITextOwner.h"
|
||||
|
||||
#include "gui/IGUITextOwner.h"
|
||||
#include "scriptinterface/ScriptInterface.h"
|
||||
|
||||
JSFunctionSpec JSI_IGUITextOwner::JSI_methods[] =
|
||||
{
|
||||
JS_FN("getTextSize", GetTextSize, 0, 0),
|
||||
JS_FS_END
|
||||
};
|
||||
|
||||
void JSI_IGUITextOwner::RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj)
|
||||
{
|
||||
JS_DefineFunctions(cx, obj, JSI_methods);
|
||||
}
|
||||
|
||||
bool JSI_IGUITextOwner::GetTextSize(JSContext* cx, uint UNUSED(argc), JS::Value* vp)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
|
||||
JS::RootedObject thisObj(cx, &rec.thisv().toObject());
|
||||
|
||||
IGUIObject* obj = static_cast<IGUIObject*>(JS_GetInstancePrivate(cx, thisObj, &JSI_IGUIObject::JSI_class, nullptr));
|
||||
if (!obj)
|
||||
{
|
||||
JS_ReportError(cx, "This is not an IGUIObject!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Avoid dynamic_cast for performance reasons
|
||||
IGUITextOwner* objText = static_cast<IGUITextOwner*>(obj->GetTextOwner());
|
||||
if (!objText)
|
||||
{
|
||||
JS_ReportError(cx, "This IGUIObject is not an IGUITextOwner!");
|
||||
return false;
|
||||
}
|
||||
|
||||
ScriptInterface::ToJSVal(cx, rec.rval(), objText->CalculateTextSize());
|
||||
return true;
|
||||
}
|
32
source/gui/scripting/JSInterface_IGUITextOwner.h
Normal file
32
source/gui/scripting/JSInterface_IGUITextOwner.h
Normal file
@ -0,0 +1,32 @@
|
||||
/* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef INCLUDED_JSI_IGUITEXTOWNER
|
||||
#define INCLUDED_JSI_IGUITEXTOWNER
|
||||
|
||||
#include "scriptinterface/ScriptInterface.h"
|
||||
|
||||
namespace JSI_IGUITextOwner
|
||||
{
|
||||
extern JSFunctionSpec JSI_methods[];
|
||||
|
||||
void RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj);
|
||||
|
||||
bool GetTextSize(JSContext* cx, uint argc, JS::Value* vp);
|
||||
}
|
||||
|
||||
#endif // INCLUDED_JSI_IGUITEXTOWNER
|
Loading…
Reference in New Issue
Block a user