feat: PlayerInput now uses callables
This enables the use of callable_mp and binding inputs without stringname lookups
This commit is contained in:
		
							parent
							
								
									f0bddcf074
								
							
						
					
					
						commit
						78e6bb2cf1
					
				| 
						 | 
				
			
			@ -1,9 +1,9 @@
 | 
			
		|||
#include "player_input.hpp"
 | 
			
		||||
#include "godot_cpp/variant/utility_functions.hpp"
 | 
			
		||||
#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 "godot_cpp/variant/callable.hpp"
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <optional>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -13,15 +13,14 @@ void PlayerInput::_bind_methods() {}
 | 
			
		|||
Vector2 PlayerInput::lastMouseMotion{0.f, 0.f};
 | 
			
		||||
bool PlayerInput::primaryExists{false};
 | 
			
		||||
 | 
			
		||||
PlayerInput::Listener::Listener(String positive, String negative, Node *object, String method)
 | 
			
		||||
PlayerInput::Listener::Listener(String positive, String negative, Callable callable)
 | 
			
		||||
: actionNegative{negative}
 | 
			
		||||
, actionPositive{positive}
 | 
			
		||||
, methodName{method}
 | 
			
		||||
, object{object}
 | 
			
		||||
, callable{callable}
 | 
			
		||||
, isMouseEvent{positive.begins_with("_mouse_") || negative.begins_with("_mouse_")} {}
 | 
			
		||||
 | 
			
		||||
PlayerInput::Listener::Listener(String action, Node *object, String method)
 | 
			
		||||
: PlayerInput::Listener::Listener(action, String(), object, method) {}
 | 
			
		||||
PlayerInput::Listener::Listener(String action, Callable callable)
 | 
			
		||||
: PlayerInput::Listener::Listener(action, String(), callable) {}
 | 
			
		||||
 | 
			
		||||
std::optional<float> PlayerInput::Listener::evaluate_action(String const &action) {
 | 
			
		||||
    Input *input = Input::get_singleton();
 | 
			
		||||
| 
						 | 
				
			
			@ -57,13 +56,12 @@ float PlayerInput::Listener::evaluate(Ref<InputEvent> const &event) {
 | 
			
		|||
        return 0.f;
 | 
			
		||||
    float newest = positive.value() - negative.value();
 | 
			
		||||
    if(this->lastCached != newest || this->isMouseEvent)
 | 
			
		||||
        this->object->call(this->methodName, event, newest);
 | 
			
		||||
        this->callable.call(event, newest);
 | 
			
		||||
    return (this->lastCached = newest);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool PlayerInput::Listener::operator==(godot::PlayerInput::Listener const& b) {
 | 
			
		||||
    return this->methodName == b.methodName
 | 
			
		||||
        && this->object == b.object
 | 
			
		||||
    return this->callable == b.callable
 | 
			
		||||
        && this->actionNegative == b.actionNegative
 | 
			
		||||
        && this->actionPositive == b.actionPositive;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -111,7 +109,7 @@ void PlayerInput::listen_to(Listener const& listener) {
 | 
			
		|||
void PlayerInput::stop_listening(Node *node) {
 | 
			
		||||
    for(size_t i = 0; i < this->listeners.size(); ++i) {
 | 
			
		||||
        Listener& l = this->listeners.at(i);
 | 
			
		||||
        if(l.object == node) {
 | 
			
		||||
        if(l.callable.get_object() == node) {
 | 
			
		||||
            this->listeners.erase(this->listeners.begin() + i);
 | 
			
		||||
            i--;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,6 +6,7 @@
 | 
			
		|||
#include "godot_cpp/classes/input.hpp"
 | 
			
		||||
#include "godot_cpp/classes/input_event.hpp"
 | 
			
		||||
#include "godot_cpp/classes/node.hpp"
 | 
			
		||||
#include "godot_cpp/variant/callable.hpp"
 | 
			
		||||
 | 
			
		||||
namespace godot {
 | 
			
		||||
class PlayerInput : public Node {
 | 
			
		||||
| 
						 | 
				
			
			@ -29,16 +30,13 @@ public:
 | 
			
		|||
        // the last cached action, if the newest result matches this, the event will be considered
 | 
			
		||||
        // duplicate and ignored (not passed to listener)
 | 
			
		||||
        float lastCached{0.f};
 | 
			
		||||
        // name of the method to call, expected signature is void(Ref<InputEvent>, float)
 | 
			
		||||
        String methodName{""};
 | 
			
		||||
        // pointer to the node to call methodName on
 | 
			
		||||
        Node *object{nullptr};
 | 
			
		||||
        Callable callable;
 | 
			
		||||
        // if either actionNegative or actionPositive is a _mouse_ event this will be true
 | 
			
		||||
        bool isMouseEvent{false};
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
        Listener(String positive, String negative, Node *object, String method);
 | 
			
		||||
        Listener(String action, Node *object, String method);
 | 
			
		||||
        Listener(String positive, String negative, Callable callable);
 | 
			
		||||
        Listener(String action, Callable callable);
 | 
			
		||||
        // evaluate the current state of an action.
 | 
			
		||||
        static std::optional<float> evaluate_action(String const &action);
 | 
			
		||||
        // check if this event has any chance to result in a trigger, does not evaluate the event or
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue