﻿var _doGetTrimRect = function (pixelBuffer, w, h, trimThreshold) {
    // A B C
    // D x F
    // G H I

    var tx = w, ty = h,
        tw = 0, th = 0,
        ditch = w * 4;

    var x, y, i;
    var index;  // alpha index in pixels

    // trim A B C
    for (y = 0; y < h; y++) {
        index = y * ditch + 3;  // (x + y * w) * 4 + 3
        for (x = 0; x < w; x++, index += 4) {
            if (pixelBuffer[index] >= trimThreshold) {
                ty = y;
                y = h;
                break;
            }
        }
    }
    // trim G H I
    for (y = h - 1; y >= ty; y--) {
        index = y * ditch + 3;
        for (x = 0; x < w; x++, index += 4) {
            if (pixelBuffer[index] >= trimThreshold) {
                th = y - ty + 1;
                y = 0;
                break;
            }
        }
    }
    var skipTrimmedY = ditch * ty;   // skip A B C
    // trim D
    for (x = 0; x < w; x++) {
        index = skipTrimmedY + x * 4 + 3;
        for (i = 0; i < th; i++, index += ditch) {
            if (pixelBuffer[index] >= trimThreshold) {
                tx = x;
                x = w;
                break;
            }
        }
    }
    // trim F
    for (x = w - 1; x >= tx; x--) {
        index = skipTrimmedY + x * 4 + 3;
        for (i = 0; i < th; i++, index += ditch) {
            if (pixelBuffer[index] >= trimThreshold) {
                tw = x - tx + 1;
                x = 0;
                break;
            }
        }
    }

    return new Fire.Rect(tx, ty, tw, th);
};

Fire.arrayCmpFilter = function ( items, func ) {
    var results, item, i, j;

    results = [];
    for ( i = 0; i < items.length; ++i ) {
        item = items[i];
        var add = true;

        for ( j = 0; j < results.length; ++j ) {
            var addedItem = results[j];

            if ( item === addedItem ) {
                // existed
                add = false;
                break;
            }

            var cmp = func( addedItem, item );
            if ( cmp > 0 ) {
                add = false;
                break;
            }
            else if ( cmp < 0 ) {
                results.splice(j, 1);
                --j;
            }
        }

        if ( add ) {
            results.push(item);
        }
    }

    return results;
};

@@ -2291,12 +2291,783 @@
 results;%0A%7D;%0A
+%0AFire.arrayResize = function ( array, newLength ) %7B%0A    if ( array.length %3E= newLength ) %7B%0A        array.length = newLength;%0A        return;%0A    %7D%0A%0A    var start = array.length;%0A    var lastItem = array%5Barray.length-1%5D;%0A    array.length = newLength;%0A%0A    for ( var i = start; i %3C newLength; ++i ) %7B%0A        if ( lastItem.clone ) %7B%0A            array%5Bi%5D = lastItem.clone();%0A        %7D%0A        else %7B%0A            array%5Bi%5D = lastItem;%0A        %7D%0A    %7D%0A%7D;%0A%0AFire.arrayFillUndefined = function ( array, value ) %7B%0A    for ( var i = 0; i %3C array.length; ++i ) %7B%0A        if ( array%5Bi%5D !== undefined )%0A            continue;%0A%0A        if ( value !== null && value.clone ) %7B%0A            array%5Bi%5D = value.clone();%0A        %7D%0A        else %7B%0A            array%5Bi%5D = value;%0A        %7D%0A    %7D%0A%7D;%0A

add array resize and fill operation for editor