added input system
This commit is contained in:
54
TSE_Core/src/interfaces/ICursorHandler.cpp
Normal file
54
TSE_Core/src/interfaces/ICursorHandler.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "ICursorHandler.hpp"
|
||||
|
||||
#include "IInputManager.hpp"
|
||||
|
||||
TSE::ICursorHandler::~ICursorHandler()
|
||||
{
|
||||
if(enabled()) Disable();
|
||||
}
|
||||
|
||||
bool TSE::ICursorHandler::enabled() const
|
||||
{
|
||||
return Enabled;
|
||||
}
|
||||
|
||||
void TSE::ICursorHandler::Enable()
|
||||
{
|
||||
if(!enabled())
|
||||
{
|
||||
IInputManager::instance()->RegisterCursorHandler(this);
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::ICursorHandler::Disable()
|
||||
{
|
||||
if(enabled())
|
||||
{
|
||||
IInputManager::instance()->UnregisterCursorHandler(this);
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::ICursorHandler::onMouseButtonDown(ICursorHandler *handler, const MouseBtn &btn, const Modifier &mods)
|
||||
{
|
||||
handler->OnMouseButtonDown(btn, mods);
|
||||
}
|
||||
|
||||
void TSE::ICursorHandler::onMouseButtonUp(ICursorHandler *handler, const MouseBtn &btn, const Modifier &mods)
|
||||
{
|
||||
handler->OnMouseButtonUp(btn, mods);
|
||||
}
|
||||
|
||||
void TSE::ICursorHandler::onMouseButtonHold(ICursorHandler *handler, const MouseBtn &btn, const Modifier &mods)
|
||||
{
|
||||
handler->OnMouseButtonHold(btn, mods);
|
||||
}
|
||||
|
||||
void TSE::ICursorHandler::onMousePosition(ICursorHandler *handler, const Vector2 &pos)
|
||||
{
|
||||
handler->OnMousePosition(pos);
|
||||
}
|
||||
|
||||
void TSE::ICursorHandler::onMouseScroll(ICursorHandler *handler, const Vector2 &delta)
|
||||
{
|
||||
handler->OnMouseScroll(delta);
|
||||
}
|
||||
36
TSE_Core/src/interfaces/ICursorHandler.hpp
Normal file
36
TSE_Core/src/interfaces/ICursorHandler.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "enums/InputEnums.hpp"
|
||||
#include "Vector2.hpp"
|
||||
|
||||
namespace TSE
|
||||
{
|
||||
class ICursorHandler
|
||||
{
|
||||
private:
|
||||
bool Enabled = false;
|
||||
|
||||
public:
|
||||
ICursorHandler() = default;
|
||||
~ICursorHandler();
|
||||
|
||||
bool enabled() const;
|
||||
void Enable();
|
||||
void Disable();
|
||||
|
||||
inline virtual void OnMouseButtonDown(const MouseBtn& btn, const Modifier& mods) { };
|
||||
inline virtual void OnMouseButtonUp(const MouseBtn& btn, const Modifier& mods) { };
|
||||
inline virtual void OnMouseButtonHold(const MouseBtn& btn, const Modifier& mods) { };
|
||||
|
||||
inline virtual void OnMousePosition(const Vector2& pos) { };
|
||||
inline virtual void OnMouseScroll(const Vector2& delta) { };
|
||||
|
||||
private:
|
||||
static void onMouseButtonDown( ICursorHandler* handler, const MouseBtn& btn, const Modifier& mods);
|
||||
static void onMouseButtonUp( ICursorHandler* handler, const MouseBtn& btn, const Modifier& mods);
|
||||
static void onMouseButtonHold( ICursorHandler* handler, const MouseBtn& btn, const Modifier& mods);
|
||||
|
||||
static void onMousePosition( ICursorHandler* handler, const Vector2& pos);
|
||||
static void onMouseScroll( ICursorHandler* handler, const Vector2& delta);
|
||||
};
|
||||
} // namespace TSE
|
||||
42
TSE_Core/src/interfaces/IInputManager.cpp
Normal file
42
TSE_Core/src/interfaces/IInputManager.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "IInputManager.hpp"
|
||||
|
||||
TSE::IInputManager* TSE::IInputManager::Instance = nullptr;
|
||||
|
||||
TSE::IInputManager *TSE::IInputManager::instance()
|
||||
{
|
||||
return Instance;
|
||||
}
|
||||
|
||||
void TSE::IInputManager::RegisterCursorHandler(ICursorHandler *handler)
|
||||
{
|
||||
cursorHandler.push_back(handler);
|
||||
}
|
||||
|
||||
void TSE::IInputManager::UnregisterCursorHandler(ICursorHandler *handler)
|
||||
{
|
||||
for(auto it = cursorHandler.begin(); it != cursorHandler.end(); it++)
|
||||
{
|
||||
if(*it == handler)
|
||||
{
|
||||
cursorHandler.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::IInputManager::RegisterKeyHandler(IKeyInputHandler *handler)
|
||||
{
|
||||
keyHandler.push_back(handler);
|
||||
}
|
||||
|
||||
void TSE::IInputManager::UnregisterKeyHandler(IKeyInputHandler *handler)
|
||||
{
|
||||
for(auto it = keyHandler.begin(); it != keyHandler.end(); it++)
|
||||
{
|
||||
if(*it == handler)
|
||||
{
|
||||
keyHandler.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
TSE_Core/src/interfaces/IInputManager.hpp
Normal file
28
TSE_Core/src/interfaces/IInputManager.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "enums/InputEnums.hpp"
|
||||
#include "ICursorHandler.hpp"
|
||||
#include "IKeyInputHandler.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace TSE
|
||||
{
|
||||
class IInputManager
|
||||
{
|
||||
protected:
|
||||
static IInputManager* Instance;
|
||||
std::vector<ICursorHandler*> cursorHandler;
|
||||
std::vector<IKeyInputHandler*> keyHandler;
|
||||
|
||||
public:
|
||||
static IInputManager* instance();
|
||||
|
||||
virtual void Delete() = 0;
|
||||
|
||||
void RegisterCursorHandler(ICursorHandler* handler);
|
||||
void UnregisterCursorHandler(ICursorHandler* handler);
|
||||
|
||||
void RegisterKeyHandler(IKeyInputHandler* handler);
|
||||
void UnregisterKeyHandler(IKeyInputHandler* handler);
|
||||
};
|
||||
} // namespace TSE
|
||||
49
TSE_Core/src/interfaces/IKeyInputHandler.cpp
Normal file
49
TSE_Core/src/interfaces/IKeyInputHandler.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "IKeyInputHandler.hpp"
|
||||
|
||||
#include "IInputManager.hpp"
|
||||
|
||||
TSE::IKeyInputHandler::~IKeyInputHandler()
|
||||
{
|
||||
if(enabled()) Disable();
|
||||
}
|
||||
|
||||
bool TSE::IKeyInputHandler::enabled() const
|
||||
{
|
||||
return Enabled;
|
||||
}
|
||||
|
||||
void TSE::IKeyInputHandler::Enable()
|
||||
{
|
||||
if(!enabled())
|
||||
{
|
||||
IInputManager::instance()->RegisterKeyHandler(this);
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::IKeyInputHandler::Disable()
|
||||
{
|
||||
if(enabled())
|
||||
{
|
||||
IInputManager::instance()->UnregisterKeyHandler(this);
|
||||
}
|
||||
}
|
||||
|
||||
void TSE::IKeyInputHandler::onKeyDown(IKeyInputHandler *handler, const Key &key, const Modifier &mods)
|
||||
{
|
||||
handler->OnKeyDown(key, mods);
|
||||
}
|
||||
|
||||
void TSE::IKeyInputHandler::onKeyUp(IKeyInputHandler *handler, const Key &key, const Modifier &mods)
|
||||
{
|
||||
handler->OnKeyUp(key, mods);
|
||||
}
|
||||
|
||||
void TSE::IKeyInputHandler::onKeyHold(IKeyInputHandler *handler, const Key &key, const Modifier &mods)
|
||||
{
|
||||
handler->OnKeyHold(key, mods);
|
||||
}
|
||||
|
||||
void TSE::IKeyInputHandler::onChar(IKeyInputHandler *handler, const std::string &c)
|
||||
{
|
||||
handler->OnChar(c);
|
||||
}
|
||||
32
TSE_Core/src/interfaces/IKeyInputHandler.hpp
Normal file
32
TSE_Core/src/interfaces/IKeyInputHandler.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "enums/InputEnums.hpp"
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace TSE
|
||||
{
|
||||
class IKeyInputHandler
|
||||
{
|
||||
private:
|
||||
bool Enabled = false;
|
||||
|
||||
public:
|
||||
IKeyInputHandler() = default;
|
||||
~IKeyInputHandler();
|
||||
|
||||
bool enabled() const;
|
||||
void Enable();
|
||||
void Disable();
|
||||
|
||||
inline virtual void OnKeyDown(const Key& key, const Modifier& mods) { };
|
||||
inline virtual void OnKeyUp(const Key& key, const Modifier& mods) { };
|
||||
inline virtual void OnKeyHold(const Key& key, const Modifier& mods) { };
|
||||
inline virtual void OnChar(const std::string& c) { };
|
||||
|
||||
private:
|
||||
static void onKeyDown(IKeyInputHandler* handler, const Key& key, const Modifier& mods);
|
||||
static void onKeyUp(IKeyInputHandler* handler, const Key& key, const Modifier& mods);
|
||||
static void onKeyHold(IKeyInputHandler* handler, const Key& key, const Modifier& mods);
|
||||
static void onChar(IKeyInputHandler* handler, const std::string& c);
|
||||
};
|
||||
} // namespace TSE
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "LuaStateHandler.hpp"
|
||||
#include "Time.hpp"
|
||||
#include "version.h"
|
||||
#include "IInputManager.hpp"
|
||||
|
||||
TSE::IWindow* TSE::IWindow::lastWindow = nullptr;
|
||||
|
||||
@@ -21,5 +22,7 @@ void TSE::IWindow::BaseUpdate() const
|
||||
|
||||
TSE::IWindow::~IWindow()
|
||||
{
|
||||
IInputManager::instance()->Delete();
|
||||
Time::Destroy();
|
||||
Debug::Close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user