Fixed axes mapped to buttons creating an input for each axis value change.

This commit is contained in:
2026-08-17 21:12:57 -04:00
parent 05a680cc5b
commit fb0293ac14
2 changed files with 34 additions and 9 deletions
+24 -9
View File
@@ -8,6 +8,7 @@
#include "resources/input_resource.h"
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
using namespace godot;
@@ -15,15 +16,29 @@ void InputResource::update_buttons(const Ref<InputEvent> event, const float delt
{
for (uint8_t i = 0; i < this->input_actions.size(); i++)
{
const InputAction *action = cast_to<InputAction>(this->input_actions[i]);
const StringName suffix_pressed = StringName("_on_") + action->get_action_name() + INPUTACTION_SUFFIX_PRESSED;
if (event->is_action_pressed(action->get_action_name()) && this->has_signal(suffix_pressed))
this->emit_signal(suffix_pressed);
const StringName suffix_released = StringName("_on_") + action->get_action_name() + INPUTACTION_SUFFIX_RELEASED;
if (event->is_action_released(action->get_action_name()) && this->has_signal(suffix_released))
this->emit_signal(suffix_released);
InputAction *action = cast_to<InputAction>(this->input_actions[i]);
const float action_strength = event->get_action_strength(action->get_action_name());
const float action_deadzone = action->get_deadzone();
if (!action->is_active())
{
if (event->is_action_pressed(action->get_action_name()) && action_strength >= action_deadzone)
{
action->set_active(true);
const StringName pressed_callback = StringName("_on_") + action->get_action_name() + INPUTACTION_SUFFIX_PRESSED;
if (this->has_signal(pressed_callback)) this->emit_signal(pressed_callback);
}
}
else
{
if (event->is_action_released(action->get_action_name()) || (event->is_action_pressed(action->get_action_name()) && action_strength < action_deadzone))
{
action->set_active(false);
const StringName released_callback = StringName("_on_") + action->get_action_name() + INPUTACTION_SUFFIX_RELEASED;
if (this->has_signal(released_callback)) this->emit_signal(released_callback);
}
}
}
}
+10
View File
@@ -67,11 +67,17 @@ public:
void set_input_events(const TypedArray<InputEvent> events) { this->input_events = events; }
TypedArray<InputEvent> get_input_events() const { return this->input_events; }
void set_active(const bool active) { this->_active = active; }
bool is_active() const { return this->_active; }
protected:
StringName action_name = "action_name";
float deadzone = 0.5f;
TypedArray<InputEvent> input_events;
private:
bool _active = false;
// Godot boilerplate below
protected:
static void _bind_methods()
@@ -79,6 +85,10 @@ protected:
ADD_GETTER_SETTER(InputAction, action_name, Variant::STRING_NAME);
ADD_GETTER_SETTER(InputAction, deadzone, Variant::FLOAT);
ADD_GETTER_SETTER_HINTED(InputAction, input_events, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"));
ClassDB::bind_method(D_METHOD("is_active"), &InputAction::is_active);
ClassDB::bind_method(D_METHOD("set_active", "active"), &InputAction::set_active);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
}
};