]> gitweb.factorcode.org Git - factor.git/blob - extra/rosetta-code/animation/animation.factor
62e40a711f4355ab0bd8e81036c7855393c879c1
[factor.git] / extra / rosetta-code / animation / animation.factor
1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors timers calendar fonts kernel models sequences ui
4 ui.gadgets ui.gadgets.labels ui.gestures ;
5 FROM: models => change-model ;
6 IN: rosetta-code.animation
7
8 ! http://rosettacode.org/wiki/Animation
9
10 ! Animation is the foundation of a great many parts of graphical
11 ! user interfaces, including both the fancy effects when things
12 ! change used in window managers, and of course games. The core of
13 ! any animation system is a scheme for periodically changing the
14 ! display while still remaining responsive to the user. This task
15 ! demonstrates this.
16
17 ! Create a window containing the string "Hello World! " (the
18 ! trailing space is significant). Make the text appear to be
19 ! rotating right by periodically removing one letter from the end
20 ! of the string and attaching it to the front. When the user
21 ! clicks on the text, it should reverse its direction.
22
23 CONSTANT: sentence "Hello World! "
24
25 TUPLE: animated-label < label-control reversed alarm ;
26
27 : <animated-label> ( model -- <animated-model> )
28     sentence animated-label new-label swap >>model 
29     monospace-font >>font ;
30
31 : update-string ( str reverse -- str )
32     [ unclip-last prefix ] [ unclip suffix ] if ;
33
34 : update-model ( model reversed? -- )
35     [ update-string ] curry change-model ;
36
37 animated-label
38     H{
39         { T{ button-down } [ [ not ] change-reversed drop ] }
40      } set-gestures
41
42 M: animated-label graft*
43   [ [ [ model>> ] [ reversed>> ] bi update-model ] curry 400 milliseconds every ] keep
44   alarm<< ;
45
46 M: animated-label ungraft*
47     alarm>> stop-timer ;
48
49 : animated-main ( -- )
50    [ sentence <model> <animated-label> "Rosetta" open-window ] with-ui ;
51
52 MAIN: animated-main