Refactor some code

This commit is contained in:
2020-08-06 20:48:17 +02:00
parent 3673bd0d48
commit 0a693b72dd
14 changed files with 976 additions and 745 deletions

View File

@@ -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;
}