Add getTextSize() for CButton

Summary: 9c5062147a introduced the `getTextSize()` to all GUIObjects for
getting the rendered size of a text. 8190293f8b made it only available
to CText. `getTextSize()` can also be useful for CButtons so this diff
adds the function back to CButtons. There seem to be no other GUIObjects
besiders CText and CButton where `getTextSize()` makes sense.

Reviewed By: wraitii
Differential Revision: https://code.wildfiregames.com/D3195
This was SVN commit r24378.
This commit is contained in:
Imarok 2020-12-11 16:12:46 +00:00
parent 5e754a86a9
commit 00aeebea6d
4 changed files with 110 additions and 0 deletions

View file

@ -40,6 +40,7 @@ void CGUI::AddObjectTypes()
m_ProxyData.insert(JSI_GUIProxy<IGUIObject>::CreateData(*m_ScriptInterface));
m_ProxyData.insert(JSI_GUIProxy<CText>::CreateData(*m_ScriptInterface));
m_ProxyData.insert(JSI_GUIProxy<CList>::CreateData(*m_ScriptInterface));
m_ProxyData.insert(JSI_GUIProxy<CButton>::CreateData(*m_ScriptInterface));
AddObjectType("button", &CButton::ConstructObject);
AddObjectType("chart", &CChart::ConstructObject);

View file

@ -21,6 +21,7 @@
#include "gui/CGUI.h"
#include "gui/CGUIText.h"
#include "gui/Scripting/JSInterface_GUIProxy.h"
#include "gui/SettingTypes/CGUIColor.h"
CButton::CButton(CGUI& pGUI)
@ -117,3 +118,24 @@ const CGUIColor& CButton::ChooseColor()
return m_TextColorOver || m_TextColor;
}
void CButton::CreateJSObject()
{
ScriptRequest rq(m_pGUI.GetScriptInterface());
js::ProxyOptions options;
options.setClass(&JSI_GUIProxy<CButton>::ClassDefinition());
JS::RootedValue cppObj(rq.cx), data(rq.cx);
cppObj.get().setPrivate(this);
data.get().setPrivate(GetGUI().GetProxyData(&JSI_GUIProxy<CButton>::Singleton()));
m_JSObject.init(rq.cx, js::NewProxyObject(rq.cx, &JSI_GUIProxy<CButton>::Singleton(), cppObj, nullptr, options));
js::SetProxyReservedSlot(m_JSObject, 0, data);
}
void CButton::getTextSize(ScriptInterface& scriptInterface, JS::MutableHandleValue ret)
{
ScriptRequest rq(scriptInterface);
UpdateText();
ScriptInterface::ToJSVal(rq, ret, m_GeneratedTexts[0].GetSize());
}

View file

@ -28,6 +28,8 @@ class CButton : public IGUIObject, public IGUITextOwner, public IGUIButtonBehavi
{
GUI_OBJECT(CButton)
friend JSI_GUIProxy<CButton>;
public:
CButton(CGUI& pGUI);
virtual ~CButton();
@ -69,6 +71,10 @@ protected:
*/
CPos m_TextPos;
virtual void CreateJSObject();
void getTextSize(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
// Settings
float m_BufferZone;
i32 m_CellID;

View file

@ -0,0 +1,81 @@
/* Copyright (C) 2020 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_GUIProxy.h"
#include "gui/CGUI.h"
#include "gui/CGUISetting.h"
#include "gui/ObjectBases/IGUIObject.h"
#include "gui/ObjectTypes/CButton.h"
#include "ps/CLogger.h"
#include "scriptinterface/ScriptExtraHeaders.h"
#include "scriptinterface/ScriptInterface.h"
#include <string>
// Include the definition of the generic templates.
#include "JSInterface_GUIProxy_impl.h"
namespace
{
struct SData
{
JS::PersistentRootedObject m_ToString;
JS::PersistentRootedObject m_Focus;
JS::PersistentRootedObject m_Blur;
JS::PersistentRootedObject m_GetComputedSize;
JS::PersistentRootedObject m_GetTextSize;
};
}
template <>
bool JSI_GUIProxy<CButton>::FuncGetter(JS::HandleObject proxy, const std::string& propName, JS::MutableHandleValue vp) const
{
const SData& data = *static_cast<const SData*>(js::GetProxyReservedSlot(proxy, 0).toPrivate());
if (propName == "toString")
return vp.setObjectOrNull(data.m_ToString), true;
if (propName == "focus")
return vp.setObjectOrNull(data.m_Focus), true;
if (propName == "blur")
return vp.setObjectOrNull(data.m_Blur), true;
if (propName == "getComputedSize")
return vp.setObjectOrNull(data.m_GetComputedSize), true;
if (propName == "getTextSize")
return vp.setObjectOrNull(data.m_GetTextSize), true;
return false;
}
template <>
std::pair<const js::BaseProxyHandler*, void*> JSI_GUIProxy<CButton>::CreateData(ScriptInterface& scriptInterface)
{
SData* data = new SData();
ScriptRequest rq(scriptInterface);
#define func(class, func) &apply_to<CButton, class, &class::func>
data->m_ToString.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, toString), 0, 0, "toString")));
data->m_Focus.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, focus), 0, 0, "focus")));
data->m_Blur.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, blur), 0, 0, "blur")));
data->m_GetComputedSize.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(IGUIObject, getComputedSize), 0, 0, "getComputedSize")));
data->m_GetTextSize.init(rq.cx, JS_GetFunctionObject(JS_NewFunction(rq.cx, func(CButton, getTextSize), 0, 0, "getTextSize")));
#undef func
return { &Singleton(), data };
}
template class JSI_GUIProxy<CButton>;