1 USING: arrays accessors continuations kernel math system
2 sequences namespaces init vocabs combinators ;
3 FROM: namespaces => change-global ;
6 SYMBOLS: game-input-backend game-input-opened ;
8 game-input-opened [ 0 ] initialize
10 HOOK: (open-game-input) game-input-backend ( -- )
11 HOOK: (close-game-input) game-input-backend ( -- )
12 HOOK: (reset-game-input) game-input-backend ( -- )
14 HOOK: get-controllers game-input-backend ( -- sequence )
16 HOOK: product-string game-input-backend ( controller -- string )
17 HOOK: product-id game-input-backend ( controller -- id )
18 HOOK: instance-id game-input-backend ( controller -- id )
20 HOOK: read-controller game-input-backend ( controller -- controller-state )
21 HOOK: calibrate-controller game-input-backend ( controller -- )
22 HOOK: vibrate-controller game-input-backend ( controller motor1 motor2 -- )
24 HOOK: read-keyboard game-input-backend ( -- keyboard-state )
26 HOOK: read-mouse game-input-backend ( -- mouse-state )
28 HOOK: reset-mouse game-input-backend ( -- )
30 : game-input-opened? ( -- ? )
31 game-input-opened get zero? not ;
35 M: f (reset-game-input) ;
37 : reset-game-input ( -- )
40 [ reset-game-input ] "game-input" add-startup-hook
44 ERROR: game-input-not-open ;
46 : open-game-input ( -- )
50 game-input-opened [ 1 + ] change-global
52 : close-game-input ( -- )
54 dup zero? [ game-input-not-open ] when
62 : with-game-input ( quot -- )
63 open-game-input [ close-game-input ] [ ] cleanup ; inline
65 TUPLE: controller handle ;
66 TUPLE: controller-state x y z rx ry rz slider pov buttons ;
68 M: controller-state clone
69 call-next-method dup buttons>> clone >>buttons ;
73 pov-up pov-up-right pov-right pov-down-right
74 pov-down pov-down-left pov-left pov-up-left ;
76 : find-controller-products ( product-id -- sequence )
77 get-controllers [ product-id = ] with filter ;
78 : find-controller-instance ( product-id instance-id -- controller/f )
81 [ instance-id = ] bi-curry bi* and
82 ] with with find nip ;
84 TUPLE: keyboard-state keys ;
86 M: keyboard-state clone
87 call-next-method dup keys>> clone >>keys ;
89 TUPLE: mouse-state dx dy scroll-dx scroll-dy buttons ;
92 call-next-method dup buttons>> clone >>buttons ;
94 SYMBOLS: pressed released ;
96 : button-delta ( old? new? -- delta )
98 { [ 2dup xor not ] [ 2drop f ] }
99 { [ dup not ] [ 2drop released ] }
100 { [ over not ] [ 2drop pressed ] }
103 : buttons-delta-as ( old-buttons new-buttons exemplar -- delta )
104 [ button-delta ] swap 2map-as ; inline
106 : buttons-delta ( old-buttons new-buttons -- delta )
107 { } buttons-delta-as ; inline
110 { [ os windows? ] [ "game.input.dinput" require ] }
111 { [ os macosx? ] [ "game.input.iokit" require ] }
112 { [ os linux? ] [ "game.input.gtk" require ] }