Basic rectangular select tool basic

This commit is contained in:
Vince
2012-09-13 22:57:32 +02:00
parent 2aaab74b80
commit e85fe32f02
13 changed files with 197 additions and 54 deletions

View File

@ -8,33 +8,28 @@
this.currentSelection = null;
this.currentFrame = null;
$.subscribe(Events.SELECTION_CREATED, $.proxy(this.onSelectionCreated_, this));
$.subscribe(Events.CURRENT_FRAME_SET, $.proxy(this.onCurrentFrameChanged_, this));
//$.subscribe(Events.PASTE, $.proxy(this.onPaste_, this));
$.subscribe(Events.COPY, $.proxy(this.onCopy_, this)););
$.subscribe(Events.SELECTION_CREATED, $.proxy(this.onSelectionCreated_, this));
$.subscribe(Events.SELECTION_DISMISSED, $.proxy(this.onSelectionDismissed_, this));
$.subscribe(Events.SELECTION_MOVE_REQUEST, $.proxy(this.onSelectionMoved_, this));
$.subscribe(Events.PASTE, $.proxy(this.onPaste_, this));
$.subscribe(Events.COPY, $.proxy(this.onCopy_, this));
$.subscribe(Events.CUT, $.proxy(this.onCut_, this));
$.subscribe(Events.TOOL_SELECTED); // discard selection if not move
$.subscribe(Events.TOOL_SELECTED, $.proxy(this.onToolSelected_, this));
};
ns.SelectionManager.prototype.hasSelection = function() {
return (this.currentSelection == null) ? false : true;
};
ns.SelectionManager.prototype.addSelection = function(selection) {
this.currentSelection = selection;
};
ns.SelectionManager.prototype.removeSelection = function(selection) {
this.currentSelection = null;
/**
* @private
*/
ns.SelectionManager.prototype.cleanSelection_ = function(selection) {
if(this.currentSelection) {
this.currentSelection.reset();
}
this.overlayFrame.clear();
};
/**
@ -49,14 +44,38 @@
}
};
/**
* @private
*/
ns.SelectionManager.prototype.onToolSelected_ = function(evt, tool) {
if(!(tool instanceof pskl.drawingtools.Select)) {
this.cleanSelection_();
}
};
/**
* @private
*/
ns.SelectionManager.prototype.onSelectionDismissed_ = function(evt) {
this.cleanSelection_();
};
/**
* @private
*/
ns.SelectionManager.prototype.onCut_ = function(evt) {
if(this.currentSelection && this.currentFrame) {
// Put cut target into the selection:
this.currentSelection.fillSelectionFromFrame(this.currentFrame);
var pixels = this.currentSelection.pixels;
for(var i=0, l=pixels.length; i<l; i++) {
this.currentFrame.setPixel(pixels[i].col, pixels[i].row, Constants.TRANSPARENT_COLOR);
try {
this.currentFrame.setPixel(pixels[i].col, pixels[i].row, Constants.TRANSPARENT_COLOR);
}
catch(e) {
// Catchng out of frame's bound pixels without testing
}
}
}
else {
@ -68,11 +87,18 @@
if(this.currentSelection && this.currentFrame) {
var pixels = this.currentSelection.pixels;
for(var i=0, l=pixels.length; i<l; i++) {
this.currentFrame.setPixel(pixels[i].col, pixels[i].row, Constants.TRANSPARENT_COLOR);
try {
this.currentFrame.setPixel(
pixels[i].col, pixels[i].row,
pixels[i].copiedColor);
}
catch(e) {
// Catchng out of frame's bound pixels without testing
}
}
}
else {
throw "Bad state for CUT callback in SelectionManager";
throw "Bad state for PASTE callback in SelectionManager";
}
};
@ -80,16 +106,8 @@
* @private
*/
ns.SelectionManager.prototype.onCopy_ = function(evt) {
this.copiedPixels = [];
if(this.currentSelection && this.currentFrame) {
var pixels = this.currentSelection.pixels;
for(var i=0, l=pixels.length; i<l; i++) {
copiedPixels.push({
"col": pixels[i].col,
"row": pixels[i].row,
"color": this.currentFrame.getPixel(pixels[i].col, pixels[i].row)
});
}
this.currentSelection.fillSelectionFromFrame(this.currentFrame);
}
else {
throw "Bad state for CUT callback in SelectionManager";
@ -108,7 +126,19 @@
}
}
else {
throw "Bad current selection set in SelectionManager";
throw "No selection set in SelectionManager";
}
};
/**
* @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";
}
};
})();