piskel/src/js/service/HistoryService.js

168 lines
5.7 KiB
JavaScript
Raw Normal View History

(function () {
2014-04-17 03:27:49 +04:00
var ns = $.namespace('pskl.service');
2014-08-12 09:11:23 +04:00
ns.HistoryService = function (piskelController, shortcutService, deserializer) {
this.piskelController = piskelController || pskl.app.piskelController;
this.shortcutService = shortcutService || pskl.app.shortcutService;
this.deserializer = deserializer || pskl.utils.serialization.Deserializer;
2014-08-12 02:30:57 +04:00
2014-04-17 03:27:49 +04:00
this.stateQueue = [];
this.currentIndex = -1;
this.lastLoadState = -1;
this.saveNextAsSnapshot = false;
};
ns.HistoryService.SNAPSHOT = 'SNAPSHOT';
ns.HistoryService.REPLAY = 'REPLAY';
2014-08-12 09:11:23 +04:00
ns.HistoryService.SNAPSHOT_PERIOD = 50;
ns.HistoryService.LOAD_STATE_INTERVAL = 50;
/**
* This event alters the state (frames, layers) of the piskel. The event is triggered before the execution of associated command.
* Don't store snapshots for such events.
*/
ns.HistoryService.REPLAY_NO_SNAPSHOT = 'REPLAY_NO_SNAPSHOT';
ns.HistoryService.prototype.init = function () {
2014-08-12 02:30:57 +04:00
$.subscribe(Events.PISKEL_SAVE_STATE, this.onSaveStateEvent.bind(this));
this.shortcutService.addShortcut('ctrl+Z', this.undo.bind(this));
this.shortcutService.addShortcut('ctrl+Y', this.redo.bind(this));
this.saveState({
type : ns.HistoryService.SNAPSHOT
});
};
ns.HistoryService.prototype.onSaveStateEvent = function (evt, stateInfo) {
this.saveState(stateInfo);
};
ns.HistoryService.prototype.saveState = function (stateInfo) {
2014-04-17 03:27:49 +04:00
this.stateQueue = this.stateQueue.slice(0, this.currentIndex + 1);
this.currentIndex = this.currentIndex + 1;
var state = {
action : stateInfo,
frameIndex : this.piskelController.currentFrameIndex,
layerIndex : this.piskelController.currentLayerIndex
2014-04-17 03:27:49 +04:00
};
var isSnapshot = stateInfo.type === ns.HistoryService.SNAPSHOT;
var isNoSnapshot = stateInfo.type === ns.HistoryService.REPLAY_NO_SNAPSHOT;
2014-08-12 09:11:23 +04:00
var isAtAutoSnapshotInterval = this.currentIndex % ns.HistoryService.SNAPSHOT_PERIOD === 0 || this.saveNextAsSnapshot;
if (isNoSnapshot && isAtAutoSnapshotInterval) {
this.saveNextAsSnapshot = true;
} else if (isSnapshot || isAtAutoSnapshotInterval) {
state.piskel = this.piskelController.serialize(true);
this.saveNextAsSnapshot = false;
2014-04-17 03:27:49 +04:00
}
this.stateQueue.push(state);
};
2012-09-16 15:10:05 +04:00
ns.HistoryService.prototype.undo = function () {
this.loadState(this.currentIndex - 1);
};
2012-09-16 15:10:05 +04:00
ns.HistoryService.prototype.redo = function () {
this.loadState(this.currentIndex + 1);
2014-04-17 03:27:49 +04:00
};
ns.HistoryService.prototype.isLoadStateAllowed_ = function (index) {
2014-08-12 09:11:23 +04:00
var timeOk = (Date.now() - this.lastLoadState) > ns.HistoryService.LOAD_STATE_INTERVAL;
var indexInRange = index >= 0 && index < this.stateQueue.length;
return timeOk && indexInRange;
};
ns.HistoryService.prototype.getPreviousSnapshotIndex_ = function (index) {
while (this.stateQueue[index] && !this.stateQueue[index].piskel) {
index = index - 1;
}
return index;
};
2014-04-17 03:27:49 +04:00
ns.HistoryService.prototype.loadState = function (index) {
try {
if (this.isLoadStateAllowed_(index)) {
this.lastLoadState = Date.now();
var snapshotIndex = this.getPreviousSnapshotIndex_(index);
if (snapshotIndex < 0) {
throw 'Could not find previous SNAPSHOT saved in history stateQueue';
}
var serializedPiskel = this.getSnapshotFromState_(snapshotIndex);
var onPiskelLoadedCb = this.onPiskelLoaded_.bind(this, index, snapshotIndex);
this.deserializer.deserialize(serializedPiskel, onPiskelLoadedCb);
}
} catch (e) {
window.console.error("[CRITICAL ERROR] : Unable to load a history state.");
if (typeof e === "string") {
window.console.error(e);
} else {
window.console.error(e.message);
window.console.error(e.stack);
2014-04-17 03:27:49 +04:00
}
this.stateQueue = [];
this.currentIndex = -1;
}
};
2014-04-17 03:27:49 +04:00
ns.HistoryService.prototype.getSnapshotFromState_ = function (stateIndex) {
var state = this.stateQueue[stateIndex];
var piskelSnapshot = state.piskel;
2014-04-17 03:27:49 +04:00
// If the snapshot is stringified, parse it and backup the result for faster access next time
// FIXME : Memory consumption might go crazy if we keep unpacking big piskels indefinitely
// ==> should ensure I remove some of them :)
if (typeof piskelSnapshot === "string") {
piskelSnapshot = JSON.parse(piskelSnapshot);
state.piskel = piskelSnapshot;
2014-04-17 03:27:49 +04:00
}
return piskelSnapshot;
};
2014-04-17 03:27:49 +04:00
ns.HistoryService.prototype.onPiskelLoaded_ = function (index, snapshotIndex, piskel) {
var originalSize = this.getPiskelSize_();
piskel.setDescriptor(this.piskelController.piskel.getDescriptor());
this.piskelController.setPiskel(piskel);
2014-04-17 03:27:49 +04:00
for (var i = snapshotIndex + 1 ; i <= index ; i++) {
var state = this.stateQueue[i];
this.setupState(state);
2014-04-17 03:27:49 +04:00
this.replayState(state);
}
2014-08-12 09:11:23 +04:00
// Should only do this when going backwards
var lastState = this.stateQueue[index+1];
if (lastState) {
this.setupState(lastState);
}
this.currentIndex = index;
$.publish(Events.PISKEL_RESET);
if (originalSize !== this.getPiskelSize_()) {
$.publish(Events.FRAME_SIZE_CHANGED);
}
};
ns.HistoryService.prototype.getPiskelSize_ = function () {
return this.piskelController.getWidth() + 'x' + this.piskelController.getHeight();
2014-04-17 03:27:49 +04:00
};
ns.HistoryService.prototype.setupState = function (state) {
this.piskelController.setCurrentFrameIndex(state.frameIndex);
this.piskelController.setCurrentLayerIndex(state.layerIndex);
};
2014-04-17 03:27:49 +04:00
ns.HistoryService.prototype.replayState = function (state) {
var action = state.action;
var type = action.type;
var layer = this.piskelController.getLayerAt(state.layerIndex);
var frame = layer.getFrameAt(state.frameIndex);
action.scope.replay(frame, action.replay);
};
})();