]> gitweb.factorcode.org Git - factor.git/commitdiff
trails: use timers instead of threads.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 22 Jan 2018 20:22:46 +0000 (12:22 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 22 Jan 2018 20:22:46 +0000 (12:22 -0800)
extra/trails/trails.factor

index 901b43e669d16326b8bc7b4d2fe891c82ee5544f..81e2db638e833f5cdecda977787af44789c3f560 100644 (file)
@@ -1,6 +1,6 @@
 USING: accessors arrays calendar circular colors
 colors.constants fry kernel locals math math.order math.vectors
-namespaces opengl processing.shapes sequences threads ui
+namespaces opengl processing.shapes sequences threads timers ui
 ui.gadgets ui.gestures ui.render ;
 
 IN: trails
@@ -17,7 +17,13 @@ IN: trails
 
 : dot ( pos percent -- ) percent->radius draw-circle ;
 
-TUPLE: trails-gadget < gadget paused points ;
+TUPLE: trails-gadget < gadget paused points timer ;
+
+M: trails-gadget graft*
+    [ timer>> start-timer yield ] [ call-next-method ] bi ;
+
+M: trails-gadget ungraft*
+    [ timer>> stop-timer ] [ t >>paused call-next-method ] bi ;
 
 :: iterate-system ( GADGET -- )
     ! Add a valid point if the mouse is in the gadget
@@ -25,20 +31,6 @@ TUPLE: trails-gadget < gadget paused points ;
     hand-gadget get GADGET = [ mouse ] [ { -10 -10 } ] if
     GADGET points>> circular-push ;
 
-:: start-trails-thread ( GADGET -- )
-    GADGET f >>paused drop
-    [
-        [
-            GADGET paused>>
-            [ f ]
-            [ GADGET iterate-system GADGET relayout-1 1 milliseconds sleep t ]
-            if
-        ]
-        loop
-    ] "trails" spawn drop ;
-
-M: trails-gadget ungraft* t >>paused drop ;
-
 M: trails-gadget pref-dim* drop { 500 500 } ;
 
 : each-percent ( seq quot -- )
@@ -54,7 +46,8 @@ M:: trails-gadget draw-gadget* ( GADGET -- )
     trails-gadget new
         300 point-list >>points
         t >>clipped?
-    dup start-trails-thread ;
+        dup '[ _ dup iterate-system relayout-1 ]
+        f 10 milliseconds <timer> >>timer ;
 
 MAIN-WINDOW: trails-window
     { { title "Trails" } }