factor/extra/trails/trails.factor

62 lines
1.7 KiB
Factor
Raw Normal View History

2017-01-22 18:06:01 -05:00
USING: accessors arrays calendar circular colors
colors.constants fry kernel locals math math.order math.vectors
namespaces opengl processing.shapes sequences threads ui
ui.gadgets ui.gestures ui.render ;
2008-12-08 22:30:10 -05:00
IN: trails
! Example 33-15 from the Processing book
2017-01-22 18:06:01 -05:00
: mouse ( -- point )
! Return the mouse location relative to the current gadget
hand-loc get hand-gadget get screen-loc v- ;
2008-12-08 22:30:10 -05:00
2017-01-22 18:06:01 -05:00
: point-list ( n -- seq ) { 0 0 } <array> <circular> ;
2008-12-08 22:30:10 -05:00
: percent->radius ( percent -- radius ) neg 1 + 25 * 5 max ;
2017-01-22 18:06:01 -05:00
: dot ( pos percent -- ) percent->radius draw-circle ;
2008-12-08 22:30:10 -05:00
TUPLE: trails-gadget < gadget paused points ;
2008-12-08 22:30:10 -05:00
:: iterate-system ( GADGET -- )
2017-01-22 18:06:01 -05:00
! Add a valid point if the mouse is in the gadget
! Otherwise, add an "invisible" point
hand-gadget get GADGET = [ mouse ] [ { -10 -10 } ] if
GADGET points>> circular-push ;
2008-12-08 22:30:10 -05:00
:: start-trails-thread ( GADGET -- )
2017-01-22 18:06:01 -05:00
GADGET f >>paused drop
2008-12-08 22:30:10 -05:00
[
2017-01-22 18:06:01 -05:00
[
GADGET paused>>
[ f ]
[ GADGET iterate-system GADGET relayout-1 1 milliseconds sleep t ]
if
]
loop
] "trails" spawn drop ;
2008-12-08 22:30:10 -05:00
2017-01-22 18:06:01 -05:00
M: trails-gadget ungraft* t >>paused drop ;
2017-01-22 18:06:01 -05:00
M: trails-gadget pref-dim* drop { 500 500 } ;
2008-12-08 22:30:10 -05:00
: each-percent ( seq quot -- )
2017-01-22 18:06:01 -05:00
[ dup length ] dip '[ 1 + _ / @ ] each-index ; inline
M:: trails-gadget draw-gadget* ( GADGET -- )
2008-12-08 22:30:10 -05:00
T{ rgba f 1 1 1 0.4 } \ fill-color set ! White, with some transparency
T{ rgba f 0 0 0 0 } \ stroke-color set ! no stroke
COLOR: black gl-clear
GADGET points>> [ dot ] each-percent ;
2008-12-08 22:30:10 -05:00
: <trails-gadget> ( -- trails-gadget )
2017-01-22 18:06:01 -05:00
trails-gadget new
300 point-list >>points
t >>clipped?
dup start-trails-thread ;
2008-12-08 22:30:10 -05:00
2016-04-21 20:00:06 -04:00
MAIN-WINDOW: trails-window
{ { title "Trails" } }
<trails-gadget> >>gadgets ;