mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Turn ToolSelector into a prototype class (was closure based)
This commit is contained in:
parent
9a6ad78f63
commit
dbae35c5a5
@ -5,26 +5,32 @@
|
|||||||
* @require Events
|
* @require Events
|
||||||
* @require pskl.drawingtools
|
* @require pskl.drawingtools
|
||||||
*/
|
*/
|
||||||
$.namespace("pskl");
|
(function () {
|
||||||
|
var ns = $.namespace("pskl.controller");
|
||||||
|
|
||||||
pskl.ToolSelector = (function() {
|
|
||||||
|
|
||||||
var toolInstances = {
|
ns.ToolController = function () {
|
||||||
"simplePen" : new pskl.drawingtools.SimplePen(),
|
|
||||||
"eraser" : new pskl.drawingtools.Eraser(),
|
this.toolInstances = {
|
||||||
"paintBucket" : new pskl.drawingtools.PaintBucket(),
|
"simplePen" : new pskl.drawingtools.SimplePen(),
|
||||||
"stroke" : new pskl.drawingtools.Stroke(),
|
"eraser" : new pskl.drawingtools.Eraser(),
|
||||||
"rectangle" : new pskl.drawingtools.Rectangle(),
|
"paintBucket" : new pskl.drawingtools.PaintBucket(),
|
||||||
"circle" : new pskl.drawingtools.Circle(),
|
"stroke" : new pskl.drawingtools.Stroke(),
|
||||||
"move" : new pskl.drawingtools.Move(),
|
"rectangle" : new pskl.drawingtools.Rectangle(),
|
||||||
"rectangleSelect" : new pskl.drawingtools.RectangleSelect(),
|
"circle" : new pskl.drawingtools.Circle(),
|
||||||
"shapeSelect" : new pskl.drawingtools.ShapeSelect()
|
"move" : new pskl.drawingtools.Move(),
|
||||||
|
"rectangleSelect" : new pskl.drawingtools.RectangleSelect(),
|
||||||
|
"shapeSelect" : new pskl.drawingtools.ShapeSelect()
|
||||||
|
};
|
||||||
|
|
||||||
|
this.currentSelectedTool = this.toolInstances.simplePen;
|
||||||
|
this.previousSelectedTool = this.toolInstances.simplePen;
|
||||||
};
|
};
|
||||||
|
|
||||||
var currentSelectedTool = toolInstances.simplePen;
|
/**
|
||||||
var previousSelectedTool = toolInstances.simplePen;
|
* @private
|
||||||
|
*/
|
||||||
var activateToolOnStage_ = function(tool) {
|
ns.ToolController.prototype.activateToolOnStage_ = function(tool) {
|
||||||
var stage = $("body");
|
var stage = $("body");
|
||||||
var previousSelectedToolClass = stage.data("selected-tool-class");
|
var previousSelectedToolClass = stage.data("selected-tool-class");
|
||||||
if(previousSelectedToolClass) {
|
if(previousSelectedToolClass) {
|
||||||
@ -34,24 +40,27 @@ pskl.ToolSelector = (function() {
|
|||||||
stage.data("selected-tool-class", tool.toolId);
|
stage.data("selected-tool-class", tool.toolId);
|
||||||
};
|
};
|
||||||
|
|
||||||
var selectTool_ = function(tool) {
|
/**
|
||||||
console.log("Selecting Tool:" , currentSelectedTool);
|
* @private
|
||||||
currentSelectedTool = tool;
|
*/
|
||||||
activateToolOnStage_(currentSelectedTool);
|
ns.ToolController.prototype.selectTool_ = function(tool) {
|
||||||
|
console.log("Selecting Tool:" , this.currentSelectedTool);
|
||||||
|
this.currentSelectedTool = tool;
|
||||||
|
this.activateToolOnStage_(this.currentSelectedTool);
|
||||||
$.publish(Events.TOOL_SELECTED, [tool]);
|
$.publish(Events.TOOL_SELECTED, [tool]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
var onToolIconClicked_ = function(evt) {
|
ns.ToolController.prototype.onToolIconClicked_ = function(evt) {
|
||||||
var target = $(evt.target);
|
var target = $(evt.target);
|
||||||
var clickedTool = target.closest(".tool-icon");
|
var clickedTool = target.closest(".tool-icon");
|
||||||
|
|
||||||
if(clickedTool.length) {
|
if(clickedTool.length) {
|
||||||
for(var tool in toolInstances) {
|
for(var tool in this.toolInstances) {
|
||||||
if (toolInstances[tool].toolId == clickedTool.data().toolId) {
|
if (this.toolInstances[tool].toolId == clickedTool.data().toolId) {
|
||||||
selectTool_(toolInstances[tool]);
|
this.selectTool_(this.toolInstances[tool]);
|
||||||
|
|
||||||
// Show tool as selected:
|
// Show tool as selected:
|
||||||
$('#menubar .tool-icon.selected').removeClass('selected');
|
$('#menubar .tool-icon.selected').removeClass('selected');
|
||||||
@ -64,10 +73,10 @@ pskl.ToolSelector = (function() {
|
|||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
var createToolMarkup_ = function() {
|
ns.ToolController.prototype.createToolMarkup_ = function() {
|
||||||
var currentTool, toolMarkup = '';
|
var currentTool, toolMarkup = '';
|
||||||
for (var toolKey in toolInstances) {
|
for (var toolKey in this.toolInstances) {
|
||||||
currentTool = toolInstances[toolKey];
|
currentTool = this.toolInstances[toolKey];
|
||||||
toolMarkup += '<li class="tool-icon ' + currentTool.toolId + '" data-tool-id="' + currentTool.toolId +
|
toolMarkup += '<li class="tool-icon ' + currentTool.toolId + '" data-tool-id="' + currentTool.toolId +
|
||||||
'" title="' + currentTool.helpText + '"></li>';
|
'" title="' + currentTool.helpText + '"></li>';
|
||||||
}
|
}
|
||||||
@ -79,29 +88,30 @@ pskl.ToolSelector = (function() {
|
|||||||
* on the drawing canvas.
|
* on the drawing canvas.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
var isShowGridChecked_ = function() {
|
ns.ToolController.prototype.isShowGridChecked_ = function() {
|
||||||
var showGridCheckbox = $('#show-grid');
|
var showGridCheckbox = $('#show-grid');
|
||||||
var isChecked = showGridCheckbox.is(':checked');
|
var isChecked = showGridCheckbox.is(':checked');
|
||||||
return isChecked;
|
return isChecked;
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
/**
|
||||||
init: function() {
|
* @public
|
||||||
|
*/
|
||||||
|
ns.ToolController.prototype.init = function() {
|
||||||
|
|
||||||
createToolMarkup_();
|
this.createToolMarkup_();
|
||||||
|
|
||||||
// Initialize tool:
|
// Initialize tool:
|
||||||
// Set SimplePen as default selected tool:
|
// Set SimplePen as default selected tool:
|
||||||
selectTool_(toolInstances.simplePen);
|
this.selectTool_(this.toolInstances.simplePen);
|
||||||
// Activate listener on tool panel:
|
// Activate listener on tool panel:
|
||||||
$("#menubar").click(onToolIconClicked_);
|
$("#menubar").click($.proxy(this.onToolIconClicked_, this));
|
||||||
|
|
||||||
// Show/hide the grid on drawing canvas:
|
// Show/hide the grid on drawing canvas:
|
||||||
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [isShowGridChecked_()]);
|
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [this.isShowGridChecked_()]);
|
||||||
$('#show-grid').change(function(evt) {
|
$('#show-grid').change($.proxy(function(evt) {
|
||||||
var checked = isShowGridChecked_();
|
var checked = this.isShowGridChecked_();
|
||||||
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked]);
|
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked]);
|
||||||
});
|
}, this));
|
||||||
}
|
|
||||||
};
|
};
|
||||||
})();
|
})();
|
@ -144,7 +144,9 @@ $.namespace("pskl");
|
|||||||
},
|
},
|
||||||
|
|
||||||
finishInit : function () {
|
finishInit : function () {
|
||||||
pskl.ToolSelector.init();
|
var toolController = new pskl.controller.ToolController();
|
||||||
|
toolController.init();
|
||||||
|
|
||||||
pskl.Palette.init(frameSheet);
|
pskl.Palette.init(frameSheet);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user