1 ! Copyright (C) 2006, 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alarms arrays calendar kernel make math math.geometry.rect math.parser namespaces sequences system tetris.game tetris.gl ui.gadgets ui.gadgets.labels ui.gadgets.worlds ui.gadgets.status-bar ui.gestures ui.render ui ;
6 TUPLE: tetris-gadget < gadget { tetris tetris } { alarm } ;
8 : <tetris-gadget> ( tetris -- gadget )
9 tetris-gadget new-gadget swap >>tetris ;
11 M: tetris-gadget pref-dim* drop { 200 400 } ;
13 : update-status ( gadget -- )
15 "Level: " % dup level>> #
16 " Score: " % score>> #
17 ] "" make swap show-status ;
19 M: tetris-gadget draw-gadget* ( gadget -- )
21 dup rect-dim [ first ] [ second ] bi rot tetris>> draw-tetris
22 ] keep update-status ;
24 : new-tetris ( gadget -- gadget )
25 [ <new-tetris> ] change-tetris ;
28 { T{ button-down f f 1 } [ request-focus ] }
29 { T{ key-down f f "UP" } [ tetris>> rotate-right ] }
30 { T{ key-down f f "d" } [ tetris>> rotate-left ] }
31 { T{ key-down f f "f" } [ tetris>> rotate-right ] }
32 { T{ key-down f f "e" } [ tetris>> rotate-left ] } ! dvorak d
33 { T{ key-down f f "u" } [ tetris>> rotate-right ] } ! dvorak f
34 { T{ key-down f f "LEFT" } [ tetris>> move-left ] }
35 { T{ key-down f f "RIGHT" } [ tetris>> move-right ] }
36 { T{ key-down f f "DOWN" } [ tetris>> move-down ] }
37 { T{ key-down f f " " } [ tetris>> move-drop ] }
38 { T{ key-down f f "p" } [ tetris>> toggle-pause ] }
39 { T{ key-down f f "n" } [ new-tetris drop ] }
43 [ tetris>> ?update ] [ relayout-1 ] bi ;
45 M: tetris-gadget graft* ( gadget -- )
46 [ [ tick ] curry 100 milliseconds every ] keep (>>alarm) ;
48 M: tetris-gadget ungraft* ( gadget -- )
49 [ cancel-alarm f ] change-alarm drop ;
51 : tetris-window ( -- )
53 <default-tetris> <tetris-gadget>
54 "Tetris" open-status-window