-
Notifications
You must be signed in to change notification settings - Fork 3
/
canvasmenu.js
3983 lines (3312 loc) · 119 KB
/
canvasmenu.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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// html5 canvas menu library
// LifeViewer UI library.
/*
This file is part of LifeViewer
Copyright (C) 2015-2024 Chris Rowett
LifeViewer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// TextAlert
/**
* @constructor
*/
function TextAlert(/** @type {number} */ appear, /** @type {number} */ hold, /** @type {number} */ disappear, /** @type {CanvasRenderingContext2D} */ context, /** @type {MenuManager} */ menuManager) {
// menu manager
/** @type {MenuManager} */ this.menuManager = menuManager;
// steps for text to appear
/** @type {number} */ this.textAppear = appear;
// steps to hold text
/** @type {number} */ this.textHold = hold;
// steps for text to disappear
/** @type {number} */ this.textDisappear = disappear;
// current text message
/** @type {string} */ this.message = "";
// priority message steps
/** @type {number} */ this.priorityAppear = appear;
/** @type {number} */ this.priorityHold = hold;
/** @type {number} */ this.priorityDisappear = disappear;
// current priority message
/** @type {string} */ this.priorityMessage = "";
// flag for pending clear used if message is reset before disappear
/** @type {boolean} */ this.pendingNormalClear = false;
/** @type {boolean} */ this.pendingPriorityClear = false;
// drawing context
/** @type {CanvasRenderingContext2D} */ this.context = context;
// whether notification has background
/** @type {boolean} */ this.txtBg = false;
// start time for current notification
/** @type {number} */ this.startTime = 0;
// start time for priority notification
/** @type {number} */ this.priorityStart = 0;
// colour for notifications
/** @type {string} */ this.colour = "rgb(32,255,255)";
// colour for priority notifications
/** @type {string} */ this.priorityColour = "white";
// iterator for animated priority notification colour
/** @type {number} */ this.priorityIter = 0;
// whether animation required for priority notification
/** @type {boolean} */ this.animate = false;
// shadow colour for notifications
/** @type {string} */ this.shadowColour = "black";
// background colour for notifications
/** @type {string} */ this.backgroundColour = "black";
// whether notifications are enabled
/** @type {boolean} */ this.enabled = false;
// default font size
/** @type {number} */ this.defaultFontSize = 30;
// vertical adjust for notifitions
/** @type {number} */ this.notificationYOffset = 45;
// scale
/** @type {number} */ this.scale = 1;
}
// return whether a notification is displayed
/** @returns {boolean} */
TextAlert.prototype.displayed = function() {
var /** @type {boolean} */ result = false;
// check if standard or priority message are displayed
if (this.message !== "" || this.priorityMessage !== "") {
result = true;
}
// return the flag
return result;
};
// clear notification
TextAlert.prototype.clear = function(/** @type {boolean} */ priority, /** @type {boolean} */ immediately) {
var /** @type {number} */ elapsed = 0;
if (priority) {
// check if priority message is displayed
if (this.priorityMessage !== "") {
// check if immediate clear is required
if (immediately || this.pendingPriorityClear) {
this.priorityMessage = "";
this.pendingPriorityClear = false;
} else {
// switch to disappear section
elapsed = performance.now() - this.priorityStart;
if (elapsed < this.priorityAppear + this.priorityHold) {
this.priorityStart = performance.now() - (this.priorityAppear + this.priorityHold);
}
this.pendingPriorityClear = true;
}
}
} else {
// check normal message
if (this.message !== "") {
// check if immediate clear is required
if (immediately || this.pendingNormalClear) {
this.pendingNormalClear = false;
this.message = "";
} else {
// switch to disappear section
elapsed = performance.now() - this.startTime;
if (elapsed < this.textAppear + this.textHold) {
this.startTime = performance.now() - (this.textAppear + this.textHold);
}
this.pendingNormalClear = true;
}
}
}
};
// create notification
TextAlert.prototype.notify = function(/** @type {string} */ message, /** @type {number} */ appear, /** @type {number} */ hold, /** @type {number} */ disappear, /** @type {boolean} */ priority) {
// create the notification if enabled
if (this.enabled) {
// check if this is a priority message
if (priority) {
// check if message already displayed
if (this.priorityMessage !== message) {
// set priority message
this.priorityMessage = message;
// convert frames to milliseconds
this.priorityAppear = appear * 16;
this.priorityHold = hold * 16;
this.priorityDisappear = disappear * 16;
// set start time to now
this.priorityStart = performance.now();
this.pendingPriorityClear = false;
// clear the priority iterator for colour animation
this.priorityIter = 0;
// disable animation
this.animate = false;
}
} else {
// set normal message
this.message = message;
// convert frames to milliseconds
this.textAppear = appear * 16;
this.textHold = hold * 16;
this.textDisappear = disappear * 16;
// set the start time to now
this.startTime = performance.now();
this.pendingNormalClear = false;
}
}
};
// set animated colour for notification
TextAlert.prototype.setAnimatedColour = function() {
var /** @type {number} */ dR = 0,
/** @type {number} */ dG = 0,
/** @type {number} */ dB = 0,
/** @type {number} */ percent = 0;
if (this.priorityIter < 128) {
percent = this.priorityIter / 128;
} else {
percent = (255 - this.priorityIter) / 128;
}
dR = this.menuManager.fgR * percent + this.menuManager.selectR * (1 - percent);
dG = this.menuManager.fgG * percent + this.menuManager.selectG * (1 - percent);
dB = this.menuManager.fgB * percent + this.menuManager.selectB * (1 - percent);
this.context.fillStyle = "rgb(" + dR + "," + dG + "," + dB + ")";
};
// draw notification string
TextAlert.prototype.draw = function(/** @type {string} */ message, /** @type {boolean} */ isPriority, /** @type {number} */ lineHeight) {
var /** @type {number} */ xPos = 0,
// alpha for background
/** @type {number} */ alpha = 0;
// compute x position to center text horizontally
xPos = this.context.measureText(message).width >> 1;
// draw background if required
if (this.txtBg) {
this.context.fillStyle = this.backgroundColour;
alpha = this.context.globalAlpha;
this.context.globalAlpha = alpha * 0.5;
this.context.fillRect(-xPos - 20, -lineHeight, xPos * 2 + 40, lineHeight * 2);
this.context.globalAlpha = alpha;
}
// draw shadow
this.context.fillStyle = this.shadowColour;
this.context.fillText(message, -xPos + 2, 2);
// draw string
if (isPriority) {
// check if animated colour needed
if (this.animate) {
this.setAnimatedColour();
this.priorityIter = (this.priorityIter + 4) & 255;
} else {
this.context.fillStyle = this.priorityColour;
}
} else {
this.context.fillStyle = this.colour;
}
this.context.fillText(message, -xPos, 0);
};
// update notification
/** @returns {boolean} */
TextAlert.prototype.updateNotification = function(/** @type {string} */ message, /** @type {number} */ appear, /** @type {number} */ hold, /** @type {number} */ disappear, /** @type {number} */ start, /** @type {number} */ offset, /** @type {boolean} */ isPriority) {
var /** @type {number} */ scaleFactor = 0,
/** @type {number} */ elapsedTime = 0,
/** @type {number} */ index = 0,
// default font size when not in thumbnail mode
/** @type {number} */ fontSize = this.defaultFontSize * this.scale,
// line height
/** @type {number} */ lineHeight = (this.defaultFontSize + 2) * this.scale,
// thumbnail divisor
/** @type {number} */ thumbDivisor = this.menuManager.thumbnailDivisor,
// number of pixels from top of display
/** @type {number} */ fromTop = 60 * this.scale,
// flag whether to clear message
/** @type {boolean} */ clearMessage = true;
// check for noGUI
if (this.menuManager.noGUI) {
fromTop = 0;
offset = this.context.canvas.height / 2;
this.txtBg = true;
} else {
this.txtBg = false;
}
// check if there is anything to draw
if (message !== "") {
// flag there is nothing to clear
clearMessage = false;
// compute the elapsed time
elapsedTime = performance.now() - start;
// save context
this.context.save();
// check for thumbnail mode
if (this.menuManager.thumbnail) {
// check for system messages
if (isPriority) {
// vertically center the system message
fromTop = 0;
offset = this.context.canvas.height / 2;
} else {
// reduce the font size by the thumbnail divisor
fontSize = (fontSize / thumbDivisor);
lineHeight = (lineHeight / thumbDivisor) | 0;
// reduce the offset
offset = (offset / thumbDivisor) | 0;
// reduce the pixels from top of display
fromTop = (fromTop / thumbDivisor) | 0;
}
}
// set the font size
this.context.font = (fontSize | 0) + "px Arial";
// scale based on appear or disappear
this.context.translate(this.context.canvas.width / 2 - 1, fromTop + offset);
// check for appear
if (elapsedTime <= appear) {
// zoom in and increase alpha
scaleFactor = (elapsedTime / appear);
scaleFactor *= scaleFactor;
this.context.globalAlpha = scaleFactor;
this.context.scale(scaleFactor, scaleFactor);
} else {
// check for disappear
if (elapsedTime > (appear + hold) && elapsedTime <= (appear + hold + disappear)) {
// zoom out and decrease alpha
scaleFactor = (disappear - (elapsedTime - (appear + hold))) / disappear;
scaleFactor *= scaleFactor;
this.context.globalAlpha = scaleFactor;
this.context.scale(scaleFactor, scaleFactor);
}
}
// check if finished
if (elapsedTime > (appear + hold + disappear)) {
// set the clear message flag
clearMessage = true;
} else {
// check if the message contains a newline
index = message.indexOf("\\n");
if (index === -1) {
// draw the text
this.draw(message, isPriority, lineHeight);
} else {
// draw the first line
this.draw(message.substring(0, index), isPriority, lineHeight);
// go to next line
this.context.translate(0, lineHeight);
// draw the second line
this.draw(message.substring(index + 2), isPriority, lineHeight);
}
}
// restore context
this.context.restore();
}
// return the clear message flag
return clearMessage;
};
// update notification
TextAlert.prototype.update = function() {
var /** @type {number} */ yScale = this.menuManager.currentMenu.yScale;
// update the standard message
if (this.updateNotification(this.message, this.textAppear, this.textHold, this.textDisappear, this.startTime, (36 + this.notificationYOffset) * yScale, false)) {
this.message = "";
}
// update the priority message
if (!this.menuManager.thumbnail || this.priorityMessage === "Expand" || this.priorityMessage === "Launch") {
if (this.updateNotification(this.priorityMessage, this.priorityAppear, this.priorityHold, this.priorityDisappear, this.priorityStart, (this.notificationYOffset * yScale), true)) {
this.priorityMessage = "";
}
}
};
// Icon
/**
* @constructor
*/
function Icon(/** @type {string} */ name, /** @type {number} */ width, /** @type {number} */ height, /** @type {number} */ number) {
/** @type {string} */ this.name = name;
/** @type {number} */ this.width = width;
/** @type {number} */ this.height = height;
/** @type {number} */ this.number = number;
}
// IconManager
/**
* @constructor
*/
function IconManager(/** @type {CanvasRenderingContext2D} */ iconsImage, /** @type {CanvasRenderingContext2D} */ context) {
// save the drawing context
/** @type {CanvasRenderingContext2D} */ this.context = context;
// save the icon image
/** @type {CanvasRenderingContext2D} */ this.iconsImage = iconsImage;
/** @type {number} */ this.width = 0;
/** @type {number} */ this.height = 0;
/** @type {HTMLCanvasElement} */ this.iconCanvas = null;
/** @type {CanvasRenderingContext2D} */ this.iconContext = null;
/** @type {CanvasRenderingContext2D} */ this.convertedImage = null;
/** @type {CanvasRenderingContext2D} */ this.greyedOutImage = null;
/** @type {Uint32Array} */ this.iconData32 = null;
// list of icons
/** @type {Array} */ this.iconList = [];
// whether icons need recolouring
/** @type {boolean} */ this.recolour = true;
// recolour shade
/** @type {number} */ this.recolourCol = 0xffffffff;
// recolour grid shade
/** @type {number} */ this.recolourGrid = 0xffffffff;
// shadow shade
/** @type {number} */ this.shadowCol = 0;
// greyed out shade
/** @type {number} */ this.greyedOutCol = 0;
// whether initialised
/** @type {boolean} */ this.init = false;
// scale
/** @type {number} */ this.scale = 1;
}
// set scale
IconManager.prototype.setScale = function(/** @type {number} */ scale) {
/** @type {number} */ this.scale = scale;
};
// draw icon
IconManager.prototype.draw = function(/** @type {Icon} */ icon, /** @type {number} */ x, /** @type {number} */ y, /** @type {boolean} */ locked) {
var /** @type {ImageData} */ data = null,
/** @type {Uint32Array} */ data32 = null,
/** @type {Uint32Array} */ dest32 = null,
/** @type {number} */ i = 0,
/** @type {number} */ j = 0,
/** @type {number} */ destIndex = 0,
/** @type {number} */ sourceIndex = 0,
/** @type {number} */ numIcons = this.iconList.length,
/** @type {number} */ iconWidth = 40,
/** @type {number} */ iconHeight = 40,
/** @type {number} */ ix = 0,
/** @type {number} */ iy = 0,
/** @type {number} */ shadowCol = this.shadowCol,
/** @type {number} */ iconFGHex = 0xffffffff,
/** @type {number} */ iconFG = 255 << 24 | 255 << 16 | 255 << 8 | 255,
/** @type {HTMLCanvasElement} */ canvas = null;
// check if image load
if (this.iconsImage.canvas.width > 0) {
if (!this.init) {
this.width = this.iconsImage.canvas.width;
this.height = this.iconsImage.canvas.height;
// create a context the same size as the image and draw the image onto it
this.iconCanvas = /** @type {!HTMLCanvasElement} */ (document.createElement("canvas"));
this.iconCanvas.width = this.width;
this.iconCanvas.height = this.height;
this.iconContext = /** @type {!CanvasRenderingContext2D} */ (this.iconCanvas.getContext("2d"));
// create a new image for the converted colours
canvas = /** @type {!HTMLCanvasElement} */ (document.createElement("canvas"));
canvas.width = this.width;
canvas.height = this.height;
this.convertedImage = /** @type {!CanvasRenderingContext2D} */ (canvas.getContext("2d"));
// create a new image for the greyed out icons
canvas = /** @type {!HTMLCanvasElement} */ (document.createElement("canvas"));
canvas.width = this.width;
canvas.height = this.height;
this.greyedOutImage = /** @type {!CanvasRenderingContext2D} */ (canvas.getContext("2d"));
// get the pixel data from the loaded icons
this.iconContext.clearRect(0, 0, this.iconCanvas.width, this.iconCanvas.height);
this.iconContext.drawImage(this.iconsImage.canvas, 0, 0);
data = this.iconContext.getImageData(0, 0, this.iconCanvas.width, this.iconCanvas.height);
this.iconData32 = new Uint32Array(data.data.buffer);
}
// change the pixel colours if required
if (this.recolour) {
// get the pixel data from the loaded icons
data32 = new Uint32Array(this.iconData32);
// ensure shadow colour is different than foreground colour
if (shadowCol === iconFG) {
shadowCol = 0xfffefeff;
}
// create the icon shadows
j = 0;
for (i = 0; i < numIcons; i += 1) {
for (iy = iconHeight - 2; iy > 1; iy -= 1) {
destIndex = j + iy * this.width;
sourceIndex = destIndex - 2 - this.width - this.width;
for (ix = iconWidth - 2; ix > 1; ix -= 1) {
if (data32[destIndex + ix] === 0) {
if (data32[sourceIndex + ix] === iconFGHex) {
data32[destIndex + ix] = shadowCol;
}
}
}
}
j += iconWidth;
}
// change target pixel to greyed out colour
for (i = 0; i < data32.length; i += 1) {
if (data32[i] === iconFGHex || data32[i] === 0xff707070) {
data32[i] = this.greyedOutCol;
}
}
// create the greyed out icons
data = this.greyedOutImage.createImageData(this.greyedOutImage.canvas.width, this.greyedOutImage.canvas.height);
dest32 = new Uint32Array(data.data.buffer);
dest32.set(data32);
this.greyedOutImage.putImageData(data, 0,0);
// get the pixel data from the loaded icons again
data32 = new Uint32Array(this.iconData32);
// create the icon shadows
j = 0;
for (i = 0; i < numIcons; i += 1) {
for (iy = iconHeight - 2; iy > 1; iy -= 1) {
destIndex = j + iy * this.width;
sourceIndex = destIndex - 2 - this.width - this.width;
for (ix = iconWidth - 2; ix > 1; ix -= 1) {
if (data32[destIndex + ix] === 0) {
if (data32[sourceIndex + ix] === 0xffffffff) {
data32[destIndex + ix] = shadowCol;
}
}
}
}
j += iconWidth;
}
// change target pixel to new colour
for (i = 0; i < data32.length; i += 1) {
if (data32[i] === 0xffffffff) {
data32[i] = this.recolourCol;
} else {
if (data32[i] === 0xff707070) {
data32[i] = this.recolourGrid;
}
}
}
// put back the pixel data
data = this.iconContext.createImageData(this.iconContext.canvas.width, this.iconContext.canvas.height);
dest32 = new Uint32Array(data.data.buffer);
dest32.set(data32);
this.iconContext.putImageData(data, 0, 0);
this.recolour = false;
this.init = false;
}
if (!this.init) {
// create a new image with the updated colours
this.convertedImage.putImageData(data, 0, 0);
this.init = true;
}
// draw the icon onto the canvas
if (this.scale !== 1) {
this.context.save();
this.context.translate(x, y);
this.context.scale(this.scale, this.scale);
this.context.imageSmoothingEnabled = true;
x = 0;
y = 0;
}
if (locked) {
this.context.drawImage(this.greyedOutImage.canvas, icon.number * icon.width, 0, icon.width, icon.height, x, y, icon.width, icon.height);
} else {
this.context.drawImage(this.convertedImage.canvas, icon.number * icon.width, 0, icon.width, icon.height, x, y, icon.width, icon.height);
}
if (this.scale !== 1) {
this.context.restore();
this.context.imageSmoothingEnabled = false;
}
}
};
// return number of icons
/** @returns {number} */
IconManager.prototype.length = function() {
return this.iconList.length;
};
// return the named icon
/** @returns {Icon} */
IconManager.prototype.icon = function(/** @type {string} */ name) {
var /** @type {number} */ a,
/** @type {Array<Icon>} */ i = this.iconList,
/** @type {number} */ l = this.length(),
/** @type {Icon} */ result = null;
// search the list for the named icon
a = 0;
while (a < l && !result) {
if (i[a].name === name) {
result = i[a];
} else {
a += 1;
}
}
return result;
};
// add an icon to the list
IconManager.prototype.add = function(/** @type {string} */ name, /** @type {number} */ width, /** @type {number} */ height) {
// create new icon
var /** @type {number} */ iconNum = this.iconList.length,
/** @type {Icon} */ newIcon = new Icon(name, width, height, iconNum);
// add to the list of icons
this.iconList[iconNum] = newIcon;
};
// constants
var Menu = {
// orientations
auto : 0,
horizontal : 1,
vertical : 2,
// menu types
range : 0,
button : 1,
progressBar : 2,
list : 3,
label : 4,
// list selection modes
single : 0,
multi : 1,
// text alignment
left : 0,
center : 1,
right : 2,
// control position
north : 0,
northEast : 1,
east : 2,
southEast : 3,
south : 4,
southWest : 5,
west : 6,
northWest : 7,
middle : 8
};
// MenuItem
/**
* @constructor
*/
function MenuItem(/** @type {number} */ id, callback, /** @type {View} */ caller, /** @type {number} */ position, /** @type {number} */ x, /** @type {number} */ y, /** @type {number} */ width, /** @type {number} */ height,
/** @type {number|Array<string>} */ lower, /** @type {number} */ upper, /** @type {number|Array<boolean>} */ current, /** @type {number} */ type, /** @type {number} */ orientation, /** @type {boolean} */ valueDisplay, /** @type {string} */ preText, /** @type {string} */ postText, /** @type {number} */ fixed, icon, /** @type {MenuList} */ owner) {
var /** @type {number} */ i = 0;
// id
/** @type {number} */ this.id = id;
// owning menu
/** @type {MenuList} */ this.owner = owner;
// tool tip
/** @type {string|Array<string>} */ this.toolTip = null;
// item in a multi-select that is highlighted
/** @type {number} */ this.highlightItem = -1;
// whether multi-select highlight item is locked
/** @type {boolean} */ this.highlightLocked = false;
// whether deleted
/** @type {boolean} */ this.deleted = false;
// callback
this.callback = callback;
// caller
/** @type {View} */ this.caller = caller;
// icon (list)
this.icon = icon;
// absolute position top left
/** @type {number} */ this.x = x;
/** @type {number} */ this.y = y;
// relative position
/** @type {number} */ this.relX = x;
/** @type {number} */ this.relY = y;
// position type
/** @type {number} */ this.position = position;
// width and height
/** @type {number} */ this.width = width;
/** @type {number} */ this.height = height;
// relative width and height
/** @type {number} */ this.relWidth = width;
/** @type {number} */ this.relHeight = height;
// type of menu item
/** @type {number} */ this.type = type;
// menu orientation
/** @type {number} */ this.orientation = orientation;
if (orientation === Menu.auto) {
// select orientation based on width and height
if (width >= height) {
this.orientation = Menu.horizontal;
} else {
this.orientation = Menu.vertical;
}
}
// text orientation
/** @type {number} */ this.textOrientation = this.orientation;
// value range represented (order is important)
this.lower = lower;
/** @type {number} */ this.upper = upper;
// current value
if (type === Menu.range) {
// allocate value and display value
this.current = [current, current];
} else {
this.current = current;
}
// whether to display the value
/** @type {boolean} */ this.valueDisplay = valueDisplay;
// text to prefix to the value
/** @type {string} */ this.preText = preText;
// text to postfix after the value
/** @type {string} */ this.postText = postText;
// decimal places for the value (-1 not numeric)
/** @type {number} */ this.fixed = fixed;
// flag for mouse down last update
/** @type {boolean} */ this.lastMouseDown = false;
// last mouse position
/** @type {number} */ this.lastMouseX = -1;
/** @type {number} */ this.lastMouseY = -1;
// text alignment
/** @type {number} */ this.textAlign = Menu.center;
// whether enabled
/** @type {boolean} */ this.enabled = true;
// whether locked
/** @type {boolean} */ this.locked = false;
// whether unlock override is on
/** @type {boolean} */ this.overrideLocked = false;
// whether individual list items are locked
/** @type {Array<boolean>} */ this.itemLocked = [];
if (this.type === Menu.list) {
for (i = 0; i < /** @type {!Array} */ (lower).length; i += 1) {
this.itemLocked[i] = false;
}
}
// toggle menu parents
/** @type {Array} */ this.toggleMenuParents = [];
// number of toggle menu items
/** @type {number} */ this.numToggleMenuParents = 0;
// invert rule
/** @type {boolean} */ this.invert = false;
// whether to cascade toggle menu
/** @type {boolean} */ this.cascade = true;
// background colour and alpha
/** @type {string} */ this.bgCol = owner.bgCol;
/** @type {number} */ this.bgAlpha = owner.bgAlpha;
// set background colour list
if (type === Menu.list) {
/** @type {Array<string>} */ this.bgColList = [];
for (i = 0; i < /** @type {!Array} */ (lower).length; i += 1) {
this.bgColList[i] = this.bgCol;
}
}
// foreground colour and alpha
/** @type {string} */ this.fgCol = owner.fgCol;
/** @type {number} */ this.fgAlpha = owner.fgAlpha;
// highlight colour and alpha when mouse is over item
/** @type {string} */ this.hlCol = owner.hlCol;
/** @type {number} */ this.hlAlpha = owner.hlAlpha;
// selected colour and alpha when item selected
/** @type {string} */ this.selectedCol = owner.selectedCol;
/** @type {number} */ this.selectedAlpha = owner.selectedAlpha;
// locked colour and alpha when item locked
/** @type {string} */ this.lockedCol = owner.lockedCol;
/** @type {number} */ this.lockedAlpha = owner.lockedAlpha;
// border colour and alpha
/** @type {string} */ this.borderCol = owner.borderCol;
/** @type {number} */ this.borderAlpha = owner.borderAlpha;
// border thickness (or 0 for no border)
/** @type {number} */ this.border = owner.border;
// decompose the font into size and family
/** @type {number} */ this.fontSize = parseInt(owner.defaultFont.substring(0, owner.defaultFont.indexOf("px")), 10);
/** @type {string} */ this.fontFamily = owner.defaultFont.substring(owner.defaultFont.indexOf("px") + 3);
}
// delete if shown
MenuItem.prototype.deleteIfShown = function(/** @type {boolean} */ del) {
if (!this.deleted && del) {
this.deleted = true;
}
};
// set font
MenuItem.prototype.setFont = function(/** @type {string} */ font) {
this.fontSize = parseInt(font.substring(0, font.indexOf("px")), 10);
this.fontFamily = font.substring(font.indexOf("px") + 3);
};
// set item foreground and background colour
MenuItem.prototype.setColours = function(/** @type {string} */ fg, /** @type {string} */ bg, /** @type {string} */ highlight, /** @type {string} */ selected, /** @type {string} */ locked, /** @type {string} */ border) {
var /** @type {number} */ i = 0;
this.fgCol = fg;
this.bgCol = bg;
this.hlCol = highlight;
this.selectedCol = selected;
this.lockedCol = locked;
this.borderCol = border;
if (this.type === Menu.list) {
this.bgColList = [];
for (i = 0; i < /** @type {!Array} */ (this.lower).length; i += 1) {
this.bgColList[i] = this.bgCol;
}
}
};
// set border width
MenuItem.prototype.setBorder = function(/** @type {number} */ border) {
this.border = border;
};
// set a new width
MenuItem.prototype.setWidth = function(/** @type {number} */ width) {
this.relWidth = width;
};
// set a new height
MenuItem.prototype.setHeight = function(/** @type {number} */ height) {
this.relHeight = height;
};
// set a new X coordinate
MenuItem.prototype.setX = function(/** @type {number} */ x) {
this.relX = x;
};
// set a new Y coordinate
MenuItem.prototype.setY = function(/** @type {number} */ y) {
this.relY = y;
};
// set a new absolute position
MenuItem.prototype.setPosition = function(/** @type {number} */ position, /** @type {number} */ x, /** @type {number} */ y) {
this.position = position;
this.relX = x;
this.relY = y;
};
// calculate item position
MenuItem.prototype.calculatePosition = function(/** @type {number} */ width, /** @type {number} */ height) {
var /** @type {number} */ xScale = this.owner.xScale,
/** @type {number} */ yScale = this.owner.yScale,
/** @type {number} */ relXS = this.relX * xScale,
/** @type {number} */ relYS = this.relY * yScale,
/** @type {number} */ relWidthS = this.relWidth * xScale,
/** @type {number} */ relHeightS = this.relHeight * yScale;
// copy the absolute position from the relative position
this.x = relXS;
this.y = relYS;
this.width = relWidthS;
this.height = relHeightS;
// override x or y based on position type
switch (this.position) {
// north (top of display)
case Menu.north:
// center x
this.x = ((width - relWidthS) >> 1) + relXS;
break;
// north east (top right of display)
case Menu.northEast:
// make x relative to right of display
this.x = width + relXS;
break;
// east (right of display)
case Menu.east:
// center y
this.y = ((height - relHeightS) >> 1) + relYS;
// make x relative to right of display
this.x = width + relXS;
break;
// south east (bottom right of display)
case Menu.southEast:
// make x relative to right of display
this.x = width + relXS;
// make y relative to bottom of display
this.y = height + relYS;
break;
// south (bottom of display)
case Menu.south:
// center x
this.x = ((width - relWidthS) >> 1) + relXS;
// make y relative to bottom of display
this.y = height + relYS;
break;
// south west (bottom left of display)
case Menu.southWest:
// make y relative to bottom of display
this.y = height + relYS;
break;
// west (left of display)
case Menu.west:
// center y
this.y = ((height - relHeightS) >> 1) + relYS;
break;
// north west (top left of display)
case Menu.northWest:
// nothing to do since top left is always row 0, column 0
break;
// middle
case Menu.middle:
// make x and y relative to middle of display
this.x = ((width - relWidthS) >> 1) + relXS;
this.y = ((height - relHeightS) >> 1) + relYS;
break;
// ignore others
default:
break;
}
};
// add a toggle menu parent
MenuItem.prototype.addToggleMenuParent = function(/** @type {MenuItem} */ parentItem, /** @type {boolean} */ cascade) {
var /** @type {number} */ n = this.numToggleMenuParents;
// save the parent in the list and increment number
this.toggleMenuParents[n] = [parentItem, cascade];
n += 1;