Files
OpenVulkano/openVulkanoCpp/Input/InputManager.hpp
2023-10-12 12:13:14 +02:00

113 lines
2.6 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "InputDevice.hpp"
#include "InputAction.hpp"
#include "InputShortcut.hpp"
#include "Base/Utils.hpp"
#include <functional>
#include <unordered_map>
#include <vector>
namespace OpenVulkano::Input
{
class InputManager
{
InputManager() = default;
public:
static InputManager* GetInstance()
{
static InputManager* instance = new InputManager();
return instance;
}
void RegisterInputDevice(InputDevice* device)
{
devices.push_back(device);
if (!lastActiveDevice) lastActiveDevice = device;
}
void UnregisterInputDevice(InputDevice* device)
{
Utils::Remove(devices, device);
}
[[nodiscard]] InputAction* GetAction(const std::string& actionName)
{
auto& action = actionNameMapping[actionName];
if(!action)
{
action = std::make_unique<InputAction>(actionName);
}
return action.get();
}
[[nodiscard]] float GetAxis(const InputAction* action) const
{
float value = 0;
const std::vector<InputDevice*>& testDevices = action->GetDevices().empty() ? devices : action->GetDevices();
for (const InputDevice* device : testDevices)
{
for(InputKey key : action->GetKeys())
{
value += device->GetAxis(key);
}
for(const auto& keys : action->GetAxisButtons())
{
value += GetAxis(keys.first) - GetAxis(keys.second);
}
}
return value;
}
[[nodiscard]] float GetAxis(InputKey key) const
{
float value = 0;
for (const InputDevice* device : devices)
{
value += device->GetAxis(key);
}
return value;
}
[[nodiscard]] bool GetButton(InputAction* action) const
{
const std::vector<InputDevice*>& testDevices = action->GetDevices().empty() ? devices : action->GetDevices();
for (const InputDevice* device : testDevices)
{
for (const InputKey key : action->GetKeys())
{
if (device->GetButton(key)) return true;
}
}
return false;
}
[[nodiscard]] bool GetButton(InputKey key) const
{
for(const InputDevice* device : devices)
{
if (device->GetButton(key)) return true;
}
return false;
}
[[nodiscard]] InputDevice* GetLastActiveDevice() const
{
return lastActiveDevice;
}
private:
//std::unordered_map<InputKey, std::vector<InputAction*>> inputActionMapping;
std::unordered_map<std::string, std::unique_ptr<InputAction>> actionNameMapping;
std::vector<InputDevice*> devices;
InputDevice* lastActiveDevice = nullptr;
};
}