piskel/js/ToolSelector.js

107 lines
3.4 KiB
JavaScript
Raw Normal View History

/*
* @provide pskl.ToolSelector
*
* @require Constants
* @require Events
* @require pskl.drawingtools
*/
$.namespace("pskl");
pskl.ToolSelector = (function() {
2012-09-10 22:52:09 +00:00
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(),
2012-09-14 20:20:00 +00:00
"circle" : new pskl.drawingtools.Circle(),
2012-09-11 17:59:00 +00:00
"move" : new pskl.drawingtools.Move(),
2012-09-14 18:12:21 +00:00
"rectangleSelect" : new pskl.drawingtools.RectangleSelect(),
"shapeSelect" : new pskl.drawingtools.ShapeSelect()
2012-09-10 22:52:09 +00:00
};
2012-09-15 18:25:45 +00:00
2012-09-10 22:52:09 +00:00
var currentSelectedTool = toolInstances.simplePen;
var previousSelectedTool = toolInstances.simplePen;
2012-09-10 22:52:09 +00:00
var activateToolOnStage_ = function(tool) {
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-10 22:52:09 +00:00
};
2012-09-10 22:52:09 +00:00
var selectTool_ = function(tool) {
console.log("Selecting Tool:" , currentSelectedTool);
currentSelectedTool = tool;
activateToolOnStage_(currentSelectedTool);
$.publish(Events.TOOL_SELECTED, [tool]);
};
2012-09-10 22:52:09 +00:00
/**
* @private
*/
var onToolIconClicked_ = function(evt) {
var target = $(evt.target);
var clickedTool = target.closest(".tool-icon");
2012-09-10 22:52:09 +00:00
if(clickedTool.length) {
for(var tool in toolInstances) {
if (toolInstances[tool].toolId == clickedTool.data().toolId) {
selectTool_(toolInstances[tool]);
2012-09-10 22:52:09 +00:00
// Show tool as selected:
2012-09-15 18:25:45 +00:00
$('#menubar .tool-icon.selected').removeClass('selected');
clickedTool.addClass('selected');
2012-09-10 22:52:09 +00:00
}
}
}
};
2012-09-15 18:25:45 +00:00
/**
* @private
*/
var createToolMarkup_ = function() {
var currentTool, toolMarkup = '';
for (var toolKey in toolInstances) {
currentTool = toolInstances[toolKey];
toolMarkup += '<li class="tool-icon ' + currentTool.toolId + '" data-tool-id="' + currentTool.toolId
+ '" title="' + currentTool.helpText + '"></li>';
}
$('#tools-container').html(toolMarkup);
};
2012-09-10 22:52:09 +00:00
/**
* Get state for the checkbox that control the display of the grid
* on the drawing canvas.
* @private
*/
var isShowGridChecked_ = function() {
var showGridCheckbox = $('#show-grid');
var isChecked = showGridCheckbox.is(':checked');
return isChecked;
};
2012-09-08 21:43:16 +00:00
2012-09-10 22:52:09 +00:00
return {
init: function() {
2012-09-15 18:25:45 +00:00
createToolMarkup_();
2012-09-10 22:52:09 +00:00
// Initialize tool:
// Set SimplePen as default selected tool:
selectTool_(toolInstances.simplePen);
// Activate listener on tool panel:
2012-09-15 18:25:45 +00:00
$("#menubar").click(onToolIconClicked_);
2012-09-08 21:43:16 +00:00
2012-09-10 22:52:09 +00:00
// Show/hide the grid on drawing canvas:
2012-09-10 22:54:06 +00:00
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [isShowGridChecked_()]);
2012-09-10 22:52:09 +00:00
$('#show-grid').change(function(evt) {
var checked = isShowGridChecked_();
2012-09-10 22:54:06 +00:00
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked]);
2012-09-10 22:52:09 +00:00
});
}
};
2012-09-15 18:25:45 +00:00
})();