diff --git a/binaries/data/mods/public/gui/summary/summary.js b/binaries/data/mods/public/gui/summary/summary.js index 9f1f78227c..8869cfb4ed 100644 --- a/binaries/data/mods/public/gui/summary/summary.js +++ b/binaries/data/mods/public/gui/summary/summary.js @@ -265,8 +265,8 @@ function initGUICharts() function resizeDropdown(dropdown) { dropdown.size.bottom = dropdown.size.top + - (Engine.GetTextWidth(dropdown.font, dropdown.list[dropdown.selected]) > - dropdown.size.right - dropdown.size.left - 28 && + (dropdown.getPreferredHeaderTextSize().width > + dropdown.size.right - dropdown.size.left - dropdown.button_width && dropdown.list[dropdown.selected].indexOf(" ") !== -1 ? 42 : 28); } diff --git a/source/gui/GUIObjectTypes.cpp b/source/gui/GUIObjectTypes.cpp index 88c6a35869..6deebb09cc 100644 --- a/source/gui/GUIObjectTypes.cpp +++ b/source/gui/GUIObjectTypes.cpp @@ -49,6 +49,7 @@ void CGUI::AddObjectTypes() m_ProxyData.insert(JSI_GUIProxy::CreateData(*m_ScriptInterface)); m_ProxyData.insert(JSI_GUIProxy::CreateData(*m_ScriptInterface)); m_ProxyData.insert(JSI_GUIProxy::CreateData(*m_ScriptInterface)); + m_ProxyData.insert(JSI_GUIProxy::CreateData(*m_ScriptInterface)); m_ProxyData.insert(JSI_GUIProxy::CreateData(*m_ScriptInterface)); m_ProxyData.insert(JSI_GUIProxy::CreateData(*m_ScriptInterface)); m_ProxyData.insert(JSI_GUIProxy::CreateData(*m_ScriptInterface)); diff --git a/source/gui/ObjectTypes/CDropDown.cpp b/source/gui/ObjectTypes/CDropDown.cpp index 4f3a308e35..b5764205bd 100644 --- a/source/gui/ObjectTypes/CDropDown.cpp +++ b/source/gui/ObjectTypes/CDropDown.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include CDropDown::CDropDown(CGUI& pGUI) @@ -501,3 +502,11 @@ float CDropDown::GetBufferedZ() const else return bz; } + +CSize2D CDropDown::GetPreferredHeaderTextSize() const +{ + if (m_Selected == -1) + return CSize2D{0.0f, 0.0f}; + + return CGUIText{m_pGUI, m_List->m_Items[m_Selected], m_Font, std::numeric_limits::max(), m_BufferZone, m_TextAlign, this}.GetSize(); +} diff --git a/source/gui/ObjectTypes/CDropDown.h b/source/gui/ObjectTypes/CDropDown.h index 9aa2f87966..5c05eed312 100644 --- a/source/gui/ObjectTypes/CDropDown.h +++ b/source/gui/ObjectTypes/CDropDown.h @@ -74,6 +74,8 @@ public: */ virtual void Draw(CCanvas2D& canvas); + virtual void CreateJSObject() override; + // This is one of the few classes we actually need to redefine this function // this is because the size of the control changes whether it is open // or closed. @@ -81,6 +83,11 @@ public: virtual float GetBufferedZ() const; + /** + * Calculate the preferred text size of the currently selected item displayed in the header. + */ + virtual CSize2D GetPreferredHeaderTextSize() const; + protected: /** * If the size changed, the texts have to be updated as diff --git a/source/gui/Scripting/JSInterface_GUIProxy.cpp b/source/gui/Scripting/JSInterface_GUIProxy.cpp index 27b338de8b..69931ed5b8 100644 --- a/source/gui/Scripting/JSInterface_GUIProxy.cpp +++ b/source/gui/Scripting/JSInterface_GUIProxy.cpp @@ -22,6 +22,7 @@ #include "gui/ObjectBases/IGUIObject.h" #include "gui/ObjectTypes/CButton.h" +#include "gui/ObjectTypes/CDropDown.h" #include "gui/ObjectTypes/CList.h" #include "gui/ObjectTypes/CMiniMap.h" #include "gui/ObjectTypes/CScrollPanel.h" @@ -69,6 +70,13 @@ template<> void JSI_GUIProxy::CreateFunctions(const ScriptRequest& rq, GU } DECLARE_GUIPROXY(CList); +// CDropDown +template<> void JSI_GUIProxy::CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache) +{ + CreateFunction<&CDropDown::GetPreferredHeaderTextSize>(rq, cache, "getPreferredHeaderTextSize"); +} +DECLARE_GUIPROXY(CDropDown); + // CMiniMap template<> void JSI_GUIProxy::CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache) {