mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
preview zoom
This commit is contained in:
34
zoom/js/selection/BaseSelection.js
Normal file
34
zoom/js/selection/BaseSelection.js
Normal file
@@ -0,0 +1,34 @@
|
||||
(function () {
|
||||
var ns = $.namespace("pskl.selection");
|
||||
|
||||
ns.BaseSelection = function () {
|
||||
this.reset();
|
||||
};
|
||||
|
||||
ns.BaseSelection.prototype.reset = function () {
|
||||
this.pixels = [];
|
||||
this.hasPastedContent = false;
|
||||
};
|
||||
|
||||
ns.BaseSelection.prototype.move = function (colDiff, rowDiff) {
|
||||
var movedPixel, movedPixels = [];
|
||||
|
||||
for(var i=0, l=this.pixels.length; i<l; i++) {
|
||||
movedPixel = this.pixels[i];
|
||||
movedPixel.col += colDiff;
|
||||
movedPixel.row += rowDiff;
|
||||
movedPixels.push(movedPixel);
|
||||
}
|
||||
this.pixels = movedPixels;
|
||||
};
|
||||
|
||||
ns.BaseSelection.prototype.fillSelectionFromFrame = function (targetFrame) {
|
||||
var pixelWithCopiedColor;
|
||||
for(var i=0, l=this.pixels.length; i<l; i++) {
|
||||
pixelWithCopiedColor = this.pixels[i];
|
||||
pixelWithCopiedColor.copiedColor =
|
||||
targetFrame.getPixel(pixelWithCopiedColor.col, pixelWithCopiedColor.row);
|
||||
}
|
||||
this.hasPastedContent = true;
|
||||
};
|
||||
})();
|
||||
9
zoom/js/selection/RectangularSelection.js
Normal file
9
zoom/js/selection/RectangularSelection.js
Normal file
@@ -0,0 +1,9 @@
|
||||
(function () {
|
||||
var ns = $.namespace("pskl.selection");
|
||||
|
||||
ns.RectangularSelection = function (x0, y0, x1, y1) {
|
||||
this.pixels = pskl.PixelUtils.getRectanglePixels(x0, y0, x1, y1);
|
||||
};
|
||||
|
||||
pskl.utils.inherit(ns.RectangularSelection, ns.BaseSelection);
|
||||
})();
|
||||
124
zoom/js/selection/SelectionManager.js
Normal file
124
zoom/js/selection/SelectionManager.js
Normal file
@@ -0,0 +1,124 @@
|
||||
(function () {
|
||||
var ns = $.namespace("pskl.selection");
|
||||
|
||||
ns.SelectionManager = function (piskelController) {
|
||||
|
||||
this.piskelController = piskelController;
|
||||
|
||||
this.currentSelection = null;
|
||||
};
|
||||
|
||||
ns.SelectionManager.prototype.init = function () {
|
||||
$.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, $.proxy(this.onToolSelected_, this));
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.SelectionManager.prototype.cleanSelection_ = function() {
|
||||
if(this.currentSelection) {
|
||||
this.currentSelection.reset();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.SelectionManager.prototype.onToolSelected_ = function(evt, tool) {
|
||||
var isSelectionTool = tool instanceof pskl.drawingtools.BaseSelect;
|
||||
if(!isSelectionTool) {
|
||||
this.cleanSelection_();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.SelectionManager.prototype.onSelectionDismissed_ = function(evt) {
|
||||
this.cleanSelection_();
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.SelectionManager.prototype.onCut_ = function(evt) {
|
||||
if(this.currentSelection) {
|
||||
// Put cut target into the selection:
|
||||
this.currentSelection.fillSelectionFromFrame(this.piskelController.getCurrentFrame());
|
||||
|
||||
var pixels = this.currentSelection.pixels;
|
||||
var currentFrame = this.piskelController.getCurrentFrame();
|
||||
for(var i=0, l=pixels.length; i<l; i++) {
|
||||
try {
|
||||
currentFrame.setPixel(pixels[i].col, pixels[i].row, Constants.TRANSPARENT_COLOR);
|
||||
}
|
||||
catch(e) {
|
||||
// Catchng out of frame's bound pixels without testing
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw "Bad state for CUT callback in SelectionManager";
|
||||
}
|
||||
};
|
||||
|
||||
ns.SelectionManager.prototype.onPaste_ = function(evt) {
|
||||
if(this.currentSelection && this.currentSelection.hasPastedContent) {
|
||||
var pixels = this.currentSelection.pixels;
|
||||
var currentFrame = this.piskelController.getCurrentFrame();
|
||||
for(var i=0, l=pixels.length; i<l; i++) {
|
||||
try {
|
||||
currentFrame.setPixel(
|
||||
pixels[i].col, pixels[i].row,
|
||||
pixels[i].copiedColor);
|
||||
}
|
||||
catch(e) {
|
||||
// Catchng out of frame's bound pixels without testing
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.SelectionManager.prototype.onCopy_ = function(evt) {
|
||||
if(this.currentSelection && this.piskelController.getCurrentFrame()) {
|
||||
this.currentSelection.fillSelectionFromFrame(this.piskelController.getCurrentFrame());
|
||||
}
|
||||
else {
|
||||
throw "Bad state for CUT callback in SelectionManager";
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ns.SelectionManager.prototype.onSelectionCreated_ = function(evt, selection) {
|
||||
if(selection) {
|
||||
this.currentSelection = selection;
|
||||
} else {
|
||||
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";
|
||||
}
|
||||
};
|
||||
})();
|
||||
9
zoom/js/selection/ShapeSelection.js
Normal file
9
zoom/js/selection/ShapeSelection.js
Normal file
@@ -0,0 +1,9 @@
|
||||
(function () {
|
||||
var ns = $.namespace("pskl.selection");
|
||||
|
||||
ns.ShapeSelection = function (pixels) {
|
||||
this.pixels = pixels;
|
||||
};
|
||||
|
||||
pskl.utils.inherit(ns.ShapeSelection, ns.BaseSelection);
|
||||
})();
|
||||
Reference in New Issue
Block a user