-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
1290 lines (945 loc) · 37.7 KB
/
index.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
/*
Require dependencies
*/
var createShader = require("gl-shader");
var mat4 = require("gl-mat4");
var createTexture = require('gl-texture2d');
var hashString = require('hash-string');
var clamp = require('clamp');
var createBuffer = require('gl-buffer');
/*
Require resources.
*/
var fontInfo = require("./font_info.js");
var fontAtlas = require("./font_atlas.js");
var shaders = require("./shaders.js");
/*
Constructor
*/
function GUI(gl) {
/*
We use this single shader to render the GUI.
*/
this.shader = createShader(gl, shaders.vert, shaders.frag);
/*
These buffers contain all the geometry data.
*/
this.positionBufferObject = createBuffer(gl, [], gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
this.colorBufferObject = createBuffer(gl, [], gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
this.uvBufferObject = createBuffer(gl, [], gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
this.indexBufferObject = createBuffer(gl, [], gl.ELEMENT_ARRAY_BUFFER, gl.DYNAMIC_DRAW);
this._setupDefaultSettings();
this.fontAtlasTexture = createTexture(gl, fontAtlas);
this.fontAtlasTexture.magFilter = gl.LINEAR;
this.fontAtlasTexture.minFilter = gl.LINEAR;
/*
DO NOT CHANGE THIS VALUE. The entire GUI layout will break!
*/
this.textScale = 1.0;
/*
Keeps track of the ID of the widget that is currently being pressed down.
We need to keep track of this, because otherwise we can't, for instance, affect the value of a
slider while the mouse is OUTSIDE the hitbox of the slider.
*/
this.activeWidgetId = null;
/*
See _moveWindowCaret() for an explanation.
*/
this.sameLineActive = false;
this.prevWidgetSizes = null;
}
GUI.prototype.sameLine = function () {
this.sameLineActive = true;
};
GUI.prototype.sliderFloat = function (str, value, min, max, numDecimalDigits) {
if(typeof numDecimalDigits === 'undefined') {
numDecimalDigits = 2; // default value
}
this._slider(str, value, min, max, false, numDecimalDigits);
};
GUI.prototype.sliderInt = function (str, value, min, max) {
this._slider(str, value, min, max, true, 0);
};
GUI.prototype.draggerFloat4 = function (labelStr, value, minMaxValues, subLabels) {
this._draggerFloatN(labelStr, value, 4, minMaxValues, subLabels);
};
GUI.prototype.draggerFloat3 = function (labelStr, value, minMaxValues, subLabels) {
this._draggerFloatN(labelStr, value, 3, minMaxValues, subLabels);
};
GUI.prototype.draggerFloat2 = function (labelStr, value, minMaxValues, subLabels) {
this._draggerFloatN(labelStr, value, 2, minMaxValues, subLabels);
};
GUI.prototype.draggerFloat1 = function (labelStr, value, minMaxValues, subLabels) {
this._draggerFloatN(labelStr, value, 1, minMaxValues, subLabels);
};
GUI.prototype.draggerRgb = function (labelStr, value) {
this._draggerFloatN(
labelStr, value, 3, [0, 1], ["R:", "G:", "B:"],
[
[this.draggerRgbRedColor, this.draggerRgbRedColorHover],
[this.draggerRgbGreenColor, this.draggerRgbGreenColorHover],
[this.draggerRgbBlueColor, this.draggerRgbBlueColorHover]
]);
};
/*
If value.val == id, then that means this radio button is chosen.
*/
GUI.prototype.radioButton = function (labelStr, value, id) {
this._moveWindowCaret();
/*
Radio button IO
*/
var zeroHeight = this._getTextSizes("0")[1];
var innerRadius = (zeroHeight * this.radioButtonInnerRadius);
var outerRadius = (zeroHeight * this.radioButtonOuterRadius);
var radioButtonPosition = this.windowCaret;
var radioButtonSizes = [outerRadius * 2, outerRadius * 2];
var mouseCollision = _inCircle(radioButtonPosition, radioButtonSizes, this.io.mousePositionCur);
if (this.io.mouseLeftDownCur == true && this.io.mouseLeftDownPrev == false && mouseCollision) {
value.val = id;
}
var isHover = mouseCollision;
/*
CHECKBOX RENDERING
*/
this._circle(radioButtonPosition, radioButtonSizes,
isHover ? this.radioButtonOuterColorHover : this.radioButtonOuterColor, this.radioButtonCircleSegments );
if (value.val == id) {
var p = radioButtonPosition;
var s = radioButtonSizes;
var innerCirclePosition = [
Math.round(0.5 * (p[0] + (p[0] + s[0]) - innerRadius * 2 )),
Math.round(0.5 * (p[1] + (p[1] + s[1]) - innerRadius * 2 )),
];
this._circle(innerCirclePosition, [innerRadius * 2, innerRadius * 2],
isHover ? this.radioButtonInnerColorHover : this.radioButtonInnerColor, this.radioButtonCircleSegments );
}
// now render radio button label.
var labelPosition = [radioButtonPosition[0] + radioButtonSizes[0] + this.widgetLabelHorizontalSpacing, radioButtonPosition[1]]
var labelStrSizes = [this._getTextSizes(labelStr)[0], radioButtonSizes[1]];
this._textCenter(labelPosition, labelStrSizes, labelStr);
this.prevWidgetSizes = [radioButtonSizes[0] + labelStrSizes[0], radioButtonSizes[1]];
}
GUI.prototype.checkbox = function (labelStr, value) {
this._moveWindowCaret();
/*
CHECKBOX IO(if checkbox clicked, flip boolean value.)
*/
// use height of zero to determine size of checkbox, to ensure that the textl label does become higher
// than the checkbox.
var zeroHeight = this._getTextSizes("0")[1];
var innerSize = zeroHeight * this.checkBoxInnerSizeRatio;
var outerSize = zeroHeight * this.checkBoxOuterSizeRatio;
var checkboxPosition = this.windowCaret;
var checkboxSizes = [outerSize, outerSize];
var mouseCollision = _inBox(checkboxPosition, checkboxSizes, this.io.mousePositionCur);
if (this.io.mouseLeftDownCur == true && this.io.mouseLeftDownPrev == false && mouseCollision) {
value.val = !value.val;
}
var isHover = mouseCollision;
/*
CHECKBOX RENDERING
*/
// render outer box.
this._box(
checkboxPosition,
checkboxSizes, isHover ? this.checkboxOuterColorHover : this.checkboxOuterColor);
// now render a centered inner box, that shows whether the checkbox is true, or false.
if (value.val) {
var p = checkboxPosition;
var s = checkboxSizes;
var innerboxPosition = [
Math.round(0.5 * (p[0] + (p[0] + s[0]) - innerSize )),
Math.round(0.5 * (p[1] + (p[1] + s[1]) - innerSize )),
];
this._box(
innerboxPosition,
[innerSize, innerSize], isHover ? this.checkboxInnerColorHover : this.checkboxInnerColor);
}
// now render checkbox label.
var labelPosition = [checkboxPosition[0] + checkboxSizes[0] + this.widgetLabelHorizontalSpacing, checkboxPosition[1]]
var labelStrSizes = [this._getTextSizes(labelStr)[0], checkboxSizes[1]];
this._textCenter(labelPosition, labelStrSizes, labelStr);
this.prevWidgetSizes = [checkboxSizes[0] + labelStrSizes[0], checkboxSizes[1]];
}
GUI.prototype.button = function (str) {
this._moveWindowCaret();
var widgetId = hashString(str);
/*
BUTTON RENDERING
*/
var buttonPosition = this.windowCaret;
var textSizes = this._getTextSizes(str);
// button size is text size, plus the spacing around the text.
var buttonSizes = [
textSizes[0] + this.buttonSpacing * 2,
textSizes[1] + this.buttonSpacing * 2
];
var color;
var isButtonClick = false;
// we can only hover or click, when are not interacting with some other widget.
if ((this.activeWidgetId == null || this.activeWidgetId == widgetId ) && _inBox(buttonPosition, buttonSizes, this.io.mousePositionCur)) {
if (this.io.mouseLeftDownPrev && !this.io.mouseLeftDownCur) {
isButtonClick = true;
color = this.clickButtonColor;
} else if (this.io.mouseLeftDownCur) {
color = this.clickButtonColor;
this.activeWidgetId = widgetId;
} else {
color = this.hoverButtonColor;
}
} else {
color = this.buttonColor
}
this._box(
buttonPosition,
buttonSizes, color)
// Render button text.
this._text([buttonPosition[0] + this.buttonSpacing,
buttonPosition[1] + buttonSizes[1] - this.buttonSpacing], str);
this.prevWidgetSizes = (buttonSizes);
/*
BUTTON IO
If button is pressed, return true;
Otherwise, return false.
*/
if (isButtonClick) {
return true; // button press!
}
return false;
}
GUI.prototype.separator = function () {
this._moveWindowCaret();
var separatorPosition = this.windowCaret;
// the separator should fill out the windows size.
var separatorSizes = [
this.windowSizes[0] - 2 * this.windowSpacing,
this._getTextSizes("0")[1] * this.separatorHeightRatio];
this._box(separatorPosition, separatorSizes, [0.4, 0.4, 0.4]);
this.prevWidgetSizes = (separatorSizes);
}
GUI.prototype.textLine = function (str) {
this._moveWindowCaret();
var textLinePosition = this.windowCaret;
var textSizes = this._getTextSizes(str);
// Render button text.
this._textCenter(textLinePosition, textSizes, str);
this.prevWidgetSizes = textSizes;
};
GUI.prototype.begin = function (io, windowTitle) {
// sanity checking.
if(typeof io == 'undefined') {
throw new Error("argument 'io' missing ");
} else {
if(typeof io.mousePositionCur == 'undefined') {
throw new Error("property 'io.mousePositionCur' missing ");
}
if(typeof io.mousePositionPrev == 'undefined') {
throw new Error("property 'io.mousePositionPrev' missing ");
}
if(typeof io.mouseLeftDownCur == 'undefined') {
throw new Error("property 'io.mouseLeftDownCur' missing ");
}
if(typeof io.mouseLeftDownPrev == 'undefined') {
throw new Error("property 'io.mouseLeftDownPrev' missing ");
}
}
// default value.
if(typeof windowTitle == 'undefined') {
windowTitle = "Window";
}
this.windowTitle = windowTitle;
/*
Setup geometry buffers.
*/
this.indexBuffer = [];
this.positionBuffer = [];
this.colorBuffer = [];
this.uvBuffer = [];
this.indexBufferIndex = 0;
this.positionBufferIndex = 0;
this.colorBufferIndex = 0;
this.uvBufferIndex = 0;
this.io = io;
// render window.
this._window();
};
GUI.prototype.end = function (gl, canvasWidth, canvasHeight) {
if(typeof gl == 'undefined') {
throw new Error("argument 'gl' missing ");
}
if(!(typeof canvasWidth == 'number')) {
throw new Error("argument 'canvasWidth' must be a number ");
}
if(!(typeof canvasHeight == 'number')) {
throw new Error("argument 'canvasHeight' must be a number ");
}
/*
If a VAO is already bound, we need to unbound it. Otherwise, we will write into a VAO created by the user of the library
when calling vertexAttribPointer, which means that we would effectively corrupt the user data!
*/
var VAO_ext = gl.getExtension('OES_vertex_array_object');
if (VAO_ext)
VAO_ext.bindVertexArrayOES(null);
/*
We are changing some GL states when rendering the GUI. So before rendering we backup these states,
and after rendering we restore these states. This is so that the end-user does not involuntarily have his
GL-states messed with.
*/
this._backupGLState(gl);
this.positionBufferObject.update(this.positionBuffer);
gl.enableVertexAttribArray(this.shader.attributes.aPosition.location);
gl.vertexAttribPointer(this.shader.attributes.aPosition.location, 2, gl.FLOAT, false, 0, 0);
this.positionBufferObject.unbind();
this.colorBufferObject.update(this.colorBuffer);
gl.enableVertexAttribArray(this.shader.attributes.aColor.location);
gl.vertexAttribPointer(this.shader.attributes.aColor.location, 4, gl.FLOAT, false, 0, 0);
this.colorBufferObject.unbind();
this.uvBufferObject.update(this.uvBuffer);
gl.enableVertexAttribArray(this.shader.attributes.aUv.location);
gl.vertexAttribPointer(this.shader.attributes.aUv.location, 2, gl.FLOAT, false, 0, 0);
this.uvBufferObject.unbind();
this.indexBufferObject.update(this.indexBuffer);
/*
Setup matrices.
*/
var projection = mat4.create()
mat4.ortho(projection, 0, canvasWidth, canvasHeight, 0, -1.0, 1.0)
this.shader.bind()
this.shader.uniforms.uProj = projection;
this.shader.uniforms.uFontAtlas = this.fontAtlasTexture.bind()
gl.disable(gl.DEPTH_TEST) // no depth testing; we handle this by manually placing out
// widgets in the order we wish them to be rendered.
// for text rendering, enable alpha blending.
gl.enable(gl.BLEND)
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.drawElements(gl.TRIANGLES, (this.indexBufferIndex), gl.UNSIGNED_SHORT, 0);
/*
Make sure to always reset the active widget id, if mouse is released.
This makes sure that every widget does not explicitly have to reset this value
by themselves, which is a bit error-prone.
*/
if (this.activeWidgetId != null && this.io.mouseLeftDownCur == false) {
this.activeWidgetId = null;
}
this._restoreGLState(gl);
};
/*
Mouse has focus EITHER when the mouse is inside the window, OR
it is outside the window, but is interacting with a widget.
*/
GUI.prototype.hasMouseFocus = function () {
return this.mouseInWindow || this.activeWidgetId != null;
}
GUI.prototype._setupDefaultSettings = function (char) {
/*
window settings
*/
// distance from window-borders to the widgets.
this.windowSpacing = 14;
// the vertical and horizontal spacing between the widgets.
this.widgetSpacing = 11;
// position of the window.
this.windowPosition = [20, 20];
// size of the window.
this.windowSizes = [360, 500];
// color of the window.
this.windowColor = [0.1, 0.1, 0.1];
// the transparency of the window.
this.windowAlpha = 0.9;
// the title bar height.
this.titleBarHeight = 21;
// spacing between the title bars border, and the window title.
this.titleBarVerticalSpacing = 6;
// the title bar color.
this.titleBarColor = [0.2, 0.4, 0.6];
/*
button settings
*/
// the horizontal and vertical spacing between the button border and its text label.
this.buttonSpacing = 3;
// normal button color.
this.buttonColor = [0.35, 0.1, 0.1];
// button button color when mouse hover
this.hoverButtonColor = [0.40, 0.1, 0.1];
// button color when mouse click.
this.clickButtonColor = [0.50, 0.1, 0.1];
/*
slider settings
*/
// the vertical space between the number(inside the slider) and the border of the slider box.
this.sliderVerticalSpacing = 4;
// the color of the slider background.
this.sliderBackgroundColor = [0.16, 0.16, 0.16];
// the color of the bar in the slider.
this.sliderFillColor = [0.0, 0.3, 0.6];
// the color of the slider background when mouse hover,
this.sliderBackgroundColorHover = [0.19, 0.19, 0.19];
// the color of the bar in the slider when mouse hover.
this.sliderFillColorHover = [0.0, 0.3, 0.70];
/*
dragger settings
*/
// the horizontal spacing between the subdragger widgets in a dragger widget.
this.draggerWidgetHorizontalSpacing = 3;
// the vertical spacing between the top and bottom borders and the text in draggers.
this.draggerVerticalSpacing = 5;
// The colors of the three subdraggers in the rgbDragger widget.
// "Hover", refers to the color when the dragger is hovered.
this.draggerRgbRedColor = [0.3, 0.0, 0.0];
this.draggerRgbRedColorHover = [0.35, 0.0, 0.0];
this.draggerRgbGreenColor = [0.0, 0.3, 0.0];
this.draggerRgbGreenColorHover = [0.0, 0.35, 0.0];
this.draggerRgbBlueColor = [0.0, 0.0, 0.3];
this.draggerRgbBlueColorHover = [0.0, 0.0, 0.38];
//The colors of the subdraggers in the draggerFloat widgets.
// "Hover", refers to the color when the dragger is hovered.
this.draggerFloatColor = [0.30, 0.30, 0.30];
this.draggerFloatColorHover = [0.32, 0.32, 0.32];
/*
checkbox settings
*/
// the outer color is the color of the outer box of the checkbox,
// and the inner color is the color of the inner box
this.checkboxOuterColor = [0.3, 0.3, 0.3];
this.checkboxInnerColor = [0.15, 0.15, 0.15];
this.checkboxOuterColorHover = [0.33, 0.33, 0.33];
this.checkboxInnerColorHover = [0.18, 0.18, 0.18];
// size of inner box will be (height of "0")* checkBoxInnerSizeRatio
this.checkBoxInnerSizeRatio = 1.4;
// size of outer box will be (height of "0")* checkBoxOuterSizeRatio
this.checkBoxOuterSizeRatio = 2.0;
/*
radioButton settings
*/
// the outer color is the color of the outer circle of the radioButton,
// and the inner color is the color of the inner circle
this.radioButtonOuterColor = [0.3, 0.3, 0.3];
this.radioButtonInnerColor = [0.15, 0.15, 0.15];
this.radioButtonOuterColorHover = [0.33, 0.33, 0.33];
this.radioButtonInnerColorHover = [0.18, 0.18, 0.18];
// in order to render the radio button, we must triangulate the circles into triangle segments
// this number is the number of triangle segments.
this.radioButtonCircleSegments = 9;
// radius of the inner circle will be (height of "0")* innerRadiusRatio
this.radioButtonInnerRadius = 0.6;
// radius of the outer circle will be (height of "0")* outerRadiusRatio
this.radioButtonOuterRadius = 1.0;
/*
separator settings.
*/
// the color of a separator.
this.separatorColor = [0.4, 0.4, 0.4];
// the height of a separator (height of "0") * this.separatorHeightRatio
this.separatorHeightRatio = 0.2;
/*
general settings
*/
// Some widgets render a label in addition to themselves(such as the sliders and draggers)
// this value is the horizontal spacing between the label and the widget for those widgets.
this.widgetLabelHorizontalSpacing = 4;
// Some widgets will grow horizontally as the window size increases. They are grown to occupy this
// ratio of the total window width.
this.widgetHorizontalGrowRatio = 0.6;
};
GUI.prototype._getCharDesc = function (char) {
return fontInfo.chars[char.charCodeAt(0) - 32];
};
GUI.prototype._addIndex = function (index) {
this.indexBuffer[this.indexBufferIndex++] = index;
};
GUI.prototype._addPosition = function (position) {
this.positionBuffer[this.positionBufferIndex++] = position[0];
this.positionBuffer[this.positionBufferIndex++] = position[1];
};
GUI.prototype._addColor = function (color) {
this.colorBuffer[this.colorBufferIndex++] = color[0];
this.colorBuffer[this.colorBufferIndex++] = color[1];
this.colorBuffer[this.colorBufferIndex++] = color[2];
this.colorBuffer[this.colorBufferIndex++] = color[3];
};
GUI.prototype._addUv = function (uv) {
this.uvBuffer[this.uvBufferIndex++] = uv[0];
this.uvBuffer[this.uvBufferIndex++] = uv[1];
};
/*
Get width and height of a text string.
*/
GUI.prototype._getTextSizes = function (str) {
var width = 0;
var height = 0; // the height of the highest character.
for (var i = 0; i < str.length; ++i) {
var ch = str[i];
var cd = this._getCharDesc(ch);
width += (cd.xadvance) * this.textScale;
var y0 = (cd.yoff) * this.textScale;
var y1 = (cd.yoff2) * this.textScale;
var h = y1 - y0;
if (height < h) {
height = h;
}
}
return [width, height];
}
/*
render text
*/
GUI.prototype._text = function (position, str) {
/*
Make sure to round the position to integer. Otherwise, anti-aliasing causes the text to get blurry,
it seems
*/
var x = Math.round(position[0]);
var y = Math.round(position[1]);
/*
Width of a single pixel in the font atlas.
*/
var ipw = 1.0 / 256;
var iph = 1.0 / 256;
for (var i = 0; i < str.length; ++i) {
var ch = str[i];
// char desc
var cd = this._getCharDesc(ch);
/*
We will render a single character as a quad.
First we gather all information needed to render the quad:
*/
var x0 = (x + cd.xoff) * this.textScale;
var y0 = (y + cd.yoff) * this.textScale;
var x1 = (x + cd.xoff2) * this.textScale;
var y1 = (y + cd.yoff2) * this.textScale;
var s0 = (cd.x0 * ipw);
var t0 = (cd.y0 * iph);
var s1 = (cd.x1 * ipw);
var t1 = (cd.y1 * iph);
// render text as white.
var whiteColor = [1, 1, 1, 1]
/*
Now we have all the information. Now render the quad as two triangles:
*/
var baseIndex = this.positionBufferIndex / 2;
// top left
this._addPosition([x0, y0]);
this._addColor(whiteColor);
this._addUv([s0, t0]);
// bottom left
this._addPosition([x0, y1]);
this._addColor(whiteColor);
this._addUv([s0, t1]);
// top right
this._addPosition([x1, y0]);
this._addColor(whiteColor);
this._addUv([s1, t0]);
// bottom right
this._addPosition([x1, y1]);
this._addColor(whiteColor);
this._addUv([s1, t1]);
// triangle 1
this._addIndex(baseIndex + 0);
this._addIndex(baseIndex + 1);
this._addIndex(baseIndex + 2);
// triangle 2
this._addIndex(baseIndex + 3);
this._addIndex(baseIndex + 2);
this._addIndex(baseIndex + 1);
// finally, advance the x-coord, in preparation of rendering the next character.
x += (cd.xadvance) * this.textScale;
}
}
/*
Render text centered in a box with position `p`, width `s[0]`, height `[1]`,
*/
GUI.prototype._textCenter = function (p, s, str) {
var strSizes = this._getTextSizes(str);
// we must round, otherwise the text may end up between pixels(say at 1.5, or 1.6, or something ),
// and this makes it blurry
var strPosition = [
Math.round(0.5 * (p[0] + (p[0] + s[0]) - strSizes[0] )),
Math.round(0.5 * (p[1] + (p[1] + s[1]) + strSizes[1] )),
];
this._text(strPosition, str);
}
/*
Add vertex that only has one color, and does not use a texture.
*/
GUI.prototype._coloredVertex = function (position, color) {
// at this uv-coordinate, the font atlas is entirely white.
var whiteUv = [0.95, 0.95];
this._addPosition(position);
this._addColor(color);
this._addUv(whiteUv);
};
/*
Render a box.
`color` is a RGB-triplet.
the optional `alpha` argument specifies the transparency of the box.
default value of `alpha` is 1.0
*/
GUI.prototype._box = function (position, size, color, alpha) {
if (typeof alpha === 'undefined') {
alpha = 1.0; // default to 1.0
}
// top-left, bottom-left, top-right, bottom-right corners
var tl = position;
var bl = [position[0], position[1] + size[1]];
var tr = [position[0] + size[0], position[1]];
var br = [position[0] + size[0], position[1] + size[1]];
var baseIndex = this.positionBufferIndex / 2;
var c = [color[0], color[1], color[2], alpha];
// vertex 1
this._coloredVertex(tl, c);
// vertex 2
this._coloredVertex(bl, c);
// vertex 3
this._coloredVertex(tr, c);
// vertex 4
this._coloredVertex(br, c);
// triangle 1
this._addIndex(baseIndex + 0);
this._addIndex(baseIndex + 1);
this._addIndex(baseIndex + 2);
// triangle 2
this._addIndex(baseIndex + 3);
this._addIndex(baseIndex + 2);
this._addIndex(baseIndex + 1);
};
GUI.prototype._unitCircle = function (position, theta, radius) {
return [position[0] + radius * Math.cos(theta), position[1] + radius * Math.sin(theta)];
};
/*
Render a circle, where the top-left corner of the circle is `position`
Where `segments` is how many triangle segments the triangle is rendered with.
*/
GUI.prototype._circle = function (position, sizes, color, segments) {
var centerPosition = [
position[0] + 0.5 * sizes[0],
position[1] + 0.5 * sizes[1]
];
var radius = sizes[0] / 2;
var baseIndex = this.positionBufferIndex / 2;
var c = [color[0], color[1], color[2], 1.0];
// add center vertex.
this._coloredVertex(centerPosition, c);
var centerVertexIndex = baseIndex + 0;
var stepSize = (2 * Math.PI) / segments;
var curIndex = baseIndex + 1;
for (var theta = 0; theta <= 2 * Math.PI + 0.1; theta += stepSize, ++curIndex) {
// for first frame, we only create one vertex, and no triangles
if (theta == 0) {
var p = this._unitCircle(centerPosition, theta, radius);
this._coloredVertex(p, c);
} else {
var p = this._unitCircle(centerPosition, theta, radius);
this._coloredVertex(p, c);
this._addIndex(curIndex + 0);
this._addIndex(curIndex - 1);
this._addIndex(centerVertexIndex);
}
}
};
function _inCircle(p, s, x) {
// circle center
var cp = [
p[0] + 0.5 * s[0],
p[1] + 0.5 * s[1]
];
var radius = s[0] * 0.5;
// distance from `x` to circle center.
var dist = Math.sqrt((x[0] - cp[0]) * (x[0] - cp[0]) + (x[1] - cp[1]) * (x[1] - cp[1]));
return (dist <= radius);
}
/*
Given a box with position `p`, width `s[0]`, height `[1]`,
return whether the point with the position `x` is inside the box.
*/
function _inBox(p, s, x) {
var minX = p[0];
var minY = p[1];
var maxX = p[0] + s[0];
var maxY = p[1] + s[1];
return (
minX <= x[0] && x[0] <= maxX &&
minY <= x[1] && x[1] <= maxY
);
}
/*
Before adding a widget, move the window caret to the right of the previous widget if this.sameLineActive,
ELSE start a line.
you should ALWAYS call this function before adding a new widget.
*/
GUI.prototype._moveWindowCaret = function () {
if (this.prevWidgetSizes == null) {
// we have not yet laid out the first widget. Do nothing.
return;
}
if (this.sameLineActive) {
this.windowCaret = [this.windowCaret[0] + this.widgetSpacing + this.prevWidgetSizes[0], this.windowCaret[1]];
} else {
this.windowCaret = [this.windowSpacing + this.windowPosition[0], this.windowCaret[1] + this.widgetSpacing + this.prevWidgetSizes[1]];
}
// the user have to explicitly call sameLine() again if we he wants samLineActive again.
this.sameLineActive = false;
};
GUI.prototype._draggerFloat = function (widgetId, labelStr, value, color, colorHover, width, position, minVal, maxVal) {
/*
DRAGGER IO
*/
var draggerPosition = position;
var draggerSizes = [
width,
this._getTextSizes("0")[1] + 2 * this.draggerVerticalSpacing
];
var mouseCollision = _inBox(draggerPosition, draggerSizes, this.io.mousePositionCur);
if (
mouseCollision &&
this.io.mouseLeftDownCur == true && this.io.mouseLeftDownPrev == false) {
// if slider is clicked, it becomes active.
this.activeWidgetId = widgetId;
}
if (this.activeWidgetId == widgetId) {
value.val += 0.01 * (this.io.mousePositionCur[0] - this.io.mousePositionPrev[0]);
value.val = clamp(value.val, minVal, maxVal);
this.activeWidgetId = widgetId;
}
/*
DRAGGER RENDERING
*/
var sliderValueNumDecimalDigits = 2; // hardcode this value for now.
var sliderValueStr = labelStr + value.val.toFixed(sliderValueNumDecimalDigits);
/*
If either widget is active, OR we are hovering but not clicking,
switch to hover color.
*/
var isHover = (this.activeWidgetId == widgetId) || (mouseCollision && !this.io.mouseLeftDownCur );
this._box(
draggerPosition,
draggerSizes, isHover ? colorHover : color);
var sliderValueStrSizes = this._getTextSizes(sliderValueStr);
// render text in slider
this._textCenter(draggerPosition, draggerSizes, sliderValueStr);
// return top right corner, and bottom right corner of the dragger.
return {
topRight: [draggerPosition[0] + draggerSizes[0], draggerPosition[1]],
bottomRight: [draggerPosition[0] + draggerSizes[0], draggerPosition[1] + draggerSizes[1]],
};
};
/*
sublabels,
min max, for all n.
hover color, for all three.
*/
GUI.prototype._draggerFloatN = function (labelStr, value, N, minMaxValues, subLabels, colors) {
this._moveWindowCaret();
if (!minMaxValues)
minMaxValues = [];