Files
OpenVulkano/openVulkanoCpp/Input/InputDeviceController.hpp
2020-05-16 00:56:22 +02:00

110 lines
2.4 KiB
C++

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