@@ -76,7 +76,7 @@ const bool CGUIGridLayout_Impl::AddItem(CGUIElement* item, int column, int row,
76
76
// If cell is already occupied
77
77
if (m_grid[column - 1 ][row - 1 ] != 0 )
78
78
{
79
- return false ;
79
+ return false ;
80
80
}
81
81
82
82
auto * cell = GetCell (column, row);
@@ -88,6 +88,8 @@ const bool CGUIGridLayout_Impl::AddItem(CGUIElement* item, int column, int row,
88
88
item->SetPosition (CVector2D (0 .0f , 0 .0f ), true );
89
89
item->SetSize (CVector2D (1 .0f , 1 .0f ), true );
90
90
91
+ m_items.emplace (item, cell->id );
92
+
91
93
if (moveToNextCell)
92
94
{
93
95
if (m_activeRow == m_rows)
@@ -124,6 +126,8 @@ const bool CGUIGridLayout_Impl::RemoveItem(const int column, const int row, cons
124
126
element = nullptr ;
125
127
m_grid[column - 1 ][row - 1 ] = 0 ;
126
128
129
+ m_items.erase (element);
130
+
127
131
if (moveToPreviousCell)
128
132
{
129
133
if (m_activeRow == 1 )
@@ -145,17 +149,12 @@ const bool CGUIGridLayout_Impl::RemoveItem(const int column, const int row, cons
145
149
146
150
const bool CGUIGridLayout_Impl::RemoveItem (const CGUIElement* item, const bool moveToPreviousCell)
147
151
{
148
- auto & ci = m_cells. begin ( );
152
+ auto * cell = GetCell (item );
149
153
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 ;
157
156
158
- return false ;
157
+ return RemoveItem (cell-> column , cell-> row , moveToPreviousCell) ;
159
158
}
160
159
161
160
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
171
170
return cell->second ;
172
171
}
173
172
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
+
174
179
std::vector<SGridCellItem*> CGUIGridLayout_Impl::GetCellsInColumn (const int column)
175
180
{
176
181
return GetCellsInGrid (column, 0 , column, m_rows - 1 );
@@ -331,27 +336,22 @@ const std::pair<int, int> CGUIGridLayout_Impl::GetActiveCell()
331
336
332
337
void CGUIGridLayout_Impl::SetItemAlignment (const CGUIElement* item, eGridLayoutItemAlignment alignment)
333
338
{
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;
342
345
}
343
346
344
347
const eGridLayoutItemAlignment CGUIGridLayout_Impl::GetItemAlignment (const CGUIElement* item) const
345
348
{
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);
353
350
354
- return m_defaultAlignment;
351
+ if (cell == nullptr )
352
+ return m_defaultAlignment;
353
+
354
+ return cell->alignment ;
355
355
}
356
356
357
357
const bool CGUIGridLayout_Impl::SetCellAlpha (const int column, const int row, const float alpha)
@@ -406,6 +406,7 @@ void CGUIGridLayout_Impl::CreateGridCells()
406
406
// Size the cell container in the grid
407
407
cell->container ->SetSize (CVector2D (1 .0f / m_columns, 1 .0f / m_rows), true );
408
408
409
+ cell->id = m_nextId;
409
410
cell->element = nullptr ;
410
411
cell->column = i + 1 ;
411
412
cell->row = j + 1 ;
@@ -430,6 +431,8 @@ void CGUIGridLayout_Impl::CleanupGridItems()
430
431
431
432
if (cell.second ->element )
432
433
{
434
+ m_items.erase (cell.second ->element );
435
+
433
436
cell.second ->element ->SetParent (nullptr );
434
437
cell.second ->element = nullptr ;
435
438
}
0 commit comments