inputevent.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. .. _doc_inputevent:
  2. InputEvent
  3. ==========
  4. What is it?
  5. -----------
  6. Managing input is usually complex, no matter the OS or platform. To ease
  7. this a little, a special built-in type is provided, :ref:`InputEvent <class_InputEvent>`.
  8. This datatype can be configured to contain several types of input
  9. events. Input Events travel through the engine and can be received in
  10. multiple locations, depending on the purpose.
  11. How does it work?
  12. -----------------
  13. Every input event is originated from the user/player (though it's
  14. possible to generate an InputEvent and feed them back to the engine,
  15. which is useful for gestures). The OS object for each platform will read
  16. events from the device, then feed them to MainLoop. As :ref:`SceneTree <class_SceneTree>`
  17. is the default MainLoop implementation, events are fed to it. Godot
  18. provides a function to get the current SceneTree object :
  19. **get_tree()**.
  20. But SceneTree does not know what to do with the event, so it will give
  21. it to the viewports, starting by the "root" :ref:`Viewport <class_Viewport>` (the first
  22. node of the scene tree). Viewport does quite a lot of stuff with the
  23. received input, in order:
  24. .. image:: /img/input_event_flow.png
  25. 1. First of all, the standard _input function
  26. will be called in any node with input processing enabled (enable with
  27. :ref:`Node.set_process_input() <class_Node_set_process_input>` and override
  28. :ref:`Node._input() <class_Node__input>`). If any function consumes the event, it can
  29. call :ref:`SceneTree.set_input_as_handled() <class_SceneTree_set_input_as_handled>`, and the event will
  30. not spread any more. This ensures that you can filter all events of interest, even before the GUI.
  31. For gameplay input, the _unhandled_input() is generally a better fit, because it allows the GUI to intercept the events.
  32. 2. Second, it will try to feed the input to the GUI, and see if any
  33. control can receive it. If so, the :ref:`Control <class_Control>` will be called via the
  34. virtual function :ref:`Control._input_event() <class_Control__input_event>` and the signal
  35. "input_event" will be emitted (this function is re-implementable by
  36. script by inheriting from it). If the control wants to "consume" the
  37. event, it will call :ref:`Control.accept_event() <class_Control_accept_event>` and the event will
  38. not spread any more.
  39. 3. If so far no one consumed the event, the unhandled input callback
  40. will be called (enable with
  41. :ref:`Node.set_process_unhandled_input() <class_Node_set_process_unhandled_input>` and override
  42. :ref:`Node._unhandled_input() <class_Node__unhandled_input>`). If any function consumes the
  43. event, it can call :ref:`SceneTree.set_input_as_handled() <class_SceneTree_set_input_as_handled>`, and the
  44. event will not spread any more. The unhandled input callback is ideal for full-screen gameplay events, so they are not received when a GUI is active.
  45. 4. If no one wanted the event so far, and a :ref:`Camera <class_Camera>` is assigned
  46. to the Viewport, a ray to the physics world (in the ray direction from
  47. the click) will be cast. If this ray hits an object, it will call the
  48. :ref:`CollisionObject._input_event() <class_CollisionObject__input_event>` function in the relevant
  49. physics object (bodies receive this callback by default, but areas do
  50. not. This can be configured through :ref:`Area <class_Area>` properties).
  51. 5. Finally, if the event was unhandled, it will be passed to the next
  52. Viewport in the tree, otherwise it will be ignored.
  53. Anatomy of an InputEvent
  54. ------------------------
  55. :ref:`InputEvent <class_InputEvent>` is just a base built-in type, it does not represent
  56. anything and only contains some basic information, such as event ID
  57. (which is increased for each event), device index, etc.
  58. InputEvent has a "type" member. By assigning it, it can become
  59. different types of input event. Every type of InputEvent has different
  60. properties, according to its role.
  61. Example of changing event type.
  62. ::
  63. # create event
  64. var ev = InputEvent()
  65. # set type index
  66. ev.type = InputEvent.MOUSE_BUTTON
  67. # button_index is only available for the above type
  68. ev.button_index = BUTTON_LEFT
  69. There are several types of InputEvent, described in the table below:
  70. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  71. | Event | Type Index | Description |
  72. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  73. | :ref:`InputEvent <class_InputEvent>` | NONE | Empty Input Event. |
  74. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  75. | :ref:`InputEventKey <class_InputEventKey>` | KEY | Contains a scancode and unicode value, |
  76. | | | as well as modifiers. |
  77. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  78. | :ref:`InputEventMouseButton <class_InputEventMouseButton>` | MOUSE_BUTTON | Contains click information, such as |
  79. | | | button, modifiers, etc. |
  80. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  81. | :ref:`InputEventMouseMotion <class_InputEventMouseMotion>` | MOUSE_MOTION | Contains motion information, such as |
  82. | | | relative, absolute positions and speed. |
  83. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  84. | :ref:`InputEventJoystickMotion <class_InputEventJoystickMotion>` | JOYSTICK_MOTION | Contains Joystick/Joypad analog axis |
  85. | | | information. |
  86. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  87. | :ref:`InputEventJoystickButton <class_InputEventJoystickButton>` | JOYSTICK_BUTTON | Contains Joystick/Joypad button |
  88. | | | information. |
  89. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  90. | :ref:`InputEventScreenTouch <class_InputEventScreenTouch>` | SCREEN_TOUCH | Contains multi-touch press/release |
  91. | | | information. (only available on mobile |
  92. | | | devices) |
  93. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  94. | :ref:`InputEventScreenDrag <class_InputEventScreenDrag>` | SCREEN_DRAG | Contains multi-touch drag information. |
  95. | | | (only available on mobile devices) |
  96. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  97. | :ref:`InputEventAction <class_InputEventAction>` | SCREEN_ACTION | Contains a generic action. These events |
  98. | | | are often generated by the programmer |
  99. | | | as feedback. (more on this below) |
  100. +-------------------------------------------------------------------+--------------------+-----------------------------------------+
  101. Actions
  102. -------
  103. An InputEvent may or may not represent a pre-defined action. Actions are
  104. useful because they abstract the input device when programming the game
  105. logic. This allows for:
  106. - The same code to work on different devices with different inputs (e.g.,
  107. keyboard on PC, Joypad on console).
  108. - Input to be reconfigured at run-time.
  109. Actions can be created from the Project Settings menu in the Actions
  110. tab. Read :ref:`doc_simple_2d_game-input_actions_setup` for an
  111. explanation on how the action editor works.
  112. Any event has the methods :ref:`InputEvent.is_action() <class_InputEvent_is_action>`,
  113. :ref:`InputEvent.is_pressed() <class_InputEvent_is_pressed>` and :ref:`InputEvent <class_InputEvent>`.
  114. Alternatively, it may be desired to supply the game back with an action
  115. from the game code (a good example of this is detecting gestures).
  116. The Input singleton has a method for this:
  117. :ref:`Input.parse_input_event() <class_input_parse_input_event>`. You would normally use it like this:
  118. ::
  119. var ev = InputEvent()
  120. ev.type = InputEvent.ACTION
  121. # set as move_left, pressed
  122. ev.set_as_action("move_left", true)
  123. # feedback
  124. Input.parse_input_event(ev)
  125. InputMap
  126. --------
  127. Customizing and re-mapping input from code is often desired. If your
  128. whole workflow depends on actions, the :ref:`InputMap <class_InputMap>` singleton is
  129. ideal for reassigning or creating different actions at run-time. This
  130. singleton is not saved (must be modified manually) and its state is run
  131. from the project settings (engine.cfg). So any dynamic system of this
  132. type needs to store settings in the way the programmer best sees fit.