1 /** 2 A Singleton that deals with inputs. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.input; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.resource; 23 import godot.inputevent; 24 /** 25 A Singleton that deals with inputs. 26 27 This includes key presses, mouse buttons and movement, joypads, and input actions. Actions and their events can be set in the Project Settings / Input Map tab. Or be set with $(D InputMap). 28 */ 29 @GodotBaseClass struct InputSingleton 30 { 31 enum string _GODOT_internal_name = "Input"; 32 public: 33 @nogc nothrow: 34 union { godot_object _godot_object; GodotObject _GODOT_base; } 35 alias _GODOT_base this; 36 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 37 package(godot) __gshared bool _classBindingInitialized = false; 38 package(godot) static struct _classBinding 39 { 40 __gshared: 41 godot_object _singleton; 42 immutable char* _singletonName = "Input"; 43 @GodotName("is_key_pressed") GodotMethod!(bool, long) isKeyPressed; 44 @GodotName("is_mouse_button_pressed") GodotMethod!(bool, long) isMouseButtonPressed; 45 @GodotName("is_joy_button_pressed") GodotMethod!(bool, long, long) isJoyButtonPressed; 46 @GodotName("is_action_pressed") GodotMethod!(bool, String) isActionPressed; 47 @GodotName("is_action_just_pressed") GodotMethod!(bool, String) isActionJustPressed; 48 @GodotName("is_action_just_released") GodotMethod!(bool, String) isActionJustReleased; 49 @GodotName("get_action_strength") GodotMethod!(double, String) getActionStrength; 50 @GodotName("add_joy_mapping") GodotMethod!(void, String, bool) addJoyMapping; 51 @GodotName("remove_joy_mapping") GodotMethod!(void, String) removeJoyMapping; 52 @GodotName("joy_connection_changed") GodotMethod!(void, long, bool, String, String) joyConnectionChanged; 53 @GodotName("is_joy_known") GodotMethod!(bool, long) isJoyKnown; 54 @GodotName("get_joy_axis") GodotMethod!(double, long, long) getJoyAxis; 55 @GodotName("get_joy_name") GodotMethod!(String, long) getJoyName; 56 @GodotName("get_joy_guid") GodotMethod!(String, long) getJoyGuid; 57 @GodotName("get_connected_joypads") GodotMethod!(Array) getConnectedJoypads; 58 @GodotName("get_joy_vibration_strength") GodotMethod!(Vector2, long) getJoyVibrationStrength; 59 @GodotName("get_joy_vibration_duration") GodotMethod!(double, long) getJoyVibrationDuration; 60 @GodotName("get_joy_button_string") GodotMethod!(String, long) getJoyButtonString; 61 @GodotName("get_joy_button_index_from_string") GodotMethod!(long, String) getJoyButtonIndexFromString; 62 @GodotName("get_joy_axis_string") GodotMethod!(String, long) getJoyAxisString; 63 @GodotName("get_joy_axis_index_from_string") GodotMethod!(long, String) getJoyAxisIndexFromString; 64 @GodotName("start_joy_vibration") GodotMethod!(void, long, double, double, double) startJoyVibration; 65 @GodotName("stop_joy_vibration") GodotMethod!(void, long) stopJoyVibration; 66 @GodotName("get_gravity") GodotMethod!(Vector3) getGravity; 67 @GodotName("get_accelerometer") GodotMethod!(Vector3) getAccelerometer; 68 @GodotName("get_magnetometer") GodotMethod!(Vector3) getMagnetometer; 69 @GodotName("get_gyroscope") GodotMethod!(Vector3) getGyroscope; 70 @GodotName("get_last_mouse_speed") GodotMethod!(Vector2) getLastMouseSpeed; 71 @GodotName("get_mouse_button_mask") GodotMethod!(long) getMouseButtonMask; 72 @GodotName("set_mouse_mode") GodotMethod!(void, long) setMouseMode; 73 @GodotName("get_mouse_mode") GodotMethod!(Input.MouseMode) getMouseMode; 74 @GodotName("warp_mouse_position") GodotMethod!(void, Vector2) warpMousePosition; 75 @GodotName("action_press") GodotMethod!(void, String) actionPress; 76 @GodotName("action_release") GodotMethod!(void, String) actionRelease; 77 @GodotName("set_default_cursor_shape") GodotMethod!(void, long) setDefaultCursorShape; 78 @GodotName("set_custom_mouse_cursor") GodotMethod!(void, Resource, long, Vector2) setCustomMouseCursor; 79 @GodotName("parse_input_event") GodotMethod!(void, InputEvent) parseInputEvent; 80 } 81 bool opEquals(in InputSingleton other) const { return _godot_object.ptr is other._godot_object.ptr; } 82 InputSingleton opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 83 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 84 mixin baseCasts; 85 static InputSingleton _new() 86 { 87 static godot_class_constructor constructor; 88 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Input"); 89 if(constructor is null) return typeof(this).init; 90 return cast(InputSingleton)(constructor()); 91 } 92 @disable new(size_t s); 93 /// 94 enum MouseMode : int 95 { 96 /** 97 Makes the mouse cursor visible if it is hidden. 98 */ 99 mouseModeVisible = 0, 100 /** 101 Makes the mouse cursor hidden if it is visible. 102 */ 103 mouseModeHidden = 1, 104 /** 105 Captures the mouse. The mouse will be hidden and unable to leave the game window. But it will still register movement and mouse button presses. 106 */ 107 mouseModeCaptured = 2, 108 /** 109 Makes the mouse cursor visible but confines it to the game window. 110 */ 111 mouseModeConfined = 3, 112 } 113 /// 114 enum CursorShape : int 115 { 116 /** 117 Arrow cursor. Standard, default pointing cursor. 118 */ 119 cursorArrow = 0, 120 /** 121 I-beam cursor. Usually used to show where the text cursor will appear when the mouse is clicked. 122 */ 123 cursorIbeam = 1, 124 /** 125 Pointing hand cursor. Usually used to indicate the pointer is over a link or other interactable item. 126 */ 127 cursorPointingHand = 2, 128 /** 129 Cross cursor. Typically appears over regions in which a drawing operation can be performed or for selections. 130 */ 131 cursorCross = 3, 132 /** 133 Wait cursor. Indicates that the application is busy performing an operation. 134 */ 135 cursorWait = 4, 136 /** 137 Busy cursor. See `CURSOR_WAIT`. 138 */ 139 cursorBusy = 5, 140 /** 141 Drag cursor. Usually displayed when dragging something. 142 */ 143 cursorDrag = 6, 144 /** 145 Can drop cursor. Usually displayed when dragging something to indicate that it can be dropped at the current position. 146 */ 147 cursorCanDrop = 7, 148 /** 149 Forbidden cursor. Indicates that the current action is forbidden (for example, when dragging something) or that the control at a position is disabled. 150 */ 151 cursorForbidden = 8, 152 /** 153 Vertical resize mouse cursor. A double headed vertical arrow. It tells the user they can resize the window or the panel vertically. 154 */ 155 cursorVsize = 9, 156 /** 157 Horizontal resize mouse cursor. A double headed horizontal arrow. It tells the user they can resize the window or the panel horizontally. 158 */ 159 cursorHsize = 10, 160 /** 161 Window resize mouse cursor. The cursor is a double headed arrow that goes from the bottom left to the top right. It tells the user they can resize the window or the panel both horizontally and vertically. 162 */ 163 cursorBdiagsize = 11, 164 /** 165 Window resize mouse cursor. The cursor is a double headed arrow that goes from the top left to the bottom right, the opposite of `CURSOR_BDIAGSIZE`. It tells the user they can resize the window or the panel both horizontally and vertically. 166 */ 167 cursorFdiagsize = 12, 168 /** 169 Move cursor. Indicates that something can be moved. 170 */ 171 cursorMove = 13, 172 /** 173 Vertical split mouse cursor. On Windows, it's the same as `CURSOR_VSIZE`. 174 */ 175 cursorVsplit = 14, 176 /** 177 Horizontal split mouse cursor. On Windows, it's the same as `CURSOR_HSIZE`. 178 */ 179 cursorHsplit = 15, 180 /** 181 Help cursor. Usually a question mark. 182 */ 183 cursorHelp = 16, 184 } 185 /// 186 enum Constants : int 187 { 188 cursorArrow = 0, 189 mouseModeVisible = 0, 190 mouseModeHidden = 1, 191 cursorIbeam = 1, 192 mouseModeCaptured = 2, 193 cursorPointingHand = 2, 194 mouseModeConfined = 3, 195 cursorCross = 3, 196 cursorWait = 4, 197 cursorBusy = 5, 198 cursorDrag = 6, 199 cursorCanDrop = 7, 200 cursorForbidden = 8, 201 cursorVsize = 9, 202 cursorHsize = 10, 203 cursorBdiagsize = 11, 204 cursorFdiagsize = 12, 205 cursorMove = 13, 206 cursorVsplit = 14, 207 cursorHsplit = 15, 208 cursorHelp = 16, 209 } 210 /** 211 Returns `true` if you are pressing the key. You can pass `KEY_*`, which are pre-defined constants listed in $(D @GlobalScope). 212 */ 213 bool isKeyPressed(in long scancode) const 214 { 215 checkClassBinding!(typeof(this))(); 216 return ptrcall!(bool)(_classBinding.isKeyPressed, _godot_object, scancode); 217 } 218 /** 219 Returns `true` if you are pressing the mouse button. You can pass `BUTTON_*`, which are pre-defined constants listed in $(D @GlobalScope). 220 */ 221 bool isMouseButtonPressed(in long button) const 222 { 223 checkClassBinding!(typeof(this))(); 224 return ptrcall!(bool)(_classBinding.isMouseButtonPressed, _godot_object, button); 225 } 226 /** 227 Returns `true` if you are pressing the joypad button. (see `JOY_*` constants in $(D @GlobalScope)) 228 */ 229 bool isJoyButtonPressed(in long device, in long button) const 230 { 231 checkClassBinding!(typeof(this))(); 232 return ptrcall!(bool)(_classBinding.isJoyButtonPressed, _godot_object, device, button); 233 } 234 /** 235 Returns `true` if you are pressing the action event. 236 */ 237 bool isActionPressed(StringArg0)(in StringArg0 action) const 238 { 239 checkClassBinding!(typeof(this))(); 240 return ptrcall!(bool)(_classBinding.isActionPressed, _godot_object, action); 241 } 242 /** 243 Returns `true` when the user starts pressing the action event, meaning it's true only on the frame that the user pressed down the button. 244 This is useful for code that needs to run only once when an action is pressed, instead of every frame while it's pressed. 245 */ 246 bool isActionJustPressed(StringArg0)(in StringArg0 action) const 247 { 248 checkClassBinding!(typeof(this))(); 249 return ptrcall!(bool)(_classBinding.isActionJustPressed, _godot_object, action); 250 } 251 /** 252 Returns `true` when the user stops pressing the action event, meaning it's true only on the frame that the user released the button. 253 */ 254 bool isActionJustReleased(StringArg0)(in StringArg0 action) const 255 { 256 checkClassBinding!(typeof(this))(); 257 return ptrcall!(bool)(_classBinding.isActionJustReleased, _godot_object, action); 258 } 259 /** 260 Returns a value between 0 and 1 representing the intensity of the given action. In a joypad, for example, the further away the axis (analog sticks or L2, R2 triggers) is from the dead zone, the closer the value will be to 1. If the action is mapped to a control that has no axis as the keyboard, the value returned will be 0 or 1. 261 */ 262 double getActionStrength(StringArg0)(in StringArg0 action) const 263 { 264 checkClassBinding!(typeof(this))(); 265 return ptrcall!(double)(_classBinding.getActionStrength, _godot_object, action); 266 } 267 /** 268 Add a new mapping entry (in SDL2 format) to the mapping database. Optionally update already connected devices. 269 */ 270 void addJoyMapping(StringArg0)(in StringArg0 mapping, in bool update_existing = false) 271 { 272 checkClassBinding!(typeof(this))(); 273 ptrcall!(void)(_classBinding.addJoyMapping, _godot_object, mapping, update_existing); 274 } 275 /** 276 Removes all mappings from the internal db that match the given uid. 277 */ 278 void removeJoyMapping(StringArg0)(in StringArg0 guid) 279 { 280 checkClassBinding!(typeof(this))(); 281 ptrcall!(void)(_classBinding.removeJoyMapping, _godot_object, guid); 282 } 283 /** 284 285 */ 286 void joyConnectionChanged(StringArg2, StringArg3)(in long device, in bool connected, in StringArg2 name, in StringArg3 guid) 287 { 288 checkClassBinding!(typeof(this))(); 289 ptrcall!(void)(_classBinding.joyConnectionChanged, _godot_object, device, connected, name, guid); 290 } 291 /** 292 Returns `true` if the system knows the specified device. This means that it sets all button and axis indices exactly as defined in the `JOY_*` constants (see $(D @GlobalScope)). Unknown joypads are not expected to match these constants, but you can still retrieve events from them. 293 */ 294 bool isJoyKnown(in long device) 295 { 296 checkClassBinding!(typeof(this))(); 297 return ptrcall!(bool)(_classBinding.isJoyKnown, _godot_object, device); 298 } 299 /** 300 Returns the current value of the joypad axis at given index (see `JOY_*` constants in $(D @GlobalScope)) 301 */ 302 double getJoyAxis(in long device, in long axis) const 303 { 304 checkClassBinding!(typeof(this))(); 305 return ptrcall!(double)(_classBinding.getJoyAxis, _godot_object, device, axis); 306 } 307 /** 308 Returns the name of the joypad at the specified device index 309 */ 310 String getJoyName(in long device) 311 { 312 checkClassBinding!(typeof(this))(); 313 return ptrcall!(String)(_classBinding.getJoyName, _godot_object, device); 314 } 315 /** 316 Returns a SDL2 compatible device guid on platforms that use gamepad remapping. Returns "Default Gamepad" otherwise. 317 */ 318 String getJoyGuid(in long device) const 319 { 320 checkClassBinding!(typeof(this))(); 321 return ptrcall!(String)(_classBinding.getJoyGuid, _godot_object, device); 322 } 323 /** 324 Returns an $(D Array) containing the device IDs of all currently connected joypads. 325 */ 326 Array getConnectedJoypads() 327 { 328 checkClassBinding!(typeof(this))(); 329 return ptrcall!(Array)(_classBinding.getConnectedJoypads, _godot_object); 330 } 331 /** 332 Returns the strength of the joypad vibration: x is the strength of the weak motor, and y is the strength of the strong motor. 333 */ 334 Vector2 getJoyVibrationStrength(in long device) 335 { 336 checkClassBinding!(typeof(this))(); 337 return ptrcall!(Vector2)(_classBinding.getJoyVibrationStrength, _godot_object, device); 338 } 339 /** 340 Returns the duration of the current vibration effect in seconds. 341 */ 342 double getJoyVibrationDuration(in long device) 343 { 344 checkClassBinding!(typeof(this))(); 345 return ptrcall!(double)(_classBinding.getJoyVibrationDuration, _godot_object, device); 346 } 347 /** 348 Receives a `JOY_BUTTON_*` Enum and returns its equivalent name as a string. 349 */ 350 String getJoyButtonString(in long button_index) 351 { 352 checkClassBinding!(typeof(this))(); 353 return ptrcall!(String)(_classBinding.getJoyButtonString, _godot_object, button_index); 354 } 355 /** 356 Returns the index of the provided button name. 357 */ 358 long getJoyButtonIndexFromString(StringArg0)(in StringArg0 button) 359 { 360 checkClassBinding!(typeof(this))(); 361 return ptrcall!(long)(_classBinding.getJoyButtonIndexFromString, _godot_object, button); 362 } 363 /** 364 Receives a `JOY_AXIS_*` Enum and returns its equivalent name as a string. 365 */ 366 String getJoyAxisString(in long axis_index) 367 { 368 checkClassBinding!(typeof(this))(); 369 return ptrcall!(String)(_classBinding.getJoyAxisString, _godot_object, axis_index); 370 } 371 /** 372 Returns the index of the provided axis name. 373 */ 374 long getJoyAxisIndexFromString(StringArg0)(in StringArg0 axis) 375 { 376 checkClassBinding!(typeof(this))(); 377 return ptrcall!(long)(_classBinding.getJoyAxisIndexFromString, _godot_object, axis); 378 } 379 /** 380 Starts to vibrate the joypad. Joypads usually come with two rumble motors, a strong and a weak one. weak_magnitude is the strength of the weak motor (between 0 and 1) and strong_magnitude is the strength of the strong motor (between 0 and 1). duration is the duration of the effect in seconds (a duration of 0 will try to play the vibration indefinitely). 381 Note that not every hardware is compatible with long effect durations, it is recommended to restart an effect if in need to play it for more than a few seconds. 382 */ 383 void startJoyVibration(in long device, in double weak_magnitude, in double strong_magnitude, in double duration = 0) 384 { 385 checkClassBinding!(typeof(this))(); 386 ptrcall!(void)(_classBinding.startJoyVibration, _godot_object, device, weak_magnitude, strong_magnitude, duration); 387 } 388 /** 389 Stops the vibration of the joypad. 390 */ 391 void stopJoyVibration(in long device) 392 { 393 checkClassBinding!(typeof(this))(); 394 ptrcall!(void)(_classBinding.stopJoyVibration, _godot_object, device); 395 } 396 /** 397 If the device has an accelerometer, this will return the gravity. Otherwise, it returns an empty $(D Vector3). 398 */ 399 Vector3 getGravity() const 400 { 401 checkClassBinding!(typeof(this))(); 402 return ptrcall!(Vector3)(_classBinding.getGravity, _godot_object); 403 } 404 /** 405 If the device has an accelerometer, this will return the acceleration. Otherwise, it returns an empty $(D Vector3). 406 Note this method returns an empty $(D Vector3) when running from the editor even when your device has an accelerometer. You must export your project to a supported device to read values from the accelerometer. 407 */ 408 Vector3 getAccelerometer() const 409 { 410 checkClassBinding!(typeof(this))(); 411 return ptrcall!(Vector3)(_classBinding.getAccelerometer, _godot_object); 412 } 413 /** 414 If the device has a magnetometer, this will return the magnetic field strength in micro-Tesla for all axes. 415 */ 416 Vector3 getMagnetometer() const 417 { 418 checkClassBinding!(typeof(this))(); 419 return ptrcall!(Vector3)(_classBinding.getMagnetometer, _godot_object); 420 } 421 /** 422 If the device has a gyroscope, this will return the rate of rotation in rad/s around a device's x, y, and z axis. Otherwise, it returns an empty $(D Vector3). 423 */ 424 Vector3 getGyroscope() const 425 { 426 checkClassBinding!(typeof(this))(); 427 return ptrcall!(Vector3)(_classBinding.getGyroscope, _godot_object); 428 } 429 /** 430 Returns the mouse speed for the last time the cursor was moved, and this until the next frame where the mouse moves. This means that even if the mouse is not moving, this function will still return the value of the last motion. 431 */ 432 Vector2 getLastMouseSpeed() const 433 { 434 checkClassBinding!(typeof(this))(); 435 return ptrcall!(Vector2)(_classBinding.getLastMouseSpeed, _godot_object); 436 } 437 /** 438 Returns mouse buttons as a bitmask. If multiple mouse buttons are pressed at the same time the bits are added together. 439 */ 440 long getMouseButtonMask() const 441 { 442 checkClassBinding!(typeof(this))(); 443 return ptrcall!(long)(_classBinding.getMouseButtonMask, _godot_object); 444 } 445 /** 446 Set the mouse mode. See the constants for more information. 447 */ 448 void setMouseMode(in long mode) 449 { 450 checkClassBinding!(typeof(this))(); 451 ptrcall!(void)(_classBinding.setMouseMode, _godot_object, mode); 452 } 453 /** 454 Return the mouse mode. See the constants for more information. 455 */ 456 Input.MouseMode getMouseMode() const 457 { 458 checkClassBinding!(typeof(this))(); 459 return ptrcall!(Input.MouseMode)(_classBinding.getMouseMode, _godot_object); 460 } 461 /** 462 Sets the mouse position to the specified vector. 463 */ 464 void warpMousePosition(in Vector2 to) 465 { 466 checkClassBinding!(typeof(this))(); 467 ptrcall!(void)(_classBinding.warpMousePosition, _godot_object, to); 468 } 469 /** 470 This will simulate pressing the specified action. 471 */ 472 void actionPress(StringArg0)(in StringArg0 action) 473 { 474 checkClassBinding!(typeof(this))(); 475 ptrcall!(void)(_classBinding.actionPress, _godot_object, action); 476 } 477 /** 478 If the specified action is already pressed, this will release it. 479 */ 480 void actionRelease(StringArg0)(in StringArg0 action) 481 { 482 checkClassBinding!(typeof(this))(); 483 ptrcall!(void)(_classBinding.actionRelease, _godot_object, action); 484 } 485 /** 486 Sets the default cursor shape to be used in the viewport instead of `CURSOR_ARROW`. 487 Note that if you want to change the default cursor shape for $(D Control)'s nodes, use $(D Control.mouseDefaultCursorShape) instead. 488 */ 489 void setDefaultCursorShape(in long shape = 0) 490 { 491 checkClassBinding!(typeof(this))(); 492 ptrcall!(void)(_classBinding.setDefaultCursorShape, _godot_object, shape); 493 } 494 /** 495 Sets a custom mouse cursor image, which is only visible inside the game window. The hotspot can also be specified. Passing `null` to the image parameter resets to the system cursor. See enum `CURSOR_*` for the list of shapes. 496 `image`'s size must be lower than 256x256. 497 `hotspot` must be within `image`'s size. 498 */ 499 void setCustomMouseCursor(Resource image, in long shape = 0, in Vector2 hotspot = Vector2(0, 0)) 500 { 501 checkClassBinding!(typeof(this))(); 502 ptrcall!(void)(_classBinding.setCustomMouseCursor, _godot_object, image, shape, hotspot); 503 } 504 /** 505 Feeds an $(D InputEvent) to the game. Can be used to artificially trigger input events from code. 506 */ 507 void parseInputEvent(InputEvent event) 508 { 509 checkClassBinding!(typeof(this))(); 510 ptrcall!(void)(_classBinding.parseInputEvent, _godot_object, event); 511 } 512 } 513 /// Returns: the InputSingleton 514 @property @nogc nothrow pragma(inline, true) 515 InputSingleton Input() 516 { 517 checkClassBinding!InputSingleton(); 518 return InputSingleton(InputSingleton._classBinding._singleton); 519 }