2012-09-12 14:01:47 +04:00
|
|
|
(function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.selection");
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.SelectionManager = function (piskelController) {
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController = piskelController;
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.currentSelection = null;
|
2013-08-10 16:28:10 +04:00
|
|
|
};
|
2013-08-06 01:34:11 +04:00
|
|
|
|
2013-08-10 16:28:10 +04:00
|
|
|
ns.SelectionManager.prototype.init = function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
$.subscribe(Events.SELECTION_CREATED, $.proxy(this.onSelectionCreated_, this));
|
2013-09-29 01:52:51 +04:00
|
|
|
$.subscribe(Events.SELECTION_DISMISSED, $.proxy(this.onSelectionDismissed_, this));
|
2013-08-10 14:11:16 +04:00
|
|
|
$.subscribe(Events.SELECTION_MOVE_REQUEST, $.proxy(this.onSelectionMoved_, this));
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
pskl.app.shortcutService.addShortcut('ctrl+V', this.paste.bind(this));
|
|
|
|
pskl.app.shortcutService.addShortcut('ctrl+X', this.cut.bind(this));
|
|
|
|
pskl.app.shortcutService.addShortcut('ctrl+C', this.copy.bind(this));
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2013-09-29 01:52:51 +04:00
|
|
|
$.subscribe(Events.TOOL_SELECTED, $.proxy(this.onToolSelected_, this));
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-10 16:28:10 +04:00
|
|
|
ns.SelectionManager.prototype.cleanSelection_ = function() {
|
2013-08-10 14:11:16 +04:00
|
|
|
if(this.currentSelection) {
|
|
|
|
this.currentSelection.reset();
|
|
|
|
}
|
|
|
|
};
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.SelectionManager.prototype.onToolSelected_ = function(evt, tool) {
|
|
|
|
var isSelectionTool = tool instanceof pskl.drawingtools.BaseSelect;
|
|
|
|
if(!isSelectionTool) {
|
|
|
|
this.cleanSelection_();
|
|
|
|
}
|
|
|
|
};
|
2012-09-14 00:57:32 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.SelectionManager.prototype.onSelectionDismissed_ = function(evt) {
|
|
|
|
this.cleanSelection_();
|
|
|
|
};
|
2012-09-14 00:57:32 +04:00
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
ns.SelectionManager.prototype.cut = function() {
|
2013-08-10 14:11:16 +04:00
|
|
|
if(this.currentSelection) {
|
|
|
|
// Put cut target into the selection:
|
2013-09-22 23:02:43 +04:00
|
|
|
this.currentSelection.fillSelectionFromFrame(this.piskelController.getCurrentFrame());
|
2012-09-14 00:57:32 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var pixels = this.currentSelection.pixels;
|
2013-09-22 23:02:43 +04:00
|
|
|
var currentFrame = this.piskelController.getCurrentFrame();
|
2013-08-10 14:11:16 +04:00
|
|
|
for(var i=0, l=pixels.length; i<l; i++) {
|
|
|
|
try {
|
|
|
|
currentFrame.setPixel(pixels[i].col, pixels[i].row, Constants.TRANSPARENT_COLOR);
|
2013-11-20 02:46:33 +04:00
|
|
|
} catch(e) {
|
|
|
|
// Catching out of frame's bound pixels without testing
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw "Bad state for CUT callback in SelectionManager";
|
|
|
|
}
|
|
|
|
};
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
ns.SelectionManager.prototype.paste = function() {
|
2013-08-10 14:11:16 +04:00
|
|
|
if(this.currentSelection && this.currentSelection.hasPastedContent) {
|
|
|
|
var pixels = this.currentSelection.pixels;
|
2013-09-22 23:02:43 +04:00
|
|
|
var currentFrame = this.piskelController.getCurrentFrame();
|
2013-08-10 14:11:16 +04:00
|
|
|
for(var i=0, l=pixels.length; i<l; i++) {
|
|
|
|
try {
|
|
|
|
currentFrame.setPixel(
|
2013-09-29 01:52:51 +04:00
|
|
|
pixels[i].col, pixels[i].row,
|
2013-08-10 14:11:16 +04:00
|
|
|
pixels[i].copiedColor);
|
2013-11-20 02:46:33 +04:00
|
|
|
} catch(e) {
|
|
|
|
// Catching out of frame's bound pixels without testing
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
ns.SelectionManager.prototype.copy = function() {
|
2013-09-22 23:02:43 +04:00
|
|
|
if(this.currentSelection && this.piskelController.getCurrentFrame()) {
|
|
|
|
this.currentSelection.fillSelectionFromFrame(this.piskelController.getCurrentFrame());
|
2013-11-20 02:46:33 +04:00
|
|
|
} else {
|
2013-08-10 14:11:16 +04:00
|
|
|
throw "Bad state for CUT callback in SelectionManager";
|
|
|
|
}
|
|
|
|
};
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.SelectionManager.prototype.onSelectionCreated_ = function(evt, selection) {
|
|
|
|
if(selection) {
|
|
|
|
this.currentSelection = selection;
|
2013-08-10 16:28:10 +04:00
|
|
|
} else {
|
2013-08-10 14:11:16 +04:00
|
|
|
throw "No selection set in SelectionManager";
|
|
|
|
}
|
|
|
|
};
|
2012-09-14 00:57:32 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.SelectionManager.prototype.onSelectionMoved_ = function(evt, colDiff, rowDiff) {
|
|
|
|
if(this.currentSelection) {
|
|
|
|
this.currentSelection.move(colDiff, rowDiff);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw "Bad state: No currentSelection set when trying to move it in SelectionManager";
|
|
|
|
}
|
|
|
|
};
|
2012-09-12 14:01:47 +04:00
|
|
|
})();
|