From cff57e56977466a5e5240cb7781f74a3f6a2ec75 Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Fri, 1 Apr 2022 14:15:09 +0200 Subject: [PATCH] Fix inserting to RectPack after freeing node --- Source/Engine/Utilities/RectPack.h | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/Source/Engine/Utilities/RectPack.h b/Source/Engine/Utilities/RectPack.h index 1811684ea..b7528834c 100644 --- a/Source/Engine/Utilities/RectPack.h +++ b/Source/Engine/Utilities/RectPack.h @@ -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)...); + 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)...); - return result; - } - // The width and height of the new child node const SizeType remainingWidth = Math::Max(0, Width - paddedWidth); const SizeType remainingHeight = Math::Max(0, Height - paddedHeight);