]> gitweb.factorcode.org Git - factor.git/blob - extra/trails/trails.factor
extra: cleanup USING: lists
[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 : point-list ( n -- seq ) { 0 0 } <array> <circular> ;
15
16 : percent->radius ( percent -- radius ) neg 1 + 25 * 5 max ;
17
18 : dot ( pos percent -- ) percent->radius draw-circle ;
19
20 TUPLE: trails-gadget < gadget points timer ;
21
22 M: trails-gadget graft*
23     [ timer>> start-timer ] [ call-next-method ] bi ;
24
25 M: trails-gadget ungraft*
26     [ timer>> stop-timer ] [ call-next-method ] bi ;
27
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 ;
33
34 M: trails-gadget pref-dim* drop { 500 500 } ;
35
36 : each-percent ( seq quot -- )
37     [ dup length ] dip '[ 1 + _ / @ ] each-index ; inline
38
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
42     COLOR: black gl-clear
43     GADGET points>> [ dot ] each-percent ;
44
45 : <trails-gadget> ( -- trails-gadget )
46     trails-gadget new
47         300 point-list >>points
48         t >>clipped?
49         dup '[ _ dup iterate-system relayout-1 ]
50         f 10 milliseconds <timer> >>timer ;
51
52 MAIN-WINDOW: trails-window
53     { { title "Trails" } }
54     <trails-gadget> >>gadgets ;