Cleanup input stuff

This commit is contained in:
2023-10-12 12:13:14 +02:00
parent 31eef79f22
commit b2ec209ef0
6 changed files with 533 additions and 486 deletions

View File

@@ -0,0 +1,48 @@
/*
* 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 <string>
#include <vector>
namespace OpenVulkano::Input
{
class InputDevice;
class BaseInputAction
{
std::string m_name;
std::vector<InputDevice*> m_devices;
bool m_enabled;
protected:
BaseInputAction(const std::string& name) : m_name(name), m_enabled(true) {}
public:
virtual ~BaseInputAction() = default;
[[nodiscard]] const std::string& GetName() const
{
return m_name;
}
[[nodiscard]] const std::vector<InputDevice*>& GetDevices() const
{
return m_devices;
}
[[nodiscard]] bool IsEnabled() const
{
return m_enabled;
}
void SetEnabled(bool enabled)
{
m_enabled = enabled;
}
};
}

View File

@@ -0,0 +1,43 @@
/*
* 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 "BaseInputAction.hpp"
#include "InputKey.hpp"
namespace OpenVulkano::Input
{
class InputAction final : public BaseInputAction
{
std::vector<InputKey> keys;
std::vector<std::pair<InputKey, InputKey>> axisButtons;
public:
InputAction(const std::string& name) : BaseInputAction(name)
{}
[[nodiscard]] const std::vector<InputKey>& GetKeys() const
{
return keys;
}
[[nodiscard]] const std::vector<std::pair<InputKey, InputKey>>& GetAxisButtons() const
{
return axisButtons;
}
void BindKey(InputKey key)
{
keys.push_back(key);
}
void BindAxisButtons(InputKey keyPositive, InputKey keyNegative)
{
axisButtons.emplace_back(keyPositive, keyNegative);
}
};
}

View File

@@ -9,10 +9,8 @@
#include "Base/ICloseable.hpp"
#include "InputKey.hpp"
namespace OpenVulkano
namespace OpenVulkano::Input
{
namespace Input
{
class InputDevice : public ICloseable
{
InputDeviceType deviceType = InputDeviceType::UNKNOWN;
@@ -106,5 +104,5 @@ namespace OpenVulkano
return GetAxis(keyPositive) - GetAxis(keyNegative);
}
};
}
}

View File

@@ -1,10 +1,15 @@
/*
* 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 "InputDeviceType.hpp"
namespace OpenVulkano
namespace OpenVulkano::Input
{
namespace Input
{
class InputKey
{
public:
@@ -263,11 +268,10 @@ namespace OpenVulkano
deviceType(InputDeviceType::CONTROLLER), type(InputType::AXIS), key(controllerAxis)
{}
InputDeviceType GetInputDeviceType() const { return deviceType; }
[[nodiscard]] InputDeviceType GetInputDeviceType() const { return deviceType; }
InputType GetInputType() const { return type; }
[[nodiscard]] InputType GetInputType() const { return type; }
int16_t GetInputKey() const { return key; }
[[nodiscard]] int16_t GetInputKey() const { return key; }
};
}
}

View File

@@ -1,91 +1,22 @@
/*
* 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 "InputKey.hpp"
#include "InputDevice.hpp"
#include "InputAction.hpp"
#include "InputShortcut.hpp"
#include "Base/Utils.hpp"
#include <functional>
#include <unordered_map>
#include <vector>
namespace OpenVulkano
namespace OpenVulkano::Input
{
namespace Input
{
class BaseInputAction
{
std::string name;
std::vector<InputDevice*> devices;
bool enabled;
protected:
BaseInputAction(const std::string& name) : name(name), enabled(true) {}
public:
virtual ~BaseInputAction() = default;
const std::string& GetName() const
{
return name;
}
const std::vector<InputDevice*>& GetDevices() const
{
return devices;
}
bool IsEnabled() const
{
return enabled;
}
void SetEnabled(bool enabled)
{
this->enabled = enabled;
}
};
class InputAction : public BaseInputAction
{
std::vector<InputKey> keys;
std::vector<std::pair<InputKey, InputKey>> axisButtons;
public:
InputAction(const std::string& name) : BaseInputAction(name)
{}
const std::vector<InputKey>& GetKeys() const
{
return keys;
}
const std::vector<std::pair<InputKey, InputKey>>& GetAxisButtons() const
{
return axisButtons;
}
void BindKey(InputKey key)
{
keys.push_back(key);
}
void BindAxisButtons(InputKey keyPositive, InputKey keyNegative)
{
axisButtons.emplace_back(keyPositive, keyNegative);
}
};
class InputShortcut : public BaseInputAction
{
std::string name;
std::vector<InputDevice*> devices;
std::vector<std::vector<InputKey>> buttonBindings;
public:
InputShortcut(const std::string& name) : BaseInputAction(name)
{}
};
class InputManager
{
InputManager() = default;
@@ -107,17 +38,17 @@ namespace OpenVulkano
Utils::Remove(devices, device);
}
InputAction* GetAction(const std::string& actionName)
[[nodiscard]] InputAction* GetAction(const std::string& actionName)
{
InputAction*& action = actionNameMapping[actionName];
auto& action = actionNameMapping[actionName];
if(!action)
{
action = new InputAction(actionName);
action = std::make_unique<InputAction>(actionName);
}
return action;
return action.get();
}
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();
@@ -135,7 +66,7 @@ namespace OpenVulkano
return value;
}
float GetAxis(InputKey key) const
[[nodiscard]] float GetAxis(InputKey key) const
{
float value = 0;
for (const InputDevice* device : devices)
@@ -145,7 +76,7 @@ namespace OpenVulkano
return value;
}
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)
@@ -158,7 +89,7 @@ namespace OpenVulkano
return false;
}
bool GetButton(InputKey key) const
[[nodiscard]] bool GetButton(InputKey key) const
{
for(const InputDevice* device : devices)
{
@@ -167,16 +98,15 @@ namespace OpenVulkano
return false;
}
InputDevice* GetLastActiveDevice() const
[[nodiscard]] InputDevice* GetLastActiveDevice() const
{
return lastActiveDevice;
}
private:
//std::unordered_map<InputKey, std::vector<InputAction*>> inputActionMapping;
std::unordered_map<std::string, InputAction*> actionNameMapping;
std::unordered_map<std::string, std::unique_ptr<InputAction>> actionNameMapping;
std::vector<InputDevice*> devices;
InputDevice* lastActiveDevice = nullptr;
};
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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 "BaseInputAction.hpp"
#include "InputKey.hpp"
namespace OpenVulkano::Input
{
class InputShortcut final : public BaseInputAction
{
std::string name;
std::vector<InputDevice*> devices;
std::vector<std::vector<InputKey>> buttonBindings;
public:
InputShortcut(const std::string& name) : BaseInputAction(name)
{}
};
}