MAIN: for joystick-demo. make it ok to open-game-input multiple times
parent
9724ac4a13
commit
9562452ccb
|
@ -188,15 +188,14 @@ TUPLE: window-rect < rect window-loc ;
|
||||||
+keyboard-device+ global
|
+keyboard-device+ global
|
||||||
[ com-release f ] change-at ;
|
[ com-release f ] change-at ;
|
||||||
|
|
||||||
M: dinput-game-input-backend open-game-input
|
M: dinput-game-input-backend (open-game-input)
|
||||||
+dinput+ get-global [ "game-input already open" throw ] when
|
|
||||||
create-dinput
|
create-dinput
|
||||||
create-device-change-window
|
create-device-change-window
|
||||||
find-keyboard
|
find-keyboard
|
||||||
set-up-controllers
|
set-up-controllers
|
||||||
add-wm-devicechange ;
|
add-wm-devicechange ;
|
||||||
|
|
||||||
M: dinput-game-input-backend close-game-input
|
M: dinput-game-input-backend (close-game-input)
|
||||||
remove-wm-devicechange
|
remove-wm-devicechange
|
||||||
release-controllers
|
release-controllers
|
||||||
release-keyboard
|
release-keyboard
|
||||||
|
|
|
@ -218,8 +218,7 @@ SYMBOLS: +hid-manager+ +keyboard-state+ +controller-states+ ;
|
||||||
4 <vector> +controller-states+ set-global
|
4 <vector> +controller-states+ set-global
|
||||||
256 f <array> +keyboard-state+ set-global ;
|
256 f <array> +keyboard-state+ set-global ;
|
||||||
|
|
||||||
M: iokit-game-input-backend open-game-input
|
M: iokit-game-input-backend (open-game-input)
|
||||||
+hid-manager+ get-global [ "game-input already open" throw ] when
|
|
||||||
hid-manager-matching-game-devices {
|
hid-manager-matching-game-devices {
|
||||||
[ initialize-variables ]
|
[ initialize-variables ]
|
||||||
[ device-matched-callback f IOHIDManagerRegisterDeviceMatchingCallback ]
|
[ device-matched-callback f IOHIDManagerRegisterDeviceMatchingCallback ]
|
||||||
|
@ -232,7 +231,7 @@ M: iokit-game-input-backend open-game-input
|
||||||
]
|
]
|
||||||
} cleave ;
|
} cleave ;
|
||||||
|
|
||||||
M: iokit-game-input-backend close-game-input
|
M: iokit-game-input-backend (close-game-input)
|
||||||
+hid-manager+ get-global [
|
+hid-manager+ get-global [
|
||||||
+hid-manager+ global [
|
+hid-manager+ global [
|
||||||
[
|
[
|
||||||
|
|
|
@ -25,16 +25,20 @@ ARTICLE: "game-input" "Game controller input"
|
||||||
{ $subsection keyboard-state } ;
|
{ $subsection keyboard-state } ;
|
||||||
|
|
||||||
HELP: open-game-input
|
HELP: open-game-input
|
||||||
{ $description "Initializes the game input interface. An exception will be thrown if the initialization fails." } ;
|
{ $description "Initializes the game input interface. An exception will be thrown if the initialization fails. If the game input interface is already opened, nothing happens." } ;
|
||||||
|
|
||||||
HELP: close-game-input
|
HELP: close-game-input
|
||||||
{ $description "Closes the game input interface, releasing any allocated resources. Once this word is called, any remaining " { $link controller } " objects are invalid." } ;
|
{ $description "Closes the game input interface, releasing any allocated resources. Once this word is called, any remaining " { $link controller } " objects are invalid. If the game input interface is not opened, nothing happens." } ;
|
||||||
|
|
||||||
|
HELP: game-input-opened?
|
||||||
|
{ $values { "?" "a boolean" } }
|
||||||
|
{ $description "Returns true if the game input interface is open, false otherwise." } ;
|
||||||
|
|
||||||
HELP: with-game-input
|
HELP: with-game-input
|
||||||
{ $values { "quot" quotation } }
|
{ $values { "quot" quotation } }
|
||||||
{ $description "Initializes the game input interface for the dynamic extent of " { $snippet "quotation" } "." } ;
|
{ $description "Initializes the game input interface for the dynamic extent of " { $snippet "quotation" } "." } ;
|
||||||
|
|
||||||
{ open-game-input close-game-input with-game-input } related-words
|
{ open-game-input close-game-input with-game-input game-input-opened? } related-words
|
||||||
|
|
||||||
HELP: get-controllers
|
HELP: get-controllers
|
||||||
{ $values { "sequence" "A " { $link sequence } " of " { $link controller } "s" } }
|
{ $values { "sequence" "A " { $link sequence } " of " { $link controller } "s" } }
|
||||||
|
@ -71,14 +75,16 @@ HELP: calibrate-controller
|
||||||
|
|
||||||
HELP: read-controller
|
HELP: read-controller
|
||||||
{ $values { "controller" controller } { "controller-state" controller-state } }
|
{ $values { "controller" controller } { "controller-state" controller-state } }
|
||||||
{ $description "Reads the current state of " { $snippet "controller" } ". See the documentation for the " { $link controller-state } " class for details of the returned value's format. If the device is no longer available, " { $link f } " is returned." } ;
|
{ $description "Reads the current state of " { $snippet "controller" } ". See the documentation for the " { $link controller-state } " class for details of the returned value's format. If the device is no longer available, " { $link f } " is returned." }
|
||||||
|
{ $warning "For efficiency, the implementation may reuse the returned " { $snippet "controller-state" } " object next time " { $snippet "read-controller" } " is called on the same controller. You should " { $link clone } " any values from the returned tuple you need to preserve." } ;
|
||||||
|
|
||||||
{ controller-state controller read-controller } related-words
|
{ controller-state controller read-controller } related-words
|
||||||
|
|
||||||
HELP: read-keyboard
|
HELP: read-keyboard
|
||||||
{ $values { "keyboard-state" keyboard-state } }
|
{ $values { "keyboard-state" keyboard-state } }
|
||||||
{ $description "Reads the current raw state of the keyboard. See the documentation for the " { $link keyboard-state } " class for details on the returned value's format." }
|
{ $description "Reads the current raw state of the keyboard. See the documentation for the " { $link keyboard-state } " class for details on the returned value's format." }
|
||||||
{ $warning "The keyboard state returned by this word is unprocessed by any keymaps, modifier keys, key repeat settings, or other operating environment postprocessing. Because of this, " { $snippet "read-keyboard" } " should not be used for text entry purposes. The Factor UI's standard gesture mechanism should be used in cases where the logical meaning of keypresses is needed; see " { $link "keyboard-gestures" } "." } ;
|
{ $warning "For efficiency, the implementation may reuse the returned " { $snippet "keyboard-state" } " object next time " { $snippet "read-keyboard" } " is called. You should " { $link clone } " any values from the returned tuple you need to preserve."
|
||||||
|
$nl "The keyboard state returned by this word is unprocessed by any keymaps, modifier keys, key repeat settings, or other operating environment postprocessing. Because of this, " { $snippet "read-keyboard" } " should not be used for text entry purposes. The Factor UI's standard gesture mechanism should be used in cases where the logical meaning of keypresses is needed; see " { $link "keyboard-gestures" } "." } ;
|
||||||
|
|
||||||
HELP: controller-state
|
HELP: controller-state
|
||||||
{ $class-description "The " { $link read-controller } " word returns objects of this class. " { $snippet "controller-state" } " objects have the following slots:"
|
{ $class-description "The " { $link read-controller } " word returns objects of this class. " { $snippet "controller-state" } " objects have the following slots:"
|
||||||
|
|
|
@ -1,11 +1,25 @@
|
||||||
USING: arrays accessors continuations kernel symbols
|
USING: arrays accessors continuations kernel symbols
|
||||||
combinators.lib sequences ;
|
combinators.lib sequences namespaces ;
|
||||||
IN: game-input
|
IN: game-input
|
||||||
|
|
||||||
SYMBOL: game-input-backend
|
SYMBOLS: game-input-backend game-input-opened ;
|
||||||
|
|
||||||
HOOK: open-game-input game-input-backend ( -- )
|
HOOK: (open-game-input) game-input-backend ( -- )
|
||||||
HOOK: close-game-input game-input-backend ( -- )
|
HOOK: (close-game-input) game-input-backend ( -- )
|
||||||
|
|
||||||
|
: game-input-opened? ( -- ? )
|
||||||
|
game-input-opened get ;
|
||||||
|
|
||||||
|
: open-game-input ( -- )
|
||||||
|
game-input-opened? [
|
||||||
|
(open-game-input)
|
||||||
|
game-input-opened on
|
||||||
|
] unless ;
|
||||||
|
: close-game-input ( -- )
|
||||||
|
game-input-opened? [
|
||||||
|
(close-game-input)
|
||||||
|
game-input-opened off
|
||||||
|
] when ;
|
||||||
|
|
||||||
: with-game-input ( quot -- )
|
: with-game-input ( quot -- )
|
||||||
open-game-input [ close-game-input ] [ ] cleanup ;
|
open-game-input [ close-game-input ] [ ] cleanup ;
|
||||||
|
|
|
@ -80,18 +80,37 @@ TUPLE: joystick-demo-gadget < pack axis raxis controller buttons alarm ;
|
||||||
: update-buttons ( buttons button-states -- )
|
: update-buttons ( buttons button-states -- )
|
||||||
[ >>selected? drop ] 2each ;
|
[ >>selected? drop ] 2each ;
|
||||||
|
|
||||||
: update-axes ( gadget -- )
|
: kill-update-axes ( gadget -- )
|
||||||
dup controller>> read-controller {
|
gray <solid> >>interior
|
||||||
[ [ axis>> ] [ [ x>> ] [ y>> ] [ z>> ] tri ] bi* move-axis ]
|
[ cancel-alarm f ] change-alarm
|
||||||
[ [ raxis>> ] [ [ rx>> ] [ ry>> ] [ rz>> ] tri ] bi* move-axis ]
|
relayout-1 ;
|
||||||
[ [ buttons>> ] [ buttons>> ] bi* update-buttons ]
|
|
||||||
|
: (update-axes) ( gadget controller-state -- )
|
||||||
|
{
|
||||||
|
[ [ axis>> ] [ [ x>> ] [ y>> ] [ z>> ] tri ] bi* move-axis ]
|
||||||
|
[ [ raxis>> ] [ [ rx>> ] [ ry>> ] [ rz>> ] tri ] bi* move-axis ]
|
||||||
|
[ [ buttons>> ] [ buttons>> ] bi* update-buttons ]
|
||||||
[ drop relayout-1 ]
|
[ drop relayout-1 ]
|
||||||
} 2cleave ;
|
} 2cleave ;
|
||||||
|
|
||||||
|
: update-axes ( gadget -- )
|
||||||
|
dup controller>> read-controller
|
||||||
|
[ (update-axes) ] [ kill-update-axes ] if* ;
|
||||||
|
|
||||||
M: joystick-demo-gadget graft*
|
M: joystick-demo-gadget graft*
|
||||||
dup '[ , update-axes ] FREQUENCY every >>alarm
|
dup '[ , update-axes ] FREQUENCY every >>alarm
|
||||||
drop ;
|
drop ;
|
||||||
|
|
||||||
M: joystick-demo-gadget ungraft*
|
M: joystick-demo-gadget ungraft*
|
||||||
alarm>> cancel-alarm ;
|
alarm>> [ cancel-alarm ] when* ;
|
||||||
|
|
||||||
|
: joystick-window ( controller -- )
|
||||||
|
[ <joystick-demo-gadget> ] [ product-string ] bi
|
||||||
|
open-window ;
|
||||||
|
|
||||||
|
: joystick-demo ( -- )
|
||||||
|
open-game-input [
|
||||||
|
get-controllers [ joystick-window ] each
|
||||||
|
] with-ui ;
|
||||||
|
|
||||||
|
MAIN: joystick-demo
|
||||||
|
|
Loading…
Reference in New Issue