-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
833 lines (711 loc) · 19.4 KB
/
functions.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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//<editor-fold desc="Menu Functions">
function enableOverlay()
{
document.getElementById("overlay").style.visibility = "visible";
}
function disableOverlay()
{
document.getElementById("overlay").style.visibility = "collapse";
}
function showMenu(id, title)
{
var target = document.getElementById(id);
var titleTarget = document.getElementById(id + "Title");
if (typeof title != "undefined")
titleTarget.innerHTML = title;
var children = target.childNodes;
for (var i = 0; i < children.length; i++)
{
if (children[i].tagName == "DIV")
{
var subChild = children[i].childNodes;
for (var z = 0; z < subChild.length; z++)
{
if (subChild[z].tagName == "INPUT")
{
subChild[z].focus();
z = subChild.length;
}
}
}
else if (children[i].tagName == "INPUT")
{
children[i].focus();
i = children.length;
}
}
target.style.left = "50%";
}
function hideMenu(id)
{
var target = document.getElementById(id);
target.style.left = "-1000px";
}
function menuOpen(id, title)
{
menuClose();
currentMenu = id;
enableOverlay();
showMenu(id, title);
}
function menuClose()
{
if (currentMenu != null)
{
menuHistory.push(currentMenu);
hideMenu(currentMenu);
currentMenu = null;
}
}
function menuExit()
{
if (currentMenu != null)
{
menuHistory = [];
hideMenu(currentMenu);
currentMenu = null;
disableOverlay();
}
}
function menuBack()
{
if (currentMenu != null)
{
hideMenu(currentMenu);
if (menuHistory.length > 0)
{
var prevMenu = menuHistory.pop();
currentMenu = prevMenu;
showMenu(prevMenu);
}
else
disableOverlay();
}
}
/**
* @param widgetId {Number}
* @param center {Array}
* @param left {Array}
* @param right {Array}
*/
function enableWidgetConfigModules(widgetId, center, left, right)
{
var i;
widgetConfigModuleContainer.center.innerHTML = "";
widgetConfigModuleContainer.left.innerHTML = "";
widgetConfigModuleContainer.right.innerHTML = "";
var updates = 0;
if (typeof center != "undefined" && center.length > 0)
{
//console.log(center);
for (i = 0; i < center.length; i++)
{
widgetConfigModuleContainer.center.appendChild(modules.getModule(center[i]));
}
modules.updateConfigFields(center, widgetId);
updates += 1;
}
if (typeof left != "undefined" && left.length > 0)
{
//console.log(left);
for (i = 0; i < left.length; i++)
{
widgetConfigModuleContainer.left.appendChild(modules.getModule(left[i]));
}
modules.updateConfigFields(left, widgetId);
updates += 1;
}
if (typeof right != "undefined" && right.length > 0)
{
//console.log(right);
for (i = 0; i < right.length; i++)
{
widgetConfigModuleContainer.right.appendChild(modules.getModule(right[i]));
}
modules.updateConfigFields(right, widgetId);
updates += 1;
}
return updates != 0;
}
//</editor-fold>
//<editor-fold desc="Context Menu Functions">
function contextMenuShow(e)
{
var element = document.getElementById("contextMenu");
element.style.top = e.clientY + "px";
element.style.left = e.clientX + "px";
}
function contextMenuHide()
{
var element = document.getElementById("contextMenu");
element.style.top = "-1000px";
element.style.left = "-1000px";
}
//</editor-fold>
//<editor-fold desc="Message Functions">
function addMessage(message)
{
var time = new Date().getTime();
var container = document.getElementById("messageBox");
var div = document.createElement("div");
div.className = "message";
div.innerHTML = message;
div.setAttribute("data-expires", time + messageTimeout);
container.appendChild(div);
}
function killMessages()
{
var target = document.getElementById("messageBox");
for (var i = 0; i < target.children.length; i++)
{
var value = target.children[i];
var time = new Date().getTime();
if (value.getAttribute("data-expires") < time)
target.removeChild(value);
}
}
//</editor-fold>
function applicationReset()
{
lastStartFocused = 0;
lastWidgetFocused = 0;
centerWidgetContainer();
}
/**
*
* @param url {String}
*/
function loadProgram(url)
{
if (typeof url == "undefined" || url == "" || url == "<? echo $p;?>")
{
console.log("Url not set");
return false;
}
$.post("get.program.php", {url: url})
.done(function (payload)
{
payload = payload.split(";");
var test = payload[0];
var content = payload[1];
if (test == "true")
{
parseProgramFromJson(content);
}
else
{
console.warn(content);
console.log("Test returned " + test + " for url \"" + url + "\"");
addMessage("An error occurred!");
}
})
.fail(function (payload)
{
console.warn("An error prevented the request from being sent (" + payload.status + ")");
addMessage("An error prevented the request from being sent (" + payload.status + ")");
});
}
/**
* Attempts to parse a json string into a program object
* @param program {String}
*/
function parseProgramFromJson(program)
{
var successes = 0;
var failures = 0;
widgetContainer.innerHTML = "<svg id=\"lineContainer\"></svg>";
Program.json = program;
Program.widgets = JSON.parse(Program.json).widgets;
var widgets = Program.widgets.value;
for (var i = 0; i < widgets.length; i++)
{
var object = widgets[i];
var altTexture = false;
if (object["multiplyDivide"])
altTexture = object["multiplyDivide"].value;
var result = createWidget(object.name.value, i, altTexture, object.x.value, object.y.value, defaultSpriteScale);
if (result)
successes++;
else
{
failures++;
console.warn("Failed to create piece \"" + object.name.value + "\"");
}
}
if (failures > 0)
{
addMessage("The program was loaded! Failed to load " + failures + " pieces!");
}
else
{
addMessage("The program was loaded!");
applicationReset();
}
console.log("Program.widgets.value:");
console.log(Program.widgets.value);
updateWidgetPositionList();
redrawWidgetConnections();
}
/**
*
* @param name {String}
* @param id {Number}
* @param altTexture {Boolean}
* @param x {Number}
* @param y {Number}
* @param defaultSpriteScale {Number}
* @returns {boolean}
*/
function createWidget(name, id, altTexture, x, y, defaultSpriteScale)
{
var container = document.getElementById("widgetContainer");
var div = document.createElement("div");
var sprite = widgets.getSprite(name, altTexture);
if (!sprite)
return false;
var size = widgets.getSize(name);
if (!size)
return false;
div.className = "programWidget";
div.style.backgroundImage = "url('" + sprite + "')";
div.style.top = y + "px";
div.style.left = x + "px";
div.style.height = size.height * defaultSpriteScale + "px";
div.style.width = size.width * defaultSpriteScale + "px";
div.id = "widget_" + id;
div.onmouseover = enableWidgetTooltip;
div.onmouseout = disableWidgetTooltip;
//div.onmouseup = widgetConfigMenu;
div.setAttribute("data-widget-name", name);
div.setAttribute("data-base-x", x);
div.setAttribute("data-base-y", y);
div.setAttribute("data-base-height", (size.height * defaultSpriteScale).toString());
div.setAttribute("data-base-width", (size.width * defaultSpriteScale).toString());
container.appendChild(div);
return true;
}
//<editor-fold desc="Zoom functions">
/**
*
* @param scale {Number}
*/
function setWidgetScale(scale)
{
var widgets = document.getElementsByClassName("programWidget");
for (var i = 0; i < widgets.length; i++)
{
var object = widgets[i];
var x = parseInt(object.getAttribute("data-base-x"));
var y = parseInt(object.getAttribute("data-base-y"));
var height = parseInt(object.getAttribute("data-base-height"));
var width = parseInt(object.getAttribute("data-base-width"));
var new_height = height * scale;
var new_width = width * scale;
var new_x = x * scale;
var new_y = y * scale;
object.style.left = new_x + "px";
object.style.top = new_y + "px";
object.style.height = new_height + "px";
object.style.width = new_width + "px";
}
}
function increaseScale()
{
var newScale = currentScale + scaleStep;
if (newScale <= maxScale)
{
setWidgetScale(newScale);
currentScale = newScale;
clearLines();
setTimeout(redrawWidgetConnections, 1000);
}
}
function decreaseScale()
{
var newScale = currentScale - scaleStep;
if (newScale >= minScale)
{
setWidgetScale(newScale);
currentScale = newScale;
clearLines();
setTimeout(redrawWidgetConnections, 1000);
}
}
//</editor-fold>
/**
*
* @param type {Number}
* @returns {String|boolean}
*/
function convertDataType(type)
{
switch (type)
{
case 1 :
return "number";
case 2 :
return "number";
case 3 :
return "number";
case 4 :
return "number";
case 5 :
return "number";
case 6 :
return "number";
case 7 :
return "object";
case 8 :
return "string";
case 9 :
return "object";
case 10 :
return false;
case 11 :
return false;
}
}
function centerWidgetContainer()
{
widgetContainer.style.top = window.innerHeight * .5 + "px";
widgetContainer.style.left = window.innerWidth * .5 + "px";
}
function getWindowCenter()
{
return {height: window.innerHeight * .5, width: window.innerWidth * .5};
}
function enableWidgetTooltip(e)
{
var name = e.target.getAttribute("data-widget-name");
var displayName = widgets.getName(name);
if (displayName == false)
return false;
elements[1].children[0].innerHTML = displayName;
var tooltip = widgets.getTooltip(parseInt(e.target.id.substring(7)));
if (typeof tooltip == "string")
elements[1].children[1].innerHTML = tooltip.nl2br();
else
elements[1].children[1].innerHTML = "";
tooltipEnabled = true;
}
function disableWidgetTooltip()
{
tooltipEnabled = false;
elements[1].style.top = "-1000px";
elements[1].style.left = "-1000px";
}
function focusOnWidgetPos(widgetId)
{
var newX;
var newY;
var widget = document.getElementById("widget_" + widgetId);
var thisWidget = Program.widgets.value[widgetId];
var screenCenterX = getWindowCenter().width;
var screenCenterY = getWindowCenter().height;
var widgetX = parseInt(widget.style.left);
var widgetY = parseInt(widget.style.top);
var widgetSize = widgets.getSize(thisWidget.name.value);
if (typeof widgetSize != "undefined" && widgetSize != false)
{
newX = screenCenterX + (widgetX * -1) - (widgetSize.width * (currentScale * defaultSpriteScale * 0.5));
newY = screenCenterY + (widgetY * -1) - (widgetSize.height * (currentScale * defaultSpriteScale * 0.5));
}
else
{
newX = screenCenterX + (widgetX * -1);
newY = screenCenterY + (widgetY * -1);
}
widgetContainer.style.top = newY + "px";
widgetContainer.style.left = newX + "px";
}
function focusFirstStart()
{
var widgets = Program.widgets.value;
for (var i = 0; i < widgets.length; i++)
{
if (widgets[i].name.value == "start")
{
lastWidgetFocused = i;
focusOnWidgetPos(i);
highlightFocus();
return true;
}
}
addMessage("No start piece found.");
return false;
}
function focusNextStart()
{
var widgets = Program.widgets.value;
for (var i = lastStartFocused + 1; i < widgets.length; i++)
{
if (widgets[i].name.value == "start")
{
lastWidgetFocused = i;
focusOnWidgetPos(i);
highlightFocus();
return true;
}
}
lastWidgetFocused = 0;
focusFirstStart();
}
function focusFirstLabel()
{
var widgets = Program.widgets.value;
for (var i = 0; i < widgets.length; i++)
{
if (widgets[i].name.value == "label")
{
lastWidgetFocused = i;
focusOnWidgetPos(i);
highlightFocus();
return true;
}
}
addMessage("No label piece found.");
return false;
}
function focusNextLabel()
{
var widgets = Program.widgets.value;
for (var i = lastWidgetFocused + 1; i < widgets.length; i++)
{
if (widgets[i].name.value == "label")
{
lastWidgetFocused = i;
focusOnWidgetPos(i);
highlightFocus();
return true;
}
}
lastWidgetFocused = 0;
focusFirstLabel();
}
function focusFirstWidget()
{
var widgets = Program.widgets.value;
lastWidgetFocused = 0;
focusOnWidgetPos(lastWidgetFocused);
highlightFocus();
return true;
}
function focusNextWidget()
{
var widgets = Program.widgets.value;
var i = lastWidgetFocused + 1;
if (i < widgets.length)
{
lastWidgetFocused = i;
focusOnWidgetPos(i);
highlightFocus();
return true;
}
focusFirstWidget();
}
function highlightFocus()
{
var element = document.getElementById("focusIndicator");
var tmp = element.style.transitionDuration;
element.style.visibility = "visible";
element.style.transitionDuration = "0s";
element.style.backgroundColor = "red";
setTimeout(function (element) {element.style.transitionDuration = null; element.style.backgroundColor = "transparent"; element.style.visibility = "collapse"}, 1000, element);
}
function getWidgetCurrentSize(widgetId)
{
var element = document.getElementById("widget_"+widgetId);
return {width: parseInt(element.style.width), height: parseInt(element.style.height)};
}
function getWidgetPos(widgetId)
{
var widgetArray = Program.widgets.value;
var thisWidget = widgetArray[widgetId];
return {x: thisWidget.x.value * currentScale, y: thisWidget.y.value * currentScale};
}
/**
* @param widgetId {Number}
* @param [widgetName] {String}
* @returns {{x: Number, y: Number}}
*/
function getWidgetCenterPos(widgetId, widgetName)
{
if (typeof widgetId != "number" || widgetId == null)
throw new Error("First parameter widgetId must be a number");
var widgetArray = Program.widgets.value;
var thisWidget = widgetArray[widgetId];
if (typeof widgetName == "undefined" || widgetName == null)
widgetName = thisWidget.name.value;
return {x: (getWidgetPos(widgetId).x + (getWidgetCurrentSize(widgetId).width * 0.5)), y: (getWidgetPos(widgetId).y + (getWidgetCurrentSize(widgetId).height * 0.5))};
}
/**
*
* @param x1 {Number}
* @param y1 {Number}
* @param x2 {Number}
* @param y2 {Number}
* @param [container] {Element}
* @returns {boolean}
*/
function drawLine(x1, y1, x2, y2, container)
{
if (typeof x1 == "undefined" || typeof y1 == "undefined" || typeof x2 == "undefined" || typeof y2 == "undefined")
return false;
if (typeof container == "undefined")
container = document.getElementById("lineContainer");
var line = "<line x1=\"{0}\" y1=\"{1}\" x2=\"{2}\" y2=\"{3}\" class=\"lineConnection\"></line>";
container.innerHTML += line.insert(x1, y1, x2, y2);
return true;
}
/**
*
* @param originWidgetId {Number}
* @param targetWidgetIds {Number|Array}
* @param [replace] {boolean}
*/
function addLinesToWidget(originWidgetId, targetWidgetIds, replace)
{
if (typeof replace == "undefined")
replace = false;
if (typeof originWidgetId != "number")
return;
if (typeof targetWidgetIds == "number")
targetWidgetIds = [targetWidgetIds];
var element = document.getElementById("widget_" + originWidgetId);
var container = document.getElementById("lineContainer");
var widgetArray = Program.widgets.value;
if (replace)
container.innerHTML = "";
var lineStart = getWidgetCenterPos(originWidgetId);
for (var i = 0; i < targetWidgetIds.length; i++)
{
var targetWidgetId = targetWidgetIds[i];
var lineEnd = getWidgetCenterPos(targetWidgetId);
//console.log("LineStart: x:" + lineStart.x + " y:" + lineStart.y + ", LineEnd: x:" + lineEnd.x + " y:" + lineEnd.y);
drawLine(lineStart.x, lineStart.y, lineEnd.x, lineEnd.y, container);
}
}
function clearLines()
{
document.getElementById("lineContainer").innerHTML = "";
}
function updateWidgetPositionList()
{
widgetPositionList = [];
var widgetArray = Program.widgets.value;
for (var i = 0; i < widgetArray.length; i++)
{
var widget = widgetArray[i];
var x = widget.x.value;
var y = widget.y.value;
var id = i;
if (typeof widgetPositionList[y] == "undefined")
widgetPositionList[y] = [];
widgetPositionList[y][x] = id;
}
}
function redrawWidgetConnections()
{
var widgetArray = Program.widgets.value;
var parents = [];
clearLines();
for (var i = 0; i < widgetArray.length; i++)
{
var widget = widgetArray[i];
if (widgets.widgetIsValidLineRoot(i) != false)
{
parents.push({arguments: widgets.getArguments(i), id: i});
}
}
console.log("Redrawing lines! Found " + parents.length + " valid line roots");
for (var c = 0; c < parents.length; c++)
{
var thisArguments = parents[c];
var widgetType = widgets.widgetIsValidLineRoot(thisArguments.id);
if (widgetType != false)
{
if (thisArguments.arguments.argumentsFound > 0)
{
var argumentIdLeft = false;
var argumentIdRight = false;
if (widgetType == 1)
{
argumentIdLeft = thisArguments.arguments.left.level1[0];
argumentIdRight = thisArguments.arguments.right.level1[0];
}
else if (widgetType == 2)
{
argumentIdLeft = thisArguments.arguments.left.level2[0];
argumentIdRight = thisArguments.arguments.right.level2[0];
}
else if (widgetType == 3)
{
argumentIdLeft = thisArguments.arguments.left.level3[0];
argumentIdRight = thisArguments.arguments.right.level3[0];
}
if (typeof argumentIdLeft == "number")
{
var targetArgumentsLeft = findTextWidgetByString(widgetArray[argumentIdLeft].string.value);
for (var o = 0; o < targetArgumentsLeft.length; o++)
{
var targetLeft = widgets.getParentOfArgument(targetArgumentsLeft[o]);
if (targetLeft != false && widgets.widgetIsValidLineTarget(targetLeft))
{
addLinesToWidget(thisArguments.id, targetLeft);
}
}
}
if (typeof argumentIdRight == "number")
{
var targetArgumentsRight = findTextWidgetByString(widgetArray[argumentIdRight].string.value);
for (var p = 0; p < targetArgumentsRight.length; p++)
{
var targetRight = widgets.getParentOfArgument(targetArgumentsRight[p]);
if (targetRight != false && widgets.widgetIsValidLineTarget(targetRight))
{
addLinesToWidget(thisArguments.id, targetRight);
}
}
}
}
else
console.log("Widget has no arguments");
}
else
console.log("Invalid widget type");
}
}
/**
* Attempts to locate a text piece by it's string value. Returns widget id on success or false on failure
* @param string {String}
* @returns {Array}
*/
function findTextWidgetByString(string)
{
var widgetArray = Program.widgets.value;
var widgets = [];
for (var i = 0; i < widgetArray.length; i++)
{
var thisWidget = widgetArray[i];
if (typeof thisWidget != "undefined" && thisWidget.name != "undefined" && thisWidget.name.value == "text")
{
if (typeof thisWidget.string != "undefined" && thisWidget.string.value == string)
widgets.push(i);
}
}
return widgets;
}
function focusOnTextWidgetWithContent(string)
{
if (typeof string != "undefined" && string != "")
{
var widgets = findTextWidgetByString(string);
if (widgets.length > 0)
{
focusOnWidgetPos(widgets[0]);
highlightFocus();
}
else
addMessage("No matching widgets found.");
}
}