Optimized Button class. Added IsPressed and GetPressed methods.

This commit is contained in:
2024-08-04 00:47:38 -07:00
parent 3970b8d402
commit 8d4420528a
14 changed files with 240 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
#include "ehs/io/hid/InputHandler.h"
#include "ehs/system/CPU.h"
namespace ehs
{
InputHandler::~InputHandler()
@@ -9,23 +11,24 @@ namespace ehs
}
InputHandler::InputHandler()
: hashId(0)
: hashId(0), start(0)
{
}
InputHandler::InputHandler(Str_8 id)
: hashId(id.Hash_64()), id((Str_8&&)id)
: hashId(id.Hash_64()), id((Str_8&&)id), start(CPU::GetTSC())
{
}
InputHandler::InputHandler(InputHandler&& ih) noexcept
: hashId(ih.hashId), id((Str_8&&)ih.id), devices((Array<HID*>&&)ih.devices)
: hashId(ih.hashId), id((Str_8&&)ih.id), devices((Array<HID*>&&)ih.devices), start(ih.start)
{
ih.hashId = 0;
ih.start = 0;
}
InputHandler::InputHandler(const InputHandler& ih)
: hashId(ih.hashId), id(ih.id), devices(ih.devices.Size())
: hashId(ih.hashId), id(ih.id), devices(ih.devices.Size()), start(ih.start)
{
for (UInt_64 i = 0; i < devices.Size(); i++)
devices[i] = ih.devices[i]->Clone();
@@ -39,8 +42,10 @@ namespace ehs
hashId = ih.hashId;
id = (Str_8&&)ih.id;
devices = (Array<HID*>&&)ih.devices;
start = ih.start;
ih.hashId = 0;
ih.start = 0;
return *this;
}
@@ -56,6 +61,7 @@ namespace ehs
hashId = ih.hashId;
id = ih.id;
devices = Array<HID*>(ih.devices.Size());
start = ih.start;
for (UInt_64 i = 0; i < devices.Size(); i++)
devices[i] = ih.devices[i]->Clone();
@@ -63,12 +69,12 @@ namespace ehs
return *this;
}
bool InputHandler::operator==(const UInt_64 otherHashId)
bool InputHandler::operator==(const UInt_64 otherHashId) const
{
return hashId == otherHashId;
}
bool InputHandler::operator!=(const UInt_64 otherHashId)
bool InputHandler::operator!=(const UInt_64 otherHashId) const
{
return hashId != otherHashId;
}
@@ -96,8 +102,14 @@ namespace ehs
void InputHandler::Poll()
{
static UInt_64 freq = CPU::GetTSC_Freq();
const UInt_64 newTSC = CPU::GetTSC();
delta = newTSC - start;
start = newTSC;
for (UInt_64 i = 0; i < devices.Size(); i++)
devices[i]->Poll();
devices[i]->Poll((float)delta / (float)freq);
}
UInt_64 InputHandler::GetHashId() const
@@ -157,4 +169,4 @@ namespace ehs
{
return false;
}
}
}