2014-09-21 23:39:54 +04:00
|
|
|
describe("FrameUtils suite", function() {
|
|
|
|
var black = '#000000';
|
|
|
|
var red = '#ff0000';
|
|
|
|
var transparent = Constants.TRANSPARENT_COLOR;
|
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
// shortcuts
|
|
|
|
var toFrameGrid = test.testutils.toFrameGrid;
|
|
|
|
var frameEqualsGrid = test.testutils.frameEqualsGrid;
|
|
|
|
|
2014-09-21 23:39:54 +04:00
|
|
|
it("merges 2 frames", function () {
|
2015-08-09 13:37:03 +03:00
|
|
|
var B = black, R = red, T = transparent;
|
2014-09-21 23:39:54 +04:00
|
|
|
var frame1 = pskl.model.Frame.fromPixelGrid([
|
2015-08-09 13:37:03 +03:00
|
|
|
[B, T],
|
|
|
|
[T, B]
|
2014-09-21 23:39:54 +04:00
|
|
|
]);
|
|
|
|
|
|
|
|
var frame2 = pskl.model.Frame.fromPixelGrid([
|
2015-08-09 13:37:03 +03:00
|
|
|
[T, R],
|
|
|
|
[R, T]
|
2014-09-21 23:39:54 +04:00
|
|
|
]);
|
|
|
|
|
|
|
|
var mergedFrame = pskl.utils.FrameUtils.merge([frame1, frame2]);
|
2015-08-09 13:37:03 +03:00
|
|
|
frameEqualsGrid(mergedFrame, [
|
|
|
|
[B, R],
|
|
|
|
[R, B]
|
|
|
|
]);
|
2014-09-21 23:39:54 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns same frame when merging single frame", function () {
|
2015-08-09 13:37:03 +03:00
|
|
|
var B = black, T = transparent;
|
|
|
|
var frame1 = pskl.model.Frame.fromPixelGrid(toFrameGrid([
|
|
|
|
[B, T],
|
|
|
|
[B, T]
|
|
|
|
]));
|
2014-09-21 23:39:54 +04:00
|
|
|
|
|
|
|
var mergedFrame = pskl.utils.FrameUtils.merge([frame1]);
|
2015-08-09 13:37:03 +03:00
|
|
|
frameEqualsGrid(mergedFrame, [
|
|
|
|
[B, T],
|
|
|
|
[B, T]
|
|
|
|
]);
|
2014-09-21 23:39:54 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
var checkPixelsColor = function (frame, pixels, color) {
|
|
|
|
pixels.forEach(function (pixel) {
|
|
|
|
var pixelColor = frame.getPixel(pixel[0], pixel[1]);
|
|
|
|
expect(pixelColor).toBe(color);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
it ("converts an image to a frame", function () {
|
2015-08-09 13:37:03 +03:00
|
|
|
var B = black, T = transparent;
|
2014-09-21 23:39:54 +04:00
|
|
|
var frame1 = pskl.model.Frame.fromPixelGrid([
|
2015-08-09 13:37:03 +03:00
|
|
|
[B, T],
|
|
|
|
[T, B]
|
2014-09-21 23:39:54 +04:00
|
|
|
]);
|
|
|
|
|
|
|
|
var image = pskl.utils.FrameUtils.toImage(frame1);
|
|
|
|
expect(image.width).toBe(2);
|
|
|
|
expect(image.height).toBe(2);
|
|
|
|
|
|
|
|
var biggerImage = pskl.utils.FrameUtils.toImage(frame1, 3);
|
|
|
|
expect(biggerImage.width).toBe(6);
|
|
|
|
expect(biggerImage.height).toBe(6);
|
|
|
|
|
|
|
|
var biggerFrame = pskl.utils.FrameUtils.createFromImage(biggerImage);
|
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
frameEqualsGrid(biggerFrame, [
|
|
|
|
[B, B, B, T, T, T],
|
|
|
|
[B, B, B, T, T, T],
|
|
|
|
[B, B, B, T, T, T],
|
|
|
|
[T, T, T, B, B, B],
|
|
|
|
[T, T, T, B, B, B],
|
|
|
|
[T, T, T, B, B, B]
|
|
|
|
]);
|
2014-09-21 23:39:54 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it ("[LayerUtils] creates a layer from a simple spritesheet", function () {
|
2015-08-09 13:37:03 +03:00
|
|
|
var B = black, R = red;
|
|
|
|
|
|
|
|
// original image in 4x2
|
|
|
|
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
|
|
|
|
[B, R, B, R],
|
|
|
|
[R, B, B, R]
|
|
|
|
]));
|
|
|
|
|
2014-09-21 23:39:54 +04:00
|
|
|
var spritesheet = pskl.utils.FrameUtils.toImage(frame);
|
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
// split the spritesheet by 4
|
2014-09-21 23:39:54 +04:00
|
|
|
var frames = pskl.utils.LayerUtils.createLayerFromSpritesheet(spritesheet, 4);
|
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
// expect 4 frames of 1x2
|
|
|
|
expect(frames.length).toBe(4);
|
2014-09-21 23:39:54 +04:00
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
// verify frame content
|
|
|
|
frameEqualsGrid(frames[0], [
|
|
|
|
[B],
|
|
|
|
[R]
|
|
|
|
]);
|
|
|
|
frameEqualsGrid(frames[1], [
|
|
|
|
[R],
|
|
|
|
[B]
|
|
|
|
]);
|
|
|
|
frameEqualsGrid(frames[2], [
|
|
|
|
[B],
|
|
|
|
[B]
|
|
|
|
]);
|
|
|
|
frameEqualsGrid(frames[3], [
|
|
|
|
[R],
|
|
|
|
[R]
|
|
|
|
]);
|
|
|
|
});
|
2014-09-21 23:39:54 +04:00
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
it ("supports null values in frame array", function () {
|
|
|
|
var B = black, T = transparent;
|
|
|
|
var frame = pskl.model.Frame.fromPixelGrid([
|
|
|
|
[B, null],
|
|
|
|
[null, B]
|
|
|
|
]);
|
2014-09-21 23:39:54 +04:00
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
var image = pskl.utils.FrameUtils.toImage(frame);
|
2014-09-21 23:39:54 +04:00
|
|
|
|
2015-08-09 13:37:03 +03:00
|
|
|
// transform back to frame for ease of testing
|
|
|
|
var testFrame = pskl.utils.FrameUtils.createFromImage(image);
|
|
|
|
frameEqualsGrid(testFrame, [
|
|
|
|
[B, T],
|
|
|
|
[T, B]
|
|
|
|
]);
|
2014-09-21 23:39:54 +04:00
|
|
|
});
|
|
|
|
});
|