Refactor some code
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
/*
|
||||
* 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 "../Base/ICloseable.hpp"
|
||||
#include "InputKey.hpp"
|
||||
|
||||
@@ -24,35 +31,35 @@ namespace openVulkanoCpp
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
virtual float ReadAxis(int16_t key) const = 0;
|
||||
[[nodiscard]] virtual float ReadAxis(int16_t key) const = 0;
|
||||
|
||||
virtual bool ReadButton(int16_t key) const = 0;
|
||||
[[nodiscard]] virtual bool ReadButton(int16_t key) const = 0;
|
||||
|
||||
virtual bool ReadButtonUp(int16_t key) const = 0;
|
||||
[[nodiscard]] virtual bool ReadButtonUp(int16_t key) const = 0;
|
||||
|
||||
virtual bool ReadButtonDown(int16_t key) const = 0;
|
||||
[[nodiscard]] virtual bool ReadButtonDown(int16_t key) const = 0;
|
||||
|
||||
public:
|
||||
virtual ~InputDevice() = default;
|
||||
~InputDevice() override = default;
|
||||
|
||||
virtual void Close() override
|
||||
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; }
|
||||
[[nodiscard]] InputDeviceType GetType() const { return deviceType; }
|
||||
[[nodiscard]] int GetIndex() const { return index; }
|
||||
[[nodiscard]] const std::string& GetName() const { return name; }
|
||||
|
||||
float GetAxisAsButtonThreshold() const { return axisAsButtonThreshold; }
|
||||
[[nodiscard]] float GetAxisAsButtonThreshold() const { return axisAsButtonThreshold; }
|
||||
void SetAxisAsButtonThreshold(float value) { axisAsButtonThreshold = value; }
|
||||
|
||||
float GetButtonAsAxisValue() const { return buttonAsAxisValue; }
|
||||
[[nodiscard]] float GetButtonAsAxisValue() const { return buttonAsAxisValue; }
|
||||
void SetButtonAsAxisValue(float value) { buttonAsAxisValue = value; }
|
||||
|
||||
bool GetButton(InputKey key) const
|
||||
[[nodiscard]] bool GetButton(InputKey key) const
|
||||
{
|
||||
if (key.GetInputDeviceType() != deviceType) return false;
|
||||
if (key.GetInputType() == InputKey::InputType::AXIS)
|
||||
@@ -62,7 +69,7 @@ namespace openVulkanoCpp
|
||||
return ReadButton(key.GetInputKey());
|
||||
}
|
||||
|
||||
bool GetButtonUp(InputKey key) const
|
||||
[[nodiscard]] bool GetButtonUp(InputKey key) const
|
||||
{
|
||||
if (key.GetInputDeviceType() != deviceType) return false;
|
||||
if (key.GetInputType() == InputKey::InputType::AXIS)
|
||||
@@ -73,7 +80,7 @@ namespace openVulkanoCpp
|
||||
return ReadButtonUp(key.GetInputKey());
|
||||
}
|
||||
|
||||
bool GetButtonDown(InputKey key) const
|
||||
[[nodiscard]] bool GetButtonDown(InputKey key) const
|
||||
{
|
||||
if (key.GetInputDeviceType() != deviceType) return false;
|
||||
if (key.GetInputType() == InputKey::InputType::AXIS)
|
||||
@@ -84,7 +91,7 @@ namespace openVulkanoCpp
|
||||
return ReadButtonDown(key.GetInputKey());
|
||||
}
|
||||
|
||||
float GetAxis(InputKey key) const
|
||||
[[nodiscard]] float GetAxis(InputKey key) const
|
||||
{
|
||||
if (key.GetInputDeviceType() != deviceType) return 0;
|
||||
if (key.GetInputType() == InputKey::InputType::BUTTON)
|
||||
@@ -94,7 +101,7 @@ namespace openVulkanoCpp
|
||||
return ReadAxis(key.GetInputKey());
|
||||
}
|
||||
|
||||
float GetAxis(InputKey keyPositive, InputKey keyNegative) const
|
||||
[[nodiscard]] float GetAxis(InputKey keyPositive, InputKey keyNegative) const
|
||||
{
|
||||
return GetAxis(keyPositive) - GetAxis(keyNegative);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* 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 "../Base/Logger.hpp"
|
||||
@@ -26,7 +32,7 @@ namespace openVulkanoCpp
|
||||
uint32_t pressedButtons = 0, lastPressedButtons = 0;
|
||||
const ControllerType controllerType = ControllerType::GENERIC_JOYSTICK;
|
||||
|
||||
bool GetLastButton(InputKey::Controller::Button button) const
|
||||
[[nodiscard]] bool GetLastButton(InputKey::Controller::Button button) const
|
||||
{
|
||||
return lastPressedButtons & (1 << button);
|
||||
}
|
||||
@@ -59,49 +65,49 @@ namespace openVulkanoCpp
|
||||
lastPressedButtons = pressedButtons;
|
||||
pressedButtons = buttonStates;
|
||||
}
|
||||
|
||||
float ReadAxis(int16_t key) const override final
|
||||
|
||||
[[nodiscard]] float ReadAxis(int16_t key) const final
|
||||
{
|
||||
return GetAxis(static_cast<InputKey::Controller::Axis>(key));
|
||||
}
|
||||
|
||||
bool ReadButton(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButton(int16_t key) const final
|
||||
{
|
||||
return GetButton(static_cast<InputKey::Controller::Button>(key));
|
||||
}
|
||||
|
||||
bool ReadButtonUp(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButtonUp(int16_t key) const final
|
||||
{
|
||||
return GetButtonUp(static_cast<InputKey::Controller::Button>(key));
|
||||
}
|
||||
|
||||
bool ReadButtonDown(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButtonDown(int16_t key) const final
|
||||
{
|
||||
return GetButtonDown(static_cast<InputKey::Controller::Button>(key));
|
||||
}
|
||||
|
||||
public:
|
||||
bool GetButton(const InputKey::Controller::Button button) const
|
||||
[[nodiscard]] bool GetButton(const InputKey::Controller::Button button) const
|
||||
{
|
||||
return pressedButtons & (1 << button);
|
||||
}
|
||||
|
||||
bool GetButtonUp(const InputKey::Controller::Button button) const
|
||||
[[nodiscard]] bool GetButtonUp(const InputKey::Controller::Button button) const
|
||||
{
|
||||
return !GetButton(button) && GetLastButton(button);
|
||||
}
|
||||
|
||||
bool GetButtonDown(const InputKey::Controller::Button button) const
|
||||
[[nodiscard]] bool GetButtonDown(const InputKey::Controller::Button button) const
|
||||
{
|
||||
return GetButton(button) && !GetLastButton(button);
|
||||
}
|
||||
|
||||
float GetAxis(const InputKey::Controller::Axis axis) const
|
||||
[[nodiscard]] float GetAxis(const InputKey::Controller::Axis axis) const
|
||||
{
|
||||
return axes[axis];
|
||||
}
|
||||
|
||||
ControllerType GetControllerType() const
|
||||
[[nodiscard]] ControllerType GetControllerType() const
|
||||
{
|
||||
return controllerType;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
/*
|
||||
* 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 "InputDevice.hpp"
|
||||
#include <bitset>
|
||||
|
||||
@@ -12,7 +19,7 @@ namespace openVulkanoCpp
|
||||
{
|
||||
std::bitset<InputKey::Keyboard::KEY_LAST + 1> keyState;
|
||||
std::bitset<InputKey::Keyboard::KEY_LAST + 1> lastKeyState;
|
||||
IWindow* lastWindow;
|
||||
IWindow* lastWindow = nullptr;
|
||||
|
||||
protected:
|
||||
InputDeviceKeyboard() = default;
|
||||
@@ -36,44 +43,44 @@ namespace openVulkanoCpp
|
||||
{
|
||||
lastWindow = window;
|
||||
}
|
||||
|
||||
float ReadAxis(int16_t key) const override final
|
||||
|
||||
[[nodiscard]] float ReadAxis(int16_t key) const final
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ReadButton(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButton(int16_t key) const final
|
||||
{
|
||||
return GetButton(static_cast<InputKey::Keyboard::Key>(key));
|
||||
}
|
||||
|
||||
bool ReadButtonUp(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButtonUp(int16_t key) const final
|
||||
{
|
||||
return GetButtonUp(static_cast<InputKey::Keyboard::Key>(key));
|
||||
}
|
||||
|
||||
bool ReadButtonDown(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButtonDown(int16_t key) const final
|
||||
{
|
||||
return GetButtonDown(static_cast<InputKey::Keyboard::Key>(key));
|
||||
}
|
||||
|
||||
public:
|
||||
bool GetButton(const InputKey::Keyboard::Key button) const
|
||||
[[nodiscard]] bool GetButton(const InputKey::Keyboard::Key button) const
|
||||
{
|
||||
return keyState[button] > 0;
|
||||
}
|
||||
|
||||
bool GetButtonUp(const InputKey::Keyboard::Key button) const
|
||||
[[nodiscard]] bool GetButtonUp(const InputKey::Keyboard::Key button) const
|
||||
{
|
||||
return !keyState[button] && lastKeyState[button];
|
||||
}
|
||||
|
||||
bool GetButtonDown(const InputKey::Keyboard::Key button) const
|
||||
[[nodiscard]] bool GetButtonDown(const InputKey::Keyboard::Key button) const
|
||||
{
|
||||
return keyState[button] && !lastKeyState[button];
|
||||
}
|
||||
|
||||
IWindow* GetActiveWindow() const
|
||||
[[nodiscard]] IWindow* GetActiveWindow() const
|
||||
{
|
||||
return lastWindow;
|
||||
}
|
||||
|
||||
61
openVulkanoCpp/Input/InputDeviceMouse.cpp
Normal file
61
openVulkanoCpp/Input/InputDeviceMouse.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 "InputDeviceMouse.hpp"
|
||||
#include "../Base/Logger.hpp"
|
||||
|
||||
namespace openVulkanoCpp::Input
|
||||
{
|
||||
void InputDeviceMouse::Init(const int index, const std::string& name)
|
||||
{
|
||||
InputDevice::Init(InputDeviceType::MOUSE, index, name);
|
||||
|
||||
ClearAxes();
|
||||
pressedButtons = 0;
|
||||
lastPressedButtons = 0;
|
||||
mousePosX = 0;
|
||||
mousePosY = 0;
|
||||
lastWindow = nullptr;
|
||||
}
|
||||
|
||||
void InputDeviceMouse::UpdateButtons(uint8_t newPressedButtons)
|
||||
{
|
||||
lastPressedButtons = pressedButtons;
|
||||
pressedButtons = newPressedButtons;
|
||||
if (lastPressedButtons != pressedButtons)
|
||||
Logger::INPUT->debug("Mouse button state changed {0:08b}", pressedButtons);
|
||||
}
|
||||
|
||||
void InputDeviceMouse::UpdatePosition(double posX, double posY)
|
||||
{
|
||||
axes[InputKey::Mouse::AXIS_X] = static_cast<float>(posX - mousePosX);
|
||||
axes[InputKey::Mouse::AXIS_Y] = static_cast<float>(posY - mousePosY);
|
||||
mousePosX = posX;
|
||||
mousePosY = posY;
|
||||
Logger::INPUT->debug("Mouse moved posX: {0} posY: {1}, relativeX: {2}, relativeY: {3}", posX, posY, axes[InputKey::Mouse::AXIS_X], axes[InputKey::Mouse::AXIS_Y]);
|
||||
}
|
||||
|
||||
void InputDeviceMouse::UpdateWheel(float wheelX, float wheelY)
|
||||
{
|
||||
axes[InputKey::Mouse::AXIS_WHEEL_X] = wheelX;
|
||||
axes[InputKey::Mouse::AXIS_WHEEL_Y] = wheelY;
|
||||
Logger::INPUT->debug("Mouse scrolled x: {0} y: {1}", wheelX, wheelY);
|
||||
}
|
||||
|
||||
void InputDeviceMouse::UpdateActiveWindow(IWindow* window)
|
||||
{
|
||||
ClearAxes();
|
||||
lastWindow = window;
|
||||
}
|
||||
|
||||
void InputDeviceMouse::ClearAxes()
|
||||
{
|
||||
for (float& axis : axes)
|
||||
{
|
||||
axis = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,13 @@
|
||||
/*
|
||||
* 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 "InputDevice.hpp"
|
||||
#include "../Base/Logger.hpp"
|
||||
#include "glm/vec2.hpp"
|
||||
|
||||
namespace openVulkanoCpp
|
||||
{
|
||||
@@ -18,120 +25,82 @@ namespace openVulkanoCpp
|
||||
protected:
|
||||
InputDeviceMouse() = default;
|
||||
|
||||
void Init(const int index, const std::string& name)
|
||||
{
|
||||
InputDevice::Init(InputDeviceType::MOUSE, index, name);
|
||||
|
||||
ClearAxes();
|
||||
pressedButtons = 0;
|
||||
lastPressedButtons = 0;
|
||||
mousePosX = 0;
|
||||
mousePosY = 0;
|
||||
lastWindow = nullptr;
|
||||
}
|
||||
void Init(int index, const std::string& name);
|
||||
|
||||
void UpdateButtons(uint8_t newPressedButtons)
|
||||
{
|
||||
lastPressedButtons = pressedButtons;
|
||||
pressedButtons = newPressedButtons;
|
||||
if (lastPressedButtons != pressedButtons)
|
||||
Logger::INPUT->debug("Mouse button state changed {0:08b}", pressedButtons);
|
||||
}
|
||||
void UpdateButtons(uint8_t newPressedButtons);
|
||||
|
||||
void UpdatePosition(double posX, double posY)
|
||||
{
|
||||
axes[InputKey::Mouse::AXIS_X] = static_cast<float>(posX - mousePosX);
|
||||
axes[InputKey::Mouse::AXIS_Y] = static_cast<float>(posY - mousePosY);
|
||||
mousePosX = posX;
|
||||
mousePosY = posY;
|
||||
Logger::INPUT->debug("Mouse moved posX: {0} posY: {1}, relativeX: {2}, relativeY: {3}", posX, posY, axes[InputKey::Mouse::AXIS_X], axes[InputKey::Mouse::AXIS_Y]);
|
||||
}
|
||||
void UpdatePosition(double posX, double posY);
|
||||
|
||||
void UpdateWheel(float wheelX, float wheelY)
|
||||
{
|
||||
axes[InputKey::Mouse::AXIS_WHEEL_X] = wheelX;
|
||||
axes[InputKey::Mouse::AXIS_WHEEL_Y] = wheelY;
|
||||
Logger::INPUT->debug("Mouse scrolled x: {0} y: {1}", wheelX, wheelY);
|
||||
}
|
||||
void UpdateWheel(float wheelX, float wheelY);
|
||||
|
||||
void UpdateActiveWindow(IWindow* window)
|
||||
{
|
||||
ClearAxes();
|
||||
lastWindow = window;
|
||||
}
|
||||
void UpdateActiveWindow(IWindow* window);
|
||||
|
||||
void ClearAxes()
|
||||
{
|
||||
for (float& axis : axes)
|
||||
{
|
||||
axis = 0;
|
||||
}
|
||||
}
|
||||
void ClearAxes();
|
||||
|
||||
private:
|
||||
bool GetLastButton(InputKey::Mouse::Button button) const
|
||||
[[nodiscard]] bool GetLastButton(InputKey::Mouse::Button button) const
|
||||
{
|
||||
return lastPressedButtons & (1 << button);
|
||||
}
|
||||
|
||||
protected:
|
||||
float ReadAxis(int16_t key) const override final
|
||||
[[nodiscard]] float ReadAxis(int16_t key) const final
|
||||
{
|
||||
return GetAxis(static_cast<InputKey::Mouse::Axis>(key));
|
||||
}
|
||||
|
||||
bool ReadButton(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButton(int16_t key) const final
|
||||
{
|
||||
return GetButton(static_cast<InputKey::Mouse::Button>(key));
|
||||
}
|
||||
|
||||
bool ReadButtonUp(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButtonUp(int16_t key) const final
|
||||
{
|
||||
return GetButtonUp(static_cast<InputKey::Mouse::Button>(key));
|
||||
}
|
||||
|
||||
bool ReadButtonDown(int16_t key) const override final
|
||||
[[nodiscard]] bool ReadButtonDown(int16_t key) const final
|
||||
{
|
||||
return GetButtonDown(static_cast<InputKey::Mouse::Button>(key));
|
||||
}
|
||||
|
||||
public:
|
||||
bool GetButton(const InputKey::Mouse::Button button) const
|
||||
[[nodiscard]] bool GetButton(const InputKey::Mouse::Button button) const
|
||||
{
|
||||
return pressedButtons & (1 << button);
|
||||
}
|
||||
|
||||
bool GetButtonUp(const InputKey::Mouse::Button button) const
|
||||
[[nodiscard]] bool GetButtonUp(const InputKey::Mouse::Button button) const
|
||||
{
|
||||
return !GetButton(button) && GetLastButton(button);
|
||||
}
|
||||
|
||||
bool GetButtonDown(const InputKey::Mouse::Button button) const
|
||||
[[nodiscard]] bool GetButtonDown(const InputKey::Mouse::Button button) const
|
||||
{
|
||||
return GetButton(button) && !GetLastButton(button);
|
||||
}
|
||||
|
||||
float GetAxis(const InputKey::Mouse::Axis axis) const
|
||||
[[nodiscard]] float GetAxis(const InputKey::Mouse::Axis axis) const
|
||||
{
|
||||
return axes[axis];
|
||||
}
|
||||
|
||||
float GetWheel() const
|
||||
[[nodiscard]] float GetWheel() const
|
||||
{
|
||||
return GetAxis(InputKey::Mouse::Axis::AXIS_WHEEL_Y);
|
||||
}
|
||||
|
||||
float GetWheelX() const
|
||||
[[nodiscard]] float GetWheelX() const
|
||||
{
|
||||
return GetAxis(InputKey::Mouse::Axis::AXIS_WHEEL_X);
|
||||
}
|
||||
|
||||
float GetWheelY() const
|
||||
[[nodiscard]] float GetWheelY() const
|
||||
{
|
||||
return GetAxis(InputKey::Mouse::Axis::AXIS_WHEEL_Y);
|
||||
}
|
||||
|
||||
glm::vec2 GetMousePosition() const
|
||||
[[nodiscard]] glm::vec2 GetMousePosition() const
|
||||
{
|
||||
return { mousePosX, mousePosY };
|
||||
}
|
||||
@@ -146,7 +115,7 @@ namespace openVulkanoCpp
|
||||
{
|
||||
if (window == lastWindow)
|
||||
{
|
||||
return { mousePosX, mousePosY };
|
||||
return GetMousePosition();
|
||||
}
|
||||
return { std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::quiet_NaN() };
|
||||
}
|
||||
@@ -165,7 +134,7 @@ namespace openVulkanoCpp
|
||||
}
|
||||
}
|
||||
|
||||
IWindow* GetActiveWindow() const
|
||||
[[nodiscard]] IWindow* GetActiveWindow() const
|
||||
{
|
||||
return lastWindow;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user