2009-05-08 14:00:34 -04:00
|
|
|
USING: arrays accessors continuations kernel math system
|
2011-11-02 14:23:41 -04:00
|
|
|
sequences namespaces init vocabs combinators ;
|
2011-09-18 22:00:30 -04:00
|
|
|
FROM: namespaces => change-global ;
|
2009-10-08 02:42:54 -04:00
|
|
|
IN: game.input
|
2008-07-19 14:04:07 -04:00
|
|
|
|
2008-07-26 00:25:46 -04:00
|
|
|
SYMBOLS: game-input-backend game-input-opened ;
|
2008-07-19 14:04:07 -04:00
|
|
|
|
2009-05-08 14:00:34 -04:00
|
|
|
game-input-opened [ 0 ] initialize
|
|
|
|
|
2008-07-26 00:25:46 -04:00
|
|
|
HOOK: (open-game-input) game-input-backend ( -- )
|
|
|
|
HOOK: (close-game-input) game-input-backend ( -- )
|
2008-07-29 22:53:00 -04:00
|
|
|
HOOK: (reset-game-input) game-input-backend ( -- )
|
2008-07-26 00:25:46 -04:00
|
|
|
|
2009-05-08 14:00:34 -04:00
|
|
|
HOOK: get-controllers game-input-backend ( -- sequence )
|
|
|
|
|
|
|
|
HOOK: product-string game-input-backend ( controller -- string )
|
|
|
|
HOOK: product-id game-input-backend ( controller -- id )
|
|
|
|
HOOK: instance-id game-input-backend ( controller -- id )
|
|
|
|
|
|
|
|
HOOK: read-controller game-input-backend ( controller -- controller-state )
|
|
|
|
HOOK: calibrate-controller game-input-backend ( controller -- )
|
2010-01-30 23:39:43 -05:00
|
|
|
HOOK: vibrate-controller game-input-backend ( controller motor1 motor2 -- )
|
2009-05-08 14:00:34 -04:00
|
|
|
|
|
|
|
HOOK: read-keyboard game-input-backend ( -- keyboard-state )
|
|
|
|
|
|
|
|
HOOK: read-mouse game-input-backend ( -- mouse-state )
|
|
|
|
|
|
|
|
HOOK: reset-mouse game-input-backend ( -- )
|
|
|
|
|
2008-07-26 00:25:46 -04:00
|
|
|
: game-input-opened? ( -- ? )
|
2009-05-08 14:00:34 -04:00
|
|
|
game-input-opened get zero? not ;
|
2008-07-26 00:25:46 -04:00
|
|
|
|
2008-07-29 01:42:28 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2008-07-29 22:53:00 -04:00
|
|
|
M: f (reset-game-input) ;
|
|
|
|
|
2008-07-29 01:42:28 -04:00
|
|
|
: reset-game-input ( -- )
|
2008-07-29 22:53:00 -04:00
|
|
|
(reset-game-input) ;
|
|
|
|
|
2009-10-19 22:17:02 -04:00
|
|
|
[ reset-game-input ] "game-input" add-startup-hook
|
2008-07-29 01:42:28 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-05-08 14:00:34 -04:00
|
|
|
ERROR: game-input-not-open ;
|
|
|
|
|
2008-07-26 00:25:46 -04:00
|
|
|
: open-game-input ( -- )
|
|
|
|
game-input-opened? [
|
2014-07-22 09:09:26 -04:00
|
|
|
(open-game-input)
|
2009-05-08 14:00:34 -04:00
|
|
|
] unless
|
2009-08-13 20:21:44 -04:00
|
|
|
game-input-opened [ 1 + ] change-global
|
2009-05-08 14:00:34 -04:00
|
|
|
reset-mouse ;
|
2008-07-26 00:25:46 -04:00
|
|
|
: close-game-input ( -- )
|
2009-05-08 14:00:34 -04:00
|
|
|
game-input-opened [
|
|
|
|
dup zero? [ game-input-not-open ] when
|
2009-08-13 20:21:44 -04:00
|
|
|
1 -
|
2009-05-08 14:00:34 -04:00
|
|
|
] change-global
|
2008-07-26 00:25:46 -04:00
|
|
|
game-input-opened? [
|
2014-07-22 09:09:26 -04:00
|
|
|
(close-game-input)
|
2008-07-29 01:42:28 -04:00
|
|
|
reset-game-input
|
2009-05-08 18:22:04 -04:00
|
|
|
] unless ;
|
2008-07-19 14:04:07 -04:00
|
|
|
|
|
|
|
: with-game-input ( quot -- )
|
2009-03-24 04:58:11 -04:00
|
|
|
open-game-input [ close-game-input ] [ ] cleanup ; inline
|
2008-07-19 14:04:07 -04:00
|
|
|
|
|
|
|
TUPLE: controller handle ;
|
|
|
|
TUPLE: controller-state x y z rx ry rz slider pov buttons ;
|
|
|
|
|
|
|
|
M: controller-state clone
|
|
|
|
call-next-method dup buttons>> clone >>buttons ;
|
|
|
|
|
|
|
|
SYMBOLS:
|
|
|
|
pov-neutral
|
2008-07-19 18:17:12 -04:00
|
|
|
pov-up pov-up-right pov-right pov-down-right
|
|
|
|
pov-down pov-down-left pov-left pov-up-left ;
|
2008-07-19 14:04:07 -04:00
|
|
|
|
2008-07-20 21:04:47 -04:00
|
|
|
: find-controller-products ( product-id -- sequence )
|
|
|
|
get-controllers [ product-id = ] with filter ;
|
|
|
|
: find-controller-instance ( product-id instance-id -- controller/f )
|
|
|
|
get-controllers [
|
|
|
|
[ product-id = ]
|
2009-11-05 18:03:24 -05:00
|
|
|
[ instance-id = ] bi-curry bi* and
|
2014-07-22 09:09:26 -04:00
|
|
|
] 2with find nip ;
|
2008-07-19 14:04:07 -04:00
|
|
|
|
|
|
|
TUPLE: keyboard-state keys ;
|
|
|
|
|
|
|
|
M: keyboard-state clone
|
|
|
|
call-next-method dup keys>> clone >>keys ;
|
|
|
|
|
2009-05-05 10:45:43 -04:00
|
|
|
TUPLE: mouse-state dx dy scroll-dx scroll-dy buttons ;
|
|
|
|
|
|
|
|
M: mouse-state clone
|
|
|
|
call-next-method dup buttons>> clone >>buttons ;
|
|
|
|
|
2010-02-20 13:10:02 -05:00
|
|
|
SYMBOLS: pressed released ;
|
|
|
|
|
|
|
|
: button-delta ( old? new? -- delta )
|
|
|
|
{
|
|
|
|
{ [ 2dup xor not ] [ 2drop f ] }
|
|
|
|
{ [ dup not ] [ 2drop released ] }
|
|
|
|
{ [ over not ] [ 2drop pressed ] }
|
|
|
|
} cond ; inline
|
|
|
|
|
|
|
|
: buttons-delta-as ( old-buttons new-buttons exemplar -- delta )
|
|
|
|
[ button-delta ] swap 2map-as ; inline
|
|
|
|
|
|
|
|
: buttons-delta ( old-buttons new-buttons -- delta )
|
|
|
|
{ } buttons-delta-as ; inline
|
|
|
|
|
2008-12-16 02:33:51 -05:00
|
|
|
{
|
2010-05-02 03:38:37 -04:00
|
|
|
{ [ os windows? ] [ "game.input.dinput" require ] }
|
2009-10-08 02:42:54 -04:00
|
|
|
{ [ os macosx? ] [ "game.input.iokit" require ] }
|
2010-07-17 12:02:36 -04:00
|
|
|
{ [ os linux? ] [ "game.input.gtk" require ] }
|
2010-02-19 00:24:23 -05:00
|
|
|
[ ]
|
2008-12-16 02:33:51 -05:00
|
|
|
} cond
|