From d78f90d7c72e65275e1126274c07e13498f5c88f Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 1 Aug 2012 11:07:06 -0700 Subject: [PATCH] noise-ui: Initial checkin. Slightly modified from http://rosettacode.org/wiki/Image_noise#Factor --- extra/noise-ui/noise-ui.factor | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 extra/noise-ui/noise-ui.factor diff --git a/extra/noise-ui/noise-ui.factor b/extra/noise-ui/noise-ui.factor new file mode 100644 index 0000000000..3292979aff --- /dev/null +++ b/extra/noise-ui/noise-ui.factor @@ -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 + +: ( dim -- bytes ) + product 2 random-integers [ zero? 0 255 ? ] B{ } map-as ; + +: ( -- image ) + + { 320 240 } [ >>dim ] [ >>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>> 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 ; + +: ( -- gadget ) + bw-noise-gadget new-image-gadget* + 0 >>cnt 0 >>old-cnt 0 >>fps-model V{ } clone >>timers ; + +: fps-gadget ( model -- gadget ) + [ number>string ] + "FPS: "