/**
* Triangles
* Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#pragma once
#include
#include
#include
#include "InputMap.hpp"
#include
template
class InputTarget
{
public:
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, 1.f);
return true;
}
}
return false;
}
void think() const
{
for(auto& pair: _eventsRealTime)
{
const Input& input = _inputMap.get(pair.first);
float power = input.test();
if(power != 0.f)
pair.second(input._event, power);
}
}
void bind(const T& key, const std::function& 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 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;
};