No smart quotes here
[factor/jcg.git] / extra / game-input / game-input.factor
blob46e3ba9e8dafc0522a33b528cc4bea5db32e7f64
1 USING: arrays accessors continuations kernel system
2 sequences namespaces init vocabs vocabs.loader combinators ;
3 IN: game-input
5 SYMBOLS: game-input-backend game-input-opened ;
7 HOOK: (open-game-input)  game-input-backend ( -- )
8 HOOK: (close-game-input) game-input-backend ( -- )
9 HOOK: (reset-game-input) game-input-backend ( -- )
11 : game-input-opened? ( -- ? )
12     game-input-opened get ;
14 <PRIVATE
16 M: f (reset-game-input) ;
18 : reset-game-input ( -- )
19     game-input-opened off
20     (reset-game-input) ;
22 [ reset-game-input ] "game-input" add-init-hook
24 PRIVATE>
26 : open-game-input ( -- )
27     game-input-opened? [
28         (open-game-input) 
29         game-input-opened on
30     ] unless ;
31 : close-game-input ( -- )
32     game-input-opened? [
33         (close-game-input) 
34         reset-game-input
35     ] when ;
37 : with-game-input ( quot -- )
38     open-game-input [ close-game-input ] [ ] cleanup ;
40 TUPLE: controller handle ;
41 TUPLE: controller-state x y z rx ry rz slider pov buttons ;
43 M: controller-state clone
44     call-next-method dup buttons>> clone >>buttons ;
46 SYMBOLS:
47     pov-neutral
48     pov-up pov-up-right pov-right pov-down-right
49     pov-down pov-down-left pov-left pov-up-left ;
51 HOOK: get-controllers game-input-backend ( -- sequence )
53 HOOK: product-string game-input-backend ( controller -- string )
54 HOOK: product-id game-input-backend ( controller -- id )
55 HOOK: instance-id game-input-backend ( controller -- id )
57 : find-controller-products ( product-id -- sequence )
58     get-controllers [ product-id = ] with filter ;
59 : find-controller-instance ( product-id instance-id -- controller/f )
60     get-controllers [
61         tuck
62         [ product-id  = ]
63         [ instance-id = ] 2bi* and
64     ] with with find nip ;
66 HOOK: read-controller game-input-backend ( controller -- controller-state )
67 HOOK: calibrate-controller game-input-backend ( controller -- )
69 TUPLE: keyboard-state keys ;
71 M: keyboard-state clone
72     call-next-method dup keys>> clone >>keys ;
74 HOOK: read-keyboard game-input-backend ( -- keyboard-state )
77     { [ os windows? ] [ "game-input.dinput" require ] }
78     { [ os macosx? ] [ "game-input.iokit" require ] }
79     { [ t ] [ ] }
80 } cond