Skip to content

Commit 5c47f44

Browse files
committed
Add map for item lookup
1 parent 4f47d47 commit 5c47f44

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

Client/gui/CGUIGridLayout_Impl.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const bool CGUIGridLayout_Impl::AddItem(CGUIElement* item, int column, int row,
7676
// If cell is already occupied
7777
if (m_grid[column - 1][row - 1] != 0)
7878
{
79-
return false;
79+
return false;
8080
}
8181

8282
auto* cell = GetCell(column, row);
@@ -88,6 +88,8 @@ const bool CGUIGridLayout_Impl::AddItem(CGUIElement* item, int column, int row,
8888
item->SetPosition(CVector2D(0.0f, 0.0f), true);
8989
item->SetSize(CVector2D(1.0f, 1.0f), true);
9090

91+
m_items.emplace(item, cell->id);
92+
9193
if (moveToNextCell)
9294
{
9395
if (m_activeRow == m_rows)
@@ -124,6 +126,8 @@ const bool CGUIGridLayout_Impl::RemoveItem(const int column, const int row, cons
124126
element = nullptr;
125127
m_grid[column - 1][row - 1] = 0;
126128

129+
m_items.erase(element);
130+
127131
if (moveToPreviousCell)
128132
{
129133
if (m_activeRow == 1)
@@ -145,17 +149,12 @@ const bool CGUIGridLayout_Impl::RemoveItem(const int column, const int row, cons
145149

146150
const bool CGUIGridLayout_Impl::RemoveItem(const CGUIElement* item, const bool moveToPreviousCell)
147151
{
148-
auto& ci = m_cells.begin();
152+
auto* cell = GetCell(item);
149153

150-
while (ci != m_cells.end())
151-
{
152-
if (ci->second->element == item) // todo: store column/row on CGUIElement and use that to find the cell instead of iterating
153-
return RemoveItem(ci->second->column, ci->second->row, moveToPreviousCell);
154-
else
155-
ci++;
156-
}
154+
if (cell == nullptr)
155+
return false;
157156

158-
return false;
157+
return RemoveItem(cell->column, cell->row, moveToPreviousCell);
159158
}
160159

161160
SGridCellItem* CGUIGridLayout_Impl::GetCell(const int column, const int row) const
@@ -171,6 +170,12 @@ SGridCellItem* CGUIGridLayout_Impl::GetCell(const int column, const int row) con
171170
return cell->second;
172171
}
173172

173+
SGridCellItem* CGUIGridLayout_Impl::GetCell(const CGUIElement* item) const
174+
{
175+
int id = m_items.count(item) ? m_items.at(item) : 0;
176+
return m_cells.count(id) ? m_cells.at(id) : nullptr;
177+
}
178+
174179
std::vector<SGridCellItem*> CGUIGridLayout_Impl::GetCellsInColumn(const int column)
175180
{
176181
return GetCellsInGrid(column, 0, column, m_rows - 1);
@@ -331,27 +336,22 @@ const std::pair<int, int> CGUIGridLayout_Impl::GetActiveCell()
331336

332337
void CGUIGridLayout_Impl::SetItemAlignment(const CGUIElement* item, eGridLayoutItemAlignment alignment)
333338
{
334-
for (auto& cell : m_cells)
335-
{
336-
if (cell.second->element == item) // todo: store column/row on CGUIElement and use that to find the cell instead of iterating
337-
{
338-
cell.second->alignment = alignment;
339-
return;
340-
}
341-
}
339+
auto* cell = GetCell(item);
340+
341+
if (cell == nullptr)
342+
return;
343+
344+
cell->alignment = alignment;
342345
}
343346

344347
const eGridLayoutItemAlignment CGUIGridLayout_Impl::GetItemAlignment(const CGUIElement* item) const
345348
{
346-
for (auto& cell : m_cells)
347-
{
348-
if (cell.second->element == item) // todo: store column/row on CGUIElement and use that to find the cell instead of iterating
349-
{
350-
return cell.second->alignment;
351-
}
352-
}
349+
const auto* cell = GetCell(item);
353350

354-
return m_defaultAlignment;
351+
if (cell == nullptr)
352+
return m_defaultAlignment;
353+
354+
return cell->alignment;
355355
}
356356

357357
const bool CGUIGridLayout_Impl::SetCellAlpha(const int column, const int row, const float alpha)
@@ -406,6 +406,7 @@ void CGUIGridLayout_Impl::CreateGridCells()
406406
// Size the cell container in the grid
407407
cell->container->SetSize(CVector2D(1.0f / m_columns, 1.0f / m_rows), true);
408408

409+
cell->id = m_nextId;
409410
cell->element = nullptr;
410411
cell->column = i + 1;
411412
cell->row = j + 1;
@@ -430,6 +431,8 @@ void CGUIGridLayout_Impl::CleanupGridItems()
430431

431432
if (cell.second->element)
432433
{
434+
m_items.erase(cell.second->element);
435+
433436
cell.second->element->SetParent(nullptr);
434437
cell.second->element = nullptr;
435438
}

Client/gui/CGUIGridLayout_Impl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CGUIGridLayout_Impl : public CGUIGridLayout, public CGUIElement_Impl
4242
const bool RemoveItem(const CGUIElement* item, const bool moveToPreviousCell = false);
4343

4444
SGridCellItem* GetCell(const int column, const int row) const;
45+
SGridCellItem* GetCell(const CGUIElement* item) const;
4546

4647
std::vector<SGridCellItem*> GetCellsInGrid(const int startColumn, const int startRow, const int endColumn, const int endRow);
4748
std::vector<SGridCellItem*> GetCellsOutsideGrid(const int startColumn, const int startRow, const int endColumn, const int endRow);
@@ -70,8 +71,9 @@ class CGUIGridLayout_Impl : public CGUIGridLayout, public CGUIElement_Impl
7071

7172
float m_defaultCellAlpha = 1.0f;
7273

73-
std::vector<std::vector<int>> m_grid;
74-
std::unordered_map<int, SGridCellItem*> m_cells;
74+
std::vector<std::vector<int>> m_grid;
75+
std::unordered_map<int, SGridCellItem*> m_cells;
76+
std::unordered_map<const CGUIElement*, int> m_items;
7577

7678
CGUITexture* m_cellTexture = nullptr;
7779
CGUITexture* m_cellTextureAlt = nullptr;

Client/sdk/gui/CGUIGridLayout.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum class eGridLayoutItemAlignment
3333

3434
struct SGridCellItem
3535
{
36+
int id;
3637
CGUIStaticImage* container;
3738
CGUIElement* element;
3839
eGridLayoutItemAlignment alignment;
@@ -67,6 +68,7 @@ class CGUIGridLayout : public CGUIElement
6768
virtual const bool RemoveItem(const CGUIElement* item, const bool moveToPreviousCell = false) = 0;
6869

6970
virtual SGridCellItem* GetCell(const int column, const int row) const = 0;
71+
virtual SGridCellItem* GetCell(const CGUIElement* item) const = 0;
7072

7173
virtual std::vector<SGridCellItem*> GetCellsInGrid(const int startColumn, const int startRow, const int endColumn, const int endRow) = 0;
7274
virtual std::vector<SGridCellItem*> GetCellsOutsideGrid(const int startColumn, const int startRow, const int endColumn, const int endRow) = 0;

0 commit comments

Comments
 (0)