Pixel-Composer/scripts/pack_bottom_left/pack_bottom_left.gml

37 lines
1 KiB
Text
Raw Normal View History

2023-03-19 09:17:39 +01:00
function sprite_pack_bottom_left(rectangles, height = 999999) {
var packedRectangles = [];
var maxW = 0;
var maxH = 0;
2024-03-14 14:35:19 +01:00
array_sort(rectangles, function(a, b) { return b.h - a.h; });
2023-03-19 09:17:39 +01:00
var xx = 0;
for (var i = 0; i < array_length(rectangles); i++) {
var rect = rectangles[i];
var placed = false;
for (var j = 0; j < array_length(packedRectangles); j++) {
var packedRect = packedRectangles[j];
if (packedRect.w >= rect.w && packedRect.h + rect.h <= height) {
rect.x = packedRect.x;
rect.y = packedRect.y + packedRect.h;
packedRect.h += rect.h;
placed = true;
break;
}
}
if (!placed) {
rect.x = xx;
rect.y = 0;
xx += rect.w;
array_push(packedRectangles, rect.clone());
}
maxW = max(maxW, rect.x + rect.w);
maxH = max(maxH, rect.y + rect.h);
}
return [ new Rectangle(0, 0, maxW, maxH), rectangles ];
}