2024-02-13 13:59:01 +00:00
|
|
|
#include "player_input.hpp"
|
2024-05-29 22:49:37 +00:00
|
|
|
#include "godot_macros.hpp"
|
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/input_event_mouse_motion.hpp>
|
|
|
|
#include <godot_cpp/variant/callable.hpp>
|
2024-08-09 16:25:14 +00:00
|
|
|
#include <godot_cpp/variant/utility_functions.hpp>
|
2024-02-19 00:04:14 +00:00
|
|
|
#include <optional>
|
2024-02-13 13:59:01 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
namespace utils {
|
|
|
|
void PlayerInput::_bind_methods() {
|
|
|
|
#define CLASSNAME PlayerInput
|
|
|
|
}
|
2024-02-13 13:59:01 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
gd::Vector2 PlayerInput::lastMouseMotion{0.f, 0.f};
|
2024-02-19 00:04:14 +00:00
|
|
|
bool PlayerInput::primaryExists{false};
|
|
|
|
|
2024-05-28 14:57:58 +00:00
|
|
|
PlayerInput::Listener::Listener(gd::String negative, gd::String positive, gd::Callable callable)
|
2024-02-13 13:59:01 +00:00
|
|
|
: actionNegative{negative}
|
|
|
|
, actionPositive{positive}
|
2024-04-10 10:44:50 +00:00
|
|
|
, callable{callable}
|
2024-02-13 20:39:50 +00:00
|
|
|
, isMouseEvent{positive.begins_with("_mouse_") || negative.begins_with("_mouse_")} {}
|
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
PlayerInput::Listener::Listener(gd::String action, gd::Callable callable)
|
2024-05-28 14:57:58 +00:00
|
|
|
: PlayerInput::Listener::Listener(gd::String(), action, callable) {}
|
2024-02-23 20:50:29 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
std::optional<float> PlayerInput::Listener::evaluate_action(gd::String const &action) {
|
|
|
|
gd::Input *input = gd::Input::get_singleton();
|
2024-02-19 00:04:14 +00:00
|
|
|
if(action.begins_with("_mouse_")) {
|
2024-05-28 14:28:36 +00:00
|
|
|
gd::Vector2 vector = PlayerInput::get_last_mouse_motion();
|
2024-05-28 21:55:44 +00:00
|
|
|
if(action == "_mouse_up")
|
2024-02-19 00:04:14 +00:00
|
|
|
return vector.y > 0.f ? vector.y : 0.f;
|
2024-05-28 21:55:44 +00:00
|
|
|
else if(action == "_mouse_down")
|
2024-02-19 00:04:14 +00:00
|
|
|
return vector.y < 0.f ? -vector.y : 0.f;
|
2024-05-28 21:55:44 +00:00
|
|
|
else if(action == "_mouse_right")
|
2024-02-19 00:04:14 +00:00
|
|
|
return vector.x > 0.f ? vector.x : 0.f;
|
2024-05-28 21:55:44 +00:00
|
|
|
else if(action == "_mouse_left")
|
2024-02-19 00:04:14 +00:00
|
|
|
return vector.x < 0.f ? -vector.x : 0.f;
|
2024-03-20 08:44:25 +00:00
|
|
|
}
|
|
|
|
if(action.is_empty()) {
|
2024-02-19 00:04:14 +00:00
|
|
|
return 0.f;
|
2024-02-13 20:39:50 +00:00
|
|
|
} else {
|
2024-02-19 00:04:14 +00:00
|
|
|
return float(input->is_action_pressed(action));
|
2024-02-13 20:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-13 13:59:01 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
bool PlayerInput::Listener::has_changed(gd::Ref<gd::InputEvent> const &event) {
|
2024-05-28 21:55:44 +00:00
|
|
|
// do not evaluate mouse events as anything but
|
|
|
|
if(this->isMouseEvent)
|
|
|
|
return event->is_class("InputEventMouseMotion");
|
2024-03-20 08:44:25 +00:00
|
|
|
bool const negative_changed{!this->actionNegative.is_empty() && event->is_action(this->actionNegative)};
|
|
|
|
bool const positive_changed{!this->actionPositive.is_empty() && event->is_action(this->actionPositive)};
|
2024-05-28 21:55:44 +00:00
|
|
|
return negative_changed || positive_changed;
|
2024-02-13 13:59:01 +00:00
|
|
|
}
|
2024-02-13 20:39:50 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
float PlayerInput::Listener::evaluate(gd::Ref<gd::InputEvent> const &event) {
|
2024-02-19 00:04:14 +00:00
|
|
|
std::optional<float> positive = PlayerInput::Listener::evaluate_action(this->actionPositive);
|
|
|
|
std::optional<float> negative = PlayerInput::Listener::evaluate_action(this->actionNegative);
|
|
|
|
if(!positive.has_value() || !negative.has_value())
|
|
|
|
return 0.f;
|
|
|
|
float newest = positive.value() - negative.value();
|
|
|
|
if(this->lastCached != newest || this->isMouseEvent)
|
2024-04-10 10:44:50 +00:00
|
|
|
this->callable.call(event, newest);
|
2024-02-19 00:04:14 +00:00
|
|
|
return (this->lastCached = newest);
|
2024-02-13 13:59:01 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
bool PlayerInput::Listener::operator==(PlayerInput::Listener const& b) const {
|
2024-04-10 10:44:50 +00:00
|
|
|
return this->callable == b.callable
|
2024-02-13 13:59:01 +00:00
|
|
|
&& this->actionNegative == b.actionNegative
|
|
|
|
&& this->actionPositive == b.actionPositive;
|
|
|
|
}
|
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
gd::Vector2 PlayerInput::get_last_mouse_motion() {
|
2024-02-19 00:04:14 +00:00
|
|
|
return PlayerInput::lastMouseMotion;
|
|
|
|
}
|
|
|
|
|
2024-08-15 22:07:58 +00:00
|
|
|
void PlayerInput::_enter_tree() {
|
2024-02-19 00:04:14 +00:00
|
|
|
if(!PlayerInput::primaryExists) {
|
|
|
|
this->isPrimary = true;
|
|
|
|
PlayerInput::primaryExists = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 22:07:58 +00:00
|
|
|
void PlayerInput::_exit_tree() {
|
2024-02-19 00:04:14 +00:00
|
|
|
if(this->isPrimary) {
|
|
|
|
this->isPrimary = false;
|
|
|
|
PlayerInput::primaryExists = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 22:07:58 +00:00
|
|
|
void PlayerInput::_unhandled_input(gd::Ref<gd::InputEvent> const &event) {
|
2024-02-19 00:04:14 +00:00
|
|
|
if(this->isPrimary && event->is_class("InputEventMouseMotion"))
|
2024-05-28 14:28:36 +00:00
|
|
|
PlayerInput::lastMouseMotion = gd::Object::cast_to<gd::InputEventMouseMotion>(*event)->get_relative();
|
2024-02-13 13:59:01 +00:00
|
|
|
for(Listener& listener: this->listeners) {
|
|
|
|
if(listener.has_changed(event)) {
|
|
|
|
listener.evaluate(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 00:04:14 +00:00
|
|
|
void PlayerInput::_process(double deltaTime) {
|
2024-03-20 08:44:25 +00:00
|
|
|
if(this->isPrimary)
|
|
|
|
PlayerInput::lastMouseMotion = {0.f, 0.f};
|
2024-02-19 00:04:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 13:59:01 +00:00
|
|
|
void PlayerInput::listen_to(Listener const& listener) {
|
|
|
|
this->listeners.push_back(listener);
|
|
|
|
}
|
|
|
|
|
2024-05-28 14:58:22 +00:00
|
|
|
void PlayerInput::listen_to(gd::String action, gd::Callable callable) {
|
|
|
|
this->listeners.push_back(Listener(action, callable));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayerInput::listen_to(gd::String negative, gd::String positive, gd::Callable callable) {
|
|
|
|
this->listeners.push_back(Listener(negative, positive, callable));
|
|
|
|
}
|
|
|
|
|
2024-02-13 13:59:01 +00:00
|
|
|
void PlayerInput::stop_listening(Node *node) {
|
|
|
|
for(size_t i = 0; i < this->listeners.size(); ++i) {
|
2024-05-28 14:28:36 +00:00
|
|
|
Listener l = this->listeners.get(i);
|
2024-04-10 10:44:50 +00:00
|
|
|
if(l.callable.get_object() == node) {
|
2024-05-28 14:28:36 +00:00
|
|
|
this->listeners.remove_at(i);
|
2024-02-13 13:59:01 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayerInput::stop_listening(Listener const& listener) {
|
2024-05-28 14:28:36 +00:00
|
|
|
this->listeners.erase(listener);
|
2024-02-13 13:59:01 +00:00
|
|
|
}
|
2024-05-21 09:56:41 +00:00
|
|
|
|
|
|
|
void PlayerInput::clear_listeners() {
|
|
|
|
this->listeners.clear();
|
|
|
|
}
|
2024-05-28 21:55:44 +00:00
|
|
|
|
|
|
|
void PlayerInput::set_device(int id) {
|
|
|
|
this->device = id;
|
|
|
|
}
|
2024-02-13 13:59:01 +00:00
|
|
|
}
|
|
|
|
|