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 timers 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 points timer ;
22 M: trails-gadget graft*
23 [ timer>> start-timer ] [ call-next-method ] bi ;
25 M: trails-gadget ungraft*
26 [ timer>> stop-timer ] [ call-next-method ] bi ;
28 :: iterate-system ( GADGET -- )
29 ! Add a valid point if the mouse is in the gadget
30 ! Otherwise, add an "invisible" point
31 hand-gadget get GADGET = [ mouse ] [ { -10 -10 } ] if
32 GADGET points>> circular-push ;
34 M: trails-gadget pref-dim* drop { 500 500 } ;
36 : each-percent ( seq quot -- )
37 [ dup length ] dip '[ 1 + _ / @ ] each-index ; inline
39 M:: trails-gadget draw-gadget* ( GADGET -- )
40 T{ rgba f 1 1 1 0.4 } fill-color set ! White, with some transparency
41 T{ rgba f 0 0 0 0 } stroke-color set ! no stroke
43 GADGET points>> [ dot ] each-percent ;
45 : <trails-gadget> ( -- trails-gadget )
47 300 point-list >>points
49 dup '[ _ dup iterate-system relayout-1 ]
50 f 10 milliseconds <timer> >>timer ;
52 MAIN-WINDOW: trails-window
53 { { title "Trails" } }
54 <trails-gadget> >>gadgets ;