1 ! Copyright (C) 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays calendar jamshred.game jamshred.gl jamshred.player jamshred.log kernel math math.constants math.geometry.rect math.vectors namespaces sequences threads ui ui.backend ui.gadgets ui.gadgets.worlds ui.gestures ui.render ;
6 TUPLE: jamshred-gadget < gadget { jamshred jamshred } last-hand-loc ;
8 : <jamshred-gadget> ( jamshred -- gadget )
9 jamshred-gadget new-gadget swap >>jamshred ;
11 : default-width ( -- x ) 800 ;
12 : default-height ( -- y ) 600 ;
14 M: jamshred-gadget pref-dim*
15 drop default-width default-height 2array ;
17 M: jamshred-gadget draw-gadget* ( gadget -- )
18 [ jamshred>> ] [ rect-dim first2 draw-jamshred ] bi ;
20 : jamshred-loop ( gadget -- )
21 dup jamshred>> quit>> [
24 [ jamshred>> jamshred-update ]
26 [ 10 milliseconds sleep yield jamshred-loop ] tri
29 : fullscreen ( gadget -- )
30 find-world t swap set-fullscreen* ;
32 : no-fullscreen ( gadget -- )
33 find-world f swap set-fullscreen* ;
35 : toggle-fullscreen ( world -- )
36 [ fullscreen? not ] keep set-fullscreen* ;
38 M: jamshred-gadget graft* ( gadget -- )
39 [ jamshred-loop ] curry in-thread ;
41 M: jamshred-gadget ungraft* ( gadget -- )
42 jamshred>> t swap (>>quit) ;
44 : jamshred-restart ( jamshred-gadget -- )
45 <jamshred> >>jamshred drop ;
47 : pix>radians ( n m -- theta )
48 / pi 4 * * ; ! 2 / / pi 2 * * ;
50 : x>radians ( x gadget -- theta )
51 #! translate motion of x pixels to an angle
52 rect-dim first pix>radians neg ;
54 : y>radians ( y gadget -- theta )
55 #! translate motion of y pixels to an angle
56 rect-dim second pix>radians ;
58 : (handle-mouse-motion) ( jamshred-gadget mouse-motion -- )
60 [ first swap x>radians ] 2keep second swap y>radians
63 : handle-mouse-motion ( jamshred-gadget -- )
65 over last-hand-loc>> [
66 v- (handle-mouse-motion)
68 ] 2keep >>last-hand-loc drop ;
70 : handle-mouse-scroll ( jamshred-gadget -- )
71 jamshred>> scroll-direction get
72 [ first mouse-scroll-x ]
73 [ second mouse-scroll-y ] 2bi ;
76 [ no-fullscreen ] [ close-window ] bi ;
79 { T{ key-down f f "r" } [ jamshred-restart ] }
80 { T{ key-down f f " " } [ jamshred>> toggle-running ] }
81 { T{ key-down f f "f" } [ find-world toggle-fullscreen ] }
82 { T{ key-down f f "UP" } [ jamshred>> jamshred-player 1 swap change-player-speed ] }
83 { T{ key-down f f "DOWN" } [ jamshred>> jamshred-player -1 swap change-player-speed ] }
84 { T{ key-down f f "LEFT" } [ jamshred>> 1 jamshred-roll ] }
85 { T{ key-down f f "RIGHT" } [ jamshred>> -1 jamshred-roll ] }
86 { T{ key-down f f "q" } [ quit ] }
87 { T{ motion } [ handle-mouse-motion ] }
88 { T{ mouse-scroll } [ handle-mouse-scroll ] }
91 : jamshred-window ( -- gadget )
92 [ <jamshred> <jamshred-gadget> dup "Jamshred" open-window ] with-ui ;