godot-cpp-utils
A collection of utility classes, functions and macros for use with Godot and GDExtension.
Loading...
Searching...
No Matches
player_input.hpp
1#ifndef UTILS_PLAYER_INPUT_HPP
2#define UTILS_PLAYER_INPUT_HPP
3
4#include <optional>
5#include <godot_cpp/classes/input.hpp>
6#include <godot_cpp/classes/input_event.hpp>
7#include <godot_cpp/classes/node.hpp>
8#include <godot_cpp/templates/vector.hpp>
9#include <godot_cpp/variant/callable.hpp>
10
11namespace gd = godot;
12
13namespace utils {
18class PlayerInput : public gd::Node {
19 GDCLASS(PlayerInput, gd::Node)
20 static void _bind_methods();
21public:
29 struct Listener {
30 friend class PlayerInput;
31 private:
33 gd::String actionNegative{""};
35 gd::String actionPositive{""};
40 float lastCached{0.f};
42 gd::Callable callable;
44 bool isMouseEvent{false};
45
46 public:
47 Listener() = default;
48 Listener(gd::String negative, gd::String positive, gd::Callable callable);
49 Listener(gd::String action, gd::Callable callable);
50 // evaluate the current state of an action.
51 static std::optional<float> evaluate_action(gd::String const &action);
56 bool has_changed(gd::Ref<gd::InputEvent> const &event);
58 float evaluate(gd::Ref<gd::InputEvent> const &event);
59 bool operator==(PlayerInput::Listener const& b) const;
60 };
61public:
63 static gd::Vector2 get_last_mouse_motion();
64
65 virtual void _enter_tree() override;
66 virtual void _exit_tree() override;
67 virtual void _unhandled_input(gd::Ref<gd::InputEvent> const &event) override;
68 virtual void _process(double deltaTime) override;
69
71 void listen_to(Listener const &listener);
76 void listen_to(gd::String action, gd::Callable callable);
81 void listen_to(gd::String negative, gd::String positive, gd::Callable callable);
82
84 void stop_listening(Node *node);
86 void stop_listening(Listener const &listener);
88 void clear_listeners();
90 void set_device(int id);
91private:
93 static gd::Vector2 lastMouseMotion;
95 static bool primaryExists;
100 bool isPrimary{false};
102 int device{-1};
103
105 gd::Vector<Listener> listeners{};
106};
107}
108
109
110#endif // !UTILS_PLAYER_INPUT_HPP
An event-driven input observer.
Definition player_input.hpp:18
bool isPrimary
Is this the primary instance.
Definition player_input.hpp:100
void clear_listeners()
Remove all listeners.
Definition player_input.cpp:132
void listen_to(Listener const &listener)
Start listening for action.
Definition player_input.cpp:106
void set_device(int id)
set the device observe events from.
Definition player_input.cpp:136
static gd::Vector2 get_last_mouse_motion()
Returns the last stored mouse delta.
Definition player_input.cpp:73
gd::Vector< Listener > listeners
current listeners for this instance
Definition player_input.hpp:105
static gd::Vector2 lastMouseMotion
The last mouse motion, updated by the primary instance.
Definition player_input.hpp:93
static bool primaryExists
Does a primary instance exist.
Definition player_input.hpp:95
void stop_listening(Node *node)
Remove any listeners related to node.
Definition player_input.cpp:118
int device
which device to observe events from.
Definition player_input.hpp:102
A PlayerInput action listener.
Definition player_input.hpp:29
float evaluate(gd::Ref< gd::InputEvent > const &event)
evaluate the event for changes to either actionPositive or actionNegative
Definition player_input.cpp:56
gd::String actionPositive
Positive action on axis, evaluates to +1.
Definition player_input.hpp:35
bool isMouseEvent
If either actionNegative or actionPositive is a mouse event this will be true.
Definition player_input.hpp:44
float lastCached
The last cached action.
Definition player_input.hpp:40
gd::Callable callable
The listening function.
Definition player_input.hpp:42
bool has_changed(gd::Ref< gd::InputEvent > const &event)
Check if this event has any chance to result in a trigger.
Definition player_input.cpp:47
gd::String actionNegative
Negative action on axis, evaluates to -1.
Definition player_input.hpp:33