From 5b4960dd45f21b196d9f196e769985171a9e7d00 Mon Sep 17 00:00:00 2001 From: Calum Scott Date: Sun, 20 Jan 2013 16:35:16 +0000 Subject: [PATCH 1/4] Made rectangle parameter optional. You check to see if rectangle is null in the function so I'm guessing you forgot to set the parameter's default value to null. Fixed. --- src/org/villekoskela/utils/RectanglePacker.as | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/villekoskela/utils/RectanglePacker.as b/src/org/villekoskela/utils/RectanglePacker.as index e05afa1..5e81bc1 100644 --- a/src/org/villekoskela/utils/RectanglePacker.as +++ b/src/org/villekoskela/utils/RectanglePacker.as @@ -100,7 +100,7 @@ package org.villekoskela.utils * @param rectangle an instance where to set the rectangle's values * @return */ - public function getRectangle(index:int, rectangle:Rectangle):Rectangle + public function getRectangle(index:int, rectangle:Rectangle = null):Rectangle { var inserted:IntegerRectangle = mInsertedRectangles[index]; if (rectangle) From 8fbee7f0077ac199f97c1101d2a4e02479a68eff Mon Sep 17 00:00:00 2001 From: Calum Scott Date: Thu, 24 Jan 2013 19:08:19 +0000 Subject: [PATCH 2/4] Dynamically calculates minimum width and height. Removes the need to manually specify the width and height. Simply set the absolute maximum width and height (like 2048x2048 if you're working with a Stage3D texture) and the class will automatically calculate the smallest possible width and height after packing. Accessible via the `width` and `height` getters. --- src/org/villekoskela/utils/RectanglePacker.as | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/org/villekoskela/utils/RectanglePacker.as b/src/org/villekoskela/utils/RectanglePacker.as index 5e81bc1..ba84ba6 100644 --- a/src/org/villekoskela/utils/RectanglePacker.as +++ b/src/org/villekoskela/utils/RectanglePacker.as @@ -61,7 +61,7 @@ package org.villekoskela.utils * @param width the width of the main rectangle * @param height the height of the main rectangle */ - public function RectanglePacker(width:int, height:int) + public function RectanglePacker(width:int = 2048, height:int = 2048) { mOutsideRectangle = new IntegerRectangle(width + 1, height + 1, 0, 0); reset(width, height); @@ -175,12 +175,43 @@ package org.villekoskela.utils mInsertedRectangles[mInsertedRectangles.length] = target; } + mHeight = 0; + mWidth = 0; + + var length:int = mInsertedRectangles.length; + var rect:IntegerRectangle; + + for (var i:int = 0; i < length; i++) + { + rect = mInsertedRectangles[i]; + + if (rect.y + rect.height > mHeight) + { + mHeight = rect.y + rect.height; + } + + if (rect.x + rect.width > mWidth) + { + mWidth = rect.x + rect.width; + } + } + freeSize(sortableSize); } return rectangleCount; } + public function get height():int + { + return mHeight; + } + + public function get width():int + { + return mWidth; + } + /** * Removes rectangles from the filteredAreas that are sub rectangles of any rectangle in areas. * @param areas rectangles from which the filtering is performed From 719ca40e9857f6b3137b3e0f32256ced5fcda072 Mon Sep 17 00:00:00 2001 From: Calum Scott Date: Thu, 24 Jan 2013 19:10:30 +0000 Subject: [PATCH 3/4] Positioned code in a more suitable location. --- src/org/villekoskela/utils/RectanglePacker.as | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/org/villekoskela/utils/RectanglePacker.as b/src/org/villekoskela/utils/RectanglePacker.as index ba84ba6..e46c61f 100644 --- a/src/org/villekoskela/utils/RectanglePacker.as +++ b/src/org/villekoskela/utils/RectanglePacker.as @@ -175,30 +175,30 @@ package org.villekoskela.utils mInsertedRectangles[mInsertedRectangles.length] = target; } - mHeight = 0; - mWidth = 0; - - var length:int = mInsertedRectangles.length; - var rect:IntegerRectangle; - - for (var i:int = 0; i < length; i++) - { - rect = mInsertedRectangles[i]; - - if (rect.y + rect.height > mHeight) - { - mHeight = rect.y + rect.height; - } - - if (rect.x + rect.width > mWidth) - { - mWidth = rect.x + rect.width; - } - } - freeSize(sortableSize); } + mHeight = 0; + mWidth = 0; + + var length:int = mInsertedRectangles.length; + var rect:IntegerRectangle; + + for (var i:int = 0; i < length; i++) + { + rect = mInsertedRectangles[i]; + + if (rect.y + rect.height > mHeight) + { + mHeight = rect.y + rect.height; + } + + if (rect.x + rect.width > mWidth) + { + mWidth = rect.x + rect.width; + } + } + return rectangleCount; } From 8bd7ba4412fd94c839d815c8ee19eb14f69c8f45 Mon Sep 17 00:00:00 2001 From: Calum Scott Date: Thu, 24 Jan 2013 19:22:10 +0000 Subject: [PATCH 4/4] Positioned code in a more suitable location. --- src/org/villekoskela/utils/RectanglePacker.as | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/org/villekoskela/utils/RectanglePacker.as b/src/org/villekoskela/utils/RectanglePacker.as index e46c61f..c35fed7 100644 --- a/src/org/villekoskela/utils/RectanglePacker.as +++ b/src/org/villekoskela/utils/RectanglePacker.as @@ -54,7 +54,9 @@ package org.villekoskela.utils private var mSortableSizeStack:Vector. = new Vector.(); private var mRectangleStack:Vector. = new Vector.(); + public function get height():int { return mHeight; } public function get rectangleCount():int { return mInsertedRectangles.length; } + public function get width():int { return mWidth; } /** * Constructs new rectangle packer @@ -202,16 +204,6 @@ package org.villekoskela.utils return rectangleCount; } - public function get height():int - { - return mHeight; - } - - public function get width():int - { - return mWidth; - } - /** * Removes rectangles from the filteredAreas that are sub rectangles of any rectangle in areas. * @param areas rectangles from which the filtering is performed