input_map.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**************************************************************************/
  2. /* input_map.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/input/input_event.h"
  32. #include "core/object/object.h"
  33. #include "core/templates/hash_map.h"
  34. template <typename T>
  35. class TypedArray;
  36. class InputMap : public Object {
  37. GDCLASS(InputMap, Object);
  38. public:
  39. /**
  40. * A special value used to signify that a given Action can be triggered by any device
  41. */
  42. static constexpr int ALL_DEVICES = -1;
  43. struct Action {
  44. int id;
  45. float deadzone;
  46. List<Ref<InputEvent>> inputs;
  47. };
  48. static constexpr float DEFAULT_DEADZONE = 0.2f;
  49. // Keep bigger deadzone for toggle actions (default `ui_*` actions, axis `pressed`) (GH-103360).
  50. static constexpr float DEFAULT_TOGGLE_DEADZONE = 0.5f;
  51. private:
  52. static inline InputMap *singleton = nullptr;
  53. mutable HashMap<StringName, Action> input_map;
  54. HashMap<String, List<Ref<InputEvent>>> default_builtin_cache;
  55. HashMap<String, List<Ref<InputEvent>>> default_builtin_with_overrides_cache;
  56. List<Ref<InputEvent>>::Element *_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr, int *r_event_index = nullptr) const;
  57. TypedArray<InputEvent> _action_get_events(const StringName &p_action);
  58. protected:
  59. static void _bind_methods();
  60. #ifndef DISABLE_DEPRECATED
  61. void _add_action_bind_compat_97281(const StringName &p_action, float p_deadzone = 0.5);
  62. static void _bind_compatibility_methods();
  63. #endif // DISABLE_DEPRECATED
  64. public:
  65. static _FORCE_INLINE_ InputMap *get_singleton() { return singleton; }
  66. bool has_action(const StringName &p_action) const;
  67. TypedArray<StringName> get_actions();
  68. void add_action(const StringName &p_action, float p_deadzone = DEFAULT_DEADZONE);
  69. void erase_action(const StringName &p_action);
  70. String get_action_description(const StringName &p_action) const;
  71. float action_get_deadzone(const StringName &p_action);
  72. void action_set_deadzone(const StringName &p_action, float p_deadzone);
  73. void action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event);
  74. bool action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event);
  75. void action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event);
  76. void action_erase_events(const StringName &p_action);
  77. const List<Ref<InputEvent>> *action_get_events(const StringName &p_action);
  78. bool event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false) const;
  79. int event_get_index(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false) const;
  80. bool event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr, int *r_event_index = nullptr) const;
  81. const HashMap<StringName, Action> &get_action_map() const;
  82. void load_from_project_settings();
  83. void load_default();
  84. String suggest_actions(const StringName &p_action) const;
  85. #ifdef TOOLS_ENABLED
  86. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  87. #endif
  88. String get_builtin_display_name(const String &p_name) const;
  89. // Use an Ordered Map so insertion order is preserved. We want the elements to be 'grouped' somewhat.
  90. const HashMap<String, List<Ref<InputEvent>>> &get_builtins();
  91. const HashMap<String, List<Ref<InputEvent>>> &get_builtins_with_feature_overrides_applied();
  92. InputMap();
  93. ~InputMap();
  94. };