1 USING: accessors arrays calendar circular colors
2 colors.constants fry kernel locals math math.order math.vectors
3 namespaces opengl processing.shapes sequences 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 : percent->radius ( percent -- radius ) neg 1 + 25 * 5 max ;
16 : dot ( pos percent -- )
17 '[ _ percent->radius draw-circle ] when* ;
19 TUPLE: trails-gadget < gadget points timer ;
21 M: trails-gadget graft*
22 [ timer>> start-timer ] [ call-next-method ] bi ;
24 M: trails-gadget ungraft*
25 [ timer>> stop-timer ] [ call-next-method ] bi ;
27 :: iterate-system ( GADGET -- )
28 ! Add a valid point if the mouse is in the gadget
29 ! Otherwise, add an "invisible" point
30 hand-gadget get GADGET = [ mouse ] [ f ] if
31 GADGET points>> circular-push ;
33 M: trails-gadget pref-dim* drop { 500 500 } ;
35 : each-percent ( seq quot -- )
36 [ dup length ] dip '[ 1 + _ / @ ] each-index ; inline
38 M:: trails-gadget draw-gadget* ( GADGET -- )
39 T{ rgba f 1 1 1 0.4 } fill-color set ! White, with some transparency
40 T{ rgba f 0 0 0 0 } stroke-color set ! no stroke
42 GADGET points>> [ dot ] each-percent ;
44 : <trails-gadget> ( -- trails-gadget )
46 300 f <array> <circular> >>points
48 dup '[ _ dup iterate-system relayout-1 ]
49 f 10 milliseconds <timer> >>timer ;
51 MAIN-WINDOW: trails-window
52 { { title "Trails" } }
53 <trails-gadget> >>gadgets ;