Compare commits

...

3 Commits

3 changed files with 35 additions and 27 deletions

View File

@ -36,9 +36,7 @@ bool GameRoot3D::has_singleton() {
void GameRoot3D::_enter_tree() { GDGAMEONLY(); void GameRoot3D::_enter_tree() { GDGAMEONLY();
// TODO: Replace this with detecting input devices // TODO: Replace this with detecting input devices
if(this->players.is_empty()) {
this->player_input_connected(); this->player_input_connected();
}
this->grab_singleton(); this->grab_singleton();
} }

View File

@ -1,4 +1,5 @@
#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>
@ -25,29 +26,34 @@ PlayerInput::Listener::Listener(gd::String action, gd::Callable callable)
std::optional<float> PlayerInput::Listener::evaluate_action(gd::String const &action) { std::optional<float> PlayerInput::Listener::evaluate_action(gd::String const &action) {
gd::Input *input = gd::Input::get_singleton(); gd::Input *input = gd::Input::get_singleton();
gd::UtilityFunctions::print("evaluating ", action);
if(action.begins_with("_mouse_")) { if(action.begins_with("_mouse_")) {
gd::UtilityFunctions::print("evaluating ", action, " as mouse motion");
gd::Vector2 vector = PlayerInput::get_last_mouse_motion(); gd::Vector2 vector = PlayerInput::get_last_mouse_motion();
if(action.ends_with("_up")) if(action == "_mouse_up")
return vector.y > 0.f ? vector.y : 0.f; return vector.y > 0.f ? vector.y : 0.f;
else if(action.ends_with("_down")) else if(action == "_mouse_down")
return vector.y < 0.f ? -vector.y : 0.f; return vector.y < 0.f ? -vector.y : 0.f;
else if(action.ends_with("_right")) else if(action == "_mouse_right")
return vector.x > 0.f ? vector.x : 0.f; return vector.x > 0.f ? vector.x : 0.f;
else if(action.ends_with("_left")) else if(action == "_mouse_left")
return vector.x < 0.f ? -vector.x : 0.f; return vector.x < 0.f ? -vector.x : 0.f;
} }
if(action.is_empty()) { if(action.is_empty()) {
return 0.f; return 0.f;
} else { } else {
gd::UtilityFunctions::print("evaluating ", action, " as regular");
return float(input->is_action_pressed(action)); return float(input->is_action_pressed(action));
} }
} }
bool PlayerInput::Listener::has_changed(gd::Ref<gd::InputEvent> const &event) { bool PlayerInput::Listener::has_changed(gd::Ref<gd::InputEvent> const &event) {
bool const mouse_changed{this->isMouseEvent && event->is_class("InputEventMouseMotion")}; // do not evaluate mouse events as anything but
if(this->isMouseEvent)
return event->is_class("InputEventMouseMotion");
bool const negative_changed{!this->actionNegative.is_empty() && event->is_action(this->actionNegative)}; 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)}; bool const positive_changed{!this->actionPositive.is_empty() && event->is_action(this->actionPositive)};
return mouse_changed || negative_changed || positive_changed; return negative_changed || positive_changed;
} }
float PlayerInput::Listener::evaluate(gd::Ref<gd::InputEvent> const &event) { float PlayerInput::Listener::evaluate(gd::Ref<gd::InputEvent> const &event) {
@ -71,24 +77,21 @@ gd::Vector2 PlayerInput::get_last_mouse_motion() {
return PlayerInput::lastMouseMotion; return PlayerInput::lastMouseMotion;
} }
void PlayerInput::_enter_tree() { void PlayerInput::_enter_tree() { GDGAMEONLY();
GDGAMEONLY();
if(!PlayerInput::primaryExists) { if(!PlayerInput::primaryExists) {
this->isPrimary = true; this->isPrimary = true;
PlayerInput::primaryExists = true; PlayerInput::primaryExists = true;
} }
} }
void PlayerInput::_exit_tree() { void PlayerInput::_exit_tree() { GDGAMEONLY();
GDGAMEONLY();
if(this->isPrimary) { if(this->isPrimary) {
this->isPrimary = false; this->isPrimary = false;
PlayerInput::primaryExists = false; PlayerInput::primaryExists = false;
} }
} }
void PlayerInput::_unhandled_input(gd::Ref<gd::InputEvent> const &event) { void PlayerInput::_unhandled_input(gd::Ref<gd::InputEvent> const &event) { GDGAMEONLY();
GDGAMEONLY();
if(this->isPrimary && event->is_class("InputEventMouseMotion")) if(this->isPrimary && event->is_class("InputEventMouseMotion"))
PlayerInput::lastMouseMotion = gd::Object::cast_to<gd::InputEventMouseMotion>(*event)->get_relative(); PlayerInput::lastMouseMotion = gd::Object::cast_to<gd::InputEventMouseMotion>(*event)->get_relative();
for(Listener& listener: this->listeners) { for(Listener& listener: this->listeners) {
@ -132,5 +135,9 @@ void PlayerInput::stop_listening(Listener const& listener) {
void PlayerInput::clear_listeners() { void PlayerInput::clear_listeners() {
this->listeners.clear(); this->listeners.clear();
} }
void PlayerInput::set_device(int id) {
this->device = id;
}
} }

View File

@ -52,18 +52,6 @@ public:
bool operator==(PlayerInput::Listener const& b) const; bool operator==(PlayerInput::Listener const& b) const;
}; };
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};
// current listeners for this instance
gd::Vector<Listener> listeners{};
public: public:
static gd::Vector2 get_last_mouse_motion(); static gd::Vector2 get_last_mouse_motion();
@ -79,6 +67,21 @@ public:
void stop_listening(Node *node); void stop_listening(Node *node);
void stop_listening(Listener const &listener); void stop_listening(Listener const &listener);
void clear_listeners(); void clear_listeners();
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{};
}; };
} }