diff --git a/extra/game-input/backend/backend.factor b/extra/game-input/backend/backend.factor new file mode 100644 index 0000000000..451bbf1c34 --- /dev/null +++ b/extra/game-input/backend/backend.factor @@ -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 >> diff --git a/extra/game-input/backend/dinput/dinput.factor b/extra/game-input/backend/dinput/dinput.factor index 0be5a9832e..5744025d00 100755 --- a/extra/game-input/backend/dinput/dinput.factor +++ b/extra/game-input/backend/dinput/dinput.factor @@ -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 +controller-devices+ set-global - 4 +controller-guids+ set-global + 4 +controller-devices+ set-global + 4 +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 diff --git a/extra/game-input/backend/iokit/iokit.factor b/extra/game-input/backend/iokit/iokit.factor index 3ba6a0a691..fe2ce66789 100755 --- a/extra/game-input/backend/iokit/iokit.factor +++ b/extra/game-input/backend/iokit/iokit.factor @@ -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 +controller-states+ set-global + 4 +controller-states+ set-global 256 f +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 diff --git a/extra/joystick-demo/joystick-demo.factor b/extra/joystick-demo/joystick-demo.factor new file mode 100644 index 0000000000..d8963862b7 --- /dev/null +++ b/extra/joystick-demo/joystick-demo.factor @@ -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) ; + +: ( color -- indicator ) + indicator-polygon ; + +: (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* ; + +: ( -- gadget ) + axis-gadget new-gadget + gray [ >>indicator ] [ add-gadget ] bi + red [ >>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 gray >>boundary add-gadget ; + +: add-controller-label ( gadget controller -- gadget ) + [ >>controller ] [ product-string