Signals are no longer double-connected when a resource is added twice.

This commit is contained in:
2026-08-20 01:51:56 -04:00
parent acdd9f104e
commit 323e55c3fd
+17 -7
View File
@@ -46,27 +46,30 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource)
const TypedArray<InputAction> &input_actions = resource->get_input_actions(); const TypedArray<InputAction> &input_actions = resource->get_input_actions();
const TypedArray<InputAxis> &input_axes = resource->get_input_axes(); const TypedArray<InputAxis> &input_axes = resource->get_input_axes();
for(uint8_t i = 0; i < input_actions.size(); i++) for (uint8_t i = 0; i < input_actions.size(); i++)
{ {
if (const InputAction *action = cast_to<InputAction>(input_actions[i])) if (const InputAction *action = cast_to<InputAction>(input_actions[i]))
{ {
if (!input_map->has_action(action->get_action_name())) if (!input_map->has_action(action->get_action_name()))
input_map->add_action(action->get_action_name(), action->get_deadzone()); input_map->add_action(action->get_action_name(), action->get_deadzone());
for(uint8_t s = 0; s < 2; s++) for (uint8_t s = 0; s < 2; s++)
{ {
const StringName signal_name = StringName("_on_") + action->get_action_name() + this->input_action_suffixes[s]; const StringName signal_name = StringName("_on_") + action->get_action_name() + this->input_action_suffixes[s];
if (target->has_method(signal_name)) if (target->has_method(signal_name))
{ {
const TypedArray<InputEvent> &events = action->get_input_events();
if (!resource->has_signal(signal_name)) if (!resource->has_signal(signal_name))
{
resource->add_user_signal(signal_name); resource->add_user_signal(signal_name);
resource->connect(signal_name, Callable(target, signal_name));
}
const TypedArray<InputEvent> &events = action->get_input_events();
for (uint8_t e = 0; e < events.size(); e++) for (uint8_t e = 0; e < events.size(); e++)
{ {
const Ref<InputEvent> &event = events[e]; const Ref<InputEvent> &event = events[e];
input_map->action_add_event(action->get_action_name(), event); input_map->action_add_event(action->get_action_name(), event);
} }
resource->connect(signal_name, Callable(target, signal_name));
} }
} }
} }
@@ -81,8 +84,12 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource)
for(uint8_t s = 0; s <= axis_dimensions; s++) for(uint8_t s = 0; s <= axis_dimensions; s++)
{ {
const StringName signal_name = StringName("_on_") + axis->get_axis_name() + this->input_axis_suffixes[s]; const StringName signal_name = StringName("_on_") + axis->get_axis_name() + this->input_axis_suffixes[s];
if (!resource->has_signal(signal_name)) resource->add_user_signal(signal_name); const Callable &callable = Callable(target, signal_name);
if (target->has_method(signal_name)) resource->connect(signal_name, Callable(target, signal_name)); if (target->has_method(signal_name) && !resource->has_signal(signal_name))
{
resource->add_user_signal(signal_name);
resource->connect(signal_name, callable);
}
} }
const StringName &axis_name = axis->get_axis_name(); const StringName &axis_name = axis->get_axis_name();
@@ -151,7 +158,10 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource)
MethodBind *bind = ClassDB::bind_method(input_resource_method, &InputHandler::remove_input_resource); MethodBind *bind = ClassDB::bind_method(input_resource_method, &InputHandler::remove_input_resource);
this->bound_remove_methods.emplace(input_resource_method, bind); this->bound_remove_methods.emplace(input_resource_method, bind);
} }
target->connect("tree_exiting", Callable(this, input_resource_method).bind(target, resource), CONNECT_ONE_SHOT);
const Callable &callable = Callable(this, input_resource_method);
if (!target->is_connected("tree_exiting", callable))
target->connect("tree_exiting", callable.bind(target, resource), CONNECT_ONE_SHOT);
} }
void InputHandler::_enable_mouse_capture() void InputHandler::_enable_mouse_capture()
{ {