\ **************** UI Widget buffer ****************
\ The premise behind this buffer is that UI in a-nib apps will never be particularly complex.
\ The number of distinct widgets will be small and the event loop can correspondingly be relatively tight,
\ effectively an unrolled ring of 16 widget buffers coordinated through a stack of events.
\ Everything really complex can be done modally by swapping out the widgets.
\ With this combo, events can be broadcast around and exhibit cascading behavior, and widgets can explain themselves
\ entirely through their execution token and the address currently pointed to by the pointer widget^ .
\ Widgets are expected to manage their events cleanly; if they have intricate synchronization needs, it should be
\ handled internally with custom buffering logic and a secondary event.
\
\ Q: When Do I widget/iterate ? A: When the event applies to everything, all the time, like initialization.
\ Q: When Do I widget/broadcast ? A: When the event applies to others, not "you" (the callee).
\ Q: When Do I use widget/st/... ? A: Push associated info you need for an event type, in this pattern:
\      myinfo$ widget/st/push
\      wevent:myevent widget/broadcast \ or widget/iterate
\      widget/st/pop drop
\ Q: What are the xt, 2^, 3^, etc. words? A: They are for widget storage. xt is held at the first position,
\ and the others are intended to depend on that xt as the tag identifying what's being stored.

16 st/create widget/ev \ event stack, a convenience for events with a payload that can't be managed otherwise
: widget/st/push ( v -- ) widget/ev swap st/push ;
: widget/st/tos ( -- v ) widget/ev st/tos ;
: widget/st/pop ( -- v ) widget/ev st/pop ;

: widget-u [ 16 cells literal ] ;
create widgetbuf widget-u 16 * allot
widgetbuf value widget^
: widget/next widget^ widget-u + to widget^
    widget^ [ widgetbuf widget-u 16 * + literal ] = if widgetbuf to widget^ then ;
: widget/xt! widget^ ! ;
: widget/xt@ widget^ @ ;
: widget/2^ widget^ cell+ ;
: widget/3^ widget^ cell+ cell+ ;
: widget/4^ widget^ [ 3 cells literal ] + ;
: widget/5^ widget^ [ 4 cells literal ] + ;
: widget/6^ widget^ [ 5 cells literal ] + ;
: widget/7^ widget^ [ 6 cells literal ] + ;
: widget/8^ widget^ [ 7 cells literal ] + ;
: widget/9^ widget^ [ 8 cells literal ] + ;
: widget/10^ widget^ [ 9 cells literal ] + ;
: widget/11^ widget^ [ 10 cells literal ] + ;
: widget/12^ widget^ [ 11 cells literal ] + ;
: widget/13^ widget^ [ 12 cells literal ] + ;
: widget/14^ widget^ [ 13 cells literal ] + ;
: widget/15^ widget^ [ 14 cells literal ] + ;
: widget/16^ widget^ [ 15 cells literal ] + ;
: widget/execute widget^ @ execute widget/next ;
: widget/broadcast ( ev -- )
    \ Call all 15 other widgets
    widget/next \ skip past the callee
    dup widget/execute \ 2
    dup widget/execute
    dup widget/execute \ 4
    dup widget/execute
    dup widget/execute \ 6
    dup widget/execute
    dup widget/execute \ 8
    dup widget/execute
    dup widget/execute \ 10
    dup widget/execute
    dup widget/execute \ 12
    dup widget/execute
    dup widget/execute \ 14
    dup widget/execute
    widget/execute \ 16
;
: widget/iterate ( ev -- )
    \ Call all 16 widgets
    dup widget/execute
    dup widget/execute \ 2
    dup widget/execute
    dup widget/execute \ 4
    dup widget/execute
    dup widget/execute \ 6
    dup widget/execute
    dup widget/execute \ 8
    dup widget/execute
    dup widget/execute \ 10
    dup widget/execute
    dup widget/execute \ 12
    dup widget/execute
    dup widget/execute \ 14
    dup widget/execute
    widget/execute \ 16
;
: widget/empty-xt drop ;
: widget/reset
    widgetbuf to widget^
    16 0 do ['] widget/empty-xt widget/xt! widget/next loop
;
widget/reset

1 constant wevent:init \ when the widget event loop starts
2 constant wevent:key \ when a key is pressed or released
3 constant wevent:tick \ once per vsync

\  : widget/test-1 case
\          1 of cr ." test-1 broadcasting 2" 2 widget/broadcast endof
\          2 of cr ." test-1 recieving 2" endof
\      endcase ;
\  : widget/test-2 case
\          1 of cr ." test-2 recieving 1" endof
\          2 of cr ." test-2 recieving 2" endof
\      endcase ;
\
\  ' widget/test-1 widget/xt! widget/next
\  ' widget/test-2 widget/xt! widget/next
\  1 widget/iterate

\ **************** Keyboard Constants ****************

1 constant k/space
3 constant k/1
22 constant k/a
48 constant k/caps-a
87 constant k//
89 constant k/.
91 constant k/,
92 constant k/;
143 constant k/enter
150 constant k/up
152 constant k/down
154 constant k/left
156 constant k/right
125 constant k/esc
117 constant k/shift-l
118 constant k/shift-r
119 constant k/alt-l
120 constant k/alt-r
121 constant k/ctrl-l
122 constant k/ctrl-r
141 constant k/capslock
142 constant k/tab
159 constant k/f1
