From 17d3f26e5de3c271b5ac1d85956d2034438a12a5 Mon Sep 17 00:00:00 2001 From: kobewi Date: Sun, 10 Dec 2023 19:23:29 +0100 Subject: [PATCH] Add event_index to InputEventAction --- core/input/input.cpp | 2 +- core/input/input_event.cpp | 12 ++++++++++++ core/input/input_event.h | 4 ++++ core/input/input_map.cpp | 7 +++++++ doc/classes/InputEventAction.xml | 3 +++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/core/input/input.cpp b/core/input/input.cpp index a8409cc06d3e..ec70e545ef94 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -1244,7 +1244,7 @@ void Input::_update_action_cache(const StringName &p_action_name, ActionState &r r_action_state.cache.strength = 0.0; r_action_state.cache.raw_strength = 0.0; - int max_event = InputMap::get_singleton()->action_get_events(p_action_name)->size(); + int max_event = InputMap::get_singleton()->action_get_events(p_action_name)->size() + 1; // +1 comes from InputEventAction. for (const KeyValue &kv : r_action_state.device_states) { const ActionState::DeviceState &device_state = kv.value; for (int i = 0; i < max_event; i++) { diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index bf1de8d3b275..de3efa7a3a4f 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -1585,6 +1585,14 @@ float InputEventAction::get_strength() const { return strength; } +void InputEventAction::set_event_index(int p_index) { + event_index = p_index; +} + +int InputEventAction::get_event_index() const { + return event_index; +} + bool InputEventAction::is_match(const Ref &p_event, bool p_exact_match) const { if (p_event.is_null()) { return false; @@ -1649,9 +1657,13 @@ void InputEventAction::_bind_methods() { ClassDB::bind_method(D_METHOD("set_strength", "strength"), &InputEventAction::set_strength); ClassDB::bind_method(D_METHOD("get_strength"), &InputEventAction::get_strength); + ClassDB::bind_method(D_METHOD("set_event_index", "index"), &InputEventAction::set_event_index); + ClassDB::bind_method(D_METHOD("get_event_index"), &InputEventAction::get_event_index); + ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action"), "set_action", "get_action"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_strength", "get_strength"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "event_index", PROPERTY_HINT_RANGE, "-1,31,1"), "set_event_index", "get_event_index"); // The max value equals to Input::MAX_EVENT - 1. } /////////////////////////////////// diff --git a/core/input/input_event.h b/core/input/input_event.h index 21b61f3bc282..19176f748e92 100644 --- a/core/input/input_event.h +++ b/core/input/input_event.h @@ -453,6 +453,7 @@ class InputEventAction : public InputEvent { StringName action; float strength = 1.0f; + int event_index = -1; protected: static void _bind_methods(); @@ -466,6 +467,9 @@ class InputEventAction : public InputEvent { void set_strength(float p_strength); float get_strength() const; + void set_event_index(int p_index); + int get_event_index() const; + virtual bool is_action(const StringName &p_action) const; virtual bool action_match(const Ref &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override; diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp index 7fd1806b310d..178d02b987cf 100644 --- a/core/input/input_map.cpp +++ b/core/input/input_map.cpp @@ -274,6 +274,13 @@ bool InputMap::event_get_action_status(const Ref &p_event, const Str if (r_raw_strength != nullptr) { *r_raw_strength = strength; } + if (r_event_index) { + if (input_event_action->get_event_index() >= 0) { + *r_event_index = input_event_action->get_event_index(); + } else { + *r_event_index = E->value.inputs.size(); + } + } return input_event_action->get_action() == p_action; } diff --git a/doc/classes/InputEventAction.xml b/doc/classes/InputEventAction.xml index 4715f9fe95c0..e082adeca0c9 100644 --- a/doc/classes/InputEventAction.xml +++ b/doc/classes/InputEventAction.xml @@ -16,6 +16,9 @@ The action's name. Actions are accessed via this [String]. + + The real event index in action this event corresponds to (from events defined for this action in the [InputMap]). If [code]-1[/code], a unique ID will be used and actions pressed with this ID will need to be released with another [InputEventAction]. + If [code]true[/code], the action's state is pressed. If [code]false[/code], the action's state is released.