Repository navigation
Expand file tree
/
Copy pathjongui.bend
More file actions
3964 lines (3346 loc) · 228 KB
/
Copy pathjongui.bend
File metadata and controls
3964 lines (3346 loc) · 228 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
# Jongui - zlib license, Copyright (c) 2026 Jonathan Peris.
# Altered Bend adaptation of raygui v5.1-dev (raygui.h as raylib 6.2's
# examples ship it; zlib/libpng, Copyright (c) 2014-2026 Ramon Santamaria
# (@raysan5)); see LICENSES/raygui.txt. Jongui is to raygui what Jonlib is to
# raylib: the controls' update and drawing code in raygui's F32 and C int
# order, with raygui's global state as an explicit Gui value.
#
# Delivered: the style table (GuiLoadStyleDefault, GuiSetStyle, GuiGetStyle),
# the global state (GuiEnable, GuiDisable, GuiLock, GuiUnlock, GuiIsLocked,
# GuiSetState, GuiGetState, GuiSetAlpha), GuiGetTextWidth, GuiLabel,
# GuiButton, GuiCheckBox, GuiToggle, GuiToggleGroup, GuiSlider and
# GuiSliderBar, GuiLine and GuiGroupBox, GuiValueBox and GuiSpinner (not in
# edit mode), GuiStatusBar, GuiPanel, GuiDropdownBox, GuiTextBox,
# GuiScrollBar and GuiScrollPanel, with the default font,
# TEXT_WRAP_NONE and raygui's icon set (src/gui_icons.bend, GuiSetIconScale). An
# icon past the icon table (raygui reads past it) and the other wrap modes
# mark the frame undefined. See docs/GUI.md.
import Base
import ./jonlib.bend as J
import ./jonmath.bend as M
import ./src/fonts.bend as FontCore
import ./src/text.bend as TextCore
import ./src/gui_icons.bend as Icons
# raygui's guiFont: the default font, or the font GuiSetFont set, kept as
# Data (J.Font.Shared) since the Gui is copied as C copies a Font.
type Gui.Font is Data:
GuiFontDefault{}
GuiFontSet{font: J.Font.Shared}
# raygui's globals: guiState, guiLocked, guiAlpha, the slider drag
# (guiControlExclusiveMode and guiControlExclusiveRec), guiStyle (16
# controls of 24 properties each), guiIconScale, guiTooltip, the tooltip's
# text (guiTooltipPtr; tipped is False for NULL), the icon table
# (guiIconsPtr: guiIcons, 512 icons of eight words, until GuiLoadIcons
# replaces it) and guiFont.
type Gui is Data:
Gui{state: U32, locked: Bool, alpha: F32, exclusive: Bool, exclusive_rec: J.Rectangle, style: +List<U32>, icon_scale: U32, tips: Bool, tip: String, tipped: Bool, icons: +List<U32>, font: Gui.Font}
# guiStyle after GuiLoadStyleDefault (printed by raygui itself; raygui
# 5.1-dev: TEXT_LINE_SPACING 12, TABBAR's TAB_ITEMS_WIDTH 160).
def Gui.style.default() -> +List<U32>:
[2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 1, 0, 10, 1, 2427172351, 4126537215, 12, 1, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 4, 1, 0, 16, 1, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 4, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 4, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 1, 0, 32, 2, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 1, 0, 16, 2, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 0, 0, 24, 2, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 1, 0, 160, 0, 0, 0, 0, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 1, 0, 28, 2, 12, 1, 0, 1, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 0, 1, 0, 8, 16, 8, 8, 2, 0, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 0, 0, 1, 0, 6, 0, 0, 16, 0, 12, 0, 0,
2206434303, 3385444863, 1751673087, 1538447871, 3387948799, 1822145791, 76728319, 2548629503, 915124223, 3049374463, 3874089471, 2931276031, 1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
def Gui.new() -> Gui:
Gui{0, False{}, 1.0, False{}, J.Rectangle{0.0, 0.0, 0.0, 0.0}, Gui.style.default(), 1, False{}, "", False{}, Icons.words(), GuiFontDefault{}}
# -----------------------------------------------------------------------------
# Controls, properties, states and alignments (raygui's enums)
def Gui.DEFAULT() -> U32:
0
def Gui.LABEL() -> U32:
1
def Gui.BUTTON() -> U32:
2
def Gui.TOGGLE() -> U32:
3
def Gui.SLIDER() -> U32:
4
def Gui.PROGRESSBAR() -> U32:
5
def Gui.CHECKBOX() -> U32:
6
# The control raygui 5.1-dev added in CONTROL11's row.
def Gui.TABBAR() -> U32:
11
# TOGGLE's GROUP_WIDTH_FULL: GuiToggleGroup's bounds span the whole row.
def Gui.GROUP_WIDTH_FULL() -> U32:
17
# TABBAR's properties: the tabs' width, their close buttons (0 hidden), the
# side of the border line (0 bottom, 1 top).
def Gui.TAB_ITEMS_WIDTH() -> U32:
16
def Gui.TAB_CLOSE_BUTTON() -> U32:
17
def Gui.TAB_LINE_SIDE() -> U32:
18
# GuiResult: what the controls return.
def Gui.RESULT_NONE() -> U32:
0
def Gui.RESULT_PRESSED() -> U32:
1
def Gui.RESULT_CHANGED() -> U32:
2
def Gui.RESULT_TAB_CLOSE() -> U32:
4
def Gui.BORDER_WIDTH() -> U32:
12
def Gui.TEXT_PADDING() -> U32:
13
def Gui.TEXT_ALIGNMENT() -> U32:
14
def Gui.TEXT_SIZE() -> U32:
16
def Gui.TEXT_SPACING() -> U32:
17
def Gui.TEXT_LINE_SPACING() -> U32:
20
def Gui.TEXT_ALIGNMENT_VERTICAL() -> U32:
21
def Gui.TEXT_WRAP_MODE() -> U32:
22
def Gui.SLIDER_WIDTH() -> U32:
16
def Gui.SLIDER_PADDING() -> U32:
17
def Gui.CHECK_PADDING() -> U32:
16
def Gui.STATE_NORMAL() -> U32:
0
def Gui.STATE_FOCUSED() -> U32:
1
def Gui.STATE_PRESSED() -> U32:
2
def Gui.STATE_DISABLED() -> U32:
3
def Gui.TEXT_ALIGN_LEFT() -> U32:
0
def Gui.TEXT_ALIGN_CENTER() -> U32:
1
def Gui.TEXT_ALIGN_RIGHT() -> U32:
2
# -----------------------------------------------------------------------------
# Style and global state
def Gui.head(values: +List<U32>) -> U32:
match values:
case Con{value, _}: value
case Nil{}: 0
# GuiGetStyle(control, property): 0 outside the table.
def Gui.get_style(gui: Gui, +control: U32, +property: U32) -> U32:
Gui{_, _, _, _, _, style, _, _, _, _, _, _} = gui
Gui.head(List.drop(&2, U32, style, U32.to_nat((control * 24 + property : U32))))
# The index GuiSetStyle writes, control*24 + property in C's int arithmetic,
# or 4294967295 where it overflows or falls outside the 16x24 table
# (undefined in raygui).
def Gui.style.index(+control: U32, +property: U32) -> U32:
+product = (control * 24 : U32)
+sum = (product + property : U32)
+np = (product >= 2147483648 : U32)
+nq = (property >= 2147483648 : U32)
+ns = (sum >= 2147483648 : U32)
+wide = Bool.not((control <= 89478485 : U32) || (control >= 4205488811 : U32))
+over = (np && nq && Bool.not(ns)) || (Bool.not(np) && Bool.not(nq) && ns)
Bool.pick(U32, wide || over || (sum >= 384 : U32), 4294967295, sum)
def Gui.style.put(style: +List<U32>, +i: U32, +index: U32, +control: U32, +property: U32, +value: U32) -> +List<U32>:
match style:
case Nil{}: Nil{}
case Con{+v, rest}:
+hit = U32.is_eq(i, index) || (U32.is_eq(control, 0) && (property < 16 : U32) && U32.is_eq((i % 24 : U32), property))
Con{Bool.pick(U32, hit, value, v), Gui.style.put(rest, (i + 1 : U32), index, control, property, value)}
# GuiSetStyle(control, property, value): guiStyle[control*24 + property]
# (a property of 24 or more reaches the next controls' rows, as in raygui),
# and a DEFAULT base property (below 16) every control's. An index outside
# the table changes nothing (raygui writes outside its array).
def Gui.set_style(gui: Gui, +control: U32, +property: U32, +value: U32) -> Gui:
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font} = gui
Gui{state, locked, alpha, exclusive, rec, Gui.style.put(style, 0, Gui.style.index(control, property), control, property, value), scale, tips, tip, tipped, icons, font}
# GuiEnable: STATE_DISABLED becomes STATE_NORMAL.
def Gui.enable(gui: Gui) -> Gui:
Gui{+state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font} = gui
Gui{Bool.pick(U32, U32.is_eq(state, 3), 0, state), locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
# GuiDisable: STATE_NORMAL becomes STATE_DISABLED.
def Gui.disable(gui: Gui) -> Gui:
Gui{+state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font} = gui
Gui{Bool.pick(U32, U32.is_eq(state, 0), 3, state), locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
def Gui.lock(gui: Gui) -> Gui:
Gui{state, _, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font} = gui
Gui{state, True{}, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
def Gui.unlock(gui: Gui) -> Gui:
Gui{state, _, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font} = gui
Gui{state, False{}, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
def Gui.is_locked(gui: Gui) -> Bool:
Gui{_, locked, _, _, _, _, _, _, _, _, _, _} = gui
locked
def Gui.set_state(gui: Gui, state: U32) -> Gui:
Gui{_, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font} = gui
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
def Gui.get_state(gui: Gui) -> U32:
Gui{state, _, _, _, _, _, _, _, _, _, _, _} = gui
state
# GuiSetAlpha: clamped to [0, 1].
def Gui.set_alpha(gui: Gui, +alpha: F32) -> Gui:
Gui{state, locked, _, exclusive, rec, style, scale, tips, tip, tipped, icons, font} = gui
Gui{state, locked, Bool.pick(F32, (alpha < 0.0 : F32), 0.0, Bool.pick(F32, (alpha > 1.0 : F32), 1.0, alpha)), exclusive, rec, style, scale, tips, tip, tipped, icons, font}
# A style value as the C int raygui reads it, in F32 (exact below 2^24).
# GuiSetIconScale: scales below 1 are ignored (a C int as a word).
def Gui.set_icon_scale(gui: Gui, +scale: U32) -> Gui:
Gui{state, locked, alpha, exclusive, rec, style, old, tips, tip, tipped, icons, font} = gui
Gui{state, locked, alpha, exclusive, rec, style, Bool.pick(U32, (scale >= 1 : U32) && (scale < 2147483648 : U32), scale, old), tips, tip, tipped, icons, font}
# GuiGetIcons: the icon table, 4096 words at start (eight an icon, two rows
# of 16 pixels a word, the low bit first), or the words of the last
# GuiLoadIcons.
def Gui.icons(gui: Gui) -> +List<U32>:
Gui{_, _, _, _, _, _, _, _, _, _, icons, _} = gui
icons
def Gui.get_icons(gui: Gui) -> +List<U32>:
Gui.icons(gui)
def Gui.icons.put(gui: Gui, +icons: +List<U32>) -> Gui:
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, _, font} = gui
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
# Whether the icon table holds icon id's eight words: raygui reads
# guiIconsPtr[id*8 + i] whatever the table's length (512 icons at start,
# the loaded count after GuiLoadIcons).
def Gui.icons.has(+icons: +List<U32>, +id: U32) -> Bool:
(id < (U32.from_nat(List.length(&2, U32, icons)) / 8 : U32) : U32)
# GuiEnableTooltip, GuiDisableTooltip: guiTooltip (off in a new gui).
def Gui.enable_tooltip(gui: Gui) -> Gui:
Gui{state, locked, alpha, exclusive, rec, style, scale, _, tip, tipped, icons, font} = gui
Gui{state, locked, alpha, exclusive, rec, style, scale, True{}, tip, tipped, icons, font}
def Gui.disable_tooltip(gui: Gui) -> Gui:
Gui{state, locked, alpha, exclusive, rec, style, scale, _, tip, tipped, icons, font} = gui
Gui{state, locked, alpha, exclusive, rec, style, scale, False{}, tip, tipped, icons, font}
def Gui.tip.put(gui: Gui, tip: String, +tipped: Bool) -> Gui:
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, _, _, icons, font} = gui
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
def Gui.tip.of(tooltip: Maybe<String>, gui: Gui) -> Gui:
match tooltip:
case None{}: Gui.tip.put(gui, "", False{})
case Some{t}: Gui.tip.put(gui, t, True{})
# GuiSetTooltip(tooltip): the text tooltips show from now on (raygui keeps
# the pointer); None is NULL, which shows none.
def Gui.set_tooltip(gui: Gui, tooltip: Maybe<String>) -> Gui:
Gui.tip.of(tooltip, gui)
def Gui.font(gui: Gui) -> Gui.Font:
Gui{_, _, _, _, _, _, _, _, _, _, _, font} = gui
font
def Gui.font.face(font: Gui.Font) -> FontCore.Face:
match font:
case GuiFontDefault{}: FontCore.default.face()
case GuiFontSet{J.FontShared{+base, +padding, +glyphs, _, _, _, _, _, _, _, _, _, _, _, _, _, _}}: FontCore.Face.new(glyphs, base, padding)
# The gui font's glyphs, base size and padding.
def Gui.face(gui: Gui) -> FontCore.Face:
Gui.font.face(Gui.font(gui))
def Gui.font.base(font: Gui.Font) -> U32:
match font:
case GuiFontDefault{}: 10
case GuiFontSet{J.FontShared{+base, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}}: base
# GuiGetFont().baseSize.
def Gui.font_base_size(gui: Gui) -> U32:
Gui.font.base(Gui.font(gui))
def Gui.font.put(gui: Gui, +font: Gui.Font) -> Gui:
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, _} = gui
Gui{state, locked, alpha, exclusive, rec, style, scale, tips, tip, tipped, icons, font}
def Gui.font.shared(+gui: Gui, r: J.Font & J.Font.Shared) -> Gui & J.Font:
(font, +shared) = r
(Gui.font.put(gui, GuiFontSet{shared}), font)
def Gui.font.chosen(+set: Bool, +gui: Gui, font: J.Font) -> Gui & J.Font:
match set:
case False{}: (gui, font)
case True{}: Gui.font.shared(gui, J.Font.shared(font))
def Gui.font.informed(+gui: Gui, r: J.Font & J.FontInfo) -> Gui & J.Font:
(font, info) = r
J.FontInfo{_, _, _, +id} = info
Gui.font.chosen((id > 0 : U32), gui, font)
# GuiSetFont(font): the gui draws and measures its texts with the font from
# now on, when its texture id is above 0 (raygui ignores it otherwise). The
# gui keeps a copy (raygui keeps the struct, whose pointers the caller
# owns); the font comes back.
def Gui.set_font(gui: Gui, font: J.Font) -> Gui & J.Font:
Gui.font.informed(gui, J.Font.info(font))
def Gui.font.made(font: Gui.Font) -> J.Font:
match font:
case GuiFontDefault{}: J.Font.default()
case GuiFontSet{+shared}: J.Font.of_shared(shared)
# GuiGetFont: GetFontDefault for a new gui, or a font equal to the one
# GuiSetFont set.
def Gui.get_font(gui: Gui) -> J.Font:
Gui.font.made(Gui.font(gui))
def Gui.icon_scale(gui: Gui) -> U32:
Gui{_, _, _, _, _, _, scale, _, _, _, _, _} = gui
scale
def Gui.style.f32(+gui: Gui, +control: U32, +property: U32) -> F32:
U32.to_f32(Gui.get_style(gui, control, property))
def Gui.fade(+color: U32, +alpha: F32) -> U32:
((color .&. 4294967040) .|. (F32.to_u32(F32.trunc((U32.to_f32((color .&. 255 : U32)) * alpha : F32))) .&. 255) : U32)
# GuiFade(GetColor(style), guiAlpha): the alpha byte times guiAlpha, truncated.
def Gui.color(+gui: Gui, +control: U32, +property: U32) -> U32:
Gui{_, _, +alpha, _, _, _, _, _, _, _, _, _} = gui
Gui.fade(Gui.get_style(gui, control, property), alpha)
def Gui.alpha.of(gui: Gui) -> F32:
Gui{_, _, alpha, _, _, _, _, _, _, _, _, _} = gui
alpha
def Gui.refused(refused: Bool, frame: J.Frame) -> J.Frame:
match refused:
case True{}: J.Frame.refuse(frame)
case False{}: frame
# -----------------------------------------------------------------------------
# Text: GuiGetTextWidth and GuiDrawText with the gui's font
# The glyph's width as raygui measures and advances it: advanceX*scale, or
# the atlas rectangle's width*scale when advanceX is 0.
def Gui.glyph.scaled(glyph: FontCore.Glyph, +scale: F32) -> F32:
FontCore.Glyph{_, _, _, +advance, _, _, +width, _} = glyph
Bool.pick(F32, U32.is_eq(advance, 0), (width * scale : F32), (FontCore.int.f32(advance) * scale : F32))
def Gui.glyph.width(+face: FontCore.Face, +cp: U32, +scale: F32) -> F32:
Gui.glyph.scaled(FontCore.Face.glyph(face, cp), scale)
def Gui.text.sum(+face: FontCore.Face, cps: +List<U32>, +scale: F32, +spacing: F32, +acc: F32) -> F32:
match cps:
case Nil{}: acc
case Con{+cp, rest}: Gui.text.sum(face, rest, scale, spacing, (acc + (Gui.glyph.width(face, cp, scale) + spacing) : F32))
# The bytes of a line: up to the first '\n' (and, when drawn, '\r').
def Gui.line.bytes(bytes: +List<U32>, +stop_cr: Bool) -> +List<U32>:
match bytes:
case Nil{}: Nil{}
case Con{+b, rest}: Bool.pick(+List<U32>, U32.is_eq(b, 10) || (stop_cr && U32.is_eq(b, 13)), Nil{}, Con{b, Gui.line.bytes(rest, stop_cr)})
# TEXT_SIZE/guiFont.baseSize.
def Gui.scale(+gui: Gui) -> F32:
(Gui.style.f32(gui, 0, 16) / U32.to_f32(Gui.font_base_size(gui)) : F32)
# GuiMeasureText of bytes: the first line's glyph widths plus TEXT_SPACING
# each (at most 256 bytes).
def Gui.width.sum(+gui: Gui, bytes: +List<U32>) -> F32:
Gui.text.sum(Gui.face(gui), TextCore.codepoints(List.take(&2, U32, Gui.line.bytes(bytes, False{}), 256n)), Gui.scale(gui), Gui.style.f32(gui, 0, 17), 0.0)
def Gui.width.bytes(+gui: Gui, bytes: +List<U32>) -> F32:
F32.trunc(Gui.width.sum(gui, bytes))
def Gui.first(bytes: +List<U32>) -> U32:
match bytes:
case Con{b, _}: b
case Nil{}: 0
# GetTextIcon: a line starting with '#', up to three digits and '#' names an
# icon below RAYGUI_ICON_MAX_ICONS (512), and its text follows; id is
# 4294967295 for a line without one.
type Gui.Icon is Data:
GuiIcon{id: U32, text: +List<U32>}
def Gui.icon.digits(n: Nat, text: +List<U32>, +value: U32) -> Gui.Icon:
match n text:
case 0n Nil{}: GuiIcon{value, Nil{}}
case 0n Con{+c, +rest}: GuiIcon{value, Con{c, rest}}
case 1n+p Nil{}: GuiIcon{value, Nil{}}
case 1n+p Con{+c, +rest}: Bool.pick(Gui.Icon, (c >= 48 : U32) && (c <= 57 : U32), Gui.icon.digits(p, rest, (value * 10 + (c - 48) : U32)), GuiIcon{value, Con{c, rest}})
def Gui.icon.after(text: +List<U32>, +id: U32, +whole: +List<U32>) -> Gui.Icon:
match text:
case Con{+c, +rest}: Bool.pick(Gui.Icon, U32.is_eq(c, 35), GuiIcon{id, rest}, GuiIcon{4294967295, whole})
case Nil{}: GuiIcon{4294967295, whole}
def Gui.icon.closed(+scan: Gui.Icon, +whole: +List<U32>) -> Gui.Icon:
GuiIcon{+id, +text} = scan
Gui.icon.after(text, id, whole)
# The icon marker GetTextIcon and GetLineWidth look for: '#', up to three
# digits and '#', whatever the number (0 to 999), and the text after it.
def Gui.icon.scan(line: +List<U32>) -> Gui.Icon:
match line:
case Con{+c, +rest}: Bool.pick(Gui.Icon, U32.is_eq(c, 35), Gui.icon.closed(Gui.icon.digits(3n, rest, 0), Con{c, rest}), GuiIcon{4294967295, Con{c, rest}})
case Nil{}: GuiIcon{4294967295, Nil{}}
# A marker of 512 or more is not an icon: the line stays whole.
def Gui.icon.held(+scan: Gui.Icon, +line: +List<U32>) -> Gui.Icon:
GuiIcon{+id, +text} = scan
Bool.pick(Gui.Icon, (id < 512 : U32), GuiIcon{id, text}, GuiIcon{4294967295, line})
def Gui.icon.of(+line: +List<U32>) -> Gui.Icon:
Gui.icon.held(Gui.icon.scan(line), line)
def Gui.empty(bytes: +List<U32>) -> Bool:
match bytes:
case Nil{}: True{}
case Con{_, _}: False{}
# GetTextLines: the text split after each '\n' that is not its last byte (a
# final '\n' stays in its line, where every reader stops at it).
def Gui.lines.go(bytes: +List<U32>, +current: +List<U32>) -> +List<+List<U32>>:
match bytes:
case Nil{}: [List.reverse(&2, U32, current)]
case Con{+b, +rest}:
+split = U32.is_eq(b, 10) && Bool.not(Gui.empty(rest))
+tail = Gui.lines.go(rest, Bool.pick(+List<U32>, split, Nil{}, Con{b, current}))
Bool.pick(+List<+List<U32>>, split, Con{List.reverse(&2, U32, current), tail}, tail)
def Gui.width.marked(+scan: Gui.Icon, +gui: Gui, +bytes: +List<U32>) -> F32:
GuiIcon{+id, +text} = scan
Bool.pick(F32, U32.is_eq(id, 4294967295), Gui.width.bytes(gui, bytes), F32.trunc((Gui.width.sum(gui, text) + 20.0 : F32)))
# GetLineWidth (raygui 5.1-dev) of a line: GuiMeasureText's width, from
# after an icon marker ('#', up to three digits and '#', any number) plus 20
# for the icon and its padding when the line starts with one, truncated to an
# int.
def Gui.width.line(+gui: Gui, +bytes: +List<U32>) -> F32:
Gui.width.marked(Gui.icon.scan(bytes), gui, bytes)
# The widest line that starts within the text's first 256 bytes.
def Gui.width.lines(lines: +List<+List<U32>>, +gui: Gui, +start: U32, +best: F32) -> F32:
match lines:
case Nil{}: best
case Con{+line, rest}:
+width = Gui.width.line(gui, line)
Gui.width.lines(rest, gui, ((start + TextCore.len(line) : U32) + 1 : U32), Bool.pick(F32, (start < 256 : U32) && (width > best : F32), width, best))
# GuiGetTextWidth (raygui 5.1-dev) of bytes: the widest of its lines by
# GetLineWidth, as a C int in F32.
def Gui.width.text(+gui: Gui, +bytes: +List<U32>) -> F32:
Gui.width.lines(Gui.lines.go(bytes, Nil{}), gui, 0, 0.0)
# GuiGetTextWidth(text) as a C int in F32.
def Gui.get_text_width(+gui: Gui, text: String) -> Maybe<F32>:
Some{Gui.width.text(gui, TextCore.bytes(text))}
# The frame a text is drawn on, with the gui's font when GuiSetFont set one
# (built from the gui's copy once for the whole text).
type Gui.Pen is Type:
GuiPenDefault{frame: J.Frame}
GuiPenFont{frame: J.Frame, font: J.Font}
def Gui.pen.of(font: Gui.Font, frame: J.Frame) -> Gui.Pen:
match font:
case GuiFontDefault{}: GuiPenDefault{frame}
case GuiFontSet{+shared}: GuiPenFont{frame, J.Font.of_shared(shared)}
def Gui.pen.open(+gui: Gui, frame: J.Frame) -> Gui.Pen:
Gui.pen.of(Gui.font(gui), frame)
def Gui.pen.close(pen: Gui.Pen) -> J.Frame:
match pen:
case GuiPenDefault{frame}: frame
case GuiPenFont{frame, _}: frame
def Gui.pen.refuse(pen: Gui.Pen) -> Gui.Pen:
match pen:
case GuiPenDefault{frame}: GuiPenDefault{J.Frame.refuse(frame)}
case GuiPenFont{frame, font}: GuiPenFont{J.Frame.refuse(frame), font}
def Gui.pen.drawn(r: J.Frame & J.Font) -> Gui.Pen:
(frame, font) = r
GuiPenFont{frame, font}
# DrawTextCodepoint(guiFont, codepoint, (x, y), TEXT_SIZE, tint).
def Gui.glyph.draw(pen: Gui.Pen, +cp: U32, +x: F32, +y: F32, +size: F32, +tint: U32) -> Gui.Pen:
match pen:
case GuiPenDefault{frame}: GuiPenDefault{J.Frame.draw(frame, FontCore.codepoint(FontCore.default.face(), 1, 128.0, 128.0, cp, x, y, size, tint))}
case GuiPenFont{frame, font}: Gui.pen.drawn(J.Draw.text_codepoint(frame, font, cp, M.Vector2{x, y}, size, tint))
def Gui.dot(visible: Bool, pen: Gui.Pen, +x: F32, +y: F32, +size: F32, +tint: U32) -> Gui.Pen:
match visible:
case False{}: pen
case True{}: Gui.glyph.draw(pen, 46, x, y, size, tint)
# The dots of an overflowing line: at j = 0, ellipsis/3, ... while j < ellipsis
# (at most five for a positive step).
def Gui.dots(n: Nat, +j: F32, +step: F32, +ellipsis: F32, +x: F32, +y: F32, +size: F32, +tint: U32, pen: Gui.Pen) -> Gui.Pen:
match n:
case 0n: pen
case 1n+p: Gui.dots(p, (j + step : F32), step, ellipsis, x, y, size, tint, Gui.dot((j < ellipsis : F32), pen, (x + j : F32), y, size, tint))
# The C loop never ends for a step of 0 (an ellipsis narrower than 3).
def Gui.ellipsis(endless: Bool, +ellipsis: F32, +x: F32, +y: F32, +size: F32, +tint: U32, pen: Gui.Pen) -> Gui.Pen:
match endless:
case True{}: Gui.pen.refuse(pen)
case False{}: Gui.dots(6n, 0.0, F32.trunc((ellipsis / 3.0 : F32)), ellipsis, x, y, size, tint, pen)
# inset is what an icon takes from the line's width (16 times the icon scale, plus 4), or 0.
type Gui.Line is Data:
GuiLine{face: FontCore.Face, x: F32, y: F32, width: F32, text_width: F32, ellipsis: F32, scale: F32, spacing: F32, size: F32, tint: U32, inset: F32}
def Gui.glyphs.overflow(overflow: Bool, kind: U32) -> Bool:
match kind:
case 2: True{}
case _: overflow
# kind 0: nothing; 1: the glyph; 2: the ellipsis.
def Gui.glyphs.one(kind: U32, +cp: U32, +x: F32, +y: F32, +offset: F32, +ellipsis: F32, +size: F32, +tint: U32, pen: Gui.Pen) -> Gui.Pen:
match kind:
case 0: pen
case 1: Gui.glyph.draw(pen, cp, (x + offset : F32), y, size, tint)
case _: Gui.ellipsis((F32.trunc((ellipsis / 3.0 : F32)) < 1.0 : F32), ellipsis, (x + offset : F32), y, size, tint, pen)
# One glyph of a line: drawn unless it is a space or a tab; a line wider than
# its bounds stops at the ellipsis.
def Gui.glyphs(cps: +List<U32>, +line: Gui.Line, +offset: F32, +overflow: Bool, pen: Gui.Pen) -> Gui.Pen:
match cps:
case Nil{}: pen
case Con{+cp, rest}:
GuiLine{+face, +x, +y, +width, +text_width, +ellipsis, +scale, +spacing, +size, +tint, +inset} = line
+glyph = Gui.glyph.width(face, cp, scale)
+blank = U32.is_eq(cp, 32) || U32.is_eq(cp, 9)
+wide = (text_width > width : F32)
+fits = (offset <= ((width - glyph) - inset) - ellipsis : F32)
+kind = Bool.pick(U32, blank, 0, Bool.pick(U32, Bool.not(wide), 1, Bool.pick(U32, fits, 1, Bool.pick(U32, overflow, 0, 2))))
Gui.glyphs(rest, line, (offset + (glyph + spacing) : F32), Gui.glyphs.overflow(overflow, kind), Gui.glyphs.one(kind, cp, x, y, offset, ellipsis, size, tint, pen))
# The x of a line by its alignment, pixel-aligned ((float)(int)x).
def Gui.line.x(alignment: U32, +bx: F32, +bw: F32, +text_width: F32) -> F32:
match alignment:
case 0: bx
case 1: (bx + bw / 2.0 - F32.trunc((text_width / 2.0 : F32)) : F32)
case _: (bx + bw - text_width : F32)
# (int)h % 2, raygui's pixel-perfect vertical offset.
def Gui.pixel(+bh: F32) -> F32:
(F32.trunc(bh) - 2.0 * F32.trunc((F32.trunc(bh) / 2.0 : F32)) : F32)
# The y of a line by TEXT_ALIGNMENT_VERTICAL (TOP, MIDDLE, BOTTOM).
def Gui.line.y(vertical: U32, +by: F32, +bh: F32, +offset: F32, +total: F32) -> F32:
match vertical:
case 0: (by + offset : F32)
case 1: (by + offset + bh / 2.0 - total / 2.0 + Gui.pixel(bh) : F32)
case _: (by + offset + bh - total + Gui.pixel(bh) : F32)
# -----------------------------------------------------------------------------
# GuiDrawRectangle
def Gui.border(visible: Bool, frame: J.Frame, +x: F32, +y: F32, +w: F32, +h: F32, +b: F32, +color: U32) -> J.Frame:
match visible:
case False{}: frame
case True{}:
f1 = J.Draw.rectangle(frame, x, y, w, b, color)
f2 = J.Draw.rectangle(f1, x, (y + b : F32), b, (h - 2.0 * b : F32), color)
f3 = J.Draw.rectangle(f2, (x + w - b : F32), (y + b : F32), b, (h - 2.0 * b : F32), color)
J.Draw.rectangle(f3, x, (y + h - b : F32), w, b, color)
def Gui.fill(visible: Bool, frame: J.Frame, +x: F32, +y: F32, +w: F32, +h: F32, +color: U32) -> J.Frame:
match visible:
case False{}: frame
case True{}: J.Draw.rectangle(frame, x, y, w, h, color)
# GuiDrawRectangle(rec, borderWidth, borderColor, color), the colors already
# faded: the fill when its alpha is above 0, then four border rectangles, all
# with (int) coordinates.
def Gui.draw_rectangle(frame: J.Frame, +rec: J.Rectangle, +border: U32, +border_color: U32, +color: U32) -> J.Frame:
J.Rectangle{+rx, +ry, +rw, +rh} = rec
+x = F32.trunc(rx)
+y = F32.trunc(ry)
+w = F32.trunc(rw)
+h = F32.trunc(rh)
Gui.border((border > 0 : U32), Gui.fill(((color .&. 255) > 0 : U32), frame, x, y, w, h, color), x, y, w, h, U32.to_f32(border), border_color)
# -----------------------------------------------------------------------------
# GuiDrawIcon and GuiDrawText
def Gui.icon.bit(visible: Bool, frame: J.Frame, +x: F32, +y: F32, +pixel: F32, +color: U32) -> J.Frame:
match visible:
case False{}: frame
case True{}: Gui.draw_rectangle(frame, J.Rectangle{x, y, pixel, pixel}, 0, 0, color)
# The 32 bits of one word: two rows of 16 pixels, the low bit first. The
# offsets are C's int products (scale may be negative), then floats.
def Gui.icon.word(n: Nat, +bits: U32, +k: U32, +x: F32, +y: F32, +row: U32, +scale: U32, +color: U32, frame: J.Frame) -> J.Frame:
match n:
case 0n: frame
case 1n+p: Gui.icon.word(p, (bits >> 1n : U32), (k + 1 : U32), x, y, row, scale, color,
Gui.icon.bit(U32.is_eq((bits .&. 1 : U32), 1), frame, (x + FontCore.int.f32(((k % 16) * scale : U32)) : F32), (y + FontCore.int.f32(((row + k / 16) * scale : U32)) : F32), FontCore.int.f32(scale), color))
def Gui.icon.rows(n: Nat, words: +List<U32>, +index: U32, +x: F32, +y: F32, +scale: U32, +color: U32, frame: J.Frame) -> J.Frame:
match n words:
case 0n _: frame
case 1n+p Nil{}: frame
case 1n+p Con{+bits, +rest}: Gui.icon.rows(p, rest, (index + 1 : U32), x, y, scale, color, Gui.icon.word(32n, bits, 0, x, y, (2 * index : U32), scale, color, frame))
# GuiDrawIcon(iconId, x, y, scale, color) over the gui's icon table, the
# color already faded: one GuiDrawRectangle of scale x scale pixels for each
# set bit.
def Gui.icon.drawn(has: Bool, +icons: +List<U32>, +id: U32, +x: F32, +y: F32, +scale: U32, +color: U32, frame: J.Frame) -> J.Frame:
match has:
case False{}: frame
case True{}: Gui.icon.rows(8n, List.drop(&2, U32, icons, U32.to_nat((8 * id : U32))), 0, x, y, scale, color, frame)
def Gui.pen.icon(has: Bool, +icons: +List<U32>, +id: U32, +x: F32, +y: F32, +scale: U32, +color: U32, pen: Gui.Pen) -> Gui.Pen:
match pen:
case GuiPenDefault{frame}: GuiPenDefault{Gui.icon.drawn(has, icons, id, x, y, scale, color, frame)}
case GuiPenFont{frame, font}: GuiPenFont{Gui.icon.drawn(has, icons, id, x, y, scale, color, frame), font}
# One line: its icon, if any, at the line's x and centered on the bounds'
# height (16 pixels times the icon scale), then the text 4 pixels after it. Only the text's last line can be
# empty for raygui (the others still have their '\n' to read).
def Gui.text.line(+icon: Gui.Icon, +last: Bool, +gui: Gui, +bounds: J.Rectangle, +alignment: U32, +tint: U32, +offset: F32, +total: F32, pen: Gui.Pen) -> Gui.Pen:
GuiIcon{+id, +text} = icon
J.Rectangle{+bx, +by, +bw, +bh} = bounds
+has = U32.is_ne(id, 4294967295)
+size = Gui.style.f32(gui, 0, 16)
+empty = last && Gui.empty(text)
+scale = Gui.icon_scale(gui)
+wide = U32.to_f32((16 * scale : U32))
+text_width = (Gui.width.line(gui, text) + Bool.pick(F32, has, Bool.pick(F32, empty, wide, (wide + 4.0 : F32)), 0.0) : F32)
+x = F32.trunc(Bool.pick(F32, (text_width > bw : F32) && Bool.not(empty), bx, Gui.line.x(alignment, bx, bw, text_width)))
+y = F32.trunc(Gui.line.y(Gui.get_style(gui, 0, 21), by, bh, offset, total))
+inset = Bool.pick(F32, has, (wide + 4.0 : F32), 0.0)
+spec = {GuiLine{Gui.face(gui), (x + inset : F32), y, bw, text_width, Gui.width.bytes(gui, [46, 46, 46]), Gui.scale(gui), Gui.style.f32(gui, 0, 17), size, tint, inset} : Gui.Line}
Gui.glyphs(TextCore.codepoints(Gui.line.bytes(text, True{})), spec, 0.0, False{},
Gui.pen.icon(has, Gui.icons(gui), id, x, F32.trunc((((by + bh / 2.0 : F32) - U32.to_f32((8 * scale : U32)) : F32) + Gui.pixel(bh) : F32)), scale, tint, pen))
def Gui.lines.last(rest: +List<+List<U32>>) -> Bool:
match rest:
case Nil{}: True{}
case Con{_, _}: False{}
def Gui.text.lines(lines: +List<+List<U32>>, +gui: Gui, +bounds: J.Rectangle, +alignment: U32, +tint: U32, +offset: F32, +total: F32, pen: Gui.Pen) -> Gui.Pen:
match lines:
case Nil{}: pen
case Con{+line, +rest}:
Gui.text.lines(rest, gui, bounds, alignment, tint, (offset + (Gui.style.f32(gui, 0, 16) + Gui.style.f32(gui, 0, 20)) : F32), total,
Gui.text.line(Gui.icon.of(line), Gui.lines.last(rest), gui, bounds, alignment, tint, offset, total, pen))
# A line raygui does not draw from its own bytes: an icon whose words lie
# past the icon table (raygui reads them there).
def Gui.icon.in(icon: Gui.Icon) -> Bool:
GuiIcon{id, _} = icon
U32.is_ne(id, 4294967295)
# The table is measured only for a line with an icon.
def Gui.icon.missing(icon: Bool, +icons: +List<U32>, +id: U32) -> Bool:
match icon:
case False{}: False{}
case True{}: Bool.not(Gui.icons.has(icons, id))
def Gui.icon.out(+icon: Gui.Icon, +icons: +List<U32>) -> Bool:
GuiIcon{+id, _} = icon
Gui.icon.missing(U32.is_ne(id, 4294967295), icons, id)
def Gui.has_icon(lines: +List<+List<U32>>, +icons: +List<U32>) -> Bool:
match lines:
case Nil{}: False{}
case Con{+line, +rest}: Gui.icon.out(Gui.icon.of(line), icons) || Gui.has_icon(rest, icons)
def Gui.count(lines: +List<+List<U32>>, +n: F32) -> F32:
match lines:
case Nil{}: n
case Con{_, rest}: Gui.count(rest, (n + 1.0 : F32))
def Gui.text.checked(refused: Bool, +lines: +List<+List<U32>>, +gui: Gui, +bounds: J.Rectangle, +alignment: U32, +tint: U32, frame: J.Frame) -> J.Frame:
match refused:
case True{}: J.Frame.refuse(frame)
case False{}:
+count = Gui.count(lines, 0.0)
+total = (count * Gui.style.f32(gui, 0, 16) + (count - 1.0) * Gui.style.f32(gui, 0, 20) : F32)
Gui.pen.close(Gui.text.lines(lines, gui, bounds, alignment, tint, 0.0, total, Gui.pen.open(gui, frame)))
def Gui.icon.some(lines: +List<+List<U32>>) -> Bool:
match lines:
case Nil{}: False{}
case Con{+line, rest}: Gui.icon.in(Gui.icon.of(line)) || Gui.icon.some(rest)
def Gui.draw_text.lines(+lines: +List<+List<U32>>, +gui: Gui, frame: J.Frame, +bounds: J.Rectangle, +alignment: U32, +tint: U32) -> J.Frame:
Gui.text.checked(Gui.has_icon(lines, Gui.icons(gui)) || U32.is_ne(Gui.get_style(gui, 0, 22), 0) || ((Gui.icon_scale(gui) > 65536 : U32) && Gui.icon.some(lines)),
lines, gui, bounds, alignment, tint, frame)
# GuiDrawText(text, bounds, alignment, tint): nothing for an empty text. The
# frame is undefined for a line Gui.icon.out names, a wrap mode, or an icon
# drawn at a scale above 65536.
def Gui.draw_text.bytes(+bytes: +List<U32>, +gui: Gui, frame: J.Frame, +bounds: J.Rectangle, +alignment: U32, +tint: U32) -> J.Frame:
match bytes:
case Nil{}: frame
case Con{b, rest}: Gui.draw_text.lines(Gui.lines.go(Con{b, rest}, Nil{}), gui, frame, bounds, alignment, tint)
def Gui.draw_text(+gui: Gui, frame: J.Frame, text: String, +bounds: J.Rectangle, +alignment: U32, +tint: U32) -> J.Frame:
Gui.draw_text.bytes(TextCore.bytes(text), gui, frame, bounds, alignment, tint)
def Gui.draw_text.maybe(+gui: Gui, frame: J.Frame, text: Maybe<String>, +bounds: J.Rectangle, +alignment: U32, +tint: U32) -> J.Frame:
match text:
case None{}: frame
case Some{t}: Gui.draw_text(gui, frame, t, bounds, alignment, tint)
# -----------------------------------------------------------------------------
# GetTextBounds
# GetTextBounds(control, bounds).
def Gui.text_bounds(+gui: Gui, +control: U32, +bounds: J.Rectangle) -> J.Rectangle:
J.Rectangle{+x, +y, +w, +h} = bounds
+border = Gui.style.f32(gui, control, 12)
+padding = Gui.style.f32(gui, control, 13)
+tx = (x + border : F32)
J.Rectangle{Bool.pick(F32, U32.is_eq(Gui.get_style(gui, control, 14), 2), (tx - padding : F32), (tx + padding : F32)), (y + border + padding : F32),
(w - 2.0 * border - 2.0 * padding : F32), (h - 2.0 * border - 2.0 * padding : F32)}
# -----------------------------------------------------------------------------
# Pointer (GUI_POINTER_POSITION, GUI_BUTTON_DOWN, GUI_BUTTON_RELEASED)
def Gui.pad(button: Maybe<Bool>) -> Bool:
match button:
case Some{b}: b
case None{}: False{}
def Gui.down(+core: J.Core) -> Bool:
J.Input.is_mouse_button_down(core, 0) || Gui.pad(J.Input.is_gamepad_button_down(core, 0, 7))
def Gui.released(+core: J.Core) -> Bool:
J.Input.is_mouse_button_released(core, 0) || Gui.pad(J.Input.is_gamepad_button_released(core, 0, 7))
# Whether a control reacts: not STATE_DISABLED and not locked.
def Gui.active(+gui: Gui) -> Bool:
Gui{+state, +locked, _, _, _, _, _, _, _, _, _, _} = gui
U32.is_ne(state, 3) && Bool.not(locked)
def Gui.exclusive(gui: Gui) -> Bool:
Gui{_, _, _, exclusive, _, _, _, _, _, _, _, _} = gui
exclusive
# The state a hovered control takes: PRESSED while the button is down, else FOCUSED.
def Gui.hover(+core: J.Core) -> U32:
Bool.pick(U32, Gui.down(core), 2, 1)
# -----------------------------------------------------------------------------
# GuiDrawIcon, GuiIconText, GuiLoadIcons
# Whether C's int stays in range for an icon at x, y of pixel size: |x| and
# |y| below 2^30 and |size| below 2^26 (15 times the size added to either).
def Gui.icon.near(+v: U32, +limit: U32) -> Bool:
(v < limit : U32) || ((0 - v : U32) < limit : U32)
# GuiDrawIcon(iconId, posX, posY, pixelSize, color): a GuiDrawRectangle of
# pixelSize square at (posX + (k%16)*pixelSize, posY + row*pixelSize) for
# each set bit of the icon in the gui's table, faded by guiAlpha. Ints are
# given as their bits. An id whose words are not in the table (a negative
# one, 512 or more at start, past the icons GuiLoadIcons loaded: raygui
# reads past its table) and coordinates that may leave int's range (see
# Gui.icon.near) mark the frame undefined.
def Gui.draw_icon(+gui: Gui, frame: J.Frame, +id: U32, +x: U32, +y: U32, +size: U32, +color: U32) -> J.Frame:
+fine = Gui.icons.has(Gui.icons(gui), id) && Gui.icon.near(x, 1073741824) && Gui.icon.near(y, 1073741824) && Gui.icon.near(size, 67108864)
Gui.refused(Bool.not(fine), Gui.icon.drawn(fine, Gui.icons(gui), id, FontCore.int.f32(x), FontCore.int.f32(y), size, Gui.fade(color, Gui.alpha.of(gui)), frame))
def Gui.icon_text.cut(+long: Bool, +bytes: +List<U32>) -> Maybe<String>:
match long:
case True{}: None{}
case False{}: Some{TextCore.string(bytes)}
def Gui.icon_text.with(text: Maybe<String>, +prefix: +List<U32>) -> Maybe<String>:
match text:
case None{}: Some{TextCore.string(prefix)}
case Some{t}:
+bytes = TextCore.bytes(t)
Gui.icon_text.cut((TextCore.len(bytes) >= 1019 : U32), TextCore.glue(TextCore.take(prefix, 5), bytes))
# GuiIconText(iconId, text): "#%03i#" (the id as C's int, at least three
# digits after any sign), then the text from the prefix's sixth byte, so a
# prefix of six or more bytes loses its last ones; a NULL text (None) gives
# the whole prefix. A text of 1019 bytes or more leaves raygui's buffer
# without its terminator: None.
def Gui.icon_text(+id: U32, text: Maybe<String>) -> Maybe<String>:
Gui.icon_text.with(text, TextCore.glue([35], TextCore.glue(TextCore.integer.field(True{}, 10, False{}, id, 16, 3, False{}, 0), [35])))
# What GuiLoadIcons returns: NULL, the name ids (32 bytes each, as read),
# or a load raygui does not define.
type Gui.Names is Data:
GuiNamesNull{}
GuiNames{names: +List<+List<U32>>}
GuiNamesUndefined{}
def Gui.byte(bytes: +List<U32>, +i: U32) -> U32:
Icons.head(TextCore.drop(bytes, i))
def Gui.icons.word(+bytes: +List<U32>) -> U32:
(Gui.byte(bytes, 0) .|. (Gui.byte(bytes, 1) << 8n) .|. (Gui.byte(bytes, 2) << 16n) .|. (Gui.byte(bytes, 3) << 24n) : U32)
# The first n words of the bytes (little-endian), the table raygui
# allocates and copies them into.
def Gui.icons.words(n: Nat, +bytes: +List<U32>, +acc: +List<U32>) -> +List<U32>:
match n:
case 0n: List.reverse(&2, U32, acc)
case 1n+p: Gui.icons.words(p, TextCore.drop(bytes, 4), Con{Gui.icons.word(bytes), acc})
def Gui.icons.name(n: Nat, +bytes: +List<U32>) -> +List<U32>:
match n:
case 0n: Nil{}
case 1n+p: Con{Icons.head(bytes), Gui.icons.name(p, TextCore.drop(bytes, 1))}
def Gui.icons.names(n: Nat, +bytes: +List<U32>) -> +List<+List<U32>>:
match n:
case 0n: Nil{}
case 1n+p: Con{Gui.icons.name(32n, bytes), Gui.icons.names(p, TextCore.drop(bytes, 32))}
def Gui.icons.named(names: Bool, +count: U32, +bytes: +List<U32>) -> Gui.Names:
match names:
case False{}: GuiNamesNull{}
case True{}: GuiNames{Gui.icons.names(U32.to_nat(count), TextCore.drop(bytes, 12))}
def Gui.icons.fine(bad: Bool, +gui: Gui, +bytes: +List<U32>, +names: Bool, +count: U32, +words: U32) -> Gui & Gui.Names:
match bad:
case True{}: (gui, GuiNamesUndefined{})
case False{}: (Gui.icons.put(gui, Gui.icons.words(U32.to_nat(words), TextCore.drop(bytes, (12 + 32 * count : U32)), Nil{})), Gui.icons.named(names, count, bytes))
# A signed file: the icon count and size are shorts; the table becomes
# count*(size*size/32) words (C's int arithmetic). Undefined: a negative
# count (a gap where no word is read), an int overflow of the table's byte
# size, more than 512 name ids asked (raygui's name array), and name ids or
# words read past the data.
def Gui.icons.signed(signed: Bool, +gui: Gui, +bytes: +List<U32>, +names: Bool) -> Gui & Gui.Names:
match signed:
case False{}: (gui, GuiNamesNull{})
case True{}:
+n = TextCore.len(bytes)
+count = (Gui.byte(bytes, 8) .|. (Gui.byte(bytes, 9) << 8n) : U32)
+side = (Gui.byte(bytes, 10) .|. (Gui.byte(bytes, 11) << 8n) : U32)
+m = Bool.pick(U32, (side >= 32768 : U32), (65536 - side : U32), side)
+k = ((m * m : U32) / 32 : U32)
+over = (count > 0 : U32) && (k > (536870911 / Bool.pick(U32, U32.is_eq(count, 0), 1, count) : U32) : U32)
+words = Bool.pick(U32, over, 0, (count * k : U32))
+start = (12 + 32 * (count .&. 32767 : U32) : U32)
+named_past = names && (((count > 512 : U32)) || (start > n : U32))
+data_past = (words > 0 : U32) && ((start > n : U32) || ((4 * words : U32) > (n - start : U32) : U32))
Gui.icons.fine((count >= 32768 : U32) || over || named_past || data_past, gui, bytes, names, count, words)
def Gui.icons.headed(fits: Bool, +gui: Gui, +bytes: +List<U32>, +names: Bool) -> Gui & Gui.Names:
match fits:
case False{}: (gui, GuiNamesUndefined{})
case True{}: Gui.icons.signed(U32.is_eq(Gui.byte(bytes, 0), 114) && U32.is_eq(Gui.byte(bytes, 1), 71) && U32.is_eq(Gui.byte(bytes, 2), 73) && U32.is_eq(Gui.byte(bytes, 3), 32), gui, bytes, names)
# GuiLoadIconsFromMemory(fileData, dataSize, loadIconsName) over a .rgi
# file's bytes ("rGI ", version, reserved, icon count and size as
# little-endian shorts, a 32-byte name id an icon, then the icons' words):
# with the signature, the gui's table becomes the icon count times
# size*size/32 words (C's int division) read from the data, whatever the
# icon size, and the name ids are returned when asked (GuiNames); GuiNamesNull
# for another signature or names not asked. Undefined (GuiNamesUndefined,
# the gui unchanged): data shorter than the 12-byte header, a negative icon
# count (a gap), a table whose byte size overflows an int, more than 512 name
# ids asked, and name ids or words past the data.
def Gui.load_icons_from_memory(gui: Gui, +bytes: +List<U32>, +names: Bool) -> Gui & Gui.Names:
Gui.icons.headed((TextCore.len(bytes) >= 12 : U32), gui, bytes, names)
def Gui.icons.sized(empty: Bool, +gui: Gui, +bytes: +List<U32>, +names: Bool) -> Gui & Gui.Names:
match empty:
case True{}: (gui, GuiNamesNull{})
case False{}: Gui.load_icons_from_memory(gui, bytes, names)
def Gui.icons.loaded(r: Result<&1, &1, J.Surface.IOError, +List<U32>>, +gui: Gui, +names: Bool) -> Gui & Gui.Names:
match r:
case Fail{_}: (gui, GuiNamesNull{})
case Done{+bytes}: Gui.icons.sized(Gui.empty(bytes), gui, bytes, names)
# GuiLoadIcons(fileName, loadIconsName): the whole file through
# GuiLoadIconsFromMemory; NULL (GuiNamesNull, the gui unchanged) for a file
# that does not open or read, or an empty one.
def Gui.load_icons(gui: Gui, path: String, +names: Bool) -> IO(Gui & Gui.Names):
IO.bind(Result<&1, &1, J.Surface.IOError, +List<U32>>, Gui & Gui.Names, J.Files.load_data(path), r => IO.pure(Gui & Gui.Names, Gui.icons.loaded(r, gui, names)))
# -----------------------------------------------------------------------------
# GuiLabel, GuiButton, GuiCheckBox
# GuiLabel(bounds, text).
def Gui.label(+gui: Gui, frame: J.Frame, +bounds: J.Rectangle, text: String) -> J.Frame:
Gui.draw_text(gui, frame, text, Gui.text_bounds(gui, 1, bounds), Gui.get_style(gui, 1, 14), Gui.color(gui, 1, (2 + Gui.get_state(gui) * 3 : U32)))
# The color of GuiLine and GuiGroupBox: DEFAULT's LINE_COLOR (18), or its
# BORDER_COLOR_DISABLED (9) in STATE_DISABLED.
def Gui.line.property(+state: U32) -> U32:
Bool.pick(U32, U32.is_eq(state, 3), 9, 18)
# GuiLine with a text, "--- text ------": a rule of 8 pixels, the text from
# x + 12 in a box of its width + 2, and the rule again from 4 pixels after
# it to the right edge. A text with an icon prefix marks the frame undefined.
def Gui.line.text(width: Maybe<F32>, +gui: Gui, frame: J.Frame, +bounds: J.Rectangle, text: String, +color: U32) -> J.Frame:
match width:
case None{}: J.Frame.refuse(frame)
case Some{+w}:
J.Rectangle{+x, +y, +bw, +bh} = bounds
+tw = (w + 2.0 : F32)
+middle = (y + bh / 2.0 : F32)
f1 = Gui.draw_rectangle(frame, J.Rectangle{x, middle, 8.0, 1.0}, 0, 0, color)
f2 = Gui.draw_text(gui, f1, text, J.Rectangle{(x + 12.0 : F32), y, tw, bh}, 0, color)
Gui.draw_rectangle(f2, J.Rectangle{(((x + 12.0 : F32) + tw : F32) + 4.0 : F32), middle, (((bw - tw : F32) - 12.0 : F32) - 4.0 : F32), 1.0}, 0, 0, color)
def Gui.line.rule(frame: J.Frame, +bounds: J.Rectangle, +color: U32) -> J.Frame:
J.Rectangle{+x, +y, +w, +h} = bounds
Gui.draw_rectangle(frame, J.Rectangle{x, (y + h / 2.0 : F32), w, 1.0}, 0, 0, color)
def Gui.line.colored(text: Maybe<String>, +gui: Gui, frame: J.Frame, +bounds: J.Rectangle, +color: U32) -> J.Frame:
match text:
case None{}: Gui.line.rule(frame, bounds, color)
case Some{+t}: Gui.line.text(Gui.get_text_width(gui, t), gui, frame, bounds, t, color)
# GuiLine(bounds, text): a one-pixel rule at the middle of the bounds, with
# the text set into it when one is given (NULL is None).
def Gui.line(+gui: Gui, frame: J.Frame, +bounds: J.Rectangle, text: Maybe<String>) -> J.Frame:
Gui.line.colored(text, gui, frame, bounds, Gui.color(gui, 0, Gui.line.property(Gui.get_state(gui))))
# GuiGroupBox(bounds, text): the left, bottom and right edges, one pixel
# thick, and GuiLine across the top, TEXT_SIZE high and half of it (an
# integer division) above the bounds.
def Gui.group_box(+gui: Gui, frame: J.Frame, +bounds: J.Rectangle, text: Maybe<String>) -> J.Frame:
J.Rectangle{+x, +y, +w, +h} = bounds
+color = Gui.color(gui, 0, Gui.line.property(Gui.get_state(gui)))
+size = Gui.get_style(gui, 0, 16)
f1 = Gui.draw_rectangle(frame, J.Rectangle{x, y, 1.0, h}, 0, 0, color)
f2 = Gui.draw_rectangle(f1, J.Rectangle{x, ((y + h : F32) - 1.0 : F32), w, 1.0}, 0, 0, color)
f3 = Gui.draw_rectangle(f2, J.Rectangle{((x + w : F32) - 1.0 : F32), y, 1.0, h}, 0, 0, color)
Gui.line(gui, f3, J.Rectangle{x, (y - U32.to_f32((size / 2 : U32)) : F32), w, U32.to_f32(size)}, text)
# -----------------------------------------------------------------------------
# GuiTooltip
# The panel's rectangle: a border of 1 in LINE_COLOR over BACKGROUND_COLOR
# (BORDER_COLOR_DISABLED over BASE_COLOR_DISABLED when disabled).
def Gui.panel.body(+gui: Gui, frame: J.Frame, +bounds: J.Rectangle) -> J.Frame:
+disabled = U32.is_eq(Gui.get_state(gui), 3)
Gui.draw_rectangle(frame, bounds, 1, Gui.color(gui, 0, Bool.pick(U32, disabled, 9, 18)), Gui.color(gui, 0, Bool.pick(U32, disabled, 10, 19)))
# Whether raygui draws a tooltip: unlocked, tooltips on, a tooltip set and no
# slider dragged.
def Gui.tip.shown(gui: Gui) -> Bool:
Gui{_, locked, _, exclusive, _, _, _, tips, _, tipped, _, _} = gui
Bool.not(locked) && tips && tipped && Bool.not(exclusive)
def Gui.tip.text(gui: Gui) -> String:
Gui{_, _, _, _, _, _, _, _, tip, _, _, _} = gui
tip
# GetTextLines's count: the lines, at most 128.
def Gui.tip.count(lines: +List<+List<U32>>, +n: U32) -> U32:
match lines:
case Nil{}: n
case Con{_, rest}: Gui.tip.count(rest, U32.min((n + 1 : U32), 128))