2012-09-15 18:48:01 +00:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.controller");
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-15 18:48:01 +00:00
|
|
|
|
|
|
|
ns.ToolController = function () {
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-15 18:48:01 +00:00
|
|
|
this.toolInstances = {
|
|
|
|
"simplePen" : new pskl.drawingtools.SimplePen(),
|
2012-09-15 22:52:39 +00:00
|
|
|
"verticalMirrorPen" : new pskl.drawingtools.VerticalMirrorPen(),
|
2012-09-15 18:48:01 +00:00
|
|
|
"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(),
|
2013-04-08 23:32:30 +00:00
|
|
|
"shapeSelect" : new pskl.drawingtools.ShapeSelect(),
|
|
|
|
"colorPicker" : new pskl.drawingtools.ColorPicker()
|
2012-09-15 18:48:01 +00:00
|
|
|
};
|
2012-09-15 18:25:45 +00:00
|
|
|
|
2012-09-15 18:48:01 +00:00
|
|
|
this.currentSelectedTool = this.toolInstances.simplePen;
|
|
|
|
this.previousSelectedTool = this.toolInstances.simplePen;
|
|
|
|
};
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-15 18:48:01 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.activateToolOnStage_ = function(tool) {
|
2012-09-10 22:52:09 +00:00
|
|
|
var stage = $("body");
|
2012-08-31 08:45:07 +00:00
|
|
|
var previousSelectedToolClass = stage.data("selected-tool-class");
|
|
|
|
if(previousSelectedToolClass) {
|
|
|
|
stage.removeClass(previousSelectedToolClass);
|
|
|
|
}
|
|
|
|
stage.addClass(tool.toolId);
|
|
|
|
stage.data("selected-tool-class", tool.toolId);
|
2012-09-10 22:52:09 +00:00
|
|
|
};
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-15 18:48:01 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.selectTool_ = function(tool) {
|
|
|
|
console.log("Selecting Tool:" , this.currentSelectedTool);
|
|
|
|
this.currentSelectedTool = tool;
|
|
|
|
this.activateToolOnStage_(this.currentSelectedTool);
|
2012-09-10 22:52:09 +00:00
|
|
|
$.publish(Events.TOOL_SELECTED, [tool]);
|
|
|
|
};
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-10 22:52:09 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-15 18:48:01 +00:00
|
|
|
ns.ToolController.prototype.onToolIconClicked_ = function(evt) {
|
2012-09-10 22:52:09 +00:00
|
|
|
var target = $(evt.target);
|
|
|
|
var clickedTool = target.closest(".tool-icon");
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-10 22:52:09 +00:00
|
|
|
if(clickedTool.length) {
|
2012-09-15 18:48:01 +00:00
|
|
|
for(var tool in this.toolInstances) {
|
|
|
|
if (this.toolInstances[tool].toolId == clickedTool.data().toolId) {
|
|
|
|
this.selectTool_(this.toolInstances[tool]);
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-10 22:52:09 +00:00
|
|
|
// Show tool as selected:
|
2013-06-15 17:04:36 +00:00
|
|
|
$('#tool-section .tool-icon.selected').removeClass('selected');
|
2012-09-15 18:25:45 +00:00
|
|
|
clickedTool.addClass('selected');
|
2012-09-10 22:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-08-31 08:45:07 +00:00
|
|
|
|
2012-09-15 18:25:45 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-15 18:48:01 +00:00
|
|
|
ns.ToolController.prototype.createToolMarkup_ = function() {
|
2013-06-13 11:57:52 +00:00
|
|
|
var currentTool, toolMarkup = '', extraClass;
|
2012-09-15 22:48:53 +00:00
|
|
|
// TODO(vincz): Tools rendering order is not enforced by the data stucture (this.toolInstances), fix that.
|
2012-09-15 18:48:01 +00:00
|
|
|
for (var toolKey in this.toolInstances) {
|
|
|
|
currentTool = this.toolInstances[toolKey];
|
2013-06-13 11:57:52 +00:00
|
|
|
extraClass = currentTool.toolId;
|
|
|
|
if (this.currentSelectedTool == currentTool) {
|
|
|
|
extraClass = extraClass + " selected";
|
|
|
|
}
|
|
|
|
toolMarkup += '<li rel="tooltip" data-placement="right" class="tool-icon ' + extraClass + '" data-tool-id="' + currentTool.toolId +
|
2012-09-15 18:30:32 +00:00
|
|
|
'" title="' + currentTool.helpText + '"></li>';
|
2012-09-15 18:25:45 +00:00
|
|
|
}
|
|
|
|
$('#tools-container').html(toolMarkup);
|
|
|
|
};
|
|
|
|
|
2012-09-15 18:48:01 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.init = function() {
|
|
|
|
|
|
|
|
this.createToolMarkup_();
|
2012-09-15 18:25:45 +00:00
|
|
|
|
2012-09-15 18:48:01 +00:00
|
|
|
// Initialize tool:
|
|
|
|
// Set SimplePen as default selected tool:
|
|
|
|
this.selectTool_(this.toolInstances.simplePen);
|
|
|
|
// Activate listener on tool panel:
|
2013-06-15 17:04:36 +00:00
|
|
|
$("#tool-section").click($.proxy(this.onToolIconClicked_, this));
|
2012-09-10 22:52:09 +00:00
|
|
|
};
|
2012-09-15 18:25:45 +00:00
|
|
|
})();
|