Compare commits

..

No commits in common. "b37607e22bcaccb922ae8e5ad74bdb1b694e871d" and "527415677f308fbf997fb42a7fac677bfe151189" have entirely different histories.

3 changed files with 27 additions and 35 deletions

View File

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

View File

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

View File

@ -52,6 +52,18 @@ public:
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:
static gd::Vector2 get_last_mouse_motion();
@ -67,21 +79,6 @@ public:
void stop_listening(Node *node);
void stop_listening(Listener const &listener);
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{};
};
}