Split InputManager
This commit is contained in:
82
openVulkanoCpp/Input/InputManager.cpp
Normal file
82
openVulkanoCpp/Input/InputManager.cpp
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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(InputKey key : action->GetKeys())
|
||||||
|
{
|
||||||
|
value += device->GetAxis(key);
|
||||||
|
}
|
||||||
|
for(const auto& keys : action->GetAxisButtons())
|
||||||
|
{
|
||||||
|
value += GetAxis(keys.first) - GetAxis(keys.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
float InputManager::GetAxis(InputKey key) const
|
||||||
|
{
|
||||||
|
float value = 0;
|
||||||
|
for (const InputDevice* device : devices)
|
||||||
|
{
|
||||||
|
value += device->GetAxis(key);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InputManager::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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InputManager::GetButton(InputKey key) const
|
||||||
|
{
|
||||||
|
for(const InputDevice* device : devices)
|
||||||
|
{
|
||||||
|
if (device->GetButton(key)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,23 +9,18 @@
|
|||||||
#include "InputDevice.hpp"
|
#include "InputDevice.hpp"
|
||||||
#include "InputAction.hpp"
|
#include "InputAction.hpp"
|
||||||
#include "InputShortcut.hpp"
|
#include "InputShortcut.hpp"
|
||||||
#include "Base/Utils.hpp"
|
|
||||||
|
|
||||||
#include <functional>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace OpenVulkano::Input
|
namespace OpenVulkano::Input
|
||||||
{
|
{
|
||||||
class InputManager
|
class InputManager final
|
||||||
{
|
{
|
||||||
InputManager() = default;
|
InputManager() = default;
|
||||||
public:
|
public:
|
||||||
static InputManager* GetInstance()
|
static InputManager* GetInstance();
|
||||||
{
|
|
||||||
static InputManager* instance = new InputManager();
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegisterInputDevice(InputDevice* device)
|
void RegisterInputDevice(InputDevice* device)
|
||||||
{
|
{
|
||||||
@@ -33,70 +28,17 @@ namespace OpenVulkano::Input
|
|||||||
if (!lastActiveDevice) lastActiveDevice = device;
|
if (!lastActiveDevice) lastActiveDevice = device;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnregisterInputDevice(InputDevice* device)
|
void UnregisterInputDevice(InputDevice* device);
|
||||||
{
|
|
||||||
Utils::Remove(devices, device);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] InputAction* GetAction(const std::string& actionName)
|
[[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
|
[[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
|
[[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
|
[[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
|
[[nodiscard]] bool GetButton(InputKey key) const;
|
||||||
{
|
|
||||||
for(const InputDevice* device : devices)
|
|
||||||
{
|
|
||||||
if (device->GetButton(key)) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] InputDevice* GetLastActiveDevice() const
|
[[nodiscard]] InputDevice* GetLastActiveDevice() const
|
||||||
{
|
{
|
||||||
@@ -104,7 +46,6 @@ namespace OpenVulkano::Input
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//std::unordered_map<InputKey, std::vector<InputAction*>> inputActionMapping;
|
|
||||||
std::unordered_map<std::string, std::unique_ptr<InputAction>> actionNameMapping;
|
std::unordered_map<std::string, std::unique_ptr<InputAction>> actionNameMapping;
|
||||||
std::vector<InputDevice*> devices;
|
std::vector<InputDevice*> devices;
|
||||||
InputDevice* lastActiveDevice = nullptr;
|
InputDevice* lastActiveDevice = nullptr;
|
||||||
|
|||||||
Reference in New Issue
Block a user