2012-08-31 08:45:07 +00:00
|
|
|
/*
|
|
|
|
* @provide pskl.ToolSelector
|
|
|
|
*
|
|
|
|
* @require Constants
|
|
|
|
* @require Events
|
|
|
|
* @require pskl.drawingtools
|
|
|
|
*/
|
2012-09-02 23:24:55 +00:00
|
|
|
$.namespace("pskl");
|
2012-08-31 08:45:07 +00:00
|
|
|
|
|
|
|
pskl.ToolSelector = (function() {
|
|
|
|
|
|
|
|
var toolInstances = {
|
|
|
|
"simplePen" : new pskl.drawingtools.SimplePen(),
|
|
|
|
"eraser" : new pskl.drawingtools.Eraser(),
|
2012-09-01 22:44:55 +00:00
|
|
|
"paintBucket" : new pskl.drawingtools.PaintBucket(),
|
2012-09-02 11:19:20 +00:00
|
|
|
"stroke" : new pskl.drawingtools.Stroke(),
|
2012-09-06 23:08:25 +00:00
|
|
|
"rectangle" : new pskl.drawingtools.Rectangle(),
|
|
|
|
"move" : new pskl.drawingtools.Move()
|
2012-08-31 08:45:07 +00:00
|
|
|
};
|
|
|
|
var currentSelectedTool = toolInstances.simplePen;
|
|
|
|
var previousSelectedTool = toolInstances.simplePen;
|
|
|
|
|
|
|
|
var selectTool_ = function(tool) {
|
|
|
|
var maincontainer = $("body");
|
|
|
|
var previousSelectedToolClass = maincontainer.data("selected-tool-class");
|
|
|
|
if(previousSelectedToolClass) {
|
|
|
|
maincontainer.removeClass(previousSelectedToolClass);
|
|
|
|
}
|
|
|
|
maincontainer.addClass(toolBehavior.toolId);
|
|
|
|
$("#drawing-canvas-container").data("selected-tool-class", toolBehavior.toolId);
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
var selectTool_ = function(tool) {
|
|
|
|
console.log("Selecting Tool:" , currentSelectedTool);
|
|
|
|
currentSelectedTool = tool;
|
|
|
|
activateToolOnStage_(currentSelectedTool);
|
|
|
|
$.publish(Events.TOOL_SELECTED, [tool]);
|
|
|
|
};
|
|
|
|
|
2012-09-02 23:24:55 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-08-31 08:45:07 +00:00
|
|
|
var onToolIconClicked_ = function(evt) {
|
|
|
|
var target = $(evt.target);
|
|
|
|
var clickedTool = target.closest(".tool-icon");
|
|
|
|
|
|
|
|
if(clickedTool.length) {
|
|
|
|
for(var tool in toolInstances) {
|
|
|
|
if (toolInstances[tool].toolId == clickedTool.data()["toolId"]) {
|
|
|
|
selectTool_(toolInstances[tool]);
|
|
|
|
|
|
|
|
// Show tool as selected:
|
|
|
|
$("#tools-container .tool-icon.selected").removeClass("selected");
|
|
|
|
clickedTool.addClass("selected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-08 21:43:16 +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-08-31 08:45:07 +00:00
|
|
|
return {
|
|
|
|
init: function() {
|
2012-09-02 23:24:55 +00:00
|
|
|
|
|
|
|
// Initialize tool:
|
|
|
|
// Set SimplePen as default selected tool:
|
|
|
|
selectTool_(toolInstances.simplePen);
|
|
|
|
// Activate listener on tool panel:
|
|
|
|
$("#tools-container").click(onToolIconClicked_);
|
2012-09-08 21:43:16 +00:00
|
|
|
|
|
|
|
// Show/hide the grid on drawing canvas:
|
|
|
|
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [isShowGridChecked_()])
|
|
|
|
$('#show-grid').change(function(evt) {
|
|
|
|
var checked = isShowGridChecked_();
|
|
|
|
$.publish(Events.GRID_DISPLAY_STATE_CHANGED, [checked])
|
|
|
|
});
|
2012-08-31 08:45:07 +00:00
|
|
|
}
|
|
|
|
};
|
2012-09-02 23:24:55 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
|