From b17e8f304384b11a0682cb1a21e4a60d4e6e4462 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Wed, 26 May 2021 18:52:22 +0000 Subject: [PATCH] Removes global g_xres and g_yres dependency from GUI objects. Differential Revision: https://code.wildfiregames.com/D4014 This was SVN commit r25577. --- source/gui/CGUI.cpp | 5 +++++ source/gui/CGUI.h | 10 +++++++-- source/gui/ObjectBases/IGUIObject.cpp | 3 +-- source/gui/ObjectTypes/CDropDown.cpp | 20 ++++++++--------- source/gui/ObjectTypes/CTooltip.cpp | 32 +++++++++++++++++---------- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 77f129b2cf..6a55d21a99 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -419,6 +419,11 @@ IGUIObject* CGUI::FindObjectUnderMouse() return pNearest; } +CSize2D CGUI::GetWindowSize() const +{ + return CSize2D{static_cast(g_xres) / g_GuiScale, static_cast(g_yres) / g_GuiScale}; +} + void CGUI::SetFocusedObject(IGUIObject* pObject) { if (pObject == m_FocusedObject) diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index f4a2e13f0d..31bbbe28ae 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -29,6 +29,7 @@ #include "gui/SGUIStyle.h" #include "lib/input.h" #include "maths/Rect.h" +#include "maths/Size2D.h" #include "maths/Vector2D.h" #include "ps/XML/Xeromyces.h" #include "scriptinterface/ScriptForward.h" @@ -177,15 +178,20 @@ public: /** * Returns the current screen coordinates of the cursor. */ - const CVector2D& GetMousePos() const { return m_MousePos; }; + const CVector2D& GetMousePos() const { return m_MousePos; } /** * Returns the currently pressed mouse buttons. */ - const unsigned int& GetMouseButtons() { return m_MouseButtons; }; + const unsigned int& GetMouseButtons() { return m_MouseButtons; } const SGUIScrollBarStyle* GetScrollBarStyle(const CStr& style) const; + /** + * Returns the current GUI window size. + */ + CSize2D GetWindowSize() 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/ObjectBases/IGUIObject.cpp b/source/gui/ObjectBases/IGUIObject.cpp index d8aa5dd32b..be2d2d1713 100644 --- a/source/gui/ObjectBases/IGUIObject.cpp +++ b/source/gui/ObjectBases/IGUIObject.cpp @@ -25,7 +25,6 @@ #include "gui/Scripting/JSInterface_GUIProxy.h" #include "js/Conversions.h" #include "ps/CLogger.h" -#include "ps/GameSetup/Config.h" #include "ps/Profile.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptContext.h" @@ -208,7 +207,7 @@ void IGUIObject::UpdateCachedSize() if (!m_Absolute && m_pParent && !IsRootObject()) m_CachedActualSize = m_Size->GetSize(m_pParent->m_CachedActualSize); else - m_CachedActualSize = m_Size->GetSize(CRect(0.f, 0.f, g_xres / g_GuiScale, g_yres / g_GuiScale)); + m_CachedActualSize = m_Size->GetSize(CRect(m_pGUI.GetWindowSize())); // In a few cases, GUI objects have to resize to fill the screen // but maintain a constant aspect ratio. diff --git a/source/gui/ObjectTypes/CDropDown.cpp b/source/gui/ObjectTypes/CDropDown.cpp index f1f47220d3..f2dbd92251 100644 --- a/source/gui/ObjectTypes/CDropDown.cpp +++ b/source/gui/ObjectTypes/CDropDown.cpp @@ -348,9 +348,7 @@ InReaction CDropDown::ManuallyHandleKeys(const SDL_Event_* ev) void CDropDown::SetupListRect() { - extern int g_yres; - extern float g_GuiScale; - const float yres = g_yres / g_GuiScale; + const CSize2D windowSize = m_pGUI.GetWindowSize(); if (m_ItemsYPositions.empty()) { @@ -362,13 +360,13 @@ void CDropDown::SetupListRect() else if (m_ItemsYPositions.back() > m_DropDownSize) { // Place items below if at least some items can be placed below - if (m_CachedActualSize.bottom + m_DropDownBuffer + m_DropDownSize <= yres) + if (m_CachedActualSize.bottom + m_DropDownBuffer + m_DropDownSize <= windowSize.Height) m_CachedListRect = CRect(m_CachedActualSize.left, m_CachedActualSize.bottom + m_DropDownBuffer, m_CachedActualSize.right, m_CachedActualSize.bottom + m_DropDownBuffer + m_DropDownSize); - else if ((m_ItemsYPositions.size() > m_MinimumVisibleItems && yres - m_CachedActualSize.bottom - m_DropDownBuffer >= m_ItemsYPositions[m_MinimumVisibleItems]) || - m_CachedActualSize.top < yres - m_CachedActualSize.bottom) + else if ((m_ItemsYPositions.size() > m_MinimumVisibleItems && windowSize.Height - m_CachedActualSize.bottom - m_DropDownBuffer >= m_ItemsYPositions[m_MinimumVisibleItems]) || + m_CachedActualSize.top < windowSize.Height - m_CachedActualSize.bottom) m_CachedListRect = CRect(m_CachedActualSize.left, m_CachedActualSize.bottom + m_DropDownBuffer, - m_CachedActualSize.right, yres); + m_CachedActualSize.right, windowSize.Height); // Not enough space below, thus place items above else m_CachedListRect = CRect(m_CachedActualSize.left, std::max(0.f, m_CachedActualSize.top - m_DropDownBuffer - m_DropDownSize), @@ -379,18 +377,18 @@ void CDropDown::SetupListRect() else { // Enough space below, no scrollbar needed - if (m_CachedActualSize.bottom + m_DropDownBuffer + m_ItemsYPositions.back() <= yres) + if (m_CachedActualSize.bottom + m_DropDownBuffer + m_ItemsYPositions.back() <= windowSize.Height) { m_CachedListRect = CRect(m_CachedActualSize.left, m_CachedActualSize.bottom + m_DropDownBuffer, m_CachedActualSize.right, m_CachedActualSize.bottom + m_DropDownBuffer + m_ItemsYPositions.back()); m_HideScrollBar = true; } // Enough space below for some items, but not all, so place items below and use a scrollbar - else if ((m_ItemsYPositions.size() > m_MinimumVisibleItems && yres - m_CachedActualSize.bottom - m_DropDownBuffer >= m_ItemsYPositions[m_MinimumVisibleItems]) || - m_CachedActualSize.top < yres - m_CachedActualSize.bottom) + else if ((m_ItemsYPositions.size() > m_MinimumVisibleItems && windowSize.Height - m_CachedActualSize.bottom - m_DropDownBuffer >= m_ItemsYPositions[m_MinimumVisibleItems]) || + m_CachedActualSize.top < windowSize.Height - m_CachedActualSize.bottom) { m_CachedListRect = CRect(m_CachedActualSize.left, m_CachedActualSize.bottom + m_DropDownBuffer, - m_CachedActualSize.right, yres); + m_CachedActualSize.right, windowSize.Height); m_HideScrollBar = false; } // Not enough space below, thus place items above. Hide the scrollbar accordingly diff --git a/source/gui/ObjectTypes/CTooltip.cpp b/source/gui/ObjectTypes/CTooltip.cpp index f6df3d2005..eb7184ee54 100644 --- a/source/gui/ObjectTypes/CTooltip.cpp +++ b/source/gui/ObjectTypes/CTooltip.cpp @@ -91,22 +91,30 @@ void CTooltip::SetupText() } - // Reposition the tooltip if it's falling off the screen: - - extern int g_xres, g_yres; - extern float g_GuiScale; - float screenw = g_xres / g_GuiScale; - float screenh = g_yres / g_GuiScale; + // Reposition the tooltip if it's falling off in the GUI window. + const CSize2D windowSize = m_pGUI.GetWindowSize(); if (size.pixel.top < 0.f) - size.pixel.bottom -= size.pixel.top, size.pixel.top = 0.f; - else if (size.pixel.bottom > screenh) - size.pixel.top -= (size.pixel.bottom-screenh), size.pixel.bottom = screenh; + { + size.pixel.bottom -= size.pixel.top; + size.pixel.top = 0.f; + } + else if (size.pixel.bottom > windowSize.Height) + { + size.pixel.top -= size.pixel.bottom - windowSize.Height; + size.pixel.bottom = windowSize.Height; + } if (size.pixel.left < 0.f) - size.pixel.right -= size.pixel.left, size.pixel.left = 0.f; - else if (size.pixel.right > screenw) - size.pixel.left -= (size.pixel.right-screenw), size.pixel.right = screenw; + { + size.pixel.right -= size.pixel.left; + size.pixel.left = 0.f; + } + else if (size.pixel.right > windowSize.Width) + { + size.pixel.left -= size.pixel.right - windowSize.Width; + size.pixel.right = windowSize.Width; + } m_Size.Set(size, true); }