-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImGUI.bas
1004 lines (735 loc) · 38.2 KB
/
ImGUI.bas
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
'-----------------------------------------------------------------------------------------------------------------------
' A simple GUI library
' Copyright (c) 2024 Samuel Gomes
'
' This is very loosely based on Terry Ritchie's GLINPUT & RQBL
' The library has an input manager, tabbed focus and implements text box and push button widgets (so far)
' The framebuffer needs to be redrawn every frame and nothing the widgets drawn over is preserved
' This was born because I needed a small, fast and intuitive GUI libary for games and graphic applications
' This is a work in progress
'-----------------------------------------------------------------------------------------------------------------------
$INCLUDEONCE
'$INCLUDE:'ImGUI.bi'
' Calculates the bounding rectangle for a object given its position & size
SUB RectangleCreate (p AS Vector2LType, s AS Vector2LType, r AS RectangleType)
r.a.x = p.x
r.a.y = p.y
r.b.x = p.x + s.x - 1
r.b.y = p.y + s.y - 1
END SUB
' Does big contain small?
FUNCTION RectangleContainsRectangle%% (big AS RectangleType, small AS RectangleType)
RectangleContainsRectangle = PointCollidesWithRectangle(small.a, big) AND PointCollidesWithRectangle(small.b, big)
END FUNCTION
' Point & box collision test
FUNCTION PointCollidesWithRectangle%% (p AS Vector2LType, r AS RectangleType)
PointCollidesWithRectangle = NOT (p.x < r.a.x OR p.x > r.b.x OR p.y < r.a.y OR p.y > r.b.y)
END FUNCTION
' Draws a basic 3D box
' This can be improved ... a lot XD
' Also all colors are hardcoded
SUB WidgetDrawBox3D (r AS RectangleType, depressed AS _BYTE)
IF depressed THEN
' sunken
Graphics_DrawHorizontalLine r.a.x, r.a.y, r.b.x - 1, &HFF696969
Graphics_DrawVerticalLine r.a.x, r.a.y, r.b.y - 1, &HFF696969
Graphics_DrawHorizontalLine r.a.x, r.b.y, r.b.x, &HFFD3D3D3
Graphics_DrawVerticalLine r.b.x, r.a.y, r.b.y - 1, &HFFD3D3D3
ELSE
' raised
Graphics_DrawHorizontalLine r.a.x, r.a.y, r.b.x - 1, &HFFD3D3D3
Graphics_DrawVerticalLine r.a.x, r.a.y, r.b.y - 1, &HFFD3D3D3
Graphics_DrawHorizontalLine r.a.x, r.b.y, r.b.x, &HFF696969
Graphics_DrawVerticalLine r.b.x, r.a.y, r.b.y - 1, &HFF696969
END IF
Graphics_DrawFilledRectangle r.a.x + 1, r.a.y + 1, r.b.x - 1, r.b.y - 1, &HFF808080
END SUB
' This routine ties the whole update system and makes everything go
SUB WidgetUpdate
STATIC blinkTick AS _INTEGER64 ' stores the last blink tick (oooh!)
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
SHARED InputManager AS InputManagerType
DIM h AS LONG, r AS RectangleType, currentTick AS _INTEGER64
InputManagerUpdate ' We will gather input even if there are no widgets
IF UBOUND(Widget) = NULL THEN EXIT SUB ' Exit if there is nothing to do
' Blinky stuff
currentTick = Time_GetTicks
IF currentTick > blinkTick + WIDGET_BLINK_INTERVAL THEN
blinkTick = currentTick
WidgetManager.focusBlink = NOT WidgetManager.focusBlink
END IF
' Manage widget focus stuff
IF WidgetManager.current = NULL THEN WidgetManager.current = 1 ' if this is first time set current widget to 1
' Shift focus if it was requested
IF WidgetManager.forced <> NULL THEN ' being forced to a widget
IF WidgetManager.forced = -1 THEN ' yes, to the next one?
h = WidgetManager.current ' set scanner to current widget
DO ' start scanning
h = h + 1 ' move scanner to next handle number
IF h > UBOUND(Widget) THEN h = 1 ' return to start of widget array if limit reached
IF Widget(h).inUse AND Widget(h).visible AND NOT Widget(h).disabled THEN WidgetManager.current = h ' set current widget if in use
LOOP UNTIL WidgetManager.current = h ' leave scanner when a widget in use is found
WidgetManager.forced = NULL ' reset force indicator
ELSE ' yes, to a specific input field
IF Widget(WidgetManager.forced).inUse AND Widget(WidgetManager.forced).visible AND NOT Widget(WidgetManager.forced).disabled THEN
WidgetManager.current = WidgetManager.forced ' set the current widget
END IF
WidgetManager.forced = NULL ' reset force indicator
END IF
END IF
' Check for user input requesting focus change
IF InputManager.keyCode = _KEY_TAB THEN
WidgetManager.forced = -1 ' Move to the next widget
InputManager.keyCode = NULL ' consume the key
END IF
' Check if the user is trying to click on something to change focus
FOR h = 1 TO UBOUND(Widget)
' Reset some stuff that the user should have handled last time
Widget(h).clicked = _FALSE
Widget(h).txt.entered = _FALSE
IF Widget(h).inUse AND Widget(h).visible AND NOT Widget(h).disabled AND h <> WidgetManager.current THEN
' Find the bounding box
RectangleCreate Widget(h).position, Widget(h).size, r
IF InputManager.mouseLeftClicked THEN
IF RectangleContainsRectangle(r, InputManager.mouseLeftButtonClickedRectangle) THEN
WidgetManager.forced = h ' Move to the specific widget
END IF
END IF
IF InputManager.mouseRightClicked THEN
IF RectangleContainsRectangle(r, InputManager.mouseRightButtonClickedRectangle) THEN
WidgetManager.forced = h ' Move to the specific widget
END IF
END IF
END IF
NEXT
' Run update for the widget that has focus
IF Widget(WidgetManager.current).inUse AND Widget(WidgetManager.current).visible AND NOT Widget(WidgetManager.current).disabled THEN
SELECT CASE Widget(WidgetManager.current).class
CASE WIDGET_PUSH_BUTTON
__PushButtonUpdate
CASE WIDGET_TEXT_BOX
__TextBoxUpdate
END SELECT
END IF
' Now draw all the widget to the framebuffer
FOR h = 1 TO UBOUND(Widget)
IF Widget(h).inUse AND Widget(h).visible THEN
SELECT CASE Widget(h).class
CASE WIDGET_PUSH_BUTTON
__PushButtonDraw h
CASE WIDGET_TEXT_BOX
__TextBoxDraw h
END SELECT
END IF
NEXT
END SUB
SUB InputManagerUpdate
SHARED InputManager AS InputManagerType
STATIC AS _BYTE mouseLeftButtonDown, mouseRightButtonDown ' keeps track if the mouse buttons were held down
' Collect mouse input
DO WHILE _MOUSEINPUT
InputManager.mousePosition.x = _MOUSEX
InputManager.mousePosition.y = _MOUSEY
InputManager.mouseLeftButton = _MOUSEBUTTON(1)
InputManager.mouseRightButton = _MOUSEBUTTON(2)
' Check if the left button were previously held down and update the up position if released
IF NOT InputManager.mouseLeftButton AND mouseLeftButtonDown THEN
mouseLeftButtonDown = _FALSE
InputManager.mouseLeftButtonClickedRectangle.b = InputManager.mousePosition
InputManager.mouseLeftClicked = _TRUE
END IF
' Check if the button were previously held down and update the up position if released
IF NOT InputManager.mouseRightButton AND mouseRightButtonDown THEN
mouseRightButtonDown = _FALSE
InputManager.mouseRightButtonClickedRectangle.b = InputManager.mousePosition
InputManager.mouseRightClicked = _TRUE
END IF
' Check if the mouse button was pressed and update the down position
IF InputManager.mouseLeftButton AND NOT mouseLeftButtonDown THEN
mouseLeftButtonDown = _TRUE
InputManager.mouseLeftButtonClickedRectangle.a = InputManager.mousePosition
InputManager.mouseLeftClicked = _FALSE
END IF
' Check if the mouse button was pressed and update the down position
IF InputManager.mouseRightButton AND NOT mouseRightButtonDown THEN
mouseRightButtonDown = _TRUE
InputManager.mouseRightButtonClickedRectangle.a = InputManager.mousePosition
InputManager.mouseRightClicked = _FALSE
END IF
LOOP
' Get keyboard input from the keyboard buffer
InputManager.keyCode = _KEYHIT
END SUB
' This gets the current keyboard input
FUNCTION InputManagerKey&
SHARED InputManager AS InputManagerType
InputManagerKey = InputManager.keyCode
END FUNCTION
' Get the mouse X position
FUNCTION InputManagerMouseX&
SHARED InputManager AS InputManagerType
InputManagerMouseX = InputManager.mousePosition.x
END FUNCTION
' Get the mouse Y position
FUNCTION InputManagerMouseY&
SHARED InputManager AS InputManagerType
InputManagerMouseY = InputManager.mousePosition.y
END FUNCTION
' Is the left mouse button down?
FUNCTION InputManagerMouseLeftButton%%
SHARED InputManager AS InputManagerType
InputManagerMouseLeftButton = InputManager.mouseLeftButton
END FUNCTION
' Is the right mouse button down?
FUNCTION InputManagerMouseRightButton%%
SHARED InputManager AS InputManagerType
InputManagerMouseRightButton = InputManager.mouseRightButton
END FUNCTION
' Returns the handle number of the widget that has focus
' The function will return 0 if there are no active widgets
FUNCTION WidgetCurrent&
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
WidgetCurrent = WidgetManager.current
END FUNCTION
' This set the focus on the widget handle that is passed
' The focus changes on the next update
' -1 = move to next widget
' >0 = move to a specific widget
SUB WidgetCurrent (handle AS LONG)
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
IF UBOUND(Widget) = NULL THEN EXIT SUB ' Leave if nothing is active
IF handle < -1 OR handle = NULL OR handle > UBOUND(Widget) THEN ' is handle valid?
ERROR _ERR_INVALID_HANDLE
END IF
WidgetManager.forced = handle ' inform WidgetUpdate to change the focus
END SUB
' Closes a specific widget
SUB WidgetFree (handle AS LONG)
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
IF UBOUND(Widget) = NULL THEN EXIT SUB ' leave if nothing is active
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN ' is handle valid?
ERROR _ERR_INVALID_HANDLE
END IF
' We will not bother resizing the widget array so that subsequent allocations will be faster
' So just set the 'inUse' member to false
Widget(handle).inUse = _FALSE
IF handle = WidgetManager.current THEN WidgetManager.forced = -1 ' Set focus on the next widget if it is current
END SUB
' Closes all widgets
SUB WidgetFreeAll
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
IF UBOUND(Widget) = NULL THEN EXIT SUB ' leave if nothing is active
REDIM Widget(NULL TO NULL) AS WidgetType ' reset the widget array
WidgetManager.current = NULL
WidgetManager.forced = NULL
END SUB
' Retrieves the text from a specific widget
FUNCTION WidgetText$ (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetText = Widget(handle).text
END FUNCTION
' Sets the text of a specific widget
SUB WidgetText (handle AS LONG, text AS STRING)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).text = text
Widget(handle).changed = _TRUE
END SUB
' Reports true if the ENTER key has been pressed on the input box
FUNCTION TextBoxEntered%% (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION ' leave if nothing is active
IF handle < 1 OR handle > UBOUND(Widget) OR Widget(handle).class <> WIDGET_TEXT_BOX THEN ' is handle valid?
ERROR _ERR_INVALID_HANDLE
END IF
TextBoxEntered = Widget(handle).txt.entered
END FUNCTION
' Returns true if the text field of a text box has changed
FUNCTION TextBoxChanged%% (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION ' leave if nothing is active
IF handle < 1 OR handle > UBOUND(Widget) OR Widget(handle).class <> WIDGET_TEXT_BOX THEN ' is handle valid?
ERROR _ERR_INVALID_HANDLE
END IF
TextBoxChanged = Widget(handle).changed
END FUNCTION
' Sets up a widget and returns a handle value that points to that widget
FUNCTION __WidgetNew& (class AS LONG)
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
IF class < NULL OR class > WIDGET_CLASS_COUNT THEN
ERROR _ERR_FEATURE_UNAVAILABLE
END IF
IF UBOUND(Widget) = NULL THEN ' Reallocate the widget array if this the first time
REDIM Widget(1 TO 1) AS WidgetType
Widget(1).inUse = _FALSE
END IF
DIM h AS LONG ' the new handle number
DO ' find available handle
h = h + 1
LOOP UNTIL NOT Widget(h).inUse OR h = UBOUND(Widget)
IF Widget(h).inUse THEN ' last one in use?
h = h + 1 ' use next handle
REDIM _PRESERVE Widget(1 TO h) AS WidgetType ' increase array size
END IF
DIM temp AS WidgetType
Widget(h) = temp ' ensure everything is wiped
Widget(h).inUse = _TRUE
Widget(h).class = class ' set the class
__WidgetNew = h ' return the handle
END FUNCTION
' Duplicates a widget from a designated handle
' Returns handle value greater than 0 indicating the new widgets handle
FUNCTION WidgetCopy& (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
DIM nh AS LONG
nh = __WidgetNew(NULL) ' creat a new widget of class 0. Whatever that is, is not important
Widget(nh) = Widget(handle) ' copy all properties
WidgetCopy = nh ' return new handle
END FUNCTION
' Creates a new button
FUNCTION PushButtonNew& (text AS STRING, x AS LONG, y AS LONG, w AS _UNSIGNED LONG, h AS _UNSIGNED LONG, toggleButton AS _BYTE)
SHARED Widget() AS WidgetType
DIM b AS LONG
b = __WidgetNew(WIDGET_PUSH_BUTTON)
Widget(b).text = text
Widget(b).position.x = x
Widget(b).position.y = y
Widget(b).size.x = w
Widget(b).size.y = h
Widget(b).visible = _TRUE
' Set class specific stuff
Widget(b).flags = toggleButton
PushButtonNew = b
END FUNCTION
' Create a new input box
FUNCTION TextBoxNew& (text AS STRING, x AS LONG, y AS LONG, w AS _UNSIGNED LONG, h AS _UNSIGNED LONG, flags AS _UNSIGNED LONG)
SHARED Widget() AS WidgetType
DIM t AS LONG ' the new handle number
t = __WidgetNew(WIDGET_TEXT_BOX)
Widget(t).text = text
Widget(t).position.x = x
Widget(t).position.y = y
Widget(t).size.x = w
Widget(t).size.y = h
Widget(t).visible = _TRUE
' Set class specific stuff
Widget(t).flags = flags ' store the flags
Widget(t).txt.textPosition = 1 ' set the cursor at the beginning of the input line
Widget(t).txt.boxPosition = 1
Widget(t).txt.boxTextLength = (w - _PRINTWIDTH("W") * 2) \ _PRINTWIDTH("W") ' calculate the number of character we can show at a time
Widget(t).txt.boxStartCharacter = 1
Widget(t).txt.insertMode = _TRUE ' initial insert mode to insert
TextBoxNew = t
END FUNCTION
' Hides / shows a widget on screen
SUB WidgetVisible (handle AS LONG, visible AS _BYTE)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).visible = visible
END SUB
' Returns if a widget is hidden or shown
FUNCTION WidgetVisible%% (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetVisible = Widget(handle).visible
END FUNCTION
' Sets all active widget to visible or invisible
SUB WidgetVisibleAll (visible AS _BYTE)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB ' leave if nothing is active
DIM h AS LONG
FOR h = 1 TO UBOUND(Widget)
IF Widget(h).inUse THEN Widget(h).visible = visible
NEXT
END SUB
' Hides / shows a widget on screen
SUB WidgetDisabled (handle AS LONG, disabled AS _BYTE)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).disabled = disabled
END SUB
' Returns if a widget is hidden or shown
FUNCTION WidgetDisabled%% (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetDisabled = Widget(handle).disabled
END FUNCTION
SUB WidgetPositionX (handle AS LONG, x AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).position.x = x
END SUB
FUNCTION WidgetPositionX& (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetPositionX = Widget(handle).position.x
END FUNCTION
SUB WidgetPositionY (handle AS LONG, y AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).position.y = y
END SUB
FUNCTION WidgetPositionY& (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetPositionY = Widget(handle).position.y
END FUNCTION
SUB WidgetSizeX (handle AS LONG, x AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).size.x = x
END SUB
FUNCTION WidgetSizeX& (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetSizeX = Widget(handle).size.x
END FUNCTION
SUB WidgetSizeY (handle AS LONG, y AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).size.y = y
END SUB
FUNCTION WidgetSizeY& (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetSizeY = Widget(handle).size.y
END FUNCTION
FUNCTION WidgetClicked%% (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse THEN
ERROR _ERR_INVALID_HANDLE
END IF
WidgetClicked = Widget(handle).clicked
END FUNCTION
SUB PushButtonDepressed (handle AS LONG, depressed AS _BYTE)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse OR Widget(handle).class <> WIDGET_PUSH_BUTTON THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).cmd.depressed = depressed
END SUB
FUNCTION PushButtonDepressed%% (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT FUNCTION
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse OR Widget(handle).class <> WIDGET_PUSH_BUTTON THEN
ERROR _ERR_INVALID_HANDLE
END IF
PushButtonDepressed = Widget(handle).cmd.depressed
END FUNCTION
' Toggles the button specified between pressed/depressed
SUB PushButtonToggleDepressed (handle AS LONG)
SHARED Widget() AS WidgetType
IF UBOUND(Widget) = NULL THEN EXIT SUB
IF handle < 1 OR handle > UBOUND(Widget) OR NOT Widget(handle).inUse OR Widget(handle).class <> WIDGET_PUSH_BUTTON THEN
ERROR _ERR_INVALID_HANDLE
END IF
Widget(handle).cmd.depressed = NOT Widget(handle).cmd.depressed
END SUB
' This will update the status of a button (state & clicked etc.) based on user input
' The calling function must ensure that this is called only for visible, enabled and the correct widget with focus
SUB __PushButtonUpdate
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
SHARED InputManager AS InputManagerType
DIM r AS RectangleType, clicked AS _BYTE
' Find the bounding box
RectangleCreate Widget(WidgetManager.current).position, Widget(WidgetManager.current).size, r
IF InputManager.mouseLeftClicked THEN
IF RectangleContainsRectangle(r, InputManager.mouseLeftButtonClickedRectangle) THEN
clicked = _TRUE
InputManager.mouseLeftClicked = _FALSE ' consume mouse click
END IF
END IF
IF InputManager.mouseRightClicked THEN
IF RectangleContainsRectangle(r, InputManager.mouseRightButtonClickedRectangle) THEN
clicked = _TRUE
InputManager.mouseRightClicked = _FALSE ' consume mouse click
END IF
END IF
IF InputManager.keyCode = _KEY_ENTER OR InputManager.keyCode = KEY_SPACE THEN
clicked = _TRUE
InputManager.keyCode = NULL ' consume keystroke
END IF
Widget(WidgetManager.current).clicked = clicked
' Toggle if this is a toggle button
IF clicked AND Widget(WidgetManager.current).flags THEN
Widget(WidgetManager.current).cmd.depressed = NOT Widget(WidgetManager.current).cmd.depressed
END IF
END SUB
' This will update the state of text box based on user input
' The calling function must ensure that this is called only for visible, enabled and the correct widget with focus
SUB __TextBoxUpdate
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
SHARED InputManager AS InputManagerType
Widget(WidgetManager.current).changed = _FALSE ' Set this to false
Widget(WidgetManager.current).txt.entered = _FALSE ' Set this to false too
' First process any pressed keys
SELECT CASE InputManager.keyCode ' which key was hit?
CASE _KEY_INSERT
Widget(WidgetManager.current).txt.insertMode = NOT Widget(WidgetManager.current).txt.insertMode
InputManager.keyCode = NULL ' consume the key
CASE _KEY_RIGHT
Widget(WidgetManager.current).txt.textPosition = Widget(WidgetManager.current).txt.textPosition + 1 ' increment the cursor position
IF Widget(WidgetManager.current).txt.textPosition > LEN(Widget(WidgetManager.current).text) + 1 THEN ' will this take the cursor too far?
Widget(WidgetManager.current).txt.textPosition = LEN(Widget(WidgetManager.current).text) + 1 ' yes, keep the cursor at the end of the line
END IF
' Box cursor movement
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.boxPosition + 1
IF Widget(WidgetManager.current).txt.boxPosition > Widget(WidgetManager.current).txt.textPosition THEN
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.textPosition
END IF
IF Widget(WidgetManager.current).txt.boxPosition > Widget(WidgetManager.current).txt.boxTextLength + 1 THEN
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.boxTextLength + 1
Widget(WidgetManager.current).txt.boxStartCharacter = 1 + LEN(Widget(WidgetManager.current).text) - Widget(WidgetManager.current).txt.boxTextLength
END IF
InputManager.keyCode = NULL ' consume the key
CASE _KEY_LEFT
Widget(WidgetManager.current).txt.textPosition = Widget(WidgetManager.current).txt.textPosition - 1 ' decrement the cursor position
IF Widget(WidgetManager.current).txt.textPosition < 1 THEN ' did cursor go beyone beginning of line?
Widget(WidgetManager.current).txt.textPosition = 1 ' yes, keep the cursor at the beginning of the line
END IF
' Box cursor movement
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.boxPosition - 1
IF Widget(WidgetManager.current).txt.boxPosition < 1 THEN
Widget(WidgetManager.current).txt.boxPosition = 1
IF Widget(WidgetManager.current).txt.boxStartCharacter > 1 THEN
Widget(WidgetManager.current).txt.boxStartCharacter = Widget(WidgetManager.current).txt.boxStartCharacter - 1
END IF
END IF
InputManager.keyCode = NULL ' consume the key
CASE _KEY_BACKSPACE
IF Widget(WidgetManager.current).txt.textPosition > 1 THEN ' is the cursor at the beginning of the line?
Widget(WidgetManager.current).text = LEFT$(Widget(WidgetManager.current).text, Widget(WidgetManager.current).txt.textPosition - 2) + RIGHT$(Widget(WidgetManager.current).text, LEN(Widget(WidgetManager.current).text) - Widget(WidgetManager.current).txt.textPosition + 1) ' no, delete character
Widget(WidgetManager.current).txt.textPosition = Widget(WidgetManager.current).txt.textPosition - 1 ' decrement the cursor position
END IF
' Box cursor movement
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.boxPosition - 1
IF Widget(WidgetManager.current).txt.boxPosition < 1 THEN
Widget(WidgetManager.current).txt.boxPosition = 1
IF Widget(WidgetManager.current).txt.boxStartCharacter > 1 THEN
Widget(WidgetManager.current).txt.boxStartCharacter = Widget(WidgetManager.current).txt.boxStartCharacter - 1
END IF
END IF
InputManager.keyCode = NULL ' consume the key
Widget(WidgetManager.current).changed = _TRUE ' something changed
CASE _KEY_HOME
Widget(WidgetManager.current).txt.textPosition = 1 ' move the cursor to the beginning of the line
' Box cursor movement
Widget(WidgetManager.current).txt.boxPosition = 1
Widget(WidgetManager.current).txt.boxStartCharacter = 1
InputManager.keyCode = NULL ' consume the key
CASE _KEY_END
Widget(WidgetManager.current).txt.textPosition = LEN(Widget(WidgetManager.current).text) + 1 ' move the cursor to the end of the line
' Box cursor movement
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.boxTextLength + 1
IF Widget(WidgetManager.current).txt.boxPosition > Widget(WidgetManager.current).txt.textPosition THEN
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.textPosition
END IF
Widget(WidgetManager.current).txt.boxStartCharacter = 1 + LEN(Widget(WidgetManager.current).text) - Widget(WidgetManager.current).txt.boxTextLength
IF Widget(WidgetManager.current).txt.boxStartCharacter < 1 THEN
Widget(WidgetManager.current).txt.boxStartCharacter = 1
END IF
InputManager.keyCode = NULL ' consume the key
CASE _KEY_DELETE
IF Widget(WidgetManager.current).txt.textPosition < LEN(Widget(WidgetManager.current).text) + 1 THEN ' is the cursor at the end of the line?
Widget(WidgetManager.current).text = LEFT$(Widget(WidgetManager.current).text, Widget(WidgetManager.current).txt.textPosition - 1) + RIGHT$(Widget(WidgetManager.current).text, LEN(Widget(WidgetManager.current).text) - Widget(WidgetManager.current).txt.textPosition) ' no, delete character
END IF
InputManager.keyCode = NULL ' consume the key
Widget(WidgetManager.current).changed = _TRUE ' something changed
CASE _KEY_ENTER
Widget(WidgetManager.current).txt.entered = _TRUE ' if enter key was pressed remember it (TRUE)
Widget(WidgetManager.current).changed = _TRUE ' something changed
WidgetManager.forced = -1 ' Move to the next widget
InputManager.keyCode = NULL ' consume the key
CASE ELSE ' a character key was pressed
IF InputManager.keyCode > 31 AND InputManager.keyCode < 256 THEN ' is it a valid ASCII displayable character?
DIM K AS STRING ' yes, initialize key holder variable
SELECT CASE InputManager.keyCode ' which alphanumeric key was pressed?
CASE KEY_SPACE
K = CHR$(InputManager.keyCode) ' save the keystroke
CASE 40 TO 41 ' PARENTHESIS key was pressed
IF (Widget(WidgetManager.current).flags AND TEXT_BOX_SYMBOLS) OR (Widget(WidgetManager.current).flags AND TEXT_BOX_PAREN) THEN
K = CHR$(InputManager.keyCode) ' if it's allowed then save the keystroke
END IF
CASE 45 ' DASH (minus -) key was pressed
IF Widget(WidgetManager.current).flags AND TEXT_BOX_DASH THEN ' are dashes allowed?
K = CHR$(InputManager.keyCode) ' yes, save the keystroke
END IF
CASE 46 ' DOT
IF Widget(WidgetManager.current).flags AND TEXT_BOX_DOT THEN ' are dashes allowed?
K = CHR$(InputManager.keyCode) ' yes, save the keystroke
END IF
CASE KEY_0 TO KEY_9
IF Widget(WidgetManager.current).flags AND TEXT_BOX_NUMERIC THEN ' are numbers allowed?
K = CHR$(InputManager.keyCode) ' yes, save the keystroke
END IF
CASE 33 TO 47, 58 TO 64, 91 TO 96, 123 TO 255 ' SYMBOL key was pressed
IF Widget(WidgetManager.current).flags AND TEXT_BOX_SYMBOLS THEN ' are symbols allowed?
K = CHR$(InputManager.keyCode) ' yes, save the keystroke
END IF
CASE KEY_LOWER_A TO KEY_LOWER_Z, KEY_UPPER_A TO KEY_UPPER_Z
IF Widget(WidgetManager.current).flags AND TEXT_BOX_ALPHA THEN ' are alpha keys allowed?
K = CHR$(InputManager.keyCode) ' yes, save the keystroke
END IF
END SELECT
IF LEN(K) <> NULL THEN ' was an allowed keystroke saved?
IF Widget(WidgetManager.current).flags AND TEXT_BOX_LOWER THEN ' should it be forced to lower case?
K = LCASE$(K) ' yes, force the keystroke to lower case
END IF
IF Widget(WidgetManager.current).flags AND TEXT_BOX_UPPER THEN ' should it be forced to upper case?
K = UCASE$(K) ' yes, force the keystroke to upper case
END IF
IF Widget(WidgetManager.current).txt.textPosition = LEN(Widget(WidgetManager.current).text) + 1 THEN ' is the cursor at the end of the line?
Widget(WidgetManager.current).text = Widget(WidgetManager.current).text + K ' yes, simply add the keystroke to input text
Widget(WidgetManager.current).txt.textPosition = Widget(WidgetManager.current).txt.textPosition + 1 ' increment the cursor position
ELSEIF Widget(WidgetManager.current).txt.insertMode THEN ' no, are we in INSERT mode?
Widget(WidgetManager.current).text = LEFT$(Widget(WidgetManager.current).text, Widget(WidgetManager.current).txt.textPosition - 1) + K + RIGHT$(Widget(WidgetManager.current).text, LEN(Widget(WidgetManager.current).text) - Widget(WidgetManager.current).txt.textPosition + 1) ' yes, insert the character
Widget(WidgetManager.current).txt.textPosition = Widget(WidgetManager.current).txt.textPosition + 1 ' increment the cursor position
ELSE ' no, we are in OVERWRITE mode
Widget(WidgetManager.current).text = LEFT$(Widget(WidgetManager.current).text, Widget(WidgetManager.current).txt.textPosition - 1) + K + RIGHT$(Widget(WidgetManager.current).text, LEN(Widget(WidgetManager.current).text) - Widget(WidgetManager.current).txt.textPosition) ' overwrite with new character
Widget(WidgetManager.current).txt.textPosition = Widget(WidgetManager.current).txt.textPosition + 1 ' increment the cursor position
END IF
' Box cursor movement
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.boxPosition + 1
IF Widget(WidgetManager.current).txt.boxPosition > Widget(WidgetManager.current).txt.boxTextLength + 1 THEN
Widget(WidgetManager.current).txt.boxPosition = Widget(WidgetManager.current).txt.boxTextLength + 1
Widget(WidgetManager.current).txt.boxStartCharacter = 1 + LEN(Widget(WidgetManager.current).text) - Widget(WidgetManager.current).txt.boxTextLength
END IF
InputManager.keyCode = NULL ' consume the key
Widget(WidgetManager.current).changed = _TRUE ' something changed
END IF
END IF
END SELECT
END SUB
' Draws a push button widget
' Again, colors are hardcoded here
' The calling function must ensure that this is called only for active, visible, enabled and the correct widget class
SUB __PushButtonDraw (handle AS LONG)
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
SHARED InputManager AS InputManagerType
DIM r AS RectangleType, depressed AS _BYTE, textColor AS _UNSIGNED LONG
' Create the bounding box for the widget
RectangleCreate Widget(handle).position, Widget(handle).size, r
IF Widget(handle).disabled THEN ' Draw a widget with dull colors and disregard any user interaction
textColor = &HFFA9A9A9
depressed = Widget(handle).cmd.depressed
ELSE
textColor = &HFF000000
' Flip depressed state if mouse was clicked and is being held inside the bounding box
IF (InputManager.mouseLeftButton OR InputManager.mouseRightButton) AND PointCollidesWithRectangle(InputManager.mousePosition, r) AND (PointCollidesWithRectangle(InputManager.mouseLeftButtonClickedRectangle.a, r) OR PointCollidesWithRectangle(InputManager.mouseRightButtonClickedRectangle.a, r)) THEN
depressed = NOT Widget(handle).cmd.depressed
ELSE
depressed = Widget(handle).cmd.depressed
END IF
END IF
' Draw now
WidgetDrawBox3D r, depressed
COLOR textColor, &HFF808080 ' disabled text color
IF depressed THEN
_PRINTSTRING (1 + Widget(handle).position.x + Widget(handle).size.x \ 2 - _PRINTWIDTH(Widget(handle).text) \ 2, 1 + Widget(handle).position.y + Widget(handle).size.y \ 2 - _FONTHEIGHT \ 2), Widget(handle).text
ELSE
_PRINTSTRING (Widget(handle).position.x + Widget(handle).size.x \ 2 - _PRINTWIDTH(Widget(handle).text) \ 2, Widget(handle).position.y + Widget(handle).size.y \ 2 - _FONTHEIGHT \ 2), Widget(handle).text
END IF
' Draw a decorated box inside the bounding box if the button is focused
IF handle = WidgetManager.current AND WidgetManager.focusBlink THEN LINE (r.a.x + 4, r.a.y + 4)-(r.b.x - 4, r.b.y - 4), &HFF000000, B , &B1100110011001100
END SUB
' Draw a text box widget
' Again, colors are hardcoded here
' The calling function must ensure that this is called only for active, visible, enabled and the correct widget with focus
SUB __TextBoxDraw (handle AS LONG)
SHARED Widget() AS WidgetType
SHARED WidgetManager AS WidgetManagerType
DIM visibleText AS STRING, r AS RectangleType, textColor AS _UNSIGNED LONG, textY AS LONG
RectangleCreate Widget(handle).position, Widget(handle).size, r ' create the bounding box for the widget
' Draw a widget with dull colors if disabled
IF Widget(handle).disabled THEN
textColor = &HFFA9A9A9
ELSE
textColor = &HFF000000
END IF
' Draw the depressed box first
WidgetDrawBox3D r, _TRUE
' Next figure out what part of the text we need to draw
visibleText = MID$(Widget(handle).text, Widget(handle).txt.boxStartCharacter, Widget(handle).txt.boxTextLength)
' Calculate the Y position of the text in the box
textY = 2 + Widget(handle).position.y + (Widget(handle).size.y - 4) \ 2 - _FONTHEIGHT \ 2
' Draw the text over the box
COLOR textColor, &HFF808080
IF Widget(handle).flags AND TEXT_BOX_PASSWORD THEN
_PRINTSTRING (2 + Widget(handle).position.x, textY), STRING$(Widget(handle).txt.boxTextLength, CHR$(7))
ELSE
_PRINTSTRING (2 + Widget(handle).position.x, textY), visibleText
END IF
' Draw the cursor only if below conditions are met
IF handle = WidgetManager.current AND WidgetManager.focusBlink AND NOT Widget(handle).disabled THEN
DIM charHeight AS LONG, charWidth AS LONG, curPosX AS LONG
charHeight = _FONTHEIGHT ' get the font height
charWidth = _FONTWIDTH
IF charWidth = 0 THEN charWidth = _PRINTWIDTH("X")
curPosX = 2 + Widget(handle).position.x + (charWidth * (Widget(handle).txt.boxPosition - 1))
IF Widget(handle).txt.insertMode THEN
Graphics_DrawFilledRectangle curPosX, textY + charHeight - 4, curPosX + charWidth - 1, textY + charHeight - 1, &HFF000000
ELSE
Graphics_DrawFilledRectangle curPosX, textY, curPosX + charWidth - 1, textY + charHeight - 1, &HFF000000
END IF