#pragma once #include #include #include #include "InputMap.hpp" template class InputTarget { public: using Func = std::function; InputTarget(const InputTarget&) = delete; InputTarget& operator=(const InputTarget&) = delete; InputTarget(const InputMap& map): _inputMap(map) {} bool think(const sf::Event& event) const { for(auto& pair: _eventsPoll) { if(_inputMap.get(pair.first) == event) { pair.second(event); return true; } } return false; } void think() const { for(auto& pair: _eventsRealTime) { const Input& input = _inputMap.get(pair.first); if(input.test()) pair.second(input._event); } } void bind(const T& key, const Func& callback) { const Input& input = _inputMap.get(key); if(input._type & Input::Type::RealTime) _eventsRealTime.emplace_back(key, callback); else _eventsPoll.emplace_back(key, callback); } void duoBind(const T& key, const Func& callback) { bind(key, callback); bind(-key, callback); } void unbind(const T& key) { auto remove_func = [&key](const std::pair& pair) -> bool { return pair.first == key; }; if(_inputMap.get(key)._type & Input::Type::RealTime) _eventsRealTime.remove_if(remove_func); else _eventsPoll.remove_if(remove_func); } private: std::list> _eventsRealTime; std::list> _eventsPoll; const InputMap& _inputMap; };