Skip to content

Commit

Permalink
Refactor input system
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimvinokurov committed Aug 21, 2022
1 parent 12c01d4 commit c4a56f7
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 149 deletions.
4 changes: 2 additions & 2 deletions Sources/Components/CameraComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ Matrix4 CameraComponent::getViewMatrix() const

void CameraComponent::moveAlongDirection(float amount)
{
transform.position += direction_ * amount * -0.1f;
transform.position += direction_ * amount * -1.1f;
}

void CameraComponent::moveAlongSide(float amount)
{
transform.position += right_ * amount * 0.1f;
transform.position += right_ * amount * 1.1f;
}

Matrix4 CameraComponent::perspective(float fov, float aspect, float n, float f)
Expand Down
53 changes: 24 additions & 29 deletions Sources/Components/InputComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

#include "Core/Objects/Component.h"
#include "Core/Delegate.h"
#include "Input/Keystate.h"
#include "Input/KeyboardKey.h"
#include "Input/MouseKey.h"
#include "Input/InputKeyState.h"
#include "Input/InputKey.h"
#include <unordered_map>

class InputComponents : public Component
Expand All @@ -25,8 +24,6 @@ class InputComponents : public Component

keysAxis_[VE_MOUSE_X] = {"Turn", 1.0f};
keysAxis_[VE_MOUSE_Y] = {"LookUp", 1.0f};

keysAction_[VE_MOUSE_BUTTON_RIGHT] = "CameraLock";
}

template <class T, class M>
Expand All @@ -36,48 +33,46 @@ class InputComponents : public Component
}

template <class T, class M>
void bindAction(const std::string &name, KeyState keyState, T *objects, M method)
void bindAction(const std::string &name, InputKeyState keyState, T *objects, M method)
{
if (keyState == KeyState::PRESSED)
if (keyState == InputKeyState::PRESSED)
{
pressDelegates_[name].connect(objects, method);
}
else if (keyState == KeyState::RELEASE)
else if (keyState == InputKeyState::RELEASE)
{
releaseDelegates_[name].connect(objects, method);
}
}

void input(uint32 keyCode, KeyState keyState)
void inputPressAction(uint32 keyCode)
{
auto actionIt = keysAction_.find(keyCode);
if(actionIt != keysAction_.end()){
if(keyState == KeyState::PRESSED) {
pressDelegates_[actionIt->second].call();
return;
} else if (keyState == KeyState::RELEASE) {
releaseDelegates_[actionIt->second].call();
return;
}
auto actionKeyIt = keysAction_.find(keyCode);
if (actionKeyIt == keysAction_.end())
{
return;
}
pressDelegates_[actionKeyIt->second].call();
}

auto axisIt = keysAxis_.find(keyCode);
if(axisIt != keysAxis_.end() && keyState == KeyState::REPEATE){
axisDelegates_[axisIt->second.first].call(axisIt->second.second);
void inputReleaseAction(uint32 keyCode)
{
auto actionKeyIt = keysAction_.find(keyCode);
if (actionKeyIt == keysAction_.end())
{
return;
}
releaseDelegates_[actionKeyIt->second].call();
}

void inputMouse(float xmouse, float ymouse)
void inputAxis(uint32 keyCode, float value)
{
auto axisXIt = keysAxis_.find(VE_MOUSE_X);
if(axisXIt != keysAxis_.end()){
axisDelegates_[axisXIt->second.first].call(xmouse * axisXIt->second.second);
}
auto axisYIt = keysAxis_.find(VE_MOUSE_Y);
if(axisYIt != keysAxis_.end()){
axisDelegates_[axisYIt->second.first].call(ymouse * axisYIt->second.second);
auto axisKeyIt = keysAxis_.find(keyCode);
if (axisKeyIt == keysAxis_.end())
{
return;
}
axisDelegates_[axisKeyIt->second.first].call(value * axisKeyIt->second.second);
}

private:
Expand Down
8 changes: 4 additions & 4 deletions Sources/Core/VEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ VEngine::~VEngine()
void VEngine::onCreate()
{
ControllerSystem *controllerSystem = systemManager->addSystem<ControllerSystem>(System::HIGHEST_SYSTEM_PRIORITY);
window_->onKeyboardKeyDelegate.connect(controllerSystem, &ControllerSystem::onKeyboardKey);
window_->onMouseKeyDelegate.connect(controllerSystem, &ControllerSystem::onMouseKey);
window_->onMousePositionDelegate.connect(controllerSystem, &ControllerSystem::onMousePosition);
window_->onMouseDeltaPositionDelegate.connect(controllerSystem, &ControllerSystem::onMouseDeltaPosition);
window_->onKeyboardKeyDelegate.connect(controllerSystem, &ControllerSystem::onInputKey);
window_->onMouseKeyDelegate.connect(controllerSystem, &ControllerSystem::onInputKey);
window_->onCursorPositionDelegate.connect(controllerSystem, &ControllerSystem::onCursorPosition);
window_->onCursorDeltaDelegate.connect(controllerSystem, &ControllerSystem::onCursorDelta);

RenderSystem *renderSystem = systemManager->addSystem<RenderSystem>(System::LOWEST_SYSTEM_PRIORITY);
window_->onWindowResizeDelegate.connect(renderSystem, &RenderSystem::onWindowResize);
Expand Down
5 changes: 2 additions & 3 deletions Sources/Core/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ void Window::glfwCallbackInitialization()

glfwSetCursorPosCallback(window, [](GLFWwindow *window, double xpos, double ypos) {
auto this_ptr = static_cast<Window *>(glfwGetWindowUserPointer(window));
this_ptr->onMousePositionDelegate.call(static_cast<float>(xpos), static_cast<float>(ypos));
this_ptr->onCursorPositionDelegate.call(static_cast<float>(xpos), static_cast<float>(ypos));
if(this_ptr->mouseLockFlag) {
double cxpos, cypos;
glfwGetCursorPos(window, &cxpos, &cypos);
this_ptr->onMouseDeltaPositionDelegate.call(cxpos - this_ptr->lockXpos, cypos - this_ptr->lockYpos);
this_ptr->onCursorDeltaDelegate.call(cxpos - this_ptr->lockXpos, cypos - this_ptr->lockYpos);
glfwSetCursorPos(window, this_ptr->lockXpos, this_ptr->lockYpos);
}

});
}
void Window::setTitle(const std::string &title)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class Window
Delegate<int32, int32> onWindowResizeDelegate;
Delegate<int32, int32> onKeyboardKeyDelegate;
Delegate<int32, int32> onMouseKeyDelegate;
Delegate<float, float> onMousePositionDelegate;
Delegate<float, float> onMouseDeltaPositionDelegate;
Delegate<float, float> onCursorPositionDelegate;
Delegate<float, float> onCursorDeltaDelegate;

~Window();

Expand Down
19 changes: 19 additions & 0 deletions Sources/Input/KeyboardKey.h → Sources/Input/InputKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@
#define VE_KEYBOARDKEY_HPP


using InputKey = uint32;

/* The unknown key */
#define VE_KEY_UNKNOWN -1

/* The unknown key */
#define VE_MOUSE_BUTTON_1 0
#define VE_MOUSE_BUTTON_2 1
#define VE_MOUSE_BUTTON_3 2
#define VE_MOUSE_BUTTON_4 3
#define VE_MOUSE_BUTTON_5 4
#define VE_MOUSE_BUTTON_6 5
#define VE_MOUSE_BUTTON_7 6
#define VE_MOUSE_BUTTON_8 7
#define VE_MOUSE_BUTTON_LAST VE_MOUSE_BUTTON_8
#define VE_MOUSE_BUTTON_LEFT VE_MOUSE_BUTTON_1
#define VE_MOUSE_BUTTON_RIGHT VE_MOUSE_BUTTON_2
#define VE_MOUSE_BUTTON_MIDDLE VE_MOUSE_BUTTON_3
#define VE_MOUSE_X 8
#define VE_MOUSE_Y 9

/* Printable keys */
#define VE_KEY_SPACE 32
#define VE_KEY_APOSTROPHE 39 /* ' */
Expand Down Expand Up @@ -136,4 +153,6 @@

#define VE_KEY_LAST VE_KEY_MENU



#endif //PHISICALENGINEV1_KEYBOARDKEY_HPP
2 changes: 1 addition & 1 deletion Sources/Input/Keystate.h → Sources/Input/InputKeyState.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define VENGINE3D_VE_KEYSTATE_H


enum class KeyState : uint8 {
enum class InputKeyState : uint8 {
RELEASE = 0, PRESSED = 1, FREE = 2, REPEATE = 3
};

Expand Down
23 changes: 0 additions & 23 deletions Sources/Input/MouseKey.h

This file was deleted.

100 changes: 32 additions & 68 deletions Sources/Systems/ControllerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,8 @@

ControllerSystem::ControllerSystem(SystemPriority priority) : System(priority)
{
for (KeyState &key : keyboardState)
{
key = KeyState::FREE;
}
for (bool &r : keyboardRepeatStatus)
{
r = false;
}
for (KeyState &key : mouseState)
{
key = KeyState::FREE;
}
for (bool &r : mouseRepeatStatus)
{
r = false;
}
}

void ControllerSystem::update(float dt)
{
auto [inputComponentIt, end] = getWorld()->getComponents<InputComponents>();
Expand All @@ -33,78 +18,57 @@ void ControllerSystem::update(float dt)
return;
}

for (uint32 key = 0; key < keyboardState.size(); ++key)
for (auto &key : pressedKey)
{
if (keyboardState[key] != KeyState::FREE)
{
inputComponentIt->input(key, keyboardState[key]);
keyboardState[key] = KeyState::FREE;
}
}
for (uint32 key = 0; key < mouseState.size(); ++key)
{
if (mouseState[key] != KeyState::FREE)
{
inputComponentIt->input(key, mouseState[key]);
mouseState[key] = KeyState::FREE;
}
inputComponentIt->inputPressAction(key);
}

for (uint32 key = 0; key < keyboardRepeatStatus.size(); ++key)
for (auto &key : releasedKey)
{
if (keyboardRepeatStatus[key])
{
inputComponentIt->input(key, KeyState::REPEATE);
}
}
for (uint32 key = 0; key < mouseRepeatStatus.size(); ++key)
{
if (mouseRepeatStatus[key])
{
inputComponentIt->input(key, KeyState::REPEATE);
}
inputComponentIt->inputReleaseAction(key);
}

inputComponentIt->inputMouse(deltaxpos, deltaypos);
deltaxpos = deltaypos = 0.0f;
}
pressedKey.clear();
releasedKey.clear();

void ControllerSystem::onKeyboardKey(uint32 key, uint32 action)
{
KeyState state = static_cast<KeyState>(action);
if (state == KeyState::PRESSED)
for (auto &key : repeatingKey)
{
keyboardState[key % 512] = KeyState::PRESSED;
keyboardRepeatStatus[key % 512] = true;
}
else if (state == KeyState::RELEASE)
{
keyboardState[key % 512] = KeyState::RELEASE;
keyboardRepeatStatus[key % 512] = false;
inputComponentIt->inputAxis(key, 1);
}

inputComponentIt->inputAxis(VE_MOUSE_X, cursorDeltaXPos * cursorDeltaScale);
inputComponentIt->inputAxis(VE_MOUSE_Y, cursorDeltaYPos * cursorDeltaScale);
cursorDeltaXPos = cursorDeltaYPos = 0.0f;
}
void ControllerSystem::onMouseKey(uint32 key, uint32 action)

void ControllerSystem::onInputKey(uint32 key, uint32 action)
{
KeyState state = static_cast<KeyState>(action);
if (state == KeyState::PRESSED)
InputKeyState state = static_cast<InputKeyState>(action);
if (state == InputKeyState::PRESSED)
{
mouseState[key % 8] = KeyState::PRESSED;
mouseRepeatStatus[key % 8] = true;
pressedKey.push_back(key);
repeatingKey.push_back(key);
}
else if (state == KeyState::RELEASE)
else if (state == InputKeyState::RELEASE)
{
mouseState[key % 8] = KeyState::RELEASE;
mouseRepeatStatus[key % 8] = false;
releasedKey.push_back(key);
auto it = std::find(repeatingKey.begin(), repeatingKey.end(), key);
if (it != repeatingKey.end() && !repeatingKey.empty())
{
std::swap(*it, repeatingKey.back());
repeatingKey.pop_back();
}
}
}
void ControllerSystem::onMousePosition(float xpos, float ypos)

void ControllerSystem::onCursorPosition(float xpos, float ypos)
{
}
void ControllerSystem::onWindowResize(int32 width, int32 height)
{
}
void ControllerSystem::onMouseDeltaPosition(float dxpos, float dypos)
void ControllerSystem::onCursorDelta(float dxpos, float dypos)
{
deltaxpos = dxpos / mouseScale;
deltaypos = dypos / mouseScale;
cursorDeltaXPos = dxpos;
cursorDeltaYPos = dypos;
}
30 changes: 13 additions & 17 deletions Sources/Systems/ControllerSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@
#define VENGINE3D_CONTROLLERSYSTEM_H

#include "Core/Objects/System.h"
#include "Input/KeyboardKey.h"
#include "Input/Keystate.h"
#include "Input/MouseKey.h"
#include <array>
#include "Input/InputKey.h"
#include "Input/InputKeyState.h"
class ControllerSystem : public System
{
GENERATE_SYSTEM_BODY()
public:
ControllerSystem(SystemPriority priority = NORMAL_SYSTEM_PRIORITY);
void onKeyboardKey(uint32 key, uint32 action);
void onMouseKey(uint32 key, uint32 action);
void onMousePosition(float xpos, float ypos);
void onMouseDeltaPosition(float dxpos, float dypos);
virtual void update(float dt) override;
void onInputKey(uint32 key, uint32 action);
void onWindowResize(int32 width, int32 height);
private:
std::array<KeyState, 512> keyboardState;
std::array<bool, 512> keyboardRepeatStatus;
void onCursorPosition(float xpos, float ypos);
void onCursorDelta(float dxpos, float dypos);

std::array<KeyState, 8> mouseState;
std::array<bool, 8> mouseRepeatStatus;
virtual void update(float dt) override;
private:
std::vector<InputKey> pressedKey;
std::vector<InputKey> releasedKey;
std::vector<InputKey> repeatingKey;
float cursorDeltaXPos = 0.0f;
float cursorDeltaYPos = 0.0f;

constexpr static float mouseScale = 100.0f;
float deltaxpos = 0.0f;
float deltaypos = 0.0f;
static constexpr float cursorDeltaScale = 1 / 100.0f;
};

#endif // VENGINE3D_CONTROLLERSYSTEM_H

0 comments on commit c4a56f7

Please sign in to comment.