piskel/js/ToolSelector.js

95 lines
2.9 KiB
JavaScript
Raw Normal View History

/*
* @provide pskl.ToolSelector
*
* @require Constants
* @require Events
* @require pskl.drawingtools
*/
$.namespace("pskl");
pskl.ToolSelector = (function() {
2012-09-11 02:52:09 +04: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-15 00:20:00 +04:00
"circle" : new pskl.drawingtools.Circle(),
2012-09-11 21:59:00 +04:00
"move" : new pskl.drawingtools.Move(),
2012-09-14 22:12:21 +04:00
"rectangleSelect" : new pskl.drawingtools.RectangleSelect(),
"shapeSelect" : new pskl.drawingtools.ShapeSelect()
2012-09-11 02:52:09 +04:00
};
var currentSelectedTool = toolInstances.simplePen;
var previousSelectedTool = toolInstances.simplePen;
2012-09-11 02:52:09 +04: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-11 02:52:09 +04:00
};
2012-09-11 02:52:09 +04:00
var selectTool_ = function(tool) {
console.log("Selecting Tool:" , currentSelectedTool);
currentSelectedTool = tool;
activateToolOnStage_(currentSelectedTool);
$.publish(Events.TOOL_SELECTED, [tool]);
};
2012-09-11 02:52:09 +04:00
/**
* @private
*/
var onToolIconClicked_ = function(evt) {
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 toolInstances) {
if (toolInstances[tool].toolId == clickedTool.data().toolId) {
selectTool_(toolInstances[tool]);
2012-09-11 02:52:09 +04:00
// Show tool as selected:
$("#tools-container .tool-icon.selected").removeClass("selected");
clickedTool.addClass("selected");
}
}
}
};
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
*/
var isShowGridChecked_ = function() {
var showGridCheckbox = $('#show-grid');
var isChecked = showGridCheckbox.is(':checked');
return isChecked;
};
2012-09-09 01:43:16 +04:00
2012-09-11 02:52:09 +04:00
return {
init: function() {
// Initialize tool:
// Set SimplePen as default selected tool:
selectTool_(toolInstances.simplePen);
// Activate listener on tool panel:
$("#tools-container").click(onToolIconClicked_);
2012-09-09 01:43:16 +04:00
2012-09-11 02:52:09 +04:00
// Show/hide the grid on drawing canvas:
2012-09-11 02:54:06 +04:00
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [isShowGridChecked_()]);
2012-09-11 02:52:09 +04:00
$('#show-grid').change(function(evt) {
var checked = isShowGridChecked_();
2012-09-11 02:54:06 +04:00
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked]);
2012-09-11 02:52:09 +04:00
});
}
};
})();