]> gitweb.factorcode.org Git - factor.git/blob - basis/game/input/input-docs.factor
Factor source files should not be executable
[factor.git] / basis / game / input / input-docs.factor
1 USING: help.markup help.syntax kernel ui.gestures quotations
2 sequences strings math ;
3 IN: game.input
4
5 ARTICLE: "game-input" "Game controller input"
6 "The " { $vocab-link "game.input" } " vocabulary provides cross-platform access to game controller devices such as joysticks and gamepads. It also provides an interface for polling raw keyboard and mouse input." $nl
7 "The game input interface must be initialized before being used:"
8 { $subsections
9     open-game-input
10     close-game-input
11     with-game-input
12 }
13 "Once the game input interface is open, connected controller devices can be enumerated:"
14 { $subsections
15     get-controllers
16     find-controller-products
17     find-controller-instance
18 }
19 "These " { $link controller } " objects can be queried of their identity:"
20 { $subsections
21     product-string
22     product-id
23     instance-id
24 }
25 "A hook is provided for invoking the system calibration tool:"
26 { $subsections calibrate-controller }
27 "The current state of a controller, the keyboard, and the mouse can be read:"
28 { $subsections
29     read-controller
30     read-keyboard
31     read-mouse
32     controller-state
33     keyboard-state
34     mouse-state
35 } ;
36
37 HELP: open-game-input
38 { $description "Initializes the game input interface. An exception will be thrown if the initialization fails. Calls to open-game-input are reference counted; each call to open-game-input needs a corresponding call to close-game-input to close the game input interface." } ;
39
40 HELP: close-game-input
41 { $description "Closes the game input interface, releasing any allocated resources. Once this word is called, any remaining " { $link controller } " objects are invalid." } ;
42
43 HELP: game-input-opened?
44 { $values { "?" "a boolean" } }
45 { $description "Returns true if the game input interface is open, false otherwise." } ;
46
47 HELP: with-game-input
48 { $values { "quot" quotation } }
49 { $description "Initializes the game input interface for the dynamic extent of " { $snippet "quotation" } "." } ;
50
51 { open-game-input close-game-input with-game-input game-input-opened? } related-words
52
53 HELP: get-controllers
54 { $values { "sequence" "A " { $link sequence } " of " { $link controller } "s" } }
55 { $description "Returns a " { $link sequence } " of " { $link controller } " objects representing the currently connected game controllers. The order of the controller objects in the sequence is not significant or guaranteed to be stable between calls to " { $snippet "get-controllers" } "." } ;
56
57 HELP: find-controller-products
58 { $values { "product-id" "A product ID as returned by " { $link product-id } } { "sequence" "A " { $link sequence } " of " { $link controller } "s" } }
59 { $description "Returns a " { $link sequence } " of " { $link controller } " objects representing the currently connected game controllers with the given " { $link product-id } ". The order of the controller objects in the sequence is not significant or guaranteed to be stable between calls to " { $snippet "find-controller-products" } "." } ;
60
61 HELP: find-controller-instance
62 { $values { "product-id" "A product ID as returned by " { $link product-id } } { "instance-id" "An instance ID as returned by " { $link instance-id } "." } { "controller/f" "A " { $link controller } " object, or " { $link f } } }
63 { $description "Returns the " { $link controller } " instance identified by " { $snippet "product-id" } " and " { $snippet "instance-id" } ". If the identified device is not currently attached, " { $link f } " is returned." } ;
64
65 HELP: controller
66 { $class-description "Objects of this class represent game controller devices such as joysticks and gamepads. They should be treated as opaque by client code." } ;
67
68 HELP: product-string
69 { $values { "controller" controller } { "string" string } }
70 { $description "Returns a human-readable string describing the game controller device represented by " { $snippet "controller" } ". This string is not necessarily unique to the product or instance; to uniquely identify the device, see " { $link product-id } " and " { $link instance-id } "." } ;
71
72 HELP: product-id
73 { $values { "controller" controller } { "id" "A unique identifier" } }
74 { $description "Returns an identifier uniquely representing the kind of game controller device represented by " { $snippet "controller" } ". This identifier will be the same for devices of the same make and manufacturer. The type of the identifier value is platform-specific, but equivalent " { $snippet "product-id" } "s are guaranteed to be testable with the " { $link = } " word. The identifier can be used to find devices of the same kind with the " { $link find-controller-products } " word." } ;
75
76 HELP: instance-id
77 { $values { "controller" controller } { "id" "A unique identifier" } }
78 { $description "Returns an identifier uniquely representing the game controller device represented by " { $snippet "controller" } ". This identifier paired with the device's " { $link product-id } " provides a unique identifier for a particular device that persists between reboots (but not necessarily between computers). This unique identifier can be used to find the same device again with the " { $snippet "find-controller-instance" } " word. Depending on the platform, the instance-id may change if the device is plugged into a different port. The type of the identifier value is platform-specific, but equivalent " { $snippet "instance-id" } "s are guaranteed to be testable with the " { $link = } " word." } ;
79
80 { product-string product-id instance-id find-controller-products find-controller-instance } related-words
81
82 HELP: calibrate-controller
83 { $values { "controller" controller } }
84 { $description "Invokes the operating system's calibration tool for " { $snippet "controller" } ". If the operating system does not have a calibration tool, this word does nothing." } ;
85
86 HELP: read-controller
87 { $values { "controller" controller } { "controller-state" controller-state } }
88 { $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." }
89 { $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." } ;
90
91 { controller-state controller read-controller } related-words
92
93 HELP: read-keyboard
94 { $values { "keyboard-state" keyboard-state } }
95 { $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." }
96 { $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."
97 $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" } "." } ;
98
99 HELP: read-mouse
100 { $values { "mouse-state" mouse-state } }
101 { $description "Reads the current mouse state relative to either when the game input interface was opened with " { $link open-game-input } " or when the mouse state was reset with " { $link reset-mouse } "." }
102 { $warning "For efficiency, the implementation may reuse the returned " { $snippet "mouse-state" } " object for future " { $snippet "read-mouse" } " or " { $snippet "reset-mouse" } " calls. You should " { $link clone } " the " { $snippet "mouse-state" } " object if you need to preserve it." } ;
103
104 HELP: reset-mouse
105 { $description "Resets the mouse state. Future " { $link read-mouse } " values will be relative to the time this word is called." } ;
106
107 HELP: controller-state
108 { $class-description "The " { $link read-controller } " word returns objects of this class. " { $snippet "controller-state" } " objects have the following slots:"
109 { $list
110     { { $snippet "x" } " contains the position of the device's X axis." }
111     { { $snippet "y" } " contains the position of the device's Y axis." }
112     { { $snippet "z" } " contains the position of the device's Z axis, if any." }
113     { { $snippet "rx" } " contains the rotational position of the device's X axis, if available." }
114     { { $snippet "ry" } " contains the rotational position of the device's Y axis, if available." }
115     { { $snippet "rz" } " contains the rotational position of the device's Z axis, if available." }
116     { { $snippet "slider" } " contains the position of the device's throttle slider, if any." }
117     { { $snippet "pov" } " contains the state of the device's POV hat, if any." }
118     { { $snippet "buttons" } " contains a sequence of values indicating the state of every button on the device." }
119 }
120 "The values are formatted as follows:"
121 { $list
122     { "For the axis slots (" { $snippet "x" } ", " { $snippet "y" } ", " { $snippet "z" } ", " { $snippet "rx" } ", " { $snippet "ry" } ", " { $snippet "rz" } "), a " { $link float } " value between -1.0 and 1.0 is returned." }
123     { "For the " { $snippet "slider" } " slot, a value between 0.0 and 1.0 is returned." }
124     { "For the " { $snippet "pov" } " slot, one of the following symbols is returned:" { $list
125         { { $link pov-neutral } }
126         { { $link pov-up } }
127         { { $link pov-up-right } }
128         { { $link pov-right } }
129         { { $link pov-down-right } }
130         { { $link pov-down } }
131         { { $link pov-down-left } }
132         { { $link pov-left } }
133         { { $link pov-up-left } }
134     } }
135     { "For each element of the " { $snippet "buttons" } " array, " { $link f } " indicates that the corresponding button is released. If the button is pressed, a value between 0.0 and 1.0 is returned indicating the pressure on the button (or simply 1.0 if the device's buttons are on/off only)." }
136     { "A value of " { $link f } " in any slot (besides the elements of " { $snippet "buttons" } ") indicates that the corresponding element is not present on the device." } } } ;
137
138 HELP: keyboard-state
139 { $class-description "The " { $link read-keyboard } " word returns objects of this class. The " { $snippet "keys" } " slot of a " { $snippet "keyboard-state" } " object contains a " { $link sequence } " of 256 members representing the state of the keys on the keyboard. Each element is a boolean value indicating whether the corresponding key is pressed. The sequence is indexed by scancode as defined under usage page 7 of the USB HID standard. Named scancode constants are provided in the " { $vocab-link "game.input.scancodes" } " vocabulary." }
140 { $warning "The scancodes used to index " { $snippet "keyboard-state" } " objects correspond to physical key positions on the keyboard--they are unaffected by keymaps, modifier keys, or other operating environment postprocessing. The face value of the constants in " { $vocab-link "game.input.scancodes" } " do not necessarily correspond to what the user expects the key to type. Because of this, " { $link 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" } "." } ;
141
142 HELP: mouse-state
143 { $class-description "The " { $link read-mouse } " word returns objects of this class. " { $snippet "mouse-state" } " objects have the following slots:"
144 { $list
145     { { $snippet "dx" } " contains the mouse's X axis movement." }
146     { { $snippet "dy" } " contains the mouse's Y axis movement." }
147     { { $snippet "scroll-dx" } " contains the scroller's X axis movement." }
148     { { $snippet "scroll-dy" } " contains the scroller's Y axis movement." }
149     { { $snippet "buttons" } " contains a sequence of boolean values indicate the state of the mouse's buttons." }
150 }
151 "Mouse movement is recorded relative to when the game input interface was opened with " { $link open-game-input } " or the mouse state is reset with " { $link reset-mouse } "."
152 } ;
153
154
155 { keyboard-state read-keyboard } related-words
156
157 ABOUT: "game-input"