]> gitweb.factorcode.org Git - factor.git/commitdiff
noise-ui: Initial checkin. Slightly modified from http://rosettacode.org/wiki/Image_n...
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 1 Aug 2012 18:07:06 +0000 (11:07 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 1 Aug 2012 18:08:00 +0000 (11:08 -0700)
extra/noise-ui/noise-ui.factor [new file with mode: 0644]

diff --git a/extra/noise-ui/noise-ui.factor b/extra/noise-ui/noise-ui.factor
new file mode 100644 (file)
index 0000000..3292979
--- /dev/null
@@ -0,0 +1,62 @@
+! Copyright (C) 2012 Anonymous.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors calendar images images.viewer kernel math
+math.parser models models.arrow random sequences threads timers
+ui ui.gadgets ui.gadgets.labels ui.gadgets.packs ;
+IN: noise-ui
+: <random-images-bytes> ( dim -- bytes )
+    product 2 random-integers [ zero? 0 255 ? ] B{ } map-as ;
+
+: <random-bw-image> ( -- image )
+    <image>
+        { 320 240 } [ >>dim ] [ <random-images-bytes> >>bitmap ] bi
+        L >>component-order
+        ubyte-components >>component-type ;
+TUPLE: bw-noise-gadget < image-control timers cnt old-cnt fps-model ;
+: animate-image ( control -- )
+    [ 1 + ] change-cnt 
+    model>> <random-bw-image> swap set-model ;
+: update-cnt ( gadget -- )
+    [ cnt>> ] [ old-cnt<< ] bi ;
+
+: fps ( gadget -- fps )
+    [ cnt>> ] [ old-cnt>> ] bi - ;
+
+: fps-monitor ( gadget -- )
+    [ fps ] [ update-cnt ] [ fps-model>> set-model ] tri ;
+: start-animation ( gadget -- )
+    [ [ animate-image ] curry 1 nanoseconds every ] [ timers>> push ] bi ;
+
+: start-fps ( gadget -- )
+    [ [ fps-monitor ] curry 1 seconds every ] [ timers>> push ] bi ;
+
+: setup-timers ( gadget -- )
+    [ start-animation ] [ start-fps ] bi ;
+
+: stop-animation ( gadget -- )
+    timers>> [ [ stop-timer ] each ] [ 0 swap set-length ] bi ;
+M: bw-noise-gadget graft* [ call-next-method ] [ setup-timers ] bi ;
+
+M: bw-noise-gadget ungraft* [ stop-animation ] [ call-next-method ] bi ;
+: <bw-noise-gadget> ( -- gadget )
+    <random-bw-image> <model> bw-noise-gadget new-image-gadget* 
+    0 >>cnt 0 >>old-cnt 0 <model> >>fps-model V{ } clone >>timers ;
+
+: fps-gadget ( model -- gadget )
+    [ number>string ] <arrow> <label-control>
+    "FPS: " <label>
+    <shelf> swap add-gadget swap add-gadget ;
+: with-fps ( gadget -- gadget' )
+    [ fps-model>> fps-gadget ]
+    [ <pile> swap add-gadget swap add-gadget ] bi ;
+: open-noise-window ( -- )
+    [ <bw-noise-gadget> with-fps "Black and White noise" open-window ] with-ui ;