-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbox.js
73 lines (59 loc) · 2.04 KB
/
toolbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//container object for storing the tools. Functions to add new tools and select a tool
function Toolbox() {
var self = this;
this.tools = [];
this.selectedTool = null;
var toolbarItemClick = function() {
//remove any existing borders
var items = selectAll(".sideBarItem");
for (var i = 0; i < items.length; i++) {
items[i].style('border', '0')
}
var toolName = this.id().split("sideBarItem")[0];
self.selectTool(toolName);
//call loadPixels to make sure most recent changes are saved to pixel array
loadPixels();
}
//add a new tool icon to the html page
var addToolIcon = function(icon, name) {
var sideBarItem = createDiv("<img src='" + icon + "'></div>");
sideBarItem.class('sideBarItem')
sideBarItem.id(name + "sideBarItem")
sideBarItem.parent('sidebar');
sideBarItem.mouseClicked(toolbarItemClick);
};
//add a tool to the tools array
this.addTool = function(tool) {
//check that the object tool has an icon and a name
if (!tool.hasOwnProperty("icon") || !tool.hasOwnProperty("name")) {
alert("make sure your tool has both a name and an icon");
}
this.tools.push(tool);
addToolIcon(tool.icon, tool.name);
//if no tool is selected (ie. none have been added so far)
//make this tool the selected one.
if (this.selectedTool == null) {
this.selectTool(tool.name);
}
};
this.selectTool = function(toolName) {
//search through the tools for one that's name matches
//toolName
for (var i = 0; i < this.tools.length; i++) {
if (this.tools[i].name == toolName) {
//if the tool has an unselectTool method run it.
if (this.selectedTool != null && this.selectedTool.hasOwnProperty(
"unselectTool")) {
this.selectedTool.unselectTool();
}
//select the tool and highlight it on the toolbar
this.selectedTool = this.tools[i];
select("#" + toolName + "sideBarItem").style("border", "2px solid blue");
//if the tool has an options area. Populate it now.
if (this.selectedTool.hasOwnProperty("populateOptions")) {
this.selectedTool.populateOptions();
}
}
}
};
}