diff --git a/player_input.cpp b/player_input.cpp index 8192cbe..db0d0cf 100644 --- a/player_input.cpp +++ b/player_input.cpp @@ -2,6 +2,7 @@ #include "godot_macros.h" #include "godot_cpp/classes/input.hpp" #include "godot_cpp/classes/input_event.hpp" +#include "godot_cpp/classes/input_event_mouse_motion.hpp" #include namespace godot { @@ -11,15 +12,40 @@ PlayerInput::Listener::Listener(String positive, String negative, Node *object, : actionNegative{negative} , actionPositive{positive} , methodName{method} -, object{object} {} +, object{object} +, isMouseEvent{positive.begins_with("_mouse_") || negative.begins_with("_mouse_")} {} + +float PlayerInput::Listener::evaluate_event(Ref const &event, String const &action) { + Input *input = Input::get_singleton(); + if(!action.begins_with("_mouse_")) { + return float(input->is_action_pressed(action)); + } else { + InputEventMouseMotion *motion = Object::cast_to(*event); + if(motion == nullptr) + return 0.f; + if(action.ends_with("_up") || action.ends_with("_right")) { + return motion->get_relative().x; + } if(action.ends_with("_right") || action.ends_with("_left")) { + return motion->get_relative().y; + } + } + return 0.f; +} bool PlayerInput::Listener::has_changed(Ref const &event) { - return event->is_action(this->actionNegative) || event->is_action(this->actionPositive); + return ( + (!event->is_class("InputEventMouseMotion") || + this->isMouseEvent + ) || + event->is_action(this->actionNegative) || + event->is_action(this->actionPositive) + ); } + float PlayerInput::Listener::evaluate(Ref const &event) { - Input *input = Input::get_singleton(); - float newest = static_cast(input->is_action_pressed(this->actionPositive)) - - static_cast(input->is_action_pressed(this->actionNegative)); + float positive = PlayerInput::Listener::evaluate_event(event, this->actionPositive); + float negative = PlayerInput::Listener::evaluate_event(event, this->actionNegative); + float newest = positive - negative; if(lastCached != newest) this->object->call(this->methodName, event, newest); return (lastCached = newest); diff --git a/player_input.hpp b/player_input.hpp index 130b281..3bc1519 100644 --- a/player_input.hpp +++ b/player_input.hpp @@ -12,13 +12,18 @@ class PlayerInput : public Node { static void _bind_methods(); public: struct Listener { + friend class PlayerInput; + private: String actionNegative{""}; String actionPositive{""}; float lastCached{0.f}; String methodName{""}; - Node *object; + Node *object{nullptr}; + bool isMouseEvent{false}; + public: Listener(String positive, String negative, Node *object, String method); + static float evaluate_event(Ref const &event, String const &action); bool has_changed(Ref const &event); float evaluate(Ref const &event); bool operator==(godot::PlayerInput::Listener const& b);