1
0
forked from 0ad/0ad

Add a GetTextSize() method to GUI objects

Usage:
  let size = Engine.GetGUIObjectByName({gui_object_name}).GetTextSize()

Returns a JS object containing the height and width of the primary text
field within
the object, taking into account new lines, text wrapping, and font
changes.

Unless the object doesn't contain text, in which case the method will
return undefined.

Commented on by: vladislavbelov
Additional code by: elexis
Reviewed By: wraitii
Differential Revision: https://code.wildfiregames.com/D844
This was SVN commit r22134.
This commit is contained in:
s0600204 2019-03-18 22:15:40 +00:00
parent a65f981545
commit 9c5062147a
7 changed files with 96 additions and 24 deletions

View File

@ -98,8 +98,6 @@ function init(data)
/**
* Populate the UI elements.
*
* @todo (c++ change) Implement and use a function that fetches height of rendered text block from text object.
*/
function draw()
{
@ -111,12 +109,10 @@ function draw()
let entityStats = Engine.GetGUIObjectByName("entityStats");
entityStats.caption = buildText(g_Template, g_StatsFunctions);
// This is something of a crude hack. See above todo.
let entityInfo = Engine.GetGUIObjectByName("entityInfo");
let lines = entityStats.caption.split("\n").length;
let fontSize = +entityStats.font.split("-")[1] + 4;
let infoSize = entityInfo.size;
infoSize.top = Math.max(entityIcon.size.bottom, lines * fontSize + entityStats.size.top) + 8;
// The magic '8' below provides a gap between the bottom of the icon, and the start of the info text.
infoSize.top = Math.max(entityIcon.size.bottom + 8, entityStats.size.top + entityStats.getTextSize().height);
entityInfo.size = infoSize;
entityInfo.caption = buildText(g_Template, g_InfoFunctions, "\n\n");

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 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
@ -475,6 +475,15 @@ void CGUI::SetFocusedObject(IGUIObject* pObject)
}
}
const SGUIScrollBarStyle* CGUI::GetScrollBarStyle(const CStr& style) const
{
std::map<CStr, SGUIScrollBarStyle>::const_iterator it = m_ScrollBarStyles.find(style);
if (it == m_ScrollBarStyles.end())
return nullptr;
return &it->second;
}
// private struct used only in GenerateText(...)
struct SGenerateTextImage
{
@ -540,8 +549,9 @@ SGUIText CGUI::GenerateText(const CGUIString& string, const CStrW& FontW, const
// get the alignment type for the control we are computing the text for since
// we are computing the horizontal alignment in this method in order to not have
// to run through the TextCalls a second time in the CalculateTextPosition method again
EAlign align;
GUI<EAlign>::GetSetting(pObject, "text_align", align);
EAlign align = EAlign_Left;
if (pObject->SettingExists("text_align"))
GUI<EAlign>::GetSetting(pObject, "text_align", align);
// Go through string word by word
for (int i = 0; i < (int)string.m_Words.size()-1 && !done; ++i)

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 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,7 +68,6 @@ class CGUI
NONCOPYABLE(CGUI);
friend class IGUIObject;
friend class IGUIScrollBarOwner;
friend class CInternalCGUIAccessorBase;
private:
@ -173,6 +172,8 @@ public:
*/
IGUIObject* FindObjectUnderMouse() const;
const SGUIScrollBarStyle* GetScrollBarStyle(const CStr& style) const;
/**
* The GUI needs to have all object types inputted and
* their constructors. Also it needs to associate a type

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 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
@ -87,6 +87,7 @@ 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();

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 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
@ -47,19 +47,9 @@ void IGUIScrollBarOwner::AddScrollBar(IGUIScrollBar* scrollbar)
const SGUIScrollBarStyle* IGUIScrollBarOwner::GetScrollBarStyle(const CStr& style) const
{
if (!GetGUI())
{
// TODO Gee: Output in log
return NULL;
}
std::map<CStr, SGUIScrollBarStyle>::const_iterator it = GetGUI()->m_ScrollBarStyles.find(style);
if (it == GetGUI()->m_ScrollBarStyles.end())
{
// TODO Gee: Output in log
return NULL;
}
return &it->second;
return GetGUI()->GetScrollBarStyle(style);
}
void IGUIScrollBarOwner::HandleMessage(SGUIMessage& msg)

View File

@ -45,6 +45,7 @@ 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
};
@ -76,6 +77,7 @@ bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::Handle
propName == "toJSON" ||
propName == "focus" ||
propName == "blur" ||
propName == "getTextSize" ||
propName == "getComputedSize"
)
return true;
@ -685,6 +687,77 @@ 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, JS::ObjectValue(*JS_NewPlainObject(cx)));
try
{
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
pScriptInterface->SetProperty(objVal, "width", text.m_Size.cx, false, true);
pScriptInterface->SetProperty(objVal, "height", text.m_Size.cy, false, true);
}
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);

View File

@ -31,6 +31,7 @@ namespace JSI_IGUIObject
bool focus(JSContext* cx, uint argc, JS::Value* vp);
bool blur(JSContext* cx, uint argc, JS::Value* vp);
bool getComputedSize(JSContext* cx, uint argc, JS::Value* vp);
bool getTextSize(JSContext* cx, uint argc, JS::Value* vp);
void init(ScriptInterface& scriptInterface);
}