From 068a4975661ec5872318218651a5f3b1d06f7f77 Mon Sep 17 00:00:00 2001 From: Lpsd <40902730+Lpsd@users.noreply.github.com> Date: Sat, 18 Jan 2025 00:46:37 +0000 Subject: [PATCH] Fix cell item assignment in CGUIGridLayout --- Client/gui/CGUIGridLayout_Impl.cpp | 57 ++++++++++++++++++------------ Client/gui/CGUIGridLayout_Impl.h | 4 +-- Client/sdk/gui/CGUIGridLayout.h | 4 +-- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Client/gui/CGUIGridLayout_Impl.cpp b/Client/gui/CGUIGridLayout_Impl.cpp index ada5f5deaa..1682756b64 100644 --- a/Client/gui/CGUIGridLayout_Impl.cpp +++ b/Client/gui/CGUIGridLayout_Impl.cpp @@ -70,24 +70,20 @@ CGUIGridLayout_Impl::~CGUIGridLayout_Impl() const bool CGUIGridLayout_Impl::AddItem(CGUIElement* item, int column, int row, const bool moveToNextCell) { - if (!InGridRange(column, row)) - return false; + auto* cell = GetCell(column, row); - // If cell is already occupied - if (m_grid[column - 1][row - 1] != 0) - { + if (cell == nullptr) return false; - } - auto* cell = GetCell(column, row); - cell->element = item; + if (cell->element != nullptr) + RemoveItem(column, row, false, true); - item->SetParent(nullptr); item->SetParent(cell->container); - item->SetPosition(CVector2D(0.0f, 0.0f), true); item->SetSize(CVector2D(1.0f, 1.0f), true); + item->ForceRedraw(); + cell->element = item; m_items.emplace(item, cell->id); if (moveToNextCell) @@ -114,19 +110,21 @@ const bool CGUIGridLayout_Impl::AddItem(CGUIElement* item, const bool moveToNext return AddItem(item, m_activeColumn, m_activeRow); } -const bool CGUIGridLayout_Impl::RemoveItem(const int column, const int row, const bool moveToPreviousCell) +const bool CGUIGridLayout_Impl::RemoveItem(const int column, const int row, const bool moveToPreviousCell, const bool deleteItem) { auto* cell = GetCell(column, row); - if (cell == nullptr) + if (cell == nullptr || cell->element == nullptr) return false; - auto& element = cell->element; - element->SetParent(nullptr); - element = nullptr; - m_grid[column - 1][row - 1] = 0; + cell->element->SetParent(nullptr); + cell->element->ForceRedraw(); - m_items.erase(element); + m_items.erase(cell->element); + cell->element = nullptr; + + if (deleteItem) + delete cell->element; if (moveToPreviousCell) { @@ -147,7 +145,7 @@ const bool CGUIGridLayout_Impl::RemoveItem(const int column, const int row, cons return true; } -const bool CGUIGridLayout_Impl::RemoveItem(const CGUIElement* item, const bool moveToPreviousCell) +const bool CGUIGridLayout_Impl::RemoveItem(const CGUIElement* item, const bool moveToPreviousCell, const bool deleteItem) { auto* cell = GetCell(item); @@ -356,9 +354,6 @@ const eGridLayoutItemAlignment CGUIGridLayout_Impl::GetItemAlignment(const CGUIE const bool CGUIGridLayout_Impl::SetCellAlpha(const int column, const int row, const float alpha) { - if (!InGridRange(column, row)) - return false; - auto* cell = GetCell(column, row); if (cell == nullptr) @@ -407,7 +402,15 @@ void CGUIGridLayout_Impl::CreateGridCells() cell->container->SetSize(CVector2D(1.0f / m_columns, 1.0f / m_rows), true); cell->id = m_nextId; - cell->element = nullptr; + + auto* label = m_pManager->CreateLabel(cell->container, std::to_string(m_nextId).c_str()); + label->AutoSize(); + label->SetPosition(CVector2D(0.0f, 0.0f), true); + label->SetSize(CVector2D(1.0f, 1.0f), true); + label->SetHorizontalAlign(CGUI_ALIGN_HORIZONTALCENTER); + label->SetVerticalAlign(CGUI_ALIGN_VERTICALCENTER); + + cell->element = label; cell->column = i + 1; cell->row = j + 1; cell->alignment = m_defaultAlignment; @@ -434,6 +437,9 @@ void CGUIGridLayout_Impl::CleanupGridItems() m_items.erase(cell.second->element); cell.second->element->SetParent(nullptr); + cell.second->element->ForceRedraw(); + + delete cell.second->element; cell.second->element = nullptr; } @@ -449,6 +455,13 @@ void CGUIGridLayout_Impl::RepositionGridItems() { cell.second->container->SetPosition(CVector2D((cell.second->column - 1) * (1.0f / m_columns), (cell.second->row - 1) * (1.0f / m_rows)), true); cell.second->container->SetSize(CVector2D(1.0f / m_columns, 1.0f / m_rows), true); + + if (cell.second->element != nullptr) + { + cell.second->element->SetPosition(CVector2D(0.0f, 0.0f), true); + cell.second->element->SetSize(CVector2D(1.0f, 1.0f), true); + cell.second->element->ForceRedraw(); + } } } diff --git a/Client/gui/CGUIGridLayout_Impl.h b/Client/gui/CGUIGridLayout_Impl.h index 5c19a32947..1d62918e9c 100644 --- a/Client/gui/CGUIGridLayout_Impl.h +++ b/Client/gui/CGUIGridLayout_Impl.h @@ -38,8 +38,8 @@ class CGUIGridLayout_Impl : public CGUIGridLayout, public CGUIElement_Impl const bool AddItem(CGUIElement* item, int column, int row, const bool moveToNextCell = true); const bool AddItem(CGUIElement* item, const bool moveToNextCell = true); - const bool RemoveItem(const int column, const int row, const bool moveToPreviousCell = false); - const bool RemoveItem(const CGUIElement* item, const bool moveToPreviousCell = false); + const bool RemoveItem(const int column, const int row, const bool moveToPreviousCell = false, const bool deleteItem = false); + const bool RemoveItem(const CGUIElement* item, const bool moveToPreviousCell = false, const bool deleteItem = false); SGridCellItem* GetCell(const int column, const int row) const; SGridCellItem* GetCell(const CGUIElement* item) const; diff --git a/Client/sdk/gui/CGUIGridLayout.h b/Client/sdk/gui/CGUIGridLayout.h index 60096f463d..05833b7ac0 100644 --- a/Client/sdk/gui/CGUIGridLayout.h +++ b/Client/sdk/gui/CGUIGridLayout.h @@ -64,8 +64,8 @@ class CGUIGridLayout : public CGUIElement virtual const bool AddItem(CGUIElement* item, int column, int row, const bool moveToNextCell = true) = 0; virtual const bool AddItem(CGUIElement* item, const bool moveToNextCell = true) = 0; - virtual const bool RemoveItem(const int column, const int row, const bool moveToPreviousCell = false) = 0; - virtual const bool RemoveItem(const CGUIElement* item, const bool moveToPreviousCell = false) = 0; + virtual const bool RemoveItem(const int column, const int row, const bool moveToPreviousCell = false, const bool deleteItem = false) = 0; + virtual const bool RemoveItem(const CGUIElement* item, const bool moveToPreviousCell = false, const bool deleteItem = false) = 0; virtual SGridCellItem* GetCell(const int column, const int row) const = 0; virtual SGridCellItem* GetCell(const CGUIElement* item) const = 0;