-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
220 lines (192 loc) · 6 KB
/
input.js
File metadata and controls
220 lines (192 loc) · 6 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//Isometry -- Graph Theory Project
// input.js >> if it's user input, it goes in here.
var dragStartX;
var dragStartY;
var draggedVert=null;
function parseGraphInput(text){
//Graph input notation: <NAME>:(<TO>, W),...,(<TO>,W) <-- zero or more
// NAME: Vertex Name, (<TO>,W): Edge, with TO is the destination, W is the weight of the edge
var regex = /\w+\s*:?\s*(\(\s*\w+\s*,\s*\d+\s*\)[,; ]*)*/g
text = text.trim();
var tokens = text.match(regex);//Individual adjacency list entries
var parsedGraph = new Array();
var nameRegex = /\w+(?=\s*:?)/;//Matches the name portion of adjacency list entries
var edgeRegex = /\(\s*\w+\s*,\s*\d+\s*\)/g;//Matches all the edges
var edgeNameRegex = /\w+(?=\s*,)/;//Matches the name of an edge
var weightRegex = /\d+(?=\s*\))/;//Matches the weight of an edge
for(i=0;i<tokens.length;i++){
var adj = tokens[i];
var name = adj.match(nameRegex)[0];
var vert = new Vertex(name);//RESPECT THE ABSTRACTION BARRIER
var adjEdges = adj.match(edgeRegex);
if(adjEdges!==null){
for(j=0;j<adjEdges.length;j++){
var to = adjEdges[j].match(edgeNameRegex)[0];
var we = adjEdges[j].match(weightRegex)[0];
vert.edges.push(new Edge(name, to, we));
}
}
parsedGraph.push(vert);
}
return parsedGraph;
}
(function(){
var InputService = (function() {
//dragBegin: OnClick, stores the XY coordinates of the mouse.
var blockRepeat = false;
var ctrlDown = false;
var currentKey = -1;
function mouseDownOnVertex(svg, vert){
if(ctrlDown){
if(d3.select(vert).classed("selectedVertex")){
//vertex is already selected, deselect
deselectVertex(svg, vert);
}
else{
selectVertex(svg, vert);
}
}else{
dragBegin(svg, vert);
}
}
function mouseClickOnVertex(svg, vert){
if(ctrlDown){
if(d3.select(vert).classed("selectedVertex")){
//vertex is already selected, deselect
deselectVertex(svg, vert);
}
else{
selectVertex(svg, vert);
}
}
}
//Returns true for "redraw necessary"
function keyDown(e){
// console.log("d3 keydown: "+e.keyCode);
if(e.keyCode === 17){
ctrlDown = true;
// console.log("ctrlDown is true");
}
else if(e.keyCode === 45 && !blockRepeat){//Insert
//adds new vertex
blockRepeat = true;
var alpha = new Array("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z");
for(var i = 0; i < alpha.length; i++){
var letter = alpha[i];
var foundLetter = false;
for(var j = 0; j < graph.length; j++){
var vert = graph[j];
if (vert.name === letter){
foundLetter = true; break;
}
}
if(foundLetter){
continue;
}else{
graph.push(new Vertex(letter));
break;
}
}
return true;
}
else if(e.keyCode === 46){
//Deletes all selected vertices
var verticesToRemove = d3.select(".selectedVertex").data();
console.log(verticesToRemove);
for(var i = 0; i < verticesToRemove.length; i++){
var vertex = verticesToRemove[i];
for(var j = 0; j < graph.length; j++){
if (vertex.name === graph[j].name){
//Remove
graph.splice(j, 1);
break;
}
}
}
return true;
}
if(currentKey === -1){
currentKey = e.keyCode;
}
return false;
}
function keyUp(e){
// console.log("d3 keyup: "+e.keyCode);
if(e.keyCode === 17){
ctrlDown = false;
// console.log("ctrlDown is false");
}
blockRepeat = false;
currentKey = -1;
}
//Return true on "redraw necessary"
function mouseUpOnSVG(svg, vert){
return dragVertex(svg);
}
////// DRAG FUNCTIONS
function dragBegin(svg, vert){
var pos = d3.mouse(svg);
dragStartX = pos[0];
dragStartY = pos[1];
draggedVert = vert;
console.log(draggedVert);
}
function dragVertex(svg){
//Calculate the displacement from where the vertex would be if there were no translate()
//You're replacing the translate anyway, so don't worry about it.
if (draggedVert===null){return false;}
var vertName = d3.select(draggedVert).attr("id").substring(1);//Assumes id is form v[ACTUAL NAME]
var pos = d3.mouse(svg);
var curPos = new Array();
curPos[0] = d3.select(draggedVert).select("circle").attr("cx");
curPos[1] = d3.select(draggedVert).select("circle").attr("cy");
var newPos = new Array();
newPos.push(pos[0]-curPos[0]);
newPos.push(pos[1]-curPos[1]);
d3.select(draggedVert).attr("transform", "translate("+parseInt(newPos[0])+","+parseInt(newPos[1])+")");
//Let all the relevant edges know the vertex has shifted
console.log("size of edge array for vertex: "+d3.selectAll(".e"+vertName).length);
console.log(d3.selectAll(".e"+vertName));
d3.selectAll(".e"+vertName).each(function(d){d.vertexShift();});
draggedVert = null;
return true;
}
////// END DRAG FUNCTIONS
////// SELECT VERTEX FUNCTIONS
function selectVertex(svg, vert){
d3.select(vert).classed("selectedVertex", true);
}
function deselectVertex(svg, vert){
d3.select(vert).classed("selectedVertex", false);
}
//PUBLIC
return{
mouseDownOnVertex : mouseDownOnVertex,
mouseUpOnSVG : mouseUpOnSVG,
mouseClickOnVertex : mouseClickOnVertex,
keyUp : keyUp,
keyDown : keyDown
};
})();
window.InputService = InputService;
console.log("InputService initialized.");
})();
// function calculateCurrentTranslation(){
// var transLine = d3.select("#v"+vertName).attr("transform");
// var translateRegExp = /translate\(-?\d+,-?\d+\)/ig;
// var translation = [0, 0];
// if(translateRegExp.test(transLine)){
// //console.log("translation detected!");
// var transStatement = transLine.match(translateRegExp);
// //console.log(transStatement);
// var numbersRegExp = /-?\d+/g;
// var numbers = transStatement[0].match(numbersRegExp);
// translation[0] = numbers[0];
// translation[1] = numbers[1];
// }
// return translation;
// }