Fix inserting to RectPack after freeing node

This commit is contained in:
Wojciech Figat
2022-04-01 14:15:09 +02:00
parent 251de1b643
commit cff57e5697

View File

@@ -69,6 +69,18 @@ struct RectPack
NodeType* Insert(SizeType itemWidth, SizeType itemHeight, SizeType itemPadding, Args&&...args)
{
NodeType* result;
const SizeType paddedWidth = itemWidth + itemPadding;
const SizeType paddedHeight = itemHeight + itemPadding;
// Check if we're free and just the right size
if (!IsUsed && Width == paddedWidth && Height == paddedHeight)
{
// Insert into this slot
IsUsed = true;
result = (NodeType*)this;
result->OnInsert(Forward<Args>(args)...);
return result;
}
// If there are left and right slots there are empty regions around this slot (it also means this slot is occupied)
if (Left || Right)
@@ -85,14 +97,8 @@ struct RectPack
if (result)
return result;
}
// Not enough space
return nullptr;
}
const SizeType paddedWidth = itemWidth + itemPadding;
const SizeType paddedHeight = itemHeight + itemPadding;
// This slot can't fit or has been already occupied
if (IsUsed || paddedWidth > Width || paddedHeight > Height)
{
@@ -100,16 +106,6 @@ struct RectPack
return nullptr;
}
// Check if we're just right size
if (Width == paddedWidth && Height == paddedHeight)
{
// Insert into this slot
IsUsed = true;
result = (NodeType*)this;
result->OnInsert(Forward<Args>(args)...);
return result;
}
// The width and height of the new child node
const SizeType remainingWidth = Math::Max<SizeType>(0, Width - paddedWidth);
const SizeType remainingHeight = Math::Max<SizeType>(0, Height - paddedHeight);