1
0
forked from 0ad/0ad

Removes global g_xres and g_yres dependency from GUI objects.

Differential Revision: https://code.wildfiregames.com/D4014
This was SVN commit r25577.
This commit is contained in:
Vladislav Belov 2021-05-26 18:52:22 +00:00
parent bcac3bb896
commit b17e8f3043
5 changed files with 43 additions and 27 deletions

View File

@ -419,6 +419,11 @@ IGUIObject* CGUI::FindObjectUnderMouse()
return pNearest;
}
CSize2D CGUI::GetWindowSize() const
{
return CSize2D{static_cast<float>(g_xres) / g_GuiScale, static_cast<float>(g_yres) / g_GuiScale};
}
void CGUI::SetFocusedObject(IGUIObject* pObject)
{
if (pObject == m_FocusedObject)

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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);
}