-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.html
More file actions
412 lines (359 loc) · 17.6 KB
/
Copy pathtemplate.html
File metadata and controls
412 lines (359 loc) · 17.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
{{title}}
</title>
<meta name="description" content="Hello, World! - A-Frame">
<!-- AFRAME -->
<script src="https://aframe.io/releases/0.5.0/aframe.min.js"></script>
<script src="https://cdn.rawgit.com/donmccurdy/aframe-extras/v3.3.1/dist/aframe-extras.misc.min.js"></script><!-- For controllers-->
<script src="https://rawgit.com/ngokevin/aframe-animation-component/master/dist/aframe-animation-component.min.js"></script>
<script src="https://unpkg.com/aframe-controller-cursor-component@0.2.2/dist/aframe-controller-cursor-component.min.js"></script>
<!-- COMPONENTS -->
<script src="components/cursor-listener.js" charset="utf-8"></script>
<script src="components/debugger-control.js" charset="utf-8"></script>
<script src="components/alongpath.js" charset="utf-8"></script>
<script src="components/meshline.js" charset="utf-8"></script>
<script src="components/revert-listener.js" charset="utf-8"></script>
<script src="https://unpkg.com/aframe-teleport-controls@0.2.x/dist/aframe-teleport-controls.min.js"></script>
<!-- HELPERS -->
<script type="text/javascript" src="./helpers/helpers.js" charset="utf-8"></script>
<script type="text/javascript" src="./helpers/parse_helpers.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- CODE MIRROR -->
<script src="lib/codemirror.js"></script>
<link rel="stylesheet" href="lib/codemirror.css">
<link rel="stylesheet" href="stylesheets/monokai.css">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="stylesheets/main.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<script type="text/javascript" src="./vendor/peg.js"></script>
<script type="text/javascript" src="./lib/viz.js"></script>
<script type="text/javascript" charset="utf-8">
var signalGraph = "";
var codeMirror;
var paused = false;
var previousSignalGraphs = [];
var currentSignalGraph = 0;
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
function findSignalNode(signalGraph, signalName) {
function searchInNode(node, signalName) {
if (node.ref == signalName) {
return node;
}
else if (node.children) {
var i;
var result = null;
for(i = 0; result == null && i < node.children.length; i++){
result = searchInNode(node.children[i], signalName);
}
return result;
}
return null;
}
var i;
var result = null;
var keys = Object.keys(signalGraph);
for (i = 0; result == null && i < keys.length; i++){
if (signalGraph[keys[i]].ref ==signalName ) {
result = signalGraph[keys[i]];
}
else{
result = searchInNode(signalGraph[keys[i]], signalName);
}
}
return result;
}
function refreshScene() {
console.log("refreshing scene...")
scene = document.querySelector("a-scene")
graphElements = scene.querySelectorAll(".graphElement");
// graphElements.forEach(function (graphElement) {
// console.log("Graph element",graphElement)
// //graphElement.parentNode.removeChild(graphElement);
// })
$( ".graphElement" ).remove();
// Uh oh something wrong here
// console.log("AFTER");
previousSignalGraphs.forEach(function (graph, index) {
// console.log("Signal Graph: ")
// console.log(graph)
dotString = make_dot_file(graph);
// console.log("Dot signal Graph: ")
// console.log(dotString)
jsonGraph = get_json(dotString)
// console.log("JSON signal Graph: ")
// console.log(jsonGraph)
json_to_hml(JSON.parse(jsonGraph),index);
})
// Here delete the previously present graph
// console.log(resString)
// document.querySelector("#scene").innerHTML += resString;
}
function start() {
console.log("Starting");
codeMirror = CodeMirror(function (elt) {
document.querySelector("#codeEditor").parentNode.replaceChild(elt, document.querySelector("#codeEditor"));
}, {
value: decodeEntities(document.querySelector("#initCode").innerHTML),
mode: "javascript",
lineNumbers: true
});
signalGraph = JSON.parse(document.getElementById("graph").innerHTML);
previousSignalGraphs.push(signalGraph)
refreshScene();
/*if (typeof(Worker) !== "undefined") {
if (typeof(w) == "undefined") {
w = new Worker("webworker.js");
}
}*/
handle_signals(signalGraph);
}
var secondSignalEnabled = true;
var signal = "seconds";
function updateNode(node){
var sceneEl = document.querySelector('a-scene');
var reached = false;
// if(node.name=='seconds') console.log(node.children)
// VALUES FLOWINGs
for (var i = 0; i < node.children.length; i++) {
var fromNode = document.querySelector("[id='" + node.id + "']");
var toNode = document.querySelector("[id='" + node.children[i].id + "']");
var edgeToFollow = document.querySelector("[id='"+ node.children[i].id+"-"+node.id+"']")
// console.log("[id='" + node.children[i].id + "']");
// console.log(edgeToFollow.getAttribute('pathToFollow'));
// console.log(edgeToFollow);
var pathToFollow = []
var tempPath = edgeToFollow.getAttribute('pathToFollow').split(',').map((x) => parseFloat(x))
while (tempPath.length > 0) pathToFollow.push(tempPath.splice(0, 4));
pathToFollow = pathToFollow.sort(function (elem1, elem2) {
return elem2.y - elem1.y;
});
// console.log(pathToFollow);
var fromDest = fromNode.getAttribute('position');//TODO
var toDest = toNode.getAttribute('position');
var entityEl = document.createElement('a-entity');
var dist = {
x:(toDest.x - fromDest.x),
y:(toDest.y - fromDest.y)
}
var division = 3;
var deltaX = dist.x/division;
var deltaY = dist.y/division;
var nodeText = fromNode.getAttribute('text').value.split("\n");
var slidingText = (parseInt(nodeText[nodeText.length-1])+1).toString();
// if (slidingText!= 'seconds' && slidingText !=)
var text_ = "align: center; color: white; font: https://cdn.aframe.io/fonts/mozillavr.fnt; opacity: 1; side: double; value:" + slidingText +"; width: 4; wrapCount: 60.6; wrapPixels: 1500; zOffset: 0.01"
entityEl.setAttribute('text', text_);
// entityEl.setAttribute('position', fromDest.x + " " + fromDest.y +" " + fromDest.z)
var pathstring = 'path:'
// for (step in pathToFollow) {
// if ({}.hasOwnProperty.call(pathToFollow, step)) {
// pathstring += `${pathToFollow[step].x}` + "," + `${pathToFollow[step].y}` + "," + `${pathToFollow[step].z} `;
// }
// }
for (var j = 0; j < division; j++) {
pathstring += `${fromDest.x + j*deltaX}` + "," + `${fromDest.y + j*deltaY}` + "," + `${fromDest.z} `
}
pathstring += `${toDest.x}` + "," + `${toDest.y}` + "," + `${toDest.z} `
pathstring += `; closed: false; dur: 4000; loop: false`;
entityEl.setAttribute("alongpath", pathstring);
entityEl.setAttribute('class', "flowingText");
if (entityEl) {
sceneEl.appendChild(entityEl);
}
}
var nodeTest = document.querySelector("[id='" + node.id + "']");
var nodeText = nodeTest.getAttribute('text').value;
var newNodeText = "align: center; color: black; font: https://cdn.aframe.io/fonts/mozillavr.fnt; opacity: 1; side: double; value:" + node.name +'\n\n'+ node.value.toString() +"; width: 5.870; wrapCount: 60.6; wrapPixels: 1500; zOffset: 0.02"
nodeTest.setAttribute('text', newNodeText);
}
function update_children(parentNode, graph){
if (parentNode.children) {
for (var i = 0; i < parentNode.children.length; i++) {
node = parentNode.children[i];
if (node.globalBreakpoint) {
var event = new Event("stopDebugger");
this.dispatchEvent(event);
}
if (! node.localBreakpoint) {
var updateChildren = true;
// console.log(node.name);
switch(node.name){
case "fold":
//console.log(node.formula.replace("$$signalValue$$",parentNode.value).replace("currentValue",node.value))
node.value = eval(node.formula.replace("$$signalValue$$",parentNode.value).replace("currentValue",node.value));
//console.log("Fold value: " + node.value)
updateNode(node);
break;
case "map":
[body,param] = node.formula;
node.value = eval(body.replace(param, parentNode.value));
updateNode(node);
break;
case "filter":
[body,param] = node.formula;
signalValue = parentNode.value;
if (eval(body.replace(param, signalValue))) {
node.value = signalValue;
updateNode(node);
}
else{
updateChildren = false;
}
break;
case "merge":
[signal1, signal2, operator] = node.formula;
signal1Value = findSignalNode(graph, signal1).value;
signal2Value = findSignalNode(graph, signal2).value;
node.value = eval(signal1Value + operator + signal2Value);
if (node.value == null)
node.value = 0;
updateNode(node);
break;
}
if (updateChildren) {
update_children(node, graph);
}
}
}
}
}
function close_enough_2D(pos1,pos2) {
return (Math.abs(pos1[0]-pos2[0]) < 0.5 && Math.abs(pos1[1]-pos2[1]) < 0.5)
}
function delete_flowing_values() {
var flowing = document.querySelectorAll(".flowingText");
////console.log(flowing[flowing.length - 1],flowing[flowing.length - 1].getAttribute('scale'));
for (var i = 0; i < flowing.length-1; i++) {
//console.log(flowing[i].attributes);
////console.log(flowing[i])
try{
var pathArray = flowing[i].getAttribute('alongpath').path.split(' ');
////console.log("bib");
var destination = pathArray[pathArray.length - 1].split(",");
////console.log(destination);// Array os destination pos
var currentPos = flowing[i].getAttribute('position');
var currentPosArray = [currentPos.x,currentPos.y,currentPos.z];
if (close_enough_2D(currentPosArray, destination)) {
flowing[i].parentNode.removeChild(flowing[i]);
}
}
catch(err){
}
}
}
function handle_signals(signalGraph) {
if (secondSignalEnabled && !paused) {
var secondSignalUpdate = setInterval(function(){
signalGraph[signal].value ++;
updateNode(signalGraph[signal]);
console.log(signalGraph)
this.update_children(signalGraph[signal], signalGraph);
this.delete_flowing_values();
}, 1000);
window.onkeydown = keyPressed;
this.addEventListener("stopDebugger", () => {
paused = true;
clearInterval(secondSignalUpdate);
});
this.addEventListener("startDebugger", () => {
if (paused) {
secondSignalUpdate = null;
paused = false;
handle_signals(signalGraph);
}
});
this.addEventListener("restartDebugger", () => {
reset_graph(signalGraph[signal]);
})
}
}
function keyPressed(e){
signalGraph.input.value ++;
updateNode(signalGraph.input);
update_children(signalGraph.input, signalGraph);
}
function dive() {
document.getElementById("code").style.display="none";
document.getElementById("scene").style = null;
}
function inject() {
if (typeof(Worker) !== "undefined") {
if (typeof(w) == "undefined") {
w = new Worker("parse_worker.js");
w.onmessage = function(event){
// HERE WE GET THE JSON OF THE SIGNAL GRAPH
document.getElementById("result").innerHTML = event.data;
};
}
} else {
// Sorry! No Web Worker support..
}
}
</script>
</head>
<body style="font-family:monospace" onload=start()>
<div id="code" class="menu">
<div id="codeEditor" class="codeEditor">
</div>
<div class="footer">
<button class="btn btn-primary fullwidth" style="margin-top: 5px;" disabled="true" onclick="inject()">Inject code</button>
<button class="btn btn-primary fullwidth" style="margin-top: 5px;" onclick="dive()">Dive</button>
</div>
</div>
<a-scene class="mainContent" id = "scene">
<a-assets>
<img id="assignment" src="assignment3.png">
</a-assets>
<!-- Hands -->
<a-entity teleport-controls hand-controls="left" position="2 3.5 -0.5" controller-cursor="color: red" ></a-entity>
<a-entity teleport-controls hand-controls="right" position="2 3.5 -0.5" controller-cursor="color: red" ></a-entity>
<!-- CAMERA -->
<a-entity>
<a-camera user-height="1.6" position="2 3.5 0">
<!-- DEBUGGER CONTROLS -->
<a-entity
debugger-control
id="restart"
geometry="primitive: plane; width:0.5; height: auto"
material="color: #333399"
position="-0.55 -1 -2"
text="width: 3;value: Restart ; align: center"></a-entity>
<a-entity
debugger-control
id="pause"
geometry="primitive: plane; width:0.5; height: auto"
material="color: #333399"
position="0 -1 -2"
text="width: 3;value: Pause ; align: center"></a-entity>
<a-entity
debugger-control
id="play"
geometry="primitive: plane; width:0.5; height: auto"
material="color: #333399"
position="0.55 -1 -2"
text="width: 3;value: Play ; align: center"></a-entity>
</a-camera>
</a-entity>
<!-- HELPER BUTTONS -->
<!-- <a-image src="#assignment" position="2 8.4 -5.2" scale="3.7 1 1"></a-image> -->
<!-- SKY -->
<img id="sky_sphere-texture" src="textures/sky_sphere.jpg">
<a-sky color="#EEEEFF" material="src: #sky_sphere-texture"></a-sky>
</a-scene>
<div id="graph" style="display: none;">
{{graph}}
</div>
<div id="initCode" style="display: none;">{{code}}</div>
</body>
</html>