2012-09-15 22:48:01 +04:00
|
|
|
(function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.controller");
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2013-11-18 01:07:26 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.ToolController = function () {
|
2013-11-18 01:07:26 +04:00
|
|
|
var toDescriptor = function (id, shortcut, instance) {
|
|
|
|
return {id:id, shortcut:shortcut, instance:instance};
|
2012-09-15 22:48:01 +04:00
|
|
|
};
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2013-11-18 01:07:26 +04:00
|
|
|
this.tools = [
|
|
|
|
toDescriptor('simplePen', 'P', new pskl.drawingtools.SimplePen()),
|
|
|
|
toDescriptor('verticalMirrorPen', 'V', new pskl.drawingtools.VerticalMirrorPen()),
|
|
|
|
toDescriptor('eraser', 'E', new pskl.drawingtools.Eraser()),
|
|
|
|
toDescriptor('paintBucket', 'B', new pskl.drawingtools.PaintBucket()),
|
|
|
|
toDescriptor('stroke', 'L', new pskl.drawingtools.Stroke()),
|
|
|
|
toDescriptor('rectangle', 'R', new pskl.drawingtools.Rectangle()),
|
|
|
|
toDescriptor('circle', 'C', new pskl.drawingtools.Circle()),
|
|
|
|
toDescriptor('move', 'M', new pskl.drawingtools.Move()),
|
|
|
|
toDescriptor('rectangleSelect', 'S', new pskl.drawingtools.RectangleSelect()),
|
|
|
|
toDescriptor('shapeSelect', 'Z', new pskl.drawingtools.ShapeSelect()),
|
|
|
|
toDescriptor('colorPicker', 'O', new pskl.drawingtools.ColorPicker())
|
|
|
|
];
|
|
|
|
|
|
|
|
this.currentSelectedTool = this.tools[0];
|
|
|
|
this.previousSelectedTool = this.tools[0];
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2013-08-06 01:34:11 +04:00
|
|
|
|
2013-08-10 16:28:10 +04:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.init = function() {
|
2013-11-20 02:46:33 +04:00
|
|
|
this.createToolsDom_();
|
|
|
|
this.addKeyboardShortcuts_();
|
2013-08-06 01:34:11 +04:00
|
|
|
|
2013-08-10 16:28:10 +04:00
|
|
|
// Initialize tool:
|
|
|
|
// Set SimplePen as default selected tool:
|
2013-11-18 01:07:26 +04:00
|
|
|
this.selectTool_(this.tools[0]);
|
2013-08-10 16:28:10 +04:00
|
|
|
// Activate listener on tool panel:
|
|
|
|
$("#tool-section").click($.proxy(this.onToolIconClicked_, this));
|
|
|
|
};
|
2013-08-06 01:34:11 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.activateToolOnStage_ = function(tool) {
|
|
|
|
var stage = $("body");
|
|
|
|
var previousSelectedToolClass = stage.data("selected-tool-class");
|
|
|
|
if(previousSelectedToolClass) {
|
|
|
|
stage.removeClass(previousSelectedToolClass);
|
|
|
|
}
|
2013-11-18 01:07:26 +04:00
|
|
|
stage.addClass(tool.instance.toolId);
|
|
|
|
stage.data("selected-tool-class", tool.instance.toolId);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.selectTool_ = function(tool) {
|
|
|
|
this.currentSelectedTool = tool;
|
|
|
|
this.activateToolOnStage_(this.currentSelectedTool);
|
2013-11-18 01:47:21 +04:00
|
|
|
|
|
|
|
var selectedToolElement = $('#tool-section .tool-icon.selected');
|
|
|
|
var toolElement = $('[data-tool-id=' + tool.instance.toolId + ']');
|
|
|
|
|
|
|
|
selectedToolElement.removeClass('selected');
|
|
|
|
toolElement.addClass('selected');
|
|
|
|
|
2013-11-18 01:07:26 +04:00
|
|
|
$.publish(Events.TOOL_SELECTED, [tool.instance]);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.onToolIconClicked_ = function(evt) {
|
|
|
|
var target = $(evt.target);
|
|
|
|
var clickedTool = target.closest(".tool-icon");
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if(clickedTool.length) {
|
2013-08-10 16:28:10 +04:00
|
|
|
var toolId = clickedTool.data().toolId;
|
|
|
|
var tool = this.getToolById_(toolId);
|
|
|
|
if (tool) {
|
|
|
|
this.selectTool_(tool);
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
ns.ToolController.prototype.onKeyboardShortcut_ = function(charkey) {
|
2013-11-18 01:07:26 +04:00
|
|
|
for (var i = 0 ; i < this.tools.length ; i++) {
|
|
|
|
var tool = this.tools[i];
|
|
|
|
if (tool.shortcut.toLowerCase() === charkey.toLowerCase()) {
|
|
|
|
this.selectTool_(tool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-10 16:28:10 +04:00
|
|
|
ns.ToolController.prototype.getToolById_ = function (toolId) {
|
2013-11-18 01:07:26 +04:00
|
|
|
for(var i = 0 ; i < this.tools.length ; i++) {
|
|
|
|
var tool = this.tools[i];
|
|
|
|
if (tool.instance.toolId == toolId) {
|
|
|
|
return tool;
|
2013-08-10 16:28:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2013-11-20 02:46:33 +04:00
|
|
|
ns.ToolController.prototype.createToolsDom_ = function() {
|
|
|
|
var toolMarkup = '';
|
2013-11-18 01:07:26 +04:00
|
|
|
for(var i = 0 ; i < this.tools.length ; i++) {
|
2013-11-20 02:46:33 +04:00
|
|
|
toolMarkup += this.getToolMarkup_(this.tools[i]);
|
|
|
|
}
|
|
|
|
$('#tools-container').html(toolMarkup);
|
|
|
|
};
|
2013-11-18 01:07:26 +04:00
|
|
|
|
2013-11-20 02:46:33 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.ToolController.prototype.getToolMarkup_ = function(tool) {
|
|
|
|
var instance = tool.instance;
|
|
|
|
|
|
|
|
var classList = ['tool-icon', instance.toolId];
|
|
|
|
if (this.currentSelectedTool == tool) {
|
|
|
|
classList.push('selected');
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<li rel="tooltip" data-placement="right" class="' + classList.join(' ') + '" data-tool-id="' + instance.toolId +
|
2013-11-18 01:07:26 +04:00
|
|
|
'" title="' + instance.helpText + '"></li>';
|
2013-11-20 02:46:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.ToolController.prototype.addKeyboardShortcuts_ = function () {
|
|
|
|
for(var i = 0 ; i < this.tools.length ; i++) {
|
|
|
|
pskl.app.shortcutService.addShortcut(this.tools[i].shortcut, this.onKeyboardShortcut_.bind(this));
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
};
|
2012-09-15 22:25:45 +04:00
|
|
|
})();
|