diff --git a/src/liborng/resources/input_resource.cpp b/src/liborng/resources/input_resource.cpp index c68328f..daf1183 100644 --- a/src/liborng/resources/input_resource.cpp +++ b/src/liborng/resources/input_resource.cpp @@ -67,11 +67,40 @@ void InputResource::update_axes_mouse_event(const Ref eve const InputAxis *axis = cast_to(this->input_axes[i]); const StringName signal = StringName("_on_") + axis->get_axis_name() + INPUTAXIS_TWO_DIMENSIONAL_SUFFIX; if (axis->get_include_mouse() && this->has_signal(signal)) - this->emit_signal(signal, delta, Vector2(event->get_relative().x, event->get_relative().y) * 0.001 / delta); + { + this->emit_signal(signal, delta, event->get_relative() * 0.000001f / delta); + } } } +Ref InputResource::find_input_action(const StringName &name) const +{ + for (uint8_t i = 0; i < this->input_actions.size(); i++) + { + const Ref action = cast_to(this->input_actions[i]); + if (action->get_action_name() == name) + { + return action; + } + } + return nullptr; +} + +Ref InputResource::find_input_axis(const StringName &name) const +{ + for (uint8_t i = 0; i < this->input_axes.size(); i++) + { + const Ref axis = cast_to(this->input_axes[i]); + if (axis->get_axis_name() == name) + { + return axis; + } + } + return nullptr; +} + + Vector3 InputAxis::get_spherical_vector() const { const Input *input = Input::get_singleton(); diff --git a/src/liborng/resources/input_resource.h b/src/liborng/resources/input_resource.h index bc3e962..86bb3ea 100644 --- a/src/liborng/resources/input_resource.h +++ b/src/liborng/resources/input_resource.h @@ -88,7 +88,6 @@ protected: 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"); } }; @@ -125,8 +124,8 @@ public: void set_up_down_events(const TypedArray events) { this->up_down_events = events; } TypedArray get_up_down_events() const { return this->up_down_events; } - - + + Vector3 get_spherical_vector() const; Vector2 get_lateral_vector() const; Vector2 get_lateral_vector_square() const; @@ -195,6 +194,9 @@ public: void set_input_axes(const TypedArray input_axes) { this->input_axes = input_axes; } TypedArray get_input_axes() const { return this->input_axes; } + + Ref find_input_action(const StringName &name) const; + Ref find_input_axis(const StringName &name) const; protected: TypedArray input_actions; @@ -207,6 +209,9 @@ protected: { // input_actions, input_axes ADD_GETTER_SETTER_HINTED(InputResource, input_actions, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "InputAction")); ADD_GETTER_SETTER_HINTED(InputResource, input_axes, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "InputAxis")); + + BIND_METHOD_1PARAM(InputResource, find_input_action, name); + BIND_METHOD_1PARAM(InputResource, find_input_axis, name); } { // update diff --git a/src/liborng/singletons/input_handler.cpp b/src/liborng/singletons/input_handler.cpp index 64bdf24..4dda036 100644 --- a/src/liborng/singletons/input_handler.cpp +++ b/src/liborng/singletons/input_handler.cpp @@ -22,9 +22,7 @@ GDSINGLETON_CPP(InputHandler); void InputHandler::_process(double delta) { for (InputResource *resource : this->active_resources) - { resource->update_axes(delta); - } } void InputHandler::_input(const Ref &event) @@ -32,16 +30,12 @@ void InputHandler::_input(const Ref &event) for (InputResource *resource : this->active_resources) { if (event->is_action_type()) - { resource->update_buttons(event, this->get_process_delta_time()); - } else if (const InputEventMouseMotion *mouse_event = cast_to(event.ptr())) { const Input *input = Input::get_singleton(); if (input->get_mouse_mode() != Input::MOUSE_MODE_VISIBLE) - { resource->update_axes_mouse_event(event, this->get_process_delta_time()); - } } } } @@ -57,9 +51,7 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) if (const InputAction *action = cast_to(input_actions[i])) { if (!input_map->has_action(action->get_action_name())) - { input_map->add_action(action->get_action_name(), action->get_deadzone()); - } for(uint8_t s = 0; s < 2; s++) { @@ -68,9 +60,7 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) { const TypedArray &events = action->get_input_events(); if (!resource->has_signal(signal_name)) - { resource->add_user_signal(signal_name); - } for (uint8_t e = 0; e < events.size(); e++) { const Ref &event = events[e]; @@ -91,16 +81,10 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) for(uint8_t s = 0; s <= axis_dimensions; 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); - } - if (target->has_method(signal_name)) - { - resource->connect(signal_name, Callable(target, signal_name)); - } + if (!resource->has_signal(signal_name)) resource->add_user_signal(signal_name); + if (target->has_method(signal_name)) resource->connect(signal_name, Callable(target, signal_name)); } - + const StringName &axis_name = axis->get_axis_name(); const float &deadzone = axis->get_deadzone(); if (axis_dimensions >= InputAxis::AXIS_DIMENSIONS::ONE) @@ -109,7 +93,7 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) const StringName &right_axis_action = axis_name + INPUTAXIS_RIGHT_SUFFIX; if (!input_map->has_action(left_axis_action)) input_map->add_action(left_axis_action, deadzone); if (!input_map->has_action(right_axis_action)) input_map->add_action(right_axis_action, deadzone); - + const TypedArray &left_right_events = axis->get_left_right_events(); for(uint8_t e = 0; e < left_right_events.size(); e++) { @@ -125,7 +109,7 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) const StringName &forward_axis_action = axis_name + INPUTAXIS_FORWARD_SUFFIX; if (!input_map->has_action(back_axis_action)) input_map->add_action(back_axis_action, deadzone); if (!input_map->has_action(forward_axis_action)) input_map->add_action(forward_axis_action, deadzone); - + const TypedArray &forward_back_events = axis->get_forward_back_events(); for(uint8_t e = 0; e < forward_back_events.size(); e++) { @@ -141,7 +125,7 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) const StringName &up_axis_action = axis_name + INPUTAXIS_UP_SUFFIX; if (!input_map->has_action(down_axis_action)) input_map->add_action(down_axis_action, deadzone); if (!input_map->has_action(up_axis_action)) input_map->add_action(up_axis_action, deadzone); - + const TypedArray &up_down_events = axis->get_up_down_events(); for(uint8_t e = 0; e < up_down_events.size(); e++) { @@ -150,7 +134,7 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) input_map->action_add_event(up_axis_action, up_down_event->get_positive()); } } - + if (axis->get_include_mouse()) { Engine *engine = Engine::get_singleton(); @@ -158,9 +142,9 @@ void InputHandler::add_input_resource(Node *target, InputResource *resource) } } } - + this->active_resources.insert(resource); - + const StringName input_resource_method = target->get_name() + StringName("_") + resource->get_path(); if (this->bound_remove_methods.find(input_resource_method) == this->bound_remove_methods.end()) { @@ -177,39 +161,56 @@ void InputHandler::_enable_mouse_capture() void InputHandler::remove_input_resource(Node *target, InputResource *resource) { + Input *input = Input::get_singleton(); InputMap *input_map = InputMap::get_singleton(); const TypedArray &input_actions = resource->get_input_actions(); const TypedArray &input_axes = resource->get_input_axes(); - + for(uint8_t i = 0; i < input_actions.size(); i++) { if (const InputAction *action = cast_to(input_actions[i])) { + input->action_release(action->get_action_name()); input_map->action_erase_events(action->get_action_name()); - for(uint8_t s = 0; s < 2; s++) - { - const StringName signal_name = StringName("_on_") + action->get_action_name() + input_action_suffixes[s]; - if (target->has_method(signal_name)) - { - resource->disconnect(signal_name, Callable(target, signal_name)); - } - } + + const StringName signal_name_pressed = StringName("_on_") + action->get_action_name() + input_action_suffixes[0]; + if (target->has_method(signal_name_pressed)) resource->disconnect(signal_name_pressed, Callable(target, signal_name_pressed)); + + const StringName signal_name_released = StringName("_on_") + action->get_action_name() + input_action_suffixes[1]; + const Callable callable_released = Callable(target, signal_name_released); + callable_released.call(); + if (target->has_method(signal_name_released)) resource->disconnect(signal_name_released, callable_released); } } - + for(uint8_t i = 0; i < input_axes.size(); i++) { if (const InputAxis *axis = cast_to(input_axes[i])) { - for(uint8_t s = 0; s < 3; s++) + const StringName signal_name_one = StringName("_on_") + axis->get_axis_name() + this->input_axis_suffixes[0]; + if (target->has_method(signal_name_one)) { - const StringName signal_name = StringName("_on_") + axis->get_axis_name() + this->input_axis_suffixes[s]; - if (target->has_method(signal_name)) - { - resource->disconnect(signal_name, Callable(target, signal_name)); - } + if (resource->has_signal(signal_name_one)) + resource->emit_signal(signal_name_one, this->get_process_delta_time(), 0.0f); + resource->disconnect(signal_name_one, Callable(target, signal_name_one)); } - + + const StringName signal_name_two = StringName("_on_") + axis->get_axis_name() + this->input_axis_suffixes[1]; + if (target->has_method(signal_name_two)) + { + if (resource->has_signal(signal_name_two)) + resource->emit_signal(signal_name_two, this->get_process_delta_time(), Vector2(0.0f, 0.0f)); + resource->disconnect(signal_name_two, Callable(target, signal_name_two)); + } + + const StringName signal_name_three = StringName("_on_") + axis->get_axis_name() + this->input_axis_suffixes[2]; + if (target->has_method(signal_name_three)) + { + if (resource->has_signal(signal_name_three)) + resource->emit_signal(signal_name_three, this->get_process_delta_time(), Vector3(0.0f, 0.0f, 0.0f)); + resource->disconnect(signal_name_three, Callable(target, signal_name_three)); + } + /* * This part would be a good, safe precaution to take, but it seems * to cause inputs to stick if an input resource is removed while @@ -217,36 +218,45 @@ void InputHandler::remove_input_resource(Node *target, InputResource *resource) * side effects, but I'm keeping it commented here in case there * turns out to be a reason to put it back later. */ - // const StringName &axis_name = axis->get_axis_name(); - // const uint8_t &axis_dimensions = axis->get_axis_dimensions(); - // const float &deadzone = axis->get_deadzone(); - // if (axis_dimensions >= InputAxis::AXIS_DIMENSIONS::ONE) - // { - // const StringName &left_axis_action = axis_name + INPUTAXIS_LEFT_SUFFIX; - // const StringName &right_axis_action = axis_name + INPUTAXIS_RIGHT_SUFFIX; - - // input_map->action_erase_events(left_axis_action); - // input_map->action_erase_events(right_axis_action); - // } + const StringName &axis_name = axis->get_axis_name(); + const uint8_t &axis_dimensions = axis->get_axis_dimensions(); + const float &deadzone = axis->get_deadzone(); + if (axis_dimensions >= InputAxis::AXIS_DIMENSIONS::ONE) + { + const StringName &left_axis_action = axis_name + INPUTAXIS_LEFT_SUFFIX; + const StringName &right_axis_action = axis_name + INPUTAXIS_RIGHT_SUFFIX; + + input->action_release(left_axis_action); + input->action_release(right_axis_action); + + input_map->action_erase_events(left_axis_action); + input_map->action_erase_events(right_axis_action); + } - // if (axis_dimensions >= InputAxis::AXIS_DIMENSIONS::TWO) - // { - // const StringName &back_axis_action = axis_name + INPUTAXIS_BACK_SUFFIX; - // const StringName &forward_axis_action = axis_name + INPUTAXIS_FORWARD_SUFFIX; - - // input_map->action_erase_events(back_axis_action); - // input_map->action_erase_events(forward_axis_action); - // } + if (axis_dimensions >= InputAxis::AXIS_DIMENSIONS::TWO) + { + const StringName &back_axis_action = axis_name + INPUTAXIS_BACK_SUFFIX; + const StringName &forward_axis_action = axis_name + INPUTAXIS_FORWARD_SUFFIX; + + input->action_release(back_axis_action); + input->action_release(forward_axis_action); + + input_map->action_erase_events(back_axis_action); + input_map->action_erase_events(forward_axis_action); + } + + if (axis_dimensions >= InputAxis::AXIS_DIMENSIONS::THREE) + { + const StringName &down_axis_action = axis_name + INPUTAXIS_DOWN_SUFFIX; + const StringName &up_axis_action = axis_name + INPUTAXIS_UP_SUFFIX; + + input->action_release(down_axis_action); + input->action_release(up_axis_action); + + input_map->action_erase_events(down_axis_action); + input_map->action_erase_events(up_axis_action); + } - // if (axis_dimensions >= InputAxis::AXIS_DIMENSIONS::THREE) - // { - // const StringName &down_axis_action = axis_name + INPUTAXIS_DOWN_SUFFIX; - // const StringName &up_axis_action = axis_name + INPUTAXIS_UP_SUFFIX; - - // input_map->action_erase_events(down_axis_action); - // input_map->action_erase_events(up_axis_action); - // } - if (axis->get_include_mouse()) { Engine *engine = Engine::get_singleton(); @@ -254,7 +264,7 @@ void InputHandler::remove_input_resource(Node *target, InputResource *resource) } } } - + this->active_resources.erase(resource); } void InputHandler::_disable_mouse_capture()