First commit.
This commit is contained in:
160
src/IO/HID/Input.cpp
Normal file
160
src/IO/HID/Input.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#include "../../../include/IO/HID/Input.h"
|
||||
|
||||
namespace lwe
|
||||
{
|
||||
Input::~Input()
|
||||
{
|
||||
for (UInt_64 i = 0; i < handlers.Size(); i++)
|
||||
delete handlers[i];
|
||||
}
|
||||
|
||||
Input::Input()
|
||||
: initalized(false)
|
||||
{
|
||||
}
|
||||
|
||||
Input::Input(Input&& input) noexcept
|
||||
: handlers((Array<InputHandler*>&&)input.handlers), initalized(input.initalized)
|
||||
{
|
||||
input.initalized = false;
|
||||
}
|
||||
|
||||
Input::Input(const Input& input)
|
||||
: initalized(false)
|
||||
{
|
||||
}
|
||||
|
||||
Input& Input::operator=(Input&& input) noexcept
|
||||
{
|
||||
if (this == &input)
|
||||
return *this;
|
||||
|
||||
handlers = (Array<InputHandler*>&&)input.handlers;
|
||||
initalized = input.initalized;
|
||||
|
||||
input.initalized = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Input& Input::operator=(const Input& input)
|
||||
{
|
||||
if (this == &input)
|
||||
return *this;
|
||||
|
||||
for (UInt_64 i = 0; i < handlers.Size(); i++)
|
||||
delete handlers;
|
||||
|
||||
handlers = Array<InputHandler*>();
|
||||
initalized = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Input::Initialize()
|
||||
{
|
||||
if (initalized)
|
||||
return;
|
||||
|
||||
UInt_64 i = 0;
|
||||
while (i < handlers.Size())
|
||||
{
|
||||
if (!handlers[i]->Initialize())
|
||||
{
|
||||
if (i != handlers.Size() - 1)
|
||||
handlers.Swap(i, handlers.End());
|
||||
|
||||
delete handlers.Pop();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
initalized = true;
|
||||
}
|
||||
|
||||
void Input::Release()
|
||||
{
|
||||
if (!initalized)
|
||||
return;
|
||||
|
||||
UInt_64 i = 0;
|
||||
while (i < handlers.Size())
|
||||
{
|
||||
if (!handlers[i]->Release())
|
||||
{
|
||||
if (i != handlers.Size() - 1)
|
||||
handlers.Swap(i, handlers.End());
|
||||
|
||||
delete handlers.Pop();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
initalized = false;
|
||||
}
|
||||
|
||||
void Input::Poll()
|
||||
{
|
||||
for (UInt_64 i = 0; i < handlers.Size(); i++)
|
||||
handlers[i]->Poll();
|
||||
}
|
||||
|
||||
bool Input::HasHandler(const UInt_64 hashId) const
|
||||
{
|
||||
for (UInt_64 i = 0; i < handlers.Size(); i++)
|
||||
if (*handlers[i] == hashId)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Input::HasHandler(const Str_8& id) const
|
||||
{
|
||||
return HasHandler(id.Hash_64());
|
||||
}
|
||||
|
||||
bool Input::AddHandler(InputHandler* handler)
|
||||
{
|
||||
if (HasHandler(handler->GetHashId()))
|
||||
return false;
|
||||
|
||||
if (initalized)
|
||||
{
|
||||
bool hInitialized = handler->Initialize();
|
||||
if (!hInitialized)
|
||||
{
|
||||
delete handler;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
handlers.Push(handler);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const InputHandler* Input::GetHandler(const UInt_64 hashId) const
|
||||
{
|
||||
for (UInt_64 i = 0; i < handlers.Size(); i++)
|
||||
if (*handlers[i] == hashId)
|
||||
return handlers[i];
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const InputHandler* Input::GetHandler(const Str_8& id) const
|
||||
{
|
||||
return GetHandler(id.Hash_64());
|
||||
}
|
||||
|
||||
bool Input::IsInitialized() const
|
||||
{
|
||||
return initalized;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user