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