12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #pragma once
- #include <list>
- #include <functional>
- #include <SFML/Window.hpp>
- #include "InputMap.hpp"
- template<typename T = int>
- class InputTarget
- {
- public:
- using Func = std::function<void(const sf::Event&)>;
- InputTarget(const InputTarget<T>&) = delete;
- InputTarget<T>& operator=(const InputTarget<T>&) = delete;
-
- InputTarget(const InputMap<T>& 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<T,Func>& 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<std::pair<T, Func>> _eventsRealTime;
- std::list<std::pair<T, Func>> _eventsPoll;
-
- const InputMap<T>& _inputMap;
- };
|