noise-ui: Initial checkin. Slightly modified from http://rosettacode.org/wiki/Image_noise#Factor

db4
Doug Coleman 2012-08-01 11:07:06 -07:00
parent 3e6e6458e5
commit d78f90d7c7
1 changed files with 62 additions and 0 deletions

View File

@ -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 ;