From dbae35c5a5801b1806d3aae20b49de1ccfb66025 Mon Sep 17 00:00:00 2001 From: Vince Date: Sat, 15 Sep 2012 20:48:01 +0200 Subject: [PATCH] Turn ToolSelector into a prototype class (was closure based) --- js/ToolSelector.js | 98 +++++++++++++++++++++++++--------------------- js/piskel.js | 4 +- 2 files changed, 57 insertions(+), 45 deletions(-) diff --git a/js/ToolSelector.js b/js/ToolSelector.js index 7fa8384..cb6eb9f 100644 --- a/js/ToolSelector.js +++ b/js/ToolSelector.js @@ -5,26 +5,32 @@ * @require Events * @require pskl.drawingtools */ -$.namespace("pskl"); +(function () { + var ns = $.namespace("pskl.controller"); -pskl.ToolSelector = (function() { + + ns.ToolController = function () { - var toolInstances = { - "simplePen" : new pskl.drawingtools.SimplePen(), - "eraser" : new pskl.drawingtools.Eraser(), - "paintBucket" : new pskl.drawingtools.PaintBucket(), - "stroke" : new pskl.drawingtools.Stroke(), - "rectangle" : new pskl.drawingtools.Rectangle(), - "circle" : new pskl.drawingtools.Circle(), - "move" : new pskl.drawingtools.Move(), - "rectangleSelect" : new pskl.drawingtools.RectangleSelect(), - "shapeSelect" : new pskl.drawingtools.ShapeSelect() + this.toolInstances = { + "simplePen" : new pskl.drawingtools.SimplePen(), + "eraser" : new pskl.drawingtools.Eraser(), + "paintBucket" : new pskl.drawingtools.PaintBucket(), + "stroke" : new pskl.drawingtools.Stroke(), + "rectangle" : new pskl.drawingtools.Rectangle(), + "circle" : new pskl.drawingtools.Circle(), + "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; - - var activateToolOnStage_ = function(tool) { + /** + * @private + */ + ns.ToolController.prototype.activateToolOnStage_ = function(tool) { var stage = $("body"); var previousSelectedToolClass = stage.data("selected-tool-class"); if(previousSelectedToolClass) { @@ -34,24 +40,27 @@ pskl.ToolSelector = (function() { stage.data("selected-tool-class", tool.toolId); }; - var selectTool_ = function(tool) { - console.log("Selecting Tool:" , currentSelectedTool); - currentSelectedTool = tool; - activateToolOnStage_(currentSelectedTool); + /** + * @private + */ + ns.ToolController.prototype.selectTool_ = function(tool) { + console.log("Selecting Tool:" , this.currentSelectedTool); + this.currentSelectedTool = tool; + this.activateToolOnStage_(this.currentSelectedTool); $.publish(Events.TOOL_SELECTED, [tool]); }; /** * @private */ - var onToolIconClicked_ = function(evt) { + ns.ToolController.prototype.onToolIconClicked_ = function(evt) { var target = $(evt.target); var clickedTool = target.closest(".tool-icon"); if(clickedTool.length) { - for(var tool in toolInstances) { - if (toolInstances[tool].toolId == clickedTool.data().toolId) { - selectTool_(toolInstances[tool]); + for(var tool in this.toolInstances) { + if (this.toolInstances[tool].toolId == clickedTool.data().toolId) { + this.selectTool_(this.toolInstances[tool]); // Show tool as selected: $('#menubar .tool-icon.selected').removeClass('selected'); @@ -64,10 +73,10 @@ pskl.ToolSelector = (function() { /** * @private */ - var createToolMarkup_ = function() { + ns.ToolController.prototype.createToolMarkup_ = function() { var currentTool, toolMarkup = ''; - for (var toolKey in toolInstances) { - currentTool = toolInstances[toolKey]; + for (var toolKey in this.toolInstances) { + currentTool = this.toolInstances[toolKey]; toolMarkup += '
  • '; } @@ -79,29 +88,30 @@ pskl.ToolSelector = (function() { * on the drawing canvas. * @private */ - var isShowGridChecked_ = function() { + ns.ToolController.prototype.isShowGridChecked_ = function() { var showGridCheckbox = $('#show-grid'); var isChecked = showGridCheckbox.is(':checked'); return isChecked; }; - return { - init: function() { - - createToolMarkup_(); + /** + * @public + */ + ns.ToolController.prototype.init = function() { - // Initialize tool: - // Set SimplePen as default selected tool: - selectTool_(toolInstances.simplePen); - // Activate listener on tool panel: - $("#menubar").click(onToolIconClicked_); + this.createToolMarkup_(); - // Show/hide the grid on drawing canvas: - $.publish(Events.GRID_DISPLAY_STATE_CHANGED, [isShowGridChecked_()]); - $('#show-grid').change(function(evt) { - var checked = isShowGridChecked_(); - $.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked]); - }); - } + // Initialize tool: + // Set SimplePen as default selected tool: + this.selectTool_(this.toolInstances.simplePen); + // Activate listener on tool panel: + $("#menubar").click($.proxy(this.onToolIconClicked_, this)); + + // Show/hide the grid on drawing canvas: + $.publish(Events.GRID_DISPLAY_STATE_CHANGED, [this.isShowGridChecked_()]); + $('#show-grid').change($.proxy(function(evt) { + var checked = this.isShowGridChecked_(); + $.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked]); + }, this)); }; })(); \ No newline at end of file diff --git a/js/piskel.js b/js/piskel.js index ba12f0e..06fa644 100644 --- a/js/piskel.js +++ b/js/piskel.js @@ -144,7 +144,9 @@ $.namespace("pskl"); }, finishInit : function () { - pskl.ToolSelector.init(); + var toolController = new pskl.controller.ToolController(); + toolController.init(); + pskl.Palette.init(frameSheet); },