Joystick demo. iokit product-string cleanup. vectors > hashtables for tiny assocs

db4
Joe Groff 2008-07-25 20:31:37 -07:00
parent b41e619f77
commit 9724ac4a13
4 changed files with 115 additions and 9 deletions
extra
game-input/backend

View File

@ -0,0 +1,8 @@
USING: kernel system combinators parser ;
IN: game-input.backend
<< {
{ [ os macosx? ] [ "game-input.backend.iokit" use+ ] }
{ [ os windows? ] [ "game-input.backend.dinput" use+ ] }
{ [ t ] [ ] }
} cond >>

View File

@ -1,6 +1,6 @@
USING: windows.dinput windows.dinput.constants game-input
symbols alien.c-types windows.ole32 namespaces assocs kernel
arrays hashtables windows.kernel32 windows.com windows.dinput
arrays vectors windows.kernel32 windows.com windows.dinput
shuffle windows.user32 windows.messages sequences combinators
math.geometry.rect ui.windows accessors math windows alien
alien.strings io.encodings.utf16 continuations byte-arrays ;
@ -118,8 +118,8 @@ SYMBOLS: +dinput+ +keyboard-device+
f DIEDFL_ATTACHEDONLY IDirectInput8W::EnumDevices ole32-error ;
: set-up-controllers ( -- )
4 <hashtable> +controller-devices+ set-global
4 <hashtable> +controller-guids+ set-global
4 <vector> +controller-devices+ set-global
4 <vector> +controller-guids+ set-global
find-controllers ;
: find-and-remove-detached-devices ( -- )
@ -266,6 +266,7 @@ M: dinput-game-input-backend instance-id
[ IDirectInputDevice8W::GetDeviceState ole32-error ] keep ;
M: dinput-game-input-backend read-controller
! XXX return f if device was disconnected
handle>> [ +controller-devices+ get at clone ] [
[ "DIJOYSTATE2" heap-size get-device-state ]
with-acquisition

View File

@ -1,7 +1,7 @@
USING: cocoa cocoa.plists core-foundation iokit iokit.hid
kernel cocoa.enumeration destructors math.parser cocoa.application
sequences locals combinators.short-circuit game-input threads
symbols namespaces assocs hashtables arrays combinators
symbols namespaces assocs vectors arrays combinators
core-foundation.run-loop accessors sequences.private
alien.c-types math ;
IN: game-input.backend.iokit
@ -215,7 +215,7 @@ SYMBOLS: +hid-manager+ +keyboard-state+ +controller-states+ ;
: initialize-variables ( manager -- )
+hid-manager+ set-global
4 <hashtable> +controller-states+ set-global
4 <vector> +controller-states+ set-global
256 f <array> +keyboard-state+ set-global ;
M: iokit-game-input-backend open-game-input
@ -232,8 +232,6 @@ M: iokit-game-input-backend open-game-input
]
} cleave ;
! XXX while game-input is open, we need to reset-game-input and open-game-input as a
! boot image hook
M: iokit-game-input-backend close-game-input
+hid-manager+ get-global [
+hid-manager+ global [
@ -252,10 +250,13 @@ M: iokit-game-input-backend close-game-input
M: iokit-game-input-backend get-controllers ( -- sequence )
+controller-states+ get keys [ controller boa ] map ;
: ?join ( pre post sep -- string )
2over start [ swap 2nip ] [ [ 2array ] dip join ] if ;
M: iokit-game-input-backend product-string ( controller -- string )
handle>>
[ kIOHIDManufacturerKey device-property ]
[ kIOHIDProductKey device-property ] bi 2array " " join ;
[ kIOHIDProductKey device-property ] bi " " ?join ;
M: iokit-game-input-backend product-id ( controller -- integer )
handle>>
[ kIOHIDVendorIDKey device-property ]
@ -270,7 +271,6 @@ M: iokit-game-input-backend read-keyboard ( -- keyboard-state )
+keyboard-state+ get clone keyboard-state boa ;
M: iokit-game-input-backend calibrate-controller ( controller -- )
! XXX
drop ;
iokit-game-input-backend game-input-backend set-global

View File

@ -0,0 +1,97 @@
USING: ui ui.gadgets sequences kernel arrays math colors
ui.render math.vectors accessors fry ui.gadgets.packs game-input
game-input.backend ui.gadgets.labels ui.gadgets.borders alarms
calendar locals combinators.lib strings ui.gadgets.buttons
combinators math.parser ;
IN: joystick-demo
: SIZE { 151 151 } ;
: ZSIZE 75 ;
: INDICATOR-SIZE { 4 4 } ;
: FREQUENCY ( -- f ) 30 recip seconds ;
TUPLE: axis-gadget < gadget indicator z-indicator ;
M: axis-gadget pref-dim* drop SIZE ;
: (rect-polygon) ( lo hi -- polygon )
2dup
[ [ second ] [ first ] bi* swap 2array ]
[ [ first ] [ second ] bi* 2array ] 2bi swapd 4array ;
: indicator-polygon ( -- polygon )
{ 0 0 } INDICATOR-SIZE (rect-polygon) ;
: <indicator-gadget> ( color -- indicator )
indicator-polygon <polygon-gadget> ;
: (xy>loc) ( x y -- xyloc )
2array 0.5 v*n { 0.5 0.5 } v+ SIZE v* [ >integer ] map
INDICATOR-SIZE 2 v/n v- ;
: (z>loc) ( xyloc z -- xyloc zloc )
ZSIZE * >integer 0 swap 2array dupd v+ ;
: (xyz>loc) ( x y z -- xyloc zloc )
[ [ 0.0 ] unless* ] tri@
[ (xy>loc) ] dip (z>loc) ;
: move-axis ( gadget x y z -- )
(xyz>loc) rot
[ indicator>> (>>loc) ]
[ z-indicator>> (>>loc) ] bi+ bi* ;
: <axis-gadget> ( -- gadget )
axis-gadget new-gadget
gray <indicator-gadget> [ >>indicator ] [ add-gadget ] bi
red <indicator-gadget> [ >>z-indicator ] [ add-gadget ] bi
dup 0.0 0.0 0.0 move-axis ;
TUPLE: joystick-demo-gadget < pack axis raxis controller buttons alarm ;
: add-gadget-with-border ( parent child -- parent )
2 <border> gray <solid> >>boundary add-gadget ;
: add-controller-label ( gadget controller -- gadget )
[ >>controller ] [ product-string <label> add-gadget ] bi ;
: add-axis-gadget ( gadget shelf -- gadget shelf )
<axis-gadget> [ >>axis ] [ add-gadget-with-border ] bi+ bi* ;
: add-raxis-gadget ( gadget shelf -- gadget shelf )
<axis-gadget> [ >>raxis ] [ add-gadget-with-border ] bi+ bi* ;
:: (add-button-gadgets) ( gadget shelf -- )
gadget controller>> read-controller buttons>> length [
number>string [ ] <bevel-button>
shelf over add-gadget drop
] map gadget (>>buttons) ;
: add-button-gadgets ( gadget shelf -- gadget shelf )
[ (add-button-gadgets) ] 2keep ;
: <joystick-demo-gadget> ( controller -- gadget )
joystick-demo-gadget new-gadget
{ 0 1 } >>orientation
swap add-controller-label
<shelf> add-axis-gadget add-raxis-gadget add-gadget
<shelf> add-button-gadgets add-gadget ;
: update-buttons ( buttons button-states -- )
[ >>selected? drop ] 2each ;
: update-axes ( gadget -- )
dup controller>> read-controller {
[ [ axis>> ] [ [ x>> ] [ y>> ] [ z>> ] tri ] bi* move-axis ]
[ [ raxis>> ] [ [ rx>> ] [ ry>> ] [ rz>> ] tri ] bi* move-axis ]
[ [ buttons>> ] [ buttons>> ] bi* update-buttons ]
[ drop relayout-1 ]
} 2cleave ;
M: joystick-demo-gadget graft*
dup '[ , update-axes ] FREQUENCY every >>alarm
drop ;
M: joystick-demo-gadget ungraft*
alarm>> cancel-alarm ;