fix: error in handling of mouse motion events

main
Sara 2024-05-28 23:55:44 +02:00
parent e0f6ba276b
commit b37607e22b
2 changed files with 31 additions and 18 deletions

View File

@ -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) {
@ -129,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;
}
}

View File

@ -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{};
};
}