From 9c5062147a5f56ce75a4eb94ea230b7d03fa89cd Mon Sep 17 00:00:00 2001 From: s0600204 Date: Mon, 18 Mar 2019 22:15:40 +0000 Subject: [PATCH] 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. --- .../public/gui/reference/viewer/viewer.js | 8 +- source/gui/CGUI.cpp | 16 +++- source/gui/CGUI.h | 5 +- source/gui/IGUIObject.h | 3 +- source/gui/IGUIScrollBarOwner.cpp | 14 +--- .../gui/scripting/JSInterface_IGUIObject.cpp | 73 +++++++++++++++++++ source/gui/scripting/JSInterface_IGUIObject.h | 1 + 7 files changed, 96 insertions(+), 24 deletions(-) diff --git a/binaries/data/mods/public/gui/reference/viewer/viewer.js b/binaries/data/mods/public/gui/reference/viewer/viewer.js index 97dce4c2d4..68f5085953 100644 --- a/binaries/data/mods/public/gui/reference/viewer/viewer.js +++ b/binaries/data/mods/public/gui/reference/viewer/viewer.js @@ -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"); diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index c61d9ba8a8..5807427be0 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -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::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::GetSetting(pObject, "text_align", align); + EAlign align = EAlign_Left; + if (pObject->SettingExists("text_align")) + GUI::GetSetting(pObject, "text_align", align); // Go through string word by word for (int i = 0; i < (int)string.m_Words.size()-1 && !done; ++i) diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index 132f35e8f6..b5ee0cab09 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -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 diff --git a/source/gui/IGUIObject.h b/source/gui/IGUIObject.h index b16ce31316..e770a70900 100644 --- a/source/gui/IGUIObject.h +++ b/source/gui/IGUIObject.h @@ -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(); diff --git a/source/gui/IGUIScrollBarOwner.cpp b/source/gui/IGUIScrollBarOwner.cpp index 6a2dac3889..41460e3316 100644 --- a/source/gui/IGUIScrollBarOwner.cpp +++ b/source/gui/IGUIScrollBarOwner.cpp @@ -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::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) diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index 3ead98c4d5..3504631a70 100644 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -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::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::GetSetting(obj, "caption", caption); + else if (Type == GUIST_CStrW) + { + // CInput + CStrW captionStr; + GUI::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::GetSetting(obj, "scrollbar", scrollbar); + if (scrollbar) + { + CStr scrollbar_style; + GUI::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::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); diff --git a/source/gui/scripting/JSInterface_IGUIObject.h b/source/gui/scripting/JSInterface_IGUIObject.h index faefbdad65..dbae6d7a6d 100644 --- a/source/gui/scripting/JSInterface_IGUIObject.h +++ b/source/gui/scripting/JSInterface_IGUIObject.h @@ -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); }