-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.html
692 lines (577 loc) · 15.1 KB
/
demo.html
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<html>
<head>
<title>Graphs</title>
<!-- https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes#Rectangular_shape_example -->
<script type="text/javascript" src="convert.js"></script>
<script type="text/javascript" src="tree.js"></script>
<script type="text/javascript" src="math.js"></script>
<script type="text/javascript" src="merge.js"></script>
<script type="text/javascript">
blocks = [];
var ctx = null;
var WIDTH = 0;
var HEIGHT = 0;
var MINX = 0;
var MINY = 0;
var MAXX = 0;
var MAXY = 0;
var moving_blocks = [];
var moving_blocks2 = [];
var is_dragging = -1;
var is_dragging2 = -1;
var prevX = -1;
var prevY = -1;
var prevX2 = 0;
var prevY2 = 0;
var origX=[], origY=[];
var origX2 =[], origY2 =[];
var target;
var to_change;
var possib;
var target2;
var to_change2;
var possib2;
var firsttouch;
var secondtouch;
var multitouch = 0;
var merging = 0;
var addition = 1;
/* merge blocks on the desktop */
var fake_multitouch = -1;
function Block(text) {
this.x = 0;
this.y = 0;
this.width = 50;
this.height = 50;
this.node = new node();
this.node.value = text;
this.node.toggled = false;
}
function node_to_block(n)
{
for(i = 0; i < blocks.length; i++){
if(blocks[i].node == n)
return blocks[i];
}
return null;
}
function clear() {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
}
function align_blocks() {
var nextX = WIDTH/2 - (blocks.length/2+1)*50;
var nextY = HEIGHT/2 - 50;
for (var i = 0; i < blocks.length; i++) {
blocks[i].y = nextY;
blocks[i].x = nextX;
nextX += blocks[i].width + 10;
}
}
function draw()
{
clear();
bluegreen = '#11e57f';
red = '#ff0000';
ctx.lineWidth = 1;
ctx.font = "20pt Verdana";
ctx.fillStyle = 'black';
for (var i = 0; i < blocks.length; i++) {
if(blocks[i].node.toggled == true)
ctx.strokeStyle = red;
else
ctx.strokeStyle = bluegreen;
ctx.strokeRect(blocks[i].x, blocks[i].y,
blocks[i].width,
blocks[i].height);
//print the text
code = [blocks[i].node.value];
for (var j = 0; j < code.length; j++) {
ctx.fillText(code[j], blocks[i].x+15, blocks[i].y+j*(blocks[i].height/2)+blocks[i].height/2);
}
}
}
function distance(x1,y1,x2,y2){
return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
}
function find_near_block(x,y,excl)
{
var mini = Number.MAX_VALUE;
var best = -1;
for(var i = 0; i < blocks.length; i++){
if(i == excl) continue;
var d = distance(blocks[i].x, blocks[i].y, x, y);
if(d < mini){
mini = d;
best = i;
}
}
return best;
}
function drag_finish(b, target, to_change){
if(b < 0) return;
var block = blocks[b].node;
var equals = tree.root;
//Did it go left of equals?
if(origX[0] < node_to_block(equals).x && node_to_block(block).x > node_to_block(equals).x)
swap_left(target, to_change);
//Or did it go right?
else if(origX[0] > node_to_block(equals).x && node_to_block(block).x < node_to_block(equals).x)
swap_right(target, to_change);
render_from_tree();
if(b != -1){
//align block i to the nearest one
var near = find_near_block(block.x, block.y, b);
if(near != -1){
block.y = blocks[near].y;
}
}
fix_x_overlaps();
}
function is_overlapping(block_idx) {
b = blocks[block_idx];
for(var i = 0; i < blocks.length; i++){
a = blocks[i];
if( (a.x + a.width > b.x && a.x < b.x+b.width)
|| (b.x + b.width > a.x && b.x < a.x+a.width)){
return 1;
}
}
return 0;
}
function fix_x_overlaps(){
for(var i = 0; i < blocks.length; i++){
for(var j = 0; j < blocks.length; j++){
if(j == i) continue;
if( blocks[i].x > blocks[j].x ){
b = blocks[i];
a = blocks[j];
} else {
a = blocks[i];
b = blocks[j];
}
if(a.x+a.width+5 > b.x && a.x+5 < b.x+b.width){
overlap = a.x+a.width - b.x;
a.x -= overlap/2+10;
b.x += overlap/2+10;
fix_x_overlaps();
}
}
}
}
//in: x,y coordinates
//out: first block at those coordinates
function find_block(x,y) {
x -= MINX;
y -= MINY;
for (var i = 0; i < blocks.length; i++) {
lx = blocks[i].x;
rx = blocks[i].width + lx;
uy = blocks[i].y;
dy = (2)*blocks[i].height + uy;
//alert( MINX + " " + MINY + " :: " + lx + " " + x + " " + rx + "::" + uy + " " + y + " " + dy )
if ((lx <= x && rx >= x) && (uy <= y && dy >= y)) {
return i;
}
}
return -1;
}
function get_target(node)
{
target = blocks[is_dragging].node;
to_change = target.par;
possib = to_change;
while(possib != node)
{
if(possib.value != to_change.value)
to_change = possib;
possib = possib.par;
}
while(target.par != to_change)
target = target.par;
}
function get_target2(node)
{
target2 = blocks[is_dragging2].node;
to_change2 = target2.par;
possib2 = to_change2;
while(possib2 != node)
{
if(possib2.value != to_change2.value)
to_change2 = possib2;
possib2 = possib2.par;
}
while(target2.par != to_change2)
target2 = target2.par;
}
function ev_canvas(ev) {
if (ev.type == "mousemove") {
if (is_dragging != -1) {
deltaX = ev.layerX - prevX;
deltaY = ev.layerY - prevY;
for(var i=0; i<moving_blocks.length; i++) {
moving_blocks[i].x = deltaX + origX[i];
moving_blocks[i].y = deltaY + origY[i];
}
}
} else if (ev.type == "mousedown") {
//check through and see if the click collides with a code block
//if it does, turnon dragging
origX = [];
origY = [];
moving_blocks = [];
is_dragging = find_block(ev.layerX, ev.layerY)
if (is_dragging != -1) {
//mouse down on block i, enable dragging
prevX = ev.layerX;
prevY = ev.layerY;
get_target(tree.root);
grab_moving_blocks(target);
for(var i=0; i<moving_blocks.length; i++) {
origX.push(moving_blocks[i].x);
origY.push(moving_blocks[i].y);
}
/* fake multi touch */
if( combine_mode == 1) {
if(!combine_target){
combine_target = blocks[is_dragging].node;
combine_target.toggled = true;
} else {
//fake merge
mergeeq(combine_target, blocks[is_dragging].node);
render_from_tree();
combine_target.toggled = false;
combine_target = null;
is_dragging = -1;
}
}
}
} else if (ev.type == "mouseup") {
if(is_dragging == -1)
return;
if(blocks[is_dragging].node.value == '=' || blocks[is_dragging].node.value == '0') {
render_from_tree();
is_dragging = -1;
return;
}
drag_finish(is_dragging, target, to_change);
is_dragging = -1;
}
else if (ev.type == "touchmove") {
if(multitouch)
{
multitouchmove(ev);
return;
}
if (is_dragging != -1) {
deltaX = ev.touches[0].pageX - prevX;
deltaY = ev.touches[0].pageY - prevY;
for(var i=0; i<moving_blocks.length; i++)
{
moving_blocks[i].x = deltaX + origX[i];
moving_blocks[i].y = deltaY + origY[i];
}
}
else {}
if(is_dragging > -1) {
if(moving_blocks[0].y-30 > origY[0])
addition = 0;
else
addition = 1;
}
else {}
}
else if (ev.type == "touchstart") {
//check through and see if the click collides with a code block
//if it does, turnon dragging
//If it's the second click, then try to do a multitouch event.
//If a block wasn't clicked the second time don't do anything
if(ev.touches.length > 1) {
is_dragging2 = find_block(ev.touches[0].pageX, ev.touches[0].pageY);
if(is_dragging == is_dragging2) {
is_dragging2 = find_block(ev.touches[1].pageX, ev.touches[1].pageY);
firsttouch = 0;
secondtouch = 1;
}
else {
firsttouch = 1;
secondtouch = 0;
}
if(is_dragging != -1 && is_dragging2 != -1)
multitouchstart(ev);
return;
}
origX = [];
origY = [];
moving_blocks = [];
is_dragging = find_block(ev.touches[0].pageX, ev.touches[0].pageY);
if (is_dragging != -1) {
//mouse down on block i, enable dragging
ev.preventDefault();
prevX = ev.touches[0].pageX;
prevY = ev.touches[0].pageY;
get_target(tree.root);
grab_moving_blocks(target);
for(var i=0; i<moving_blocks.length; i++) {
origX.push(moving_blocks[i].x);
origY.push(moving_blocks[i].y);
}
}
}
else if (ev.type == "touchend") {
if(is_dragging == -1)
return;
if(blocks[is_dragging].node.value == '=' || blocks[is_dragging].node.value == '0') {
render_from_tree();
is_dragging = -1;
return;
}
if(merging) {
mergeeq(blocks[is_dragging].node, blocks[is_dragging2].node);
merging = 0;
}
drag_finish(is_dragging, target, to_change);
clean();
is_dragging = -1;
is_dragging2 = -1;
multitouch = 0;
firsttouch = 0;
secondtouch = 0;
}
}
function multitouchstart(ev)
{
multitouch = 1;
ev.preventDefault();
prevX2 = ev.touches[secondtouch].pageX;
prevY2 = ev.touches[secondtouch].pageY;
get_target2(tree.root);
if(target == target2) {
while(target == target2) {
get_target(to_change);
get_target2(to_change2);
moving_blocks = [];
grab_moving_blocks(target);
}
origX = [];
prevX = ev.touches[firsttouch].pageX;
prevY = ev.touches[firsttouch].pageY;
for(var i=0; i<moving_blocks.length; i++) {
origX.push(moving_blocks[i].x);
origY.push(moving_blocks[i].y);
}
}
moving_blocks2 = [];
grab_moving_blocks2(target2);
origX2 = [];
origY2 = [];
for(var i=0; i<moving_blocks2.length; i++) {
origX2.push(moving_blocks2[i].x);
origY2.push(moving_blocks2[i].y);
}
}
function multitouchmove(ev)
{
deltaX = ev.touches[firsttouch].pageX - prevX;
deltaY = ev.touches[firsttouch].pageY - prevY;
deltaX2 = ev.touches[secondtouch].pageX - prevX2;
deltaY2 = ev.touches[secondtouch].pageY - prevY2;
for(var i=0; i<moving_blocks.length; i++) {
moving_blocks[i].x = deltaX + origX[i];
moving_blocks[i].y = deltaY + origY[i];
}
for(var i=0; i<moving_blocks2.length; i++) {
moving_blocks2[i].x = deltaX2 + origX2[i];
moving_blocks2[i].y = deltaY2 + origY2[i];
}
//Make fail if on opposite sides of the = sign
var ep = blocks.indexOf(node_to_block(tree.root));
if((is_dragging < ep && is_dragging2 < ep) || (is_dragging > ep && is_dragging2 > ep)) {
if(origX[0] > origX2[0] && (moving_blocks[0].x - origX[0]) < 30 && (moving_blocks2[0].x - origX2[0]) > 30 )
merging = 1;
else if(origX2[0] > origX[0] && (moving_blocks2[0].x - origX2[0]) < 30 && (moving_blocks[0].x - origX[0]) > 30 )
merging = 1;
else
merging = 0;
}
else
merging = 0;
}
function multitouchend(ev)
{
}
function grab_moving_blocks(target)
{
moving_blocks.push(node_to_block(target));
if(target.left)
grab_moving_blocks(target.left);
if(target.right)
grab_moving_blocks(target.right);
}
function grab_moving_blocks2(target2)
{
moving_blocks2.push(node_to_block(target2));
if(target2.left)
grab_moving_blocks2(target2.left);
if(target2.right)
grab_moving_blocks2(target2.right);
}
var combine_mode = 0;
var combine_target = null;
function ev_keyboard(ev)
{
//a user can press down 'e' to evaluate
if(ev.keyCode != 69)
return;
if(ev.type == "keydown") {
document.body.style.backgroundColor = "blue";
combine_mode = 1;
}
else if(ev.type == "keyup") {
combine_mode = 0;
document.body.style.backgroundColor = "white";
if(combine_target)
combine_target.toggled = false;
combine_target = false;
}
}
function init() {
var canvas = document.getElementById('tutorial');
WIDTH = canvas.width;
HEIGHT = canvas.height;
MINX = canvas.offsetLeft;
MINY = canvas.offsetTop;
MAXX = MINX+WIDTH;
MAXY = MINY+HEIGHT;
align_blocks();
canvas.onmousedown = ev_canvas;
canvas.onmouseup = ev_canvas;
canvas.onmousemove = ev_canvas;
canvas.ontouchstart = ev_canvas;
canvas.ontouchend = ev_canvas;
canvas.ontouchmove = ev_canvas;
document.onkeydown = ev_keyboard;
document.onkeyup = ev_keyboard;
if (canvas.getContext) {
ctx = canvas.getContext('2d');
intervalId = setInterval(draw, 20);
return intervalId;
}
}
var tree = new mathTree();
/*
This function updates the infix notation
to match any updates to the tree.
*/
function render_from_tree()
{
var newnodes = tree.printTree();
blocks = [];
for(var idx = 0; idx < newnodes.length; idx++){
var b = new Block(null);
b.node = newnodes[idx];
blocks.push(b);
}
align_blocks();
}
var treemode = 0;
function go_tree_mode()
{
render_from_tree();
if(treemode==0) {
var root = tree.root;
print_as_tree(root, -1, -2);
treemode = 1;
document.getElementById("Treemodebutton").value = "Infix";
}
else {
treemode = 0;
document.getElementById("Treemodebutton").value = "Treemode";
}
}
function print_as_tree(root, over, up)
{
var root2 = root;
while(root2.left)
{
root2 = root2.left;
over++;
}
var newblock = node_to_block(root);
newblock.x += 3*over;
newblock.y += 50*up;
if(root.left)
print_as_tree(root.left, 0, up+1);
if(root.right)
print_as_tree(root.right, over+1, up+1);
}
function i_to_p(input) {
//convert input into an array of blocks
blocks = []
for(var idx = 0; idx < input.length; idx++){
if(input[idx] == " ") { idx++; }
if( isalnum(input[idx]) ) {
var token = "";
while(idx < input.length){
if(!isalnum(input[idx])) break;
token += input[idx++];
}
blocks.push(new Block(token));
}
op = operators.indexOf(input[idx]);
if(op != -1 || input[idx] == '(' || input[idx] == ')'){
blocks.push(new Block(input[idx]));
}
}
var post = infix_to_postfix(blocks);
var str = "";
for(var i = 0; i < post.length; i++){
str += post[i].value + " ";
}
document.getElementById("ret1").innerHTML = str;
tree = new mathTree();
tree.buildTree(post, post.length-1);
render_from_tree();
treemode = 1;
go_tree_mode();
}
</script>
<style type="text/css">
body { margin: 20px;
font-family:
arial,verdana,helvetica;
background: #fff;}
h1 { font-size: 140%;
font-weight:
normal;
color: #036; border-bottom: 1px solid #ccc;
}
canvas { border: 2px solid #000; float: left; margin-right: 20px; margin-bottom: 20px; }
pre { float: left;
display: block;
background: rgb(238,238,238);
border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0;
}
</style>
</head>
<body onload="init();">
<div>
<form action="" name="text">
<h3>Infix->Postfix</h3>
<textarea name="input1"></textarea><br />
<input type="button" value="Submit" onclick="javascript:i_to_p(document.text.input1.value);" />
<input id="Treemodebutton" type="button" value="Treemode" onclick="javascript:go_tree_mode();" />
<br />
<br/>
<input id="f1" type="text" />
<input id="f2" type="text" />
<input type="button" value="Merge" onclick="javascript:startmerge(document.text.f1.value, document.text.f2.value)">
<div>
<canvas id="tutorial" width="640" height="320"></canvas>
</div>
<div id="ret1"></div>
<div id="firsttouch"></div>
<div id="secondtouch"></div>
</div>
</body>
</html>