feat: PlayerInput now supports mouse motion through _mouse_* actions
This commit is contained in:
		
							parent
							
								
									2246409a79
								
							
						
					
					
						commit
						d5ee438080
					
				| 
						 | 
				
			
			@ -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 <algorithm>
 | 
			
		||||
 | 
			
		||||
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<InputEvent> 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<InputEventMouseMotion>(*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<InputEvent> 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<InputEvent> const &event) {
 | 
			
		||||
    Input *input = Input::get_singleton();
 | 
			
		||||
    float newest = static_cast<float>(input->is_action_pressed(this->actionPositive))
 | 
			
		||||
                 - static_cast<float>(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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<InputEvent> const &event, String const &action);
 | 
			
		||||
        bool has_changed(Ref<InputEvent> const &event);
 | 
			
		||||
        float evaluate(Ref<InputEvent> const &event);
 | 
			
		||||
        bool operator==(godot::PlayerInput::Listener const& b);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue