piskel/js/controller/ToolController.js

117 lines
4.1 KiB
JavaScript
Raw Normal View History

(function () {
var ns = $.namespace("pskl.controller");
ns.ToolController = function () {
this.toolInstances = {
"simplePen" : new pskl.drawingtools.SimplePen(),
2012-09-16 02:52:39 +04:00
"verticalMirrorPen" : new pskl.drawingtools.VerticalMirrorPen(),
"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(),
"colorPicker" : new pskl.drawingtools.ColorPicker()
};
2012-09-15 22:25:45 +04:00
this.currentSelectedTool = this.toolInstances.simplePen;
this.previousSelectedTool = this.toolInstances.simplePen;
};
/**
* @private
*/
ns.ToolController.prototype.activateToolOnStage_ = function(tool) {
2012-09-11 02:52:09 +04:00
var stage = $("body");
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-11 02:52:09 +04:00
};
/**
* @private
*/
ns.ToolController.prototype.selectTool_ = function(tool) {
console.log("Selecting Tool:" , this.currentSelectedTool);
this.currentSelectedTool = tool;
this.activateToolOnStage_(this.currentSelectedTool);
2012-09-11 02:52:09 +04:00
$.publish(Events.TOOL_SELECTED, [tool]);
};
2012-09-11 02:52:09 +04:00
/**
* @private
*/
ns.ToolController.prototype.onToolIconClicked_ = function(evt) {
2012-09-11 02:52:09 +04:00
var target = $(evt.target);
var clickedTool = target.closest(".tool-icon");
2012-09-11 02:52:09 +04:00
if(clickedTool.length) {
for(var tool in this.toolInstances) {
if (this.toolInstances[tool].toolId == clickedTool.data().toolId) {
this.selectTool_(this.toolInstances[tool]);
2012-09-11 02:52:09 +04:00
// Show tool as selected:
2012-09-15 22:25:45 +04:00
$('#menubar .tool-icon.selected').removeClass('selected');
clickedTool.addClass('selected');
2012-09-11 02:52:09 +04:00
}
}
}
};
2012-09-15 22:25:45 +04:00
/**
* @private
*/
ns.ToolController.prototype.createToolMarkup_ = function() {
2013-06-13 15:57:52 +04:00
var currentTool, toolMarkup = '', extraClass;
// TODO(vincz): Tools rendering order is not enforced by the data stucture (this.toolInstances), fix that.
for (var toolKey in this.toolInstances) {
currentTool = this.toolInstances[toolKey];
2013-06-13 15:57:52 +04: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 22:30:32 +04:00
'" title="' + currentTool.helpText + '"></li>';
2012-09-15 22:25:45 +04:00
}
$('#tools-container').html(toolMarkup);
};
2012-09-11 02:52:09 +04:00
/**
* Get state for the checkbox that control the display of the grid
* on the drawing canvas.
* @private
*/
ns.ToolController.prototype.isShowGridChecked_ = function() {
2012-09-11 02:52:09 +04:00
var showGridCheckbox = $('#show-grid');
var isChecked = showGridCheckbox.is(':checked');
return isChecked;
};
2012-09-09 01:43:16 +04:00
/**
* @public
*/
ns.ToolController.prototype.init = function() {
this.createToolMarkup_();
2012-09-15 22:25:45 +04:00
// Initialize tool:
// Set SimplePen as default selected tool:
this.selectTool_(this.toolInstances.simplePen);
// Activate listener on tool panel:
$("#menubar").click($.proxy(this.onToolIconClicked_, this));
2012-09-09 01:43:16 +04:00
// 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));
2012-09-11 02:52:09 +04:00
};
2012-09-15 22:25:45 +04:00
})();