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

@@ -5,6 +5,7 @@
*/ */
#include "InputDeviceGLFW.hpp" #include "InputDeviceGLFW.hpp"
#include "Base/Logger.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace openVulkanoCpp::GLFW namespace openVulkanoCpp::GLFW

View File

@@ -7,6 +7,7 @@
#include "InputProviderGLFW.hpp" #include "InputProviderGLFW.hpp"
#include "InputMappingGLFW.hpp" #include "InputMappingGLFW.hpp"
#include "Input/InputManager.hpp" #include "Input/InputManager.hpp"
#include "Base/Logger.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
static_assert(GLFW_JOYSTICK_LAST == 15); // if this fails, please update it and resize the controllers array accordingly as GLFW_JOYSTICK_LAST + 1 static_assert(GLFW_JOYSTICK_LAST == 15); // if this fails, please update it and resize the controllers array accordingly as GLFW_JOYSTICK_LAST + 1

View File

@@ -7,6 +7,7 @@
#include "PlatformGLFW.hpp" #include "PlatformGLFW.hpp"
#include "WindowGLFW.hpp" #include "WindowGLFW.hpp"
#include "Base/PlatformEnums.hpp" #include "Base/PlatformEnums.hpp"
#include "Base/Logger.hpp"
#include "Host/PlatformProducer.hpp" #include "Host/PlatformProducer.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>

View File

@@ -8,6 +8,8 @@
#include "Base/IPlatform.hpp" #include "Base/IPlatform.hpp"
#include "InputProviderGLFW.hpp" #include "InputProviderGLFW.hpp"
#include <memory>
#include <vector>
namespace openVulkanoCpp namespace openVulkanoCpp
{ {

View File

@@ -0,0 +1,26 @@
/*
* 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 "InputDeviceController.hpp"
#include "Base/Logger.hpp"
namespace openVulkanoCpp::Input
{
void InputDeviceController::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);
}
}

View File

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

View File

@@ -1,14 +1,18 @@
/*
* 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 #pragma once
#include <cstdint> #include <cstdint>
#include <string> #include <string>
namespace openVulkanoCpp namespace openVulkanoCpp::Input
{ {
namespace Input enum class InputDeviceType : uint8_t
{ {
enum class InputDeviceType : uint8_t UNKNOWN, KEYBOARD, MOUSE, CONTROLLER, TOUCH
{ };
UNKNOWN, KEYBOARD, MOUSE, CONTROLLER }
};
}
}