diff --git a/godot-cpp-utils b/godot-cpp-utils index 160eeba..460dc9a 160000 --- a/godot-cpp-utils +++ b/godot-cpp-utils @@ -1 +1 @@ -Subproject commit 160eeba0544ec2d10c61c5449331cdd651ba7da0 +Subproject commit 460dc9a1c6bd2b077117db8f8884c36bb9eb9a02 diff --git a/src/player_input.cpp b/src/player_input.cpp deleted file mode 100644 index 95f9f67..0000000 --- a/src/player_input.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "player_input.hpp" -#include "godot_macros.h" -#include "godot_cpp/classes/input.hpp" -#include "godot_cpp/classes/input_event.hpp" -#include - -namespace godot { -void PlayerInput::_bind_methods() {} - -PlayerInput::Listener::Listener(String positive, String negative, Node *object, String method) -: actionNegative{negative} -, actionPositive{positive} -, methodName{method} -, object{object} {} - -bool PlayerInput::Listener::has_changed(Ref const &event) { - return event->is_action(this->actionNegative) || event->is_action(this->actionPositive); -} -float PlayerInput::Listener::evaluate(Ref const &event) { - float newest = static_cast(event->is_action_pressed(this->actionPositive)) - - static_cast(event->is_action_pressed(this->actionNegative)); - if(lastCached != newest) - this->object->call(this->methodName, event, newest); - return (lastCached = newest); -} - -bool PlayerInput::Listener::operator==(godot::PlayerInput::Listener const& b) { - return this->methodName == b.methodName - && this->object == b.object - && this->actionNegative == b.actionNegative - && this->actionPositive == b.actionPositive; -} - -void PlayerInput::_unhandled_input(Ref const &event) { - GDGAMEONLY(); - for(Listener& listener: this->listeners) { - if(listener.has_changed(event)) { - listener.evaluate(event); - } - } -} - -void PlayerInput::listen_to(Listener const& listener) { - this->listeners.push_back(listener); -} - -void PlayerInput::stop_listening(Node *node) { - for(size_t i = 0; i < this->listeners.size(); ++i) { - Listener& l = this->listeners.at(i); - if(l.object == node) { - this->listeners.erase(this->listeners.begin() + i); - i--; - } - } -} - -void PlayerInput::stop_listening(Listener const& listener) { - std::vector::iterator itr = std::find(this->listeners.begin(), this->listeners.end(), listener); - if(itr != this->listeners.end()) - this->listeners.erase(itr); -} -} - diff --git a/src/player_input.hpp b/src/player_input.hpp deleted file mode 100644 index ef4ee6e..0000000 --- a/src/player_input.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef PLAYER_INPUT_HPP -#define PLAYER_INPUT_HPP - -#include -#include "godot_cpp/classes/input_event.hpp" -#include "godot_cpp/classes/node.hpp" - -namespace godot { -class PlayerInput : public Node { - GDCLASS(PlayerInput, Node) - static void _bind_methods(); -public: - struct Listener { - String actionNegative{""}; - String actionPositive{""}; - float lastCached{0.f}; - String methodName{""}; - Node *object; - - Listener(String positive, String negative, Node *object, String method); - bool has_changed(Ref const &event); - float evaluate(Ref const &event); - bool operator==(godot::PlayerInput::Listener const& b); - }; -private: - std::vector listeners; -public: - virtual void _unhandled_input(Ref const &event) override; - - void listen_to(Listener const& listener); - void stop_listening(Node *node); - void stop_listening(Listener const& listener); -}; -} - - -#endif // !PLAYER_INPUT_HPP