1 ! Copyright (C) 2006, 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors timers arrays calendar kernel make math math.rectangles
4 math.parser namespaces sequences system tetris.game tetris.gl ui.gadgets
5 ui.gadgets.labels ui.gadgets.worlds ui.gadgets.status-bar ui.gestures
9 TUPLE: tetris-gadget < gadget { tetris tetris } { timer } ;
11 : <tetris-gadget> ( tetris -- gadget )
12 tetris-gadget new swap >>tetris ;
14 M: tetris-gadget pref-dim* drop { 200 400 } ;
16 : update-status ( gadget -- )
18 [ "Level: " % level # ]
19 [ " Score: " % score>> # ]
20 [ paused?>> [ " (Paused)" % ] when ] tri
21 ] "" make swap show-status ;
23 M: tetris-gadget draw-gadget* ( gadget -- )
25 [ dim>> first2 ] [ tetris>> ] bi draw-tetris
26 ] keep update-status ;
28 : new-tetris ( gadget -- gadget )
29 [ <new-tetris> ] change-tetris ;
31 : unless-paused ( tetris quot -- )
32 over tetris>> paused?>> [
39 { T{ button-down f f 1 } [ request-focus ] }
40 { T{ key-down f f "UP" } [ [ tetris>> rotate-right ] unless-paused ] }
41 { T{ key-down f f "d" } [ [ tetris>> rotate-left ] unless-paused ] }
42 { T{ key-down f f "f" } [ [ tetris>> rotate-right ] unless-paused ] }
43 { T{ key-down f f "e" } [ [ tetris>> rotate-left ] unless-paused ] }
44 { T{ key-down f f "u" } [ [ tetris>> rotate-right ] unless-paused ] }
45 { T{ key-down f f "LEFT" } [ [ tetris>> move-left ] unless-paused ] }
46 { T{ key-down f f "RIGHT" } [ [ tetris>> move-right ] unless-paused ] }
47 { T{ key-down f f "DOWN" } [ [ tetris>> move-down ] unless-paused ] }
48 { T{ key-down f f " " } [ [ tetris>> move-drop ] unless-paused ] }
49 { T{ key-down f f "p" } [ tetris>> toggle-pause ] }
50 { T{ key-down f f "n" } [ new-tetris drop ] }
54 [ tetris>> ?update ] [ relayout-1 ] bi ;
56 M: tetris-gadget graft* ( gadget -- )
57 [ [ tick ] curry 100 milliseconds every ] keep timer<< ;
59 M: tetris-gadget ungraft* ( gadget -- )
60 [ stop-timer f ] change-timer drop ;
62 : tetris-window ( -- )
64 <default-tetris> <tetris-gadget>
65 "Tetris" open-status-window