2024-02-13 13:59:01 +00:00
|
|
|
#ifndef PLAYER_INPUT_HPP
|
|
|
|
#define PLAYER_INPUT_HPP
|
|
|
|
|
2024-02-19 00:04:14 +00:00
|
|
|
#include <optional>
|
2024-05-28 14:28:36 +00:00
|
|
|
#include <godot_cpp/classes/input.hpp>
|
|
|
|
#include <godot_cpp/classes/input_event.hpp>
|
|
|
|
#include <godot_cpp/classes/node.hpp>
|
|
|
|
#include <godot_cpp/templates/vector.hpp>
|
|
|
|
#include <godot_cpp/variant/callable.hpp>
|
2024-02-13 13:59:01 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
namespace gd = godot;
|
|
|
|
|
|
|
|
namespace utils {
|
|
|
|
/*! An event-driven input observer.
|
|
|
|
*
|
|
|
|
* Listen for events with `listen_to`, registering godot input action names to callbacks. It's possible to register an "axis" by registering a listener with a positive and negative action.
|
|
|
|
*/
|
|
|
|
class PlayerInput : public gd::Node {
|
|
|
|
GDCLASS(PlayerInput, gd::Node)
|
2024-02-13 13:59:01 +00:00
|
|
|
static void _bind_methods();
|
|
|
|
public:
|
2024-05-28 14:28:36 +00:00
|
|
|
/*! A PlayerInput action listener.
|
|
|
|
* A listener is a combination of a positive and negative action and a callable.
|
|
|
|
* The expected callable signature is `void (godot::Ref<godot::InputEvent> event, float value)`
|
|
|
|
* actions can also be "special" actions prefixed with `_`.
|
|
|
|
* Special actions include `_mouse_up`, `_mouse_down`, `_mouse_left`, and `_mouse_right`.
|
|
|
|
*/
|
2024-02-13 13:59:01 +00:00
|
|
|
struct Listener {
|
2024-02-13 20:39:50 +00:00
|
|
|
friend class PlayerInput;
|
|
|
|
private:
|
2024-02-19 00:04:14 +00:00
|
|
|
// the two actions, evaluated as positive - negative
|
2024-05-28 14:28:36 +00:00
|
|
|
gd::String actionNegative{""};
|
|
|
|
gd::String actionPositive{""};
|
2024-02-19 00:04:14 +00:00
|
|
|
// the last cached action, if the newest result matches this, the event will be considered
|
|
|
|
// duplicate and ignored (not passed to listener)
|
2024-02-13 13:59:01 +00:00
|
|
|
float lastCached{0.f};
|
2024-05-28 14:28:36 +00:00
|
|
|
gd::Callable callable;
|
2024-02-19 00:04:14 +00:00
|
|
|
// if either actionNegative or actionPositive is a _mouse_ event this will be true
|
2024-02-13 20:39:50 +00:00
|
|
|
bool isMouseEvent{false};
|
2024-02-13 13:59:01 +00:00
|
|
|
|
2024-02-13 20:39:50 +00:00
|
|
|
public:
|
2024-05-28 14:28:36 +00:00
|
|
|
Listener() = default;
|
2024-05-28 14:57:58 +00:00
|
|
|
Listener(gd::String negative, gd::String positive, gd::Callable callable);
|
2024-05-28 14:28:36 +00:00
|
|
|
Listener(gd::String action, gd::Callable callable);
|
2024-02-19 00:04:14 +00:00
|
|
|
// evaluate the current state of an action.
|
2024-05-28 14:28:36 +00:00
|
|
|
static std::optional<float> evaluate_action(gd::String const &action);
|
2024-02-19 00:04:14 +00:00
|
|
|
// check if this event has any chance to result in a trigger, does not evaluate the event or
|
|
|
|
// poll current input state
|
2024-05-28 14:28:36 +00:00
|
|
|
bool has_changed(gd::Ref<gd::InputEvent> const &event);
|
2024-02-19 00:04:14 +00:00
|
|
|
// evaluate the event for changes to either actionPositive or actionNegative
|
2024-05-28 14:28:36 +00:00
|
|
|
float evaluate(gd::Ref<gd::InputEvent> const &event);
|
2024-02-19 00:04:14 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
bool operator==(PlayerInput::Listener const& b) const;
|
2024-02-13 13:59:01 +00:00
|
|
|
};
|
|
|
|
public:
|
2024-05-28 14:28:36 +00:00
|
|
|
static gd::Vector2 get_last_mouse_motion();
|
2024-02-19 00:04:14 +00:00
|
|
|
|
|
|
|
virtual void _enter_tree() override;
|
|
|
|
virtual void _exit_tree() override;
|
2024-05-28 14:28:36 +00:00
|
|
|
virtual void _unhandled_input(gd::Ref<gd::InputEvent> const &event) override;
|
2024-02-19 00:04:14 +00:00
|
|
|
virtual void _process(double deltaTime) override;
|
2024-02-13 13:59:01 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
void listen_to(Listener const &listener);
|
2024-05-28 14:58:22 +00:00
|
|
|
void listen_to(gd::String action, gd::Callable callable);
|
|
|
|
void listen_to(gd::String negative, gd::String positive, gd::Callable callable);
|
|
|
|
|
2024-02-13 13:59:01 +00:00
|
|
|
void stop_listening(Node *node);
|
2024-05-28 14:28:36 +00:00
|
|
|
void stop_listening(Listener const &listener);
|
2024-05-21 09:56:41 +00:00
|
|
|
void clear_listeners();
|
2024-05-28 21:55:44 +00:00
|
|
|
|
|
|
|
void set_device(int id);
|
|
|
|
private:
|
|
|
|
// the last mouse motion, updated by the primary instance
|
|
|
|
static gd::Vector2 lastMouseMotion;
|
|
|
|
// does a primary instance exist
|
|
|
|
static bool primaryExists;
|
|
|
|
// is this the primary instance
|
|
|
|
// the primary instance is responsible for updating static
|
|
|
|
// variables like lastMouseMotion
|
|
|
|
bool isPrimary{false};
|
|
|
|
int device{-1};
|
|
|
|
|
|
|
|
// current listeners for this instance
|
|
|
|
gd::Vector<Listener> listeners{};
|
2024-02-13 13:59:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // !PLAYER_INPUT_HPP
|