1 USING: accessors arrays calendar circular colors
2 colors.constants fry kernel locals math math.order math.vectors
3 namespaces opengl processing.shapes sequences threads ui
4 ui.gadgets ui.gestures ui.render ;
8 ! Example 33-15 from the Processing book
11 ! Return the mouse location relative to the current gadget
12 hand-loc get hand-gadget get screen-loc v- ;
14 : point-list ( n -- seq ) { 0 0 } <array> <circular> ;
16 : percent->radius ( percent -- radius ) neg 1 + 25 * 5 max ;
18 : dot ( pos percent -- ) percent->radius draw-circle ;
20 TUPLE: trails-gadget < gadget paused points ;
22 :: iterate-system ( GADGET -- )
23 ! Add a valid point if the mouse is in the gadget
24 ! Otherwise, add an "invisible" point
25 hand-gadget get GADGET = [ mouse ] [ { -10 -10 } ] if
26 GADGET points>> circular-push ;
28 :: start-trails-thread ( GADGET -- )
29 GADGET f >>paused drop
34 [ GADGET iterate-system GADGET relayout-1 1 milliseconds sleep t ]
38 ] "trails" spawn drop ;
40 M: trails-gadget ungraft* t >>paused drop ;
42 M: trails-gadget pref-dim* drop { 500 500 } ;
44 : each-percent ( seq quot -- )
45 [ dup length ] dip '[ 1 + _ / @ ] each-index ; inline
47 M:: trails-gadget draw-gadget* ( GADGET -- )
48 T{ rgba f 1 1 1 0.4 } \ fill-color set ! White, with some transparency
49 T{ rgba f 0 0 0 0 } \ stroke-color set ! no stroke
51 GADGET points>> [ dot ] each-percent ;
53 : <trails-gadget> ( -- trails-gadget )
55 300 point-list >>points
57 dup start-trails-thread ;
59 MAIN-WINDOW: trails-window
60 { { title "Trails" } }
61 <trails-gadget> >>gadgets ;