Files
OpenVulkano/openVulkanoCpp/Input/InputManager.cpp
2025-08-21 23:56:11 +02:00

185 lines
4.4 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/.
*/
#include "InputManager.hpp"
#include "Base/Utils.hpp"
namespace OpenVulkano::Input
{
InputManager* InputManager::GetInstance()
{
static InputManager instance;
return &instance;
}
void InputManager::UnregisterInputDevice(InputDevice* device)
{
Utils::Remove(devices, device);
}
InputAction* InputManager::GetAction(const std::string& actionName)
{
auto& action = actionNameMapping[actionName];
if(!action)
{
action = std::make_unique<InputAction>(actionName);
}
return action.get();
}
float InputManager::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(KeyBinding binding : action->GetKeys())
{
if (binding.key.GetInputDeviceType() != device->GetType()) continue;
value += device->GetAxis(binding.key) * binding.scale;
}
for(const auto& binding : action->GetAxisButtons())
{
if (binding.positive.GetInputDeviceType() != device->GetType()) continue;
value += (GetAxis(binding.positive) - GetAxis(binding.negative)) * binding.scale;
}
}
return value;
}
float InputManager::GetAxis(InputKey key) const
{
float value = 0;
for (const InputDevice* device : devices)
{
if (key.GetInputDeviceType() != device->GetType()) continue;
value += device->GetAxis(key);
}
return value;
}
bool InputManager::GetButton(const InputAction* action) const
{
const std::vector<InputDevice*>& testDevices = action->GetDevices().empty() ? devices : action->GetDevices();
for (const InputDevice* device : testDevices)
{
for (const KeyBinding binding : action->GetKeys())
{
if (binding.key.GetInputDeviceType() != device->GetType()) continue;
return device->GetButton(binding.key);
}
}
return false;
}
bool InputManager::GetButtonDown(const InputAction* action) const
{
const std::vector<InputDevice*>& testDevices = action->GetDevices().empty() ? devices : action->GetDevices();
for (const InputDevice* device : testDevices)
{
for (const KeyBinding binding : action->GetKeys())
{
if (binding.key.GetInputDeviceType() != device->GetType())
{
continue;
}
return device->GetButtonDown(binding.key);
}
}
return false;
}
bool InputManager::GetButtonUp(const InputAction* action) const
{
const std::vector<InputDevice*>& testDevices = action->GetDevices().empty() ? devices : action->GetDevices();
for (const InputDevice* device : testDevices)
{
for (const KeyBinding binding : action->GetKeys())
{
if (binding.key.GetInputDeviceType() != device->GetType())
{
continue;
}
return device->GetButtonUp(binding.key);
}
}
return false;
}
bool InputManager::GetButton(InputKey key) const
{
for(const InputDevice* device : devices)
{
if (key.GetInputDeviceType() != device->GetType()) continue;
return device->GetButton(key);
}
return false;
}
bool InputManager::GetButtonUp(InputKey key) const
{
for(const InputDevice* device : devices)
{
if (key.GetInputDeviceType() != device->GetType()) continue;
return device->GetButtonUp(key);
}
return false;
}
bool InputManager::GetButtonDown(InputKey key) const
{
for(const InputDevice* device : devices)
{
if (key.GetInputDeviceType() != device->GetType()) continue;
return device->GetButtonDown(key);
}
return false;
}
InputDevice* InputManager::GetDevice(InputDeviceType type) const
{
if (type == InputDeviceType::UNKNOWN)
{
return nullptr;
}
for (InputDevice* device : devices)
{
if (device->GetType() == type)
{
return device;
}
}
return nullptr;
}
std::vector<InputDevice*> InputManager::GetDevices(InputDeviceType type) const
{
if (type == InputDeviceType::UNKNOWN)
{
return {};
}
std::vector<InputDevice*> devices;
devices.reserve(this->devices.size());
for (InputDevice* device : this->devices)
{
if (device->GetType() == type)
{
devices.push_back(device);
}
}
return devices;
}
void InputManager::Tick(double time)
{
timescale = inputTimeRange.Clamp(time);
for(InputDevice* device : devices)
{
device->Tick();
}
}
}