From 20ecb4f1a3ff7dd8cd13b9413b2df053fe04452f Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Mon, 30 Aug 2021 19:28:43 +0200 Subject: [PATCH] Refactoring of Input Devices --- openVulkanoCpp/Host/GLFW/InputDeviceGLFW.cpp | 1 + .../Host/GLFW/InputProviderGLFW.cpp | 1 + openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp | 1 + openVulkanoCpp/Host/GLFW/PlatformGLFW.hpp | 2 + .../Input/InputDeviceController.cpp | 26 +++ .../Input/InputDeviceController.hpp | 160 ++++++++---------- openVulkanoCpp/Input/InputDeviceType.hpp | 20 ++- 7 files changed, 114 insertions(+), 97 deletions(-) create mode 100644 openVulkanoCpp/Input/InputDeviceController.cpp diff --git a/openVulkanoCpp/Host/GLFW/InputDeviceGLFW.cpp b/openVulkanoCpp/Host/GLFW/InputDeviceGLFW.cpp index 3c1b71a..6f85eb3 100644 --- a/openVulkanoCpp/Host/GLFW/InputDeviceGLFW.cpp +++ b/openVulkanoCpp/Host/GLFW/InputDeviceGLFW.cpp @@ -5,6 +5,7 @@ */ #include "InputDeviceGLFW.hpp" +#include "Base/Logger.hpp" #include namespace openVulkanoCpp::GLFW diff --git a/openVulkanoCpp/Host/GLFW/InputProviderGLFW.cpp b/openVulkanoCpp/Host/GLFW/InputProviderGLFW.cpp index 22c2447..2f1d11b 100644 --- a/openVulkanoCpp/Host/GLFW/InputProviderGLFW.cpp +++ b/openVulkanoCpp/Host/GLFW/InputProviderGLFW.cpp @@ -7,6 +7,7 @@ #include "InputProviderGLFW.hpp" #include "InputMappingGLFW.hpp" #include "Input/InputManager.hpp" +#include "Base/Logger.hpp" #include static_assert(GLFW_JOYSTICK_LAST == 15); // if this fails, please update it and resize the controllers array accordingly as GLFW_JOYSTICK_LAST + 1 diff --git a/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp b/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp index bfa07a7..864172f 100644 --- a/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp +++ b/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp @@ -7,6 +7,7 @@ #include "PlatformGLFW.hpp" #include "WindowGLFW.hpp" #include "Base/PlatformEnums.hpp" +#include "Base/Logger.hpp" #include "Host/PlatformProducer.hpp" #include diff --git a/openVulkanoCpp/Host/GLFW/PlatformGLFW.hpp b/openVulkanoCpp/Host/GLFW/PlatformGLFW.hpp index 9a61c26..753c00b 100644 --- a/openVulkanoCpp/Host/GLFW/PlatformGLFW.hpp +++ b/openVulkanoCpp/Host/GLFW/PlatformGLFW.hpp @@ -8,6 +8,8 @@ #include "Base/IPlatform.hpp" #include "InputProviderGLFW.hpp" +#include +#include namespace openVulkanoCpp { diff --git a/openVulkanoCpp/Input/InputDeviceController.cpp b/openVulkanoCpp/Input/InputDeviceController.cpp new file mode 100644 index 0000000..e0bee6e --- /dev/null +++ b/openVulkanoCpp/Input/InputDeviceController.cpp @@ -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); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Input/InputDeviceController.hpp b/openVulkanoCpp/Input/InputDeviceController.hpp index 5439049..7d64892 100644 --- a/openVulkanoCpp/Input/InputDeviceController.hpp +++ b/openVulkanoCpp/Input/InputDeviceController.hpp @@ -6,111 +6,93 @@ #pragma once -#include "Base/Logger.hpp" #include "InputDevice.hpp" -#include -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(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(key)); + } - pressedButtons = 0; - lastPressedButtons = 0; - for(float& axis : axes) - { - axis = 0; - } + [[nodiscard]] bool ReadButtonUp(int16_t key) const final + { + return GetButtonUp(static_cast(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(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(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(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(key)); - } - - [[nodiscard]] bool ReadButtonDown(int16_t key) const final - { - return GetButtonDown(static_cast(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; - } - }; - } -} \ No newline at end of file + [[nodiscard]] ControllerType GetControllerType() const + { + return controllerType; + } + }; +} diff --git a/openVulkanoCpp/Input/InputDeviceType.hpp b/openVulkanoCpp/Input/InputDeviceType.hpp index d2f8bd6..5653a2b 100644 --- a/openVulkanoCpp/Input/InputDeviceType.hpp +++ b/openVulkanoCpp/Input/InputDeviceType.hpp @@ -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 + #include #include -namespace openVulkanoCpp +namespace openVulkanoCpp::Input { - namespace Input + enum class InputDeviceType : uint8_t { - enum class InputDeviceType : uint8_t - { - UNKNOWN, KEYBOARD, MOUSE, CONTROLLER - }; - } -} \ No newline at end of file + UNKNOWN, KEYBOARD, MOUSE, CONTROLLER, TOUCH + }; +}