Fixed axes mapped to buttons creating an input for each axis value change.
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include "resources/input_resource.h"
|
#include "resources/input_resource.h"
|
||||||
|
|
||||||
#include <godot_cpp/classes/input.hpp>
|
#include <godot_cpp/classes/input.hpp>
|
||||||
|
#include <godot_cpp/variant/utility_functions.hpp>
|
||||||
using namespace godot;
|
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++)
|
for (uint8_t i = 0; i < this->input_actions.size(); i++)
|
||||||
{
|
{
|
||||||
const InputAction *action = cast_to<InputAction>(this->input_actions[i]);
|
InputAction *action = cast_to<InputAction>(this->input_actions[i]);
|
||||||
|
|
||||||
const StringName suffix_pressed = StringName("_on_") + action->get_action_name() + INPUTACTION_SUFFIX_PRESSED;
|
const float action_strength = event->get_action_strength(action->get_action_name());
|
||||||
if (event->is_action_pressed(action->get_action_name()) && this->has_signal(suffix_pressed))
|
const float action_deadzone = action->get_deadzone();
|
||||||
this->emit_signal(suffix_pressed);
|
|
||||||
|
if (!action->is_active())
|
||||||
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))
|
if (event->is_action_pressed(action->get_action_name()) && action_strength >= action_deadzone)
|
||||||
this->emit_signal(suffix_released);
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,11 +67,17 @@ public:
|
|||||||
void set_input_events(const TypedArray<InputEvent> events) { this->input_events = events; }
|
void set_input_events(const TypedArray<InputEvent> events) { this->input_events = events; }
|
||||||
TypedArray<InputEvent> get_input_events() const { return this->input_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:
|
protected:
|
||||||
StringName action_name = "action_name";
|
StringName action_name = "action_name";
|
||||||
float deadzone = 0.5f;
|
float deadzone = 0.5f;
|
||||||
TypedArray<InputEvent> input_events;
|
TypedArray<InputEvent> input_events;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool _active = false;
|
||||||
|
|
||||||
// Godot boilerplate below
|
// Godot boilerplate below
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods()
|
static void _bind_methods()
|
||||||
@@ -79,6 +85,10 @@ protected:
|
|||||||
ADD_GETTER_SETTER(InputAction, action_name, Variant::STRING_NAME);
|
ADD_GETTER_SETTER(InputAction, action_name, Variant::STRING_NAME);
|
||||||
ADD_GETTER_SETTER(InputAction, deadzone, Variant::FLOAT);
|
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"));
|
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");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user