]> gitweb.factorcode.org Git - factor.git/blob - basis/game/input/input.factor
basis: ERROR: changes.
[factor.git] / basis / game / input / input.factor
1 USING: arrays accessors continuations kernel math system
2 sequences namespaces init vocabs combinators ;
3 IN: game.input
4
5 SYMBOLS: game-input-backend game-input-opened ;
6
7 game-input-opened [ 0 ] initialize
8
9 HOOK: (open-game-input)  game-input-backend ( -- )
10 HOOK: (close-game-input) game-input-backend ( -- )
11 HOOK: (reset-game-input) game-input-backend ( -- )
12
13 HOOK: get-controllers game-input-backend ( -- sequence )
14
15 HOOK: product-string game-input-backend ( controller -- string )
16 HOOK: product-id game-input-backend ( controller -- id )
17 HOOK: instance-id game-input-backend ( controller -- id )
18
19 HOOK: read-controller game-input-backend ( controller -- controller-state )
20 HOOK: calibrate-controller game-input-backend ( controller -- )
21 HOOK: vibrate-controller game-input-backend ( controller motor1 motor2 -- )
22
23 HOOK: read-keyboard game-input-backend ( -- keyboard-state )
24
25 HOOK: read-mouse game-input-backend ( -- mouse-state )
26
27 HOOK: reset-mouse game-input-backend ( -- )
28
29 : game-input-opened? ( -- ? )
30     game-input-opened get zero? not ;
31
32 <PRIVATE
33
34 M: f (reset-game-input) ;
35
36 : reset-game-input ( -- )
37     (reset-game-input) ;
38
39 [ reset-game-input ] "game-input" add-startup-hook
40
41 PRIVATE>
42
43 ERROR: game-input-not-open ;
44
45 : open-game-input ( -- )
46     game-input-opened? [
47         (open-game-input)
48     ] unless
49     game-input-opened [ 1 + ] change-global
50     reset-mouse ;
51 : close-game-input ( -- )
52     game-input-opened [
53         dup zero? [ throw-game-input-not-open ] when
54         1 -
55     ] change-global
56     game-input-opened? [
57         (close-game-input)
58         reset-game-input
59     ] unless ;
60
61 : with-game-input ( quot -- )
62     open-game-input [ close-game-input ] [ ] cleanup ; inline
63
64 TUPLE: controller handle ;
65 TUPLE: controller-state x y z rx ry rz slider pov buttons ;
66
67 M: controller-state clone
68     call-next-method dup buttons>> clone >>buttons ;
69
70 SYMBOLS:
71     pov-neutral
72     pov-up pov-up-right pov-right pov-down-right
73     pov-down pov-down-left pov-left pov-up-left ;
74
75 : find-controller-products ( product-id -- sequence )
76     get-controllers [ product-id = ] with filter ;
77 : find-controller-instance ( product-id instance-id -- controller/f )
78     get-controllers [
79         [ product-id  = ]
80         [ instance-id = ] bi-curry bi* and
81     ] 2with find nip ;
82
83 TUPLE: keyboard-state keys ;
84
85 M: keyboard-state clone
86     call-next-method dup keys>> clone >>keys ;
87
88 TUPLE: mouse-state dx dy scroll-dx scroll-dy buttons ;
89
90 M: mouse-state clone
91     call-next-method dup buttons>> clone >>buttons ;
92
93 SYMBOLS: pressed released ;
94
95 : button-delta ( old? new? -- delta )
96     {
97         { [ 2dup xor not ] [ 2drop f ] }
98         { [ dup  not     ] [ 2drop released ] }
99         { [ over not     ] [ 2drop pressed ] }
100     } cond ; inline
101
102 : buttons-delta-as ( old-buttons new-buttons exemplar -- delta )
103     [ button-delta ] swap 2map-as ; inline
104
105 : buttons-delta ( old-buttons new-buttons -- delta )
106     { } buttons-delta-as ; inline
107
108 {
109     { [ os windows? ] [ "game.input.dinput" require ] }
110     { [ os macosx? ] [ "game.input.iokit" require ] }
111     { [ os linux? ] [ "game.input.gtk" require ] }
112     [ ]
113 } cond