Compare commits
3 Commits
527415677f
...
b37607e22b
Author | SHA1 | Date |
---|---|---|
Sara | b37607e22b | |
Sara | e0f6ba276b | |
Sara | 47a5965e31 |
|
@ -36,9 +36,7 @@ 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#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>
|
||||
|
@ -25,29 +26,34 @@ 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.ends_with("_up"))
|
||||
if(action == "_mouse_up")
|
||||
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;
|
||||
else if(action.ends_with("_right"))
|
||||
else if(action == "_mouse_right")
|
||||
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;
|
||||
}
|
||||
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) {
|
||||
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 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) {
|
||||
|
@ -71,24 +77,21 @@ 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) {
|
||||
|
@ -132,5 +135,9 @@ void PlayerInput::stop_listening(Listener const& listener) {
|
|||
void PlayerInput::clear_listeners() {
|
||||
this->listeners.clear();
|
||||
}
|
||||
|
||||
void PlayerInput::set_device(int id) {
|
||||
this->device = id;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,18 +52,6 @@ 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();
|
||||
|
||||
|
@ -79,6 +67,21 @@ 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{};
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue