| 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 | |
| 31 | #ifndef INPUT_MAP_H |
| 32 | #define INPUT_MAP_H |
| 33 | |
| 34 | #include "core/input/input_event.h" |
| 35 | #include "core/object/class_db.h" |
| 36 | #include "core/object/object.h" |
| 37 | #include "core/templates/hash_map.h" |
| 38 | |
| 39 | template <typename T> |
| 40 | class TypedArray; |
| 41 | |
| 42 | class InputMap : public Object { |
| 43 | GDCLASS(InputMap, Object); |
| 44 | |
| 45 | public: |
| 46 | /** |
| 47 | * A special value used to signify that a given Action can be triggered by any device |
| 48 | */ |
| 49 | static int ALL_DEVICES; |
| 50 | |
| 51 | struct Action { |
| 52 | int id; |
| 53 | float deadzone; |
| 54 | List<Ref<InputEvent>> inputs; |
| 55 | }; |
| 56 | |
| 57 | private: |
| 58 | static InputMap *singleton; |
| 59 | |
| 60 | mutable HashMap<StringName, Action> input_map; |
| 61 | HashMap<String, List<Ref<InputEvent>>> default_builtin_cache; |
| 62 | HashMap<String, List<Ref<InputEvent>>> default_builtin_with_overrides_cache; |
| 63 | |
| 64 | 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) const; |
| 65 | |
| 66 | TypedArray<InputEvent> _action_get_events(const StringName &p_action); |
| 67 | TypedArray<StringName> _get_actions(); |
| 68 | |
| 69 | protected: |
| 70 | static void _bind_methods(); |
| 71 | |
| 72 | public: |
| 73 | static _FORCE_INLINE_ InputMap *get_singleton() { return singleton; } |
| 74 | |
| 75 | bool has_action(const StringName &p_action) const; |
| 76 | List<StringName> get_actions() const; |
| 77 | void add_action(const StringName &p_action, float p_deadzone = 0.5); |
| 78 | void erase_action(const StringName &p_action); |
| 79 | |
| 80 | float action_get_deadzone(const StringName &p_action); |
| 81 | void action_set_deadzone(const StringName &p_action, float p_deadzone); |
| 82 | void action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event); |
| 83 | bool action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event); |
| 84 | void action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event); |
| 85 | void action_erase_events(const StringName &p_action); |
| 86 | |
| 87 | const List<Ref<InputEvent>> *action_get_events(const StringName &p_action); |
| 88 | bool event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false) const; |
| 89 | 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) const; |
| 90 | |
| 91 | const HashMap<StringName, Action> &get_action_map() const; |
| 92 | void load_from_project_settings(); |
| 93 | void load_default(); |
| 94 | |
| 95 | String suggest_actions(const StringName &p_action) const; |
| 96 | |
| 97 | String get_builtin_display_name(const String &p_name) const; |
| 98 | // Use an Ordered Map so insertion order is preserved. We want the elements to be 'grouped' somewhat. |
| 99 | const HashMap<String, List<Ref<InputEvent>>> &get_builtins(); |
| 100 | const HashMap<String, List<Ref<InputEvent>>> &get_builtins_with_feature_overrides_applied(); |
| 101 | |
| 102 | InputMap(); |
| 103 | ~InputMap(); |
| 104 | }; |
| 105 | |
| 106 | #endif // INPUT_MAP_H |
| 107 | |