Refactoring of Input Devices

This commit is contained in:
2021-08-30 19:28:43 +02:00
parent e5ccf0879c
commit 20ecb4f1a3
7 changed files with 114 additions and 97 deletions

View File

@@ -6,111 +6,93 @@
#pragma once
#include "Base/Logger.hpp"
#include "InputDevice.hpp"
#include <cstring>
namespace openVulkanoCpp
namespace openVulkanoCpp::Input
{
namespace Input
class ControllerType
{
class ControllerType
public:
enum Type { GENERIC_JOYSTICK, XBOX, DUAL_SHOCK, DUAL_SENSE };
private:
Type type;
public:
ControllerType(Type type) : type(type) {}
};
class InputDeviceController : public InputDevice
{
float axes[InputKey::Controller::Axis::AXIS_LAST + 1] = {0};
uint32_t pressedButtons = 0, lastPressedButtons = 0;
const ControllerType controllerType = ControllerType::GENERIC_JOYSTICK;
[[nodiscard]] bool GetLastButton(InputKey::Controller::Button button) const
{
public:
enum Type { GENERIC_JOYSTICK, XBOX, DUAL_SHOCK, DUAL_SENSE };
return lastPressedButtons & (1 << button);
}
private:
Type type;
protected:
InputDeviceController() = default;
public:
ControllerType(Type type) : type(type) {}
};
void Init(int index, const std::string& name);
class InputDeviceController : public InputDevice
void SetAxis(InputKey::Controller::Axis axisId, float value)
{
float axes[InputKey::Controller::Axis::AXIS_LAST + 1] = {0};
uint32_t pressedButtons = 0, lastPressedButtons = 0;
const ControllerType controllerType = ControllerType::GENERIC_JOYSTICK;
axes[axisId] = value;
}
[[nodiscard]] bool GetLastButton(InputKey::Controller::Button button) const
{
return lastPressedButtons & (1 << button);
}
void SetButtons(uint32_t buttonStates)
{
lastPressedButtons = pressedButtons;
pressedButtons = buttonStates;
}
protected:
InputDeviceController() = default;
[[nodiscard]] float ReadAxis(int16_t key) const final
{
return GetAxis(static_cast<InputKey::Controller::Axis>(key));
}
void Init(const int index, const std::string& name)
{
InputDevice::Init(InputDeviceType::CONTROLLER, index, name);
[[nodiscard]] bool ReadButton(int16_t key) const final
{
return GetButton(static_cast<InputKey::Controller::Button>(key));
}
pressedButtons = 0;
lastPressedButtons = 0;
for(float& axis : axes)
{
axis = 0;
}
[[nodiscard]] bool ReadButtonUp(int16_t key) const final
{
return GetButtonUp(static_cast<InputKey::Controller::Button>(key));
}
// TODO find controller type from name
Logger::INPUT->info("Initialized controller: id: {0}, name: {1}", index, name);
}
[[nodiscard]] bool ReadButtonDown(int16_t key) const final
{
return GetButtonDown(static_cast<InputKey::Controller::Button>(key));
}
void SetAxis(InputKey::Controller::Axis axisId, float value)
{
axes[axisId] = value;
}
public:
[[nodiscard]] bool GetButton(const InputKey::Controller::Button button) const
{
return pressedButtons & (1 << button);
}
void SetButtons(uint32_t buttonStates)
{
lastPressedButtons = pressedButtons;
pressedButtons = buttonStates;
}
[[nodiscard]] bool GetButtonUp(const InputKey::Controller::Button button) const
{
return !GetButton(button) && GetLastButton(button);
}
[[nodiscard]] float ReadAxis(int16_t key) const final
{
return GetAxis(static_cast<InputKey::Controller::Axis>(key));
}
[[nodiscard]] bool GetButtonDown(const InputKey::Controller::Button button) const
{
return GetButton(button) && !GetLastButton(button);
}
[[nodiscard]] bool ReadButton(int16_t key) const final
{
return GetButton(static_cast<InputKey::Controller::Button>(key));
}
[[nodiscard]] float GetAxis(const InputKey::Controller::Axis axis) const
{
return axes[axis];
}
[[nodiscard]] bool ReadButtonUp(int16_t key) const final
{
return GetButtonUp(static_cast<InputKey::Controller::Button>(key));
}
[[nodiscard]] bool ReadButtonDown(int16_t key) const final
{
return GetButtonDown(static_cast<InputKey::Controller::Button>(key));
}
public:
[[nodiscard]] bool GetButton(const InputKey::Controller::Button button) const
{
return pressedButtons & (1 << button);
}
[[nodiscard]] bool GetButtonUp(const InputKey::Controller::Button button) const
{
return !GetButton(button) && GetLastButton(button);
}
[[nodiscard]] bool GetButtonDown(const InputKey::Controller::Button button) const
{
return GetButton(button) && !GetLastButton(button);
}
[[nodiscard]] float GetAxis(const InputKey::Controller::Axis axis) const
{
return axes[axis];
}
[[nodiscard]] ControllerType GetControllerType() const
{
return controllerType;
}
};
}
}
[[nodiscard]] ControllerType GetControllerType() const
{
return controllerType;
}
};
}