]> gitweb.factorcode.org Git - factor.git/blob - extra/game-input/game-input.factor
Merge branch 'master' into experimental
[factor.git] / extra / game-input / game-input.factor
1 USING: arrays accessors continuations kernel system
2 sequences namespaces init vocabs vocabs.loader combinators ;
3 IN: game-input
4
5 SYMBOLS: game-input-backend game-input-opened ;
6
7 HOOK: (open-game-input)  game-input-backend ( -- )
8 HOOK: (close-game-input) game-input-backend ( -- )
9 HOOK: (reset-game-input) game-input-backend ( -- )
10
11 : game-input-opened? ( -- ? )
12     game-input-opened get ;
13
14 <PRIVATE
15
16 M: f (reset-game-input) ;
17
18 : reset-game-input ( -- )
19     game-input-opened off
20     (reset-game-input) ;
21
22 [ reset-game-input ] "game-input" add-init-hook
23
24 PRIVATE>
25
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 ;
36
37 : with-game-input ( quot -- )
38     open-game-input [ close-game-input ] [ ] cleanup ;
39
40 TUPLE: controller handle ;
41 TUPLE: controller-state x y z rx ry rz slider pov buttons ;
42
43 M: controller-state clone
44     call-next-method dup buttons>> clone >>buttons ;
45
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 ;
50
51 HOOK: get-controllers game-input-backend ( -- sequence )
52
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 )
56
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 ;
65
66 HOOK: read-controller game-input-backend ( controller -- controller-state )
67 HOOK: calibrate-controller game-input-backend ( controller -- )
68
69 TUPLE: keyboard-state keys ;
70
71 M: keyboard-state clone
72     call-next-method dup keys>> clone >>keys ;
73
74 HOOK: read-keyboard game-input-backend ( -- keyboard-state )
75
76 {
77     { [ os windows? ] [ "game-input.dinput" require ] }
78     { [ os macosx? ] [ "game-input.iokit" require ] }
79     { [ t ] [ ] }
80 } cond