]> gitweb.factorcode.org Git - factor.git/blob - extra/trails/trails.factor
31d4874aacfe9ba94a995fec1e8814bcc6ea5aea
[factor.git] / extra / trails / trails.factor
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 ;
5
6 IN: trails
7
8 ! Example 33-15 from the Processing book
9
10 : mouse ( -- point )
11     ! Return the mouse location relative to the current gadget
12     hand-loc get hand-gadget get screen-loc v- ;
13
14 : percent->radius ( percent -- radius ) neg 1 + 25 * 5 max ;
15
16 : dot ( pos percent -- )
17     '[ _ percent->radius draw-circle ] when* ;
18
19 TUPLE: trails-gadget < gadget points timer ;
20
21 M: trails-gadget graft*
22     [ timer>> start-timer ] [ call-next-method ] bi ;
23
24 M: trails-gadget ungraft*
25     [ timer>> stop-timer ] [ call-next-method ] bi ;
26
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 ;
32
33 M: trails-gadget pref-dim* drop { 500 500 } ;
34
35 : each-percent ( seq quot -- )
36     [ dup length ] dip '[ 1 + _ / @ ] each-index ; inline
37
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
41     COLOR: black gl-clear
42     GADGET points>> [ dot ] each-percent ;
43
44 : <trails-gadget> ( -- trails-gadget )
45     trails-gadget new
46         300 f <array> <circular> >>points
47         t >>clipped?
48         dup '[ _ dup iterate-system relayout-1 ]
49         f 10 milliseconds <timer> >>timer ;
50
51 MAIN-WINDOW: trails-window
52     { { title "Trails" } }
53     <trails-gadget> >>gadgets ;