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

104 lines
2.7 KiB
C++

#pragma once
#include "../Base/ICloseable.hpp"
#include "InputKey.hpp"
namespace openVulkanoCpp
{
namespace Input
{
class InputDevice : public ICloseable
{
InputDeviceType deviceType = InputDeviceType::UNKNOWN;
int index = -1;
std::string name;
float axisAsButtonThreshold = 0.5f;
float buttonAsAxisValue = 1.0f;
protected:
InputDevice() = default;
void Init(InputDeviceType type, int index, const std::string& name)
{
this->deviceType = type;
this->index = index;
this->name = name;
}
virtual float ReadAxis(int16_t key) const = 0;
virtual bool ReadButton(int16_t key) const = 0;
virtual bool ReadButtonUp(int16_t key) const = 0;
virtual bool ReadButtonDown(int16_t key) const = 0;
public:
virtual ~InputDevice() = default;
virtual void Close() override
{
this->deviceType = InputDeviceType::UNKNOWN;
this->index = -1;
this->name = "";
}
InputDeviceType GetType() const { return deviceType; }
int GetIndex() const { return index; }
const std::string& GetName() const { return name; }
float GetAxisAsButtonThreshold() const { return axisAsButtonThreshold; }
void SetAxisAsButtonThreshold(float value) { axisAsButtonThreshold = value; }
float GetButtonAsAxisValue() const { return buttonAsAxisValue; }
void SetButtonAsAxisValue(float value) { buttonAsAxisValue = value; }
bool GetButton(InputKey key) const
{
if (key.GetInputDeviceType() != deviceType) return false;
if (key.GetInputType() == InputKey::InputType::AXIS)
{
return ReadAxis(key.GetInputKey()) > axisAsButtonThreshold;
}
return ReadButton(key.GetInputKey());
}
bool GetButtonUp(InputKey key) const
{
if (key.GetInputDeviceType() != deviceType) return false;
if (key.GetInputType() == InputKey::InputType::AXIS)
{
//TODO handle
return ReadAxis(key.GetInputKey()) > axisAsButtonThreshold;
}
return ReadButtonUp(key.GetInputKey());
}
bool GetButtonDown(InputKey key) const
{
if (key.GetInputDeviceType() != deviceType) return false;
if (key.GetInputType() == InputKey::InputType::AXIS)
{
//TODO handle
return ReadAxis(key.GetInputKey()) > axisAsButtonThreshold;
}
return ReadButtonDown(key.GetInputKey());
}
float GetAxis(InputKey key) const
{
if (key.GetInputDeviceType() != deviceType) return 0;
if (key.GetInputType() == InputKey::InputType::BUTTON)
{
return ReadButton(key.GetInputKey()) ? buttonAsAxisValue : 0;
}
return ReadAxis(key.GetInputKey());
}
float GetAxis(InputKey keyPositive, InputKey keyNegative) const
{
return GetAxis(keyPositive) - GetAxis(keyNegative);
}
};
}
}