Rotate non square sprites, added rotate icon, unit tests for transforms

This commit is contained in:
jdescottes
2014-11-23 15:03:35 +01:00
parent 796cd4466e
commit ce1a5c4918
14 changed files with 385 additions and 85 deletions

View File

@ -0,0 +1,253 @@
describe("FrameTransform suite", function() {
var A = '#000000';
var B = '#ff0000';
var O = Constants.TRANSPARENT_COLOR;
var HORIZONTAL = pskl.utils.FrameTransform.HORIZONTAL;
var VERTICAL = pskl.utils.FrameTransform.VERTICAL;
var CLOCKWISE = pskl.utils.FrameTransform.CLOCKWISE;
var COUNTERCLOCKWISE = pskl.utils.FrameTransform.COUNTERCLOCKWISE;
/**
* Check a frame has the pixels as a given grid
* @param {Frame} frame [description]
* @param {Array<Array<String|Color>>} grid
*/
var frameEqualsGrid = function (frame, grid) {
frame.forEachPixel(function (color, col, row) {
expect(color).toBe(grid[row][col]);
});
};
var toFrameGrid = function (normalGrid) {
var frameGrid = [];
var w = normalGrid[0].length;
var h = normalGrid.length;
for (var x = 0 ; x < w ; x++) {
frameGrid[x] = [];
for (var y = 0 ; y < h ; y++) {
frameGrid[x][y] = normalGrid[y][x];
}
}
return frameGrid;
};
/*******************************/
/************ FLIP *************/
/*******************************/
it("flips a frame vertically", function () {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[A, O],
[O, B]
]));
// should have flipped
pskl.utils.FrameTransform.flip(frame, VERTICAL);
frameEqualsGrid(frame, [
[O, A],
[B, O]
]);
});
it("flips a frame horizontally", function () {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[A, O],
[O, B]
]));
// should have flipped
pskl.utils.FrameTransform.flip(frame, HORIZONTAL);
frameEqualsGrid(frame, [
[O, B],
[A, O]
]);
});
it("flips rectangular frame", function () {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[A, O],
[A, O],
[A, O]
]));
// should have flipped
pskl.utils.FrameTransform.flip(frame, VERTICAL);
frameEqualsGrid(frame, [
[O, A],
[O, A],
[O, A]
]);
// should be the same
pskl.utils.FrameTransform.flip(frame, HORIZONTAL);
frameEqualsGrid(frame, [
[O, A],
[O, A],
[O, A]
]);
});
/*******************************/
/*********** ROTATE ************/
/*******************************/
it("rotates a frame counterclockwise", function () {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[A, O],
[O, B]
]));
// rotate once
pskl.utils.FrameTransform.rotate(frame, COUNTERCLOCKWISE);
frameEqualsGrid(frame, [
[O, B],
[A, O]
]);
// rotate twice
pskl.utils.FrameTransform.rotate(frame, COUNTERCLOCKWISE);
frameEqualsGrid(frame, [
[B, O],
[O, A]
]);
// rotate 3
pskl.utils.FrameTransform.rotate(frame, COUNTERCLOCKWISE);
frameEqualsGrid(frame, [
[O, A],
[B, O]
]);
// rotate 4 - back to initial state
pskl.utils.FrameTransform.rotate(frame, COUNTERCLOCKWISE);
frameEqualsGrid(frame, [
[A, O],
[O, B]
]);
});
it("rotates a frame clockwise", function () {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[A, O],
[O, B]
]));
// rotate once
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, A],
[B, O]
]);
// rotate twice
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[B, O],
[O, A]
]);
// rotate 3
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, B],
[A, O]
]);
// rotate 4 - back to initial state
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[A, O],
[O, B]
]);
});
it("rotates a rectangular frame", function () {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[A, O],
[A, O],
[B, O],
[B, O]
]));
// rotate once
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, O],
[B, A],
[O, O],
[O, O]
]);
// rotate twice
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, O],
[O, B],
[O, A],
[O, O]
]);
// rotate 3
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, O],
[O, O],
[A, B],
[O, O]
]);
// rotate 4
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, O],
[A, O],
[B, O],
[O, O]
]);
});
it("rotates a rectangular (horizontal) frame", function () {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[O, O, O, O],
[A, A, B, B]
]));
// rotate once
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, A, O, O],
[O, B, O, O]
]);
// rotate twice
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, B, A, O],
[O, O, O, O]
]);
// rotate 3
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, O, B, O],
[O, O, A, O]
]);
// rotate 4
pskl.utils.FrameTransform.rotate(frame, CLOCKWISE);
frameEqualsGrid(frame, [
[O, O, O, O],
[O, A, B, O]
]);
});
});

View File

@ -101,45 +101,4 @@ describe("FrameUtils suite", function() {
expect(frames[3].getPixel(0,1)).toBe(red);
});
// it("starts at -1", function() {
// historyService = createMockHistoryService();
// expect(historyService.currentIndex).toBe(-1);
// });
// it("is at 0 after init", function() {
// historyService = createMockHistoryService();
// historyService.init();
// expect(historyService.currentIndex).toBe(0);
// });
// it("stores a piskel snapshot after 5 SAVE", function () {
// // BEFORE
// var SNAPSHOT_PERIOD_BACKUP = pskl.service.HistoryService.SNAPSHOT_PERIOD;
// pskl.service.HistoryService.SNAPSHOT_PERIOD = 5;
// historyService = createMockHistoryService();
// historyService.init();
// sendSaveEvents(pskl.service.HistoryService.REPLAY).times(5);
// expect(historyService.currentIndex).toBe(5);
// expect(getLastState().piskel).toBe(SERIALIZED_PISKEL);
// sendSaveEvents(pskl.service.HistoryService.REPLAY).times(4);
// sendSaveEvents(pskl.service.HistoryService.REPLAY_NO_SNAPSHOT).once();
// expect(getLastState().piskel).toBeUndefined();
// sendSaveEvents(pskl.service.HistoryService.REPLAY_NO_SNAPSHOT).once();
// expect(getLastState().piskel).toBeUndefined();
// sendSaveEvents(pskl.service.HistoryService.REPLAY).once();
// expect(getLastState().piskel).toBe(SERIALIZED_PISKEL);
// // AFTER
// pskl.service.HistoryService.SNAPSHOT_PERIOD = SNAPSHOT_PERIOD_BACKUP;
// })
});