2014-02-14 15:50:00 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Created by [Dennis Andersen] [2013]
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
#include "..\Include\Keyboard.h"
|
|
|
|
|
|
|
|
using namespace Input;
|
|
|
|
using namespace Input::Enum;
|
|
|
|
using namespace Input::Typedefs;
|
|
|
|
using namespace Input::Struct;
|
|
|
|
|
2014-02-21 11:43:05 +01:00
|
|
|
struct Keyboard::KeyboardCallbackList
|
|
|
|
{
|
|
|
|
enum CallbackDataType
|
|
|
|
{
|
2014-02-25 14:20:11 +01:00
|
|
|
CallbackDataType_OnEvent,
|
2014-02-21 11:43:05 +01:00
|
|
|
CallbackDataType_OnPress,
|
|
|
|
CallbackDataType_OnDown,
|
|
|
|
CallbackDataType_OnRelease
|
|
|
|
} type;
|
|
|
|
union CallbackData
|
|
|
|
{
|
2014-02-25 14:20:11 +01:00
|
|
|
Typedefs::OnKeyEventCallback keyEventCallback;
|
2014-02-21 11:43:05 +01:00
|
|
|
Typedefs::OnKeyPressCallback keyPressCallback;
|
|
|
|
Typedefs::OnKeyDownCallback keyDownCallback;
|
|
|
|
Typedefs::OnKeyReleaseCallback keyReleaseCallback;
|
2014-02-14 15:50:00 +01:00
|
|
|
|
2014-02-21 11:43:05 +01:00
|
|
|
CallbackData (){ memset(this, 0, sizeof(CallbackData)); }
|
|
|
|
CallbackData (Typedefs::OnKeyPressCallback o){ keyPressCallback = o; }
|
|
|
|
bool operator ==(CallbackData o){ return o.keyDownCallback == keyDownCallback; }
|
|
|
|
bool operator ==(Typedefs::OnKeyPressCallback o ){ return o == keyPressCallback; }
|
|
|
|
operator bool(){ return this->keyDownCallback != 0; }
|
|
|
|
} function;
|
|
|
|
KeyboardCallbackList *next;
|
2014-02-25 14:20:11 +01:00
|
|
|
void* tag;
|
|
|
|
KeyboardCallbackList(CallbackData func, CallbackDataType t, void* ct) :function(func), next(0), type(t), tag(ct) { }
|
2014-02-21 11:43:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void ClearList(Keyboard::KeyboardCallbackList* first)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
Keyboard::KeyboardCallbackList* w = first;
|
|
|
|
Keyboard::KeyboardCallbackList* removee = 0;
|
2014-02-14 15:50:00 +01:00
|
|
|
|
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
removee = w;
|
|
|
|
w = w->next;
|
|
|
|
delete removee;
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 14:20:11 +01:00
|
|
|
void AddToList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data, Keyboard::KeyboardCallbackList::CallbackDataType type, void* tag)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
Keyboard::KeyboardCallbackList *w = first;
|
|
|
|
Keyboard::KeyboardCallbackList *prev = first;
|
2014-02-14 15:50:00 +01:00
|
|
|
while (w)
|
|
|
|
{ prev = w; w = w->next; }
|
|
|
|
|
2014-02-21 11:43:05 +01:00
|
|
|
Keyboard::KeyboardCallbackList::CallbackData f;
|
2014-02-14 15:50:00 +01:00
|
|
|
f = data;
|
|
|
|
|
2014-02-25 14:20:11 +01:00
|
|
|
prev->next = new Keyboard::KeyboardCallbackList(f, type, tag);
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
2014-02-21 11:43:05 +01:00
|
|
|
void RemoveFromList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
Keyboard::KeyboardCallbackList *w = first;
|
|
|
|
Keyboard::KeyboardCallbackList *prev = first;
|
2014-02-14 15:50:00 +01:00
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
if(data == w->function)
|
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
Keyboard::KeyboardCallbackList *removee = w;
|
2014-02-14 15:50:00 +01:00
|
|
|
w = w->next;
|
|
|
|
prev->next = w;
|
|
|
|
delete removee;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = w;
|
|
|
|
w = w->next;
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 11:43:05 +01:00
|
|
|
bool ExistsInList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
2014-02-21 11:43:05 +01:00
|
|
|
Keyboard::KeyboardCallbackList *w = first;
|
2014-02-14 15:50:00 +01:00
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
if(data == w->function)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
w = w->next;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-21 11:43:05 +01:00
|
|
|
bool ExistsInList(std::vector<Keyboard::KeyboardEvent*>& list, Keyboard::KeyboardEvent* data)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < list.size(); i++)
|
|
|
|
{
|
|
|
|
if(list[i] == data)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-21 11:43:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-14 15:50:00 +01:00
|
|
|
Keyboard::Keyboard()
|
|
|
|
: InputObject(SAIType_Keyboard)
|
|
|
|
, callbackList(0)
|
2014-02-21 11:43:05 +01:00
|
|
|
, active(1)
|
|
|
|
, textTarget(0)
|
|
|
|
, writePos(0)
|
2014-02-14 15:50:00 +01:00
|
|
|
{}
|
|
|
|
Keyboard::~Keyboard()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-25 14:20:11 +01:00
|
|
|
void Keyboard::InternalOnEvent(Struct::KeyboardEventData& data)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
|
|
|
|
{
|
|
|
|
if(this->keyEventSubscrivers[i])
|
|
|
|
{
|
|
|
|
this->keyEventSubscrivers[i]->OnKeyEvent(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KeyboardCallbackList *w = this->callbackList;
|
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
if(w->function)
|
|
|
|
if (w->type == KeyboardCallbackList::CallbackDataType_OnEvent)
|
|
|
|
{
|
|
|
|
data.tag = w->tag;
|
|
|
|
w->function.keyEventCallback(data);
|
|
|
|
}
|
|
|
|
w = w->next;
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 16:01:06 +01:00
|
|
|
void Keyboard::InternalOnKeyPress(Enum::SAKI key)
|
2014-02-21 11:43:05 +01:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
|
|
|
|
{
|
|
|
|
if(this->keyEventSubscrivers[i])
|
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
this->keyEventSubscrivers[i]->OnKeyPress(key, this);
|
2014-02-21 11:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
KeyboardCallbackList *w = this->callbackList;
|
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
if(w->function)
|
|
|
|
if (w->type == KeyboardCallbackList::CallbackDataType_OnPress)
|
2014-02-25 14:20:11 +01:00
|
|
|
{
|
|
|
|
w->function.keyPressCallback(key, this, w->tag);
|
|
|
|
}
|
2014-02-21 11:43:05 +01:00
|
|
|
w = w->next;
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 16:01:06 +01:00
|
|
|
void Keyboard::InternalOnKeyDown(Enum::SAKI key)
|
2014-02-21 11:43:05 +01:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
|
|
|
|
{
|
|
|
|
if(this->keyEventSubscrivers[i])
|
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
this->keyEventSubscrivers[i]->OnKeyDown(key, this);
|
2014-02-21 11:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
KeyboardCallbackList *w = this->callbackList;
|
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
if(w->function)
|
|
|
|
if (w->type == KeyboardCallbackList::CallbackDataType_OnDown)
|
2014-02-25 14:20:11 +01:00
|
|
|
w->function.keyDownCallback(key, this, w->tag);
|
2014-02-21 11:43:05 +01:00
|
|
|
w = w->next;
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 16:01:06 +01:00
|
|
|
void Keyboard::InternalOnKeyRelease(Enum::SAKI key)
|
2014-02-21 11:43:05 +01:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
|
|
|
|
{
|
|
|
|
if(this->keyEventSubscrivers[i])
|
|
|
|
{
|
2014-02-24 16:01:06 +01:00
|
|
|
this->keyEventSubscrivers[i]->OnKeyRelease(key, this);
|
2014-02-21 11:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
KeyboardCallbackList *w = this->callbackList;
|
|
|
|
while (w)
|
|
|
|
{
|
|
|
|
if(w->function)
|
|
|
|
if (w->type == KeyboardCallbackList::CallbackDataType_OnRelease)
|
2014-02-25 14:20:11 +01:00
|
|
|
w->function.keyReleaseCallback(key, this, w->tag);
|
2014-02-21 11:43:05 +01:00
|
|
|
w = w->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 14:20:11 +01:00
|
|
|
void Keyboard::AddKeyboardEvent(KeyboardEvent* object)
|
|
|
|
{
|
|
|
|
if(ExistsInList(this->keyEventSubscrivers, object)) return;
|
|
|
|
|
|
|
|
this->keyEventSubscrivers.push_back(object);
|
|
|
|
}
|
|
|
|
void Keyboard::RemoveKeyboardEvent(KeyboardEvent* object)
|
|
|
|
{
|
|
|
|
int i = -1;
|
|
|
|
if((i = ExistsInList(this->keyEventSubscrivers, object)))
|
|
|
|
{
|
|
|
|
std::swap(this->keyEventSubscrivers[i], this->keyEventSubscrivers[this->keyEventSubscrivers.size() - 1]);
|
|
|
|
this->keyEventSubscrivers.resize(this->keyEventSubscrivers.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Keyboard::operator+= (KeyboardEvent* object)
|
|
|
|
{
|
|
|
|
if(ExistsInList(this->keyEventSubscrivers, object)) return;
|
|
|
|
|
|
|
|
this->keyEventSubscrivers.push_back(object);
|
|
|
|
}
|
|
|
|
void Keyboard::operator-= (KeyboardEvent* object)
|
|
|
|
{
|
|
|
|
int i = -1;
|
|
|
|
if((i = ExistsInList(this->keyEventSubscrivers, object)))
|
|
|
|
{
|
|
|
|
std::swap(this->keyEventSubscrivers[i], this->keyEventSubscrivers[this->keyEventSubscrivers.size() - 1]);
|
|
|
|
this->keyEventSubscrivers.resize(this->keyEventSubscrivers.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Keyboard::AddOnKeyEventCallback (OnKeyEventCallback func, void* tag)
|
|
|
|
{
|
|
|
|
KeyboardCallbackList::CallbackData d;
|
|
|
|
d.keyEventCallback = func;
|
|
|
|
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnEvent, tag);
|
|
|
|
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnEvent, tag);
|
|
|
|
}
|
|
|
|
void Keyboard::AddOnKeyPressCallback (OnKeyPressCallback func, void* tag)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
|
|
|
KeyboardCallbackList::CallbackData d;
|
|
|
|
d.keyPressCallback = func;
|
2014-02-25 14:20:11 +01:00
|
|
|
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnPress, tag);
|
|
|
|
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnPress, tag);
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
2014-02-25 14:20:11 +01:00
|
|
|
void Keyboard::AddOnKeyDownCallback (OnKeyDownCallback func, void* tag)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
|
|
|
KeyboardCallbackList::CallbackData d;
|
|
|
|
d.keyDownCallback = func;
|
2014-02-25 14:20:11 +01:00
|
|
|
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnDown, tag);
|
|
|
|
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnDown, tag);
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
2014-02-25 14:20:11 +01:00
|
|
|
void Keyboard::AddOnKeyReleaseCallback (OnKeyReleaseCallback func, void* tag)
|
2014-02-14 15:50:00 +01:00
|
|
|
{
|
|
|
|
KeyboardCallbackList::CallbackData d;
|
|
|
|
d.keyReleaseCallback = func;
|
2014-02-25 14:20:11 +01:00
|
|
|
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnRelease, tag);
|
|
|
|
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnRelease, tag);
|
2014-02-14 15:50:00 +01:00
|
|
|
}
|
|
|
|
|
2014-02-25 14:20:11 +01:00
|
|
|
void Keyboard::RemoveOnKeyEventCallback (OnKeyEventCallback func)
|
|
|
|
{
|
|
|
|
Keyboard::KeyboardCallbackList::CallbackData temp;
|
|
|
|
temp.keyEventCallback = func;
|
|
|
|
RemoveFromList(this->callbackList, temp);
|
|
|
|
}
|
2014-02-14 15:50:00 +01:00
|
|
|
void Keyboard::RemoveOnKeyPressCallback (OnKeyPressCallback func)
|
|
|
|
{
|
|
|
|
RemoveFromList(this->callbackList, func);
|
|
|
|
}
|
|
|
|
void Keyboard::RemoveOnKeyDownCallback (OnKeyDownCallback func)
|
|
|
|
{
|
|
|
|
RemoveFromList(this->callbackList, func);
|
|
|
|
}
|
|
|
|
void Keyboard::RemoveOnKeyReleaseCallback (OnKeyReleaseCallback func)
|
|
|
|
{
|
|
|
|
RemoveFromList(this->callbackList, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-21 11:43:05 +01:00
|
|
|
void Keyboard::BindTextTarget( ::std::wstring *field )
|
|
|
|
{
|
|
|
|
this->textTarget = field;
|
|
|
|
|
|
|
|
if( field )
|
|
|
|
{
|
|
|
|
this->writePos = field->size();
|
|
|
|
}
|
2014-02-25 01:05:37 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this->writePos = 0;
|
|
|
|
}
|
2014-02-21 11:43:05 +01:00
|
|
|
}
|
|
|
|
void Keyboard::ReleaseTextTarget( )
|
|
|
|
{
|
|
|
|
this->BindTextTarget( nullptr );
|
|
|
|
}
|
|
|
|
|