Input - Added more functionality, custom tag and all-in-one key callback

This commit is contained in:
dean11 2014-02-25 14:20:11 +01:00
parent 13167064cb
commit 6dc60a2082
5 changed files with 159 additions and 54 deletions

View File

@ -25,6 +25,11 @@ namespace Input
*/ */
static InputManager* CreateInputManager (); static InputManager* CreateInputManager ();
/**
* @return Destroys the default input manager.
*/
static void DestroyInputManager ();
/** /**
* @return Destroys a input manager. * @return Destroys a input manager.
*/ */

View File

@ -135,11 +135,23 @@ namespace Input
}; };
} }
//----------------------------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------------------------------
namespace Struct
{
struct KeyboardEventData
{
Enum::SAKI key;
Enum::ButtonState state;
Keyboard* sender;
void* tag;
};
}
//-----------------------------------------------------------------------------------------------------------------------------
namespace Typedefs namespace Typedefs
{ {
typedef void(*OnKeyPressCallback)(Enum::SAKI key, Keyboard* sender); typedef void(*OnKeyEventCallback) (const Struct::KeyboardEventData& eventData);
typedef void(*OnKeyDownCallback)(Enum::SAKI key, Keyboard* sender); typedef void(*OnKeyPressCallback) (Enum::SAKI key, Keyboard* sender, void* tag);
typedef void(*OnKeyReleaseCallback)(Enum::SAKI key, Keyboard* sender); typedef void(*OnKeyDownCallback) (Enum::SAKI key, Keyboard* sender, void* tag);
typedef void(*OnKeyReleaseCallback) (Enum::SAKI key, Keyboard* sender, void* tag);
} }
//----------------------------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------------------------------
@ -149,9 +161,10 @@ namespace Input
class KeyboardEvent class KeyboardEvent
{ {
public: public:
virtual void OnKeyPress(Enum::SAKI key, Keyboard* sender) { } virtual void OnKeyEvent (const Struct::KeyboardEventData& eventData) { }
virtual void OnKeyDown(Enum::SAKI key, Keyboard* sender) { } virtual void OnKeyPress (Enum::SAKI key, Keyboard* sender) { }
virtual void OnKeyRelease(Enum::SAKI key, Keyboard* sender) { } virtual void OnKeyDown (Enum::SAKI key, Keyboard* sender) { }
virtual void OnKeyRelease (Enum::SAKI key, Keyboard* sender) { }
}; };
public: /* Manual check functions */ public: /* Manual check functions */
@ -166,18 +179,25 @@ namespace Input
virtual void Deactivate () override = 0; virtual void Deactivate () override = 0;
virtual bool IsActive() override = 0; virtual bool IsActive() override = 0;
public: /* global subscribe callback functions */ public: /* object subscribe functions */
void AddOnKeyPressCallback (Typedefs::OnKeyPressCallback func); void AddKeyboardEvent (KeyboardEvent* object);
void AddOnKeyDownCallback (Typedefs::OnKeyDownCallback func); void RemoveKeyboardEvent (KeyboardEvent* object);
void AddOnKeyReleaseCallback (Typedefs::OnKeyReleaseCallback func); void operator+= (KeyboardEvent* object);
void operator-= (KeyboardEvent* object);
void RemoveOnKeyPressCallback (Typedefs::OnKeyPressCallback func); public: /* global subscribe callback functions */
void RemoveOnKeyDownCallback (Typedefs::OnKeyDownCallback func); void AddOnKeyEventCallback (Typedefs::OnKeyEventCallback func, void* tag);
void AddOnKeyPressCallback (Typedefs::OnKeyPressCallback func, void* tag);
void AddOnKeyDownCallback (Typedefs::OnKeyDownCallback func, void* tag);
void AddOnKeyReleaseCallback (Typedefs::OnKeyReleaseCallback func, void* tag);
void RemoveOnKeyEventCallback (Typedefs::OnKeyEventCallback func);
void RemoveOnKeyPressCallback (Typedefs::OnKeyPressCallback func);
void RemoveOnKeyDownCallback (Typedefs::OnKeyDownCallback func);
void RemoveOnKeyReleaseCallback (Typedefs::OnKeyReleaseCallback func); void RemoveOnKeyReleaseCallback (Typedefs::OnKeyReleaseCallback func);
public: public:
void operator+= (KeyboardEvent* object);
void operator-= (KeyboardEvent* object);
void BindTextTarget( ::std::wstring *field ); void BindTextTarget( ::std::wstring *field );
void ReleaseTextTarget(); void ReleaseTextTarget();
@ -189,6 +209,7 @@ namespace Input
Keyboard(); Keyboard();
protected: /* Internal event proc */ protected: /* Internal event proc */
void InternalOnEvent(Struct::KeyboardEventData& data);
void InternalOnKeyPress(Enum::SAKI key); void InternalOnKeyPress(Enum::SAKI key);
void InternalOnKeyDown(Enum::SAKI key); void InternalOnKeyDown(Enum::SAKI key);
void InternalOnKeyRelease(Enum::SAKI key); void InternalOnKeyRelease(Enum::SAKI key);

View File

@ -57,6 +57,14 @@ InputManager* InputManager::CreateInputManager()
{ {
return CreateManager(); return CreateManager();
} }
void InputManager::DestroyInputManager()
{
if(!defaultInstance) return;
defaultInstance->Destroy();
delete defaultInstance;
defaultInstance = 0;
}
void InputManager::DestroyInputManager(InputManager* inputSystem) void InputManager::DestroyInputManager(InputManager* inputSystem)
{ {
if(!inputSystem) return; if(!inputSystem) return;

View File

@ -12,12 +12,14 @@ struct Keyboard::KeyboardCallbackList
{ {
enum CallbackDataType enum CallbackDataType
{ {
CallbackDataType_OnEvent,
CallbackDataType_OnPress, CallbackDataType_OnPress,
CallbackDataType_OnDown, CallbackDataType_OnDown,
CallbackDataType_OnRelease CallbackDataType_OnRelease
} type; } type;
union CallbackData union CallbackData
{ {
Typedefs::OnKeyEventCallback keyEventCallback;
Typedefs::OnKeyPressCallback keyPressCallback; Typedefs::OnKeyPressCallback keyPressCallback;
Typedefs::OnKeyDownCallback keyDownCallback; Typedefs::OnKeyDownCallback keyDownCallback;
Typedefs::OnKeyReleaseCallback keyReleaseCallback; Typedefs::OnKeyReleaseCallback keyReleaseCallback;
@ -29,7 +31,8 @@ struct Keyboard::KeyboardCallbackList
operator bool(){ return this->keyDownCallback != 0; } operator bool(){ return this->keyDownCallback != 0; }
} function; } function;
KeyboardCallbackList *next; KeyboardCallbackList *next;
KeyboardCallbackList(CallbackData func, CallbackDataType t) :function(func), next(0), type(t) { } void* tag;
KeyboardCallbackList(CallbackData func, CallbackDataType t, void* ct) :function(func), next(0), type(t), tag(ct) { }
}; };
void ClearList(Keyboard::KeyboardCallbackList* first) void ClearList(Keyboard::KeyboardCallbackList* first)
@ -44,7 +47,7 @@ void ClearList(Keyboard::KeyboardCallbackList* first)
delete removee; delete removee;
} }
} }
void AddToList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data, Keyboard::KeyboardCallbackList::CallbackDataType type) void AddToList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data, Keyboard::KeyboardCallbackList::CallbackDataType type, void* tag)
{ {
Keyboard::KeyboardCallbackList *w = first; Keyboard::KeyboardCallbackList *w = first;
Keyboard::KeyboardCallbackList *prev = first; Keyboard::KeyboardCallbackList *prev = first;
@ -54,7 +57,7 @@ void AddToList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallback
Keyboard::KeyboardCallbackList::CallbackData f; Keyboard::KeyboardCallbackList::CallbackData f;
f = data; f = data;
prev->next = new Keyboard::KeyboardCallbackList(f, type); prev->next = new Keyboard::KeyboardCallbackList(f, type, tag);
} }
void RemoveFromList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data) void RemoveFromList(Keyboard::KeyboardCallbackList* first, Keyboard::KeyboardCallbackList::CallbackData data)
{ {
@ -112,6 +115,27 @@ Keyboard::~Keyboard()
} }
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;
}
}
void Keyboard::InternalOnKeyPress(Enum::SAKI key) void Keyboard::InternalOnKeyPress(Enum::SAKI key)
{ {
for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++) for (unsigned int i = 0; i < this->keyEventSubscrivers.size(); i++)
@ -126,7 +150,9 @@ void Keyboard::InternalOnKeyPress(Enum::SAKI key)
{ {
if(w->function) if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnPress) if (w->type == KeyboardCallbackList::CallbackDataType_OnPress)
w->function.keyPressCallback(key, this); {
w->function.keyPressCallback(key, this, w->tag);
}
w = w->next; w = w->next;
} }
} }
@ -144,7 +170,7 @@ void Keyboard::InternalOnKeyDown(Enum::SAKI key)
{ {
if(w->function) if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnDown) if (w->type == KeyboardCallbackList::CallbackDataType_OnDown)
w->function.keyDownCallback(key, this); w->function.keyDownCallback(key, this, w->tag);
w = w->next; w = w->next;
} }
} }
@ -162,46 +188,26 @@ void Keyboard::InternalOnKeyRelease(Enum::SAKI key)
{ {
if(w->function) if(w->function)
if (w->type == KeyboardCallbackList::CallbackDataType_OnRelease) if (w->type == KeyboardCallbackList::CallbackDataType_OnRelease)
w->function.keyReleaseCallback(key, this); w->function.keyReleaseCallback(key, this, w->tag);
w = w->next; w = w->next;
} }
} }
void Keyboard::AddOnKeyPressCallback (OnKeyPressCallback func) void Keyboard::AddKeyboardEvent(KeyboardEvent* object)
{ {
KeyboardCallbackList::CallbackData d; if(ExistsInList(this->keyEventSubscrivers, object)) return;
d.keyPressCallback = func;
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnPress);
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnPress);
}
void Keyboard::AddOnKeyDownCallback (OnKeyDownCallback func)
{
KeyboardCallbackList::CallbackData d;
d.keyDownCallback = func;
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnDown);
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnDown);
}
void Keyboard::AddOnKeyReleaseCallback (OnKeyReleaseCallback func)
{
KeyboardCallbackList::CallbackData d;
d.keyReleaseCallback = func;
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnRelease);
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnRelease);
}
void Keyboard::RemoveOnKeyPressCallback (OnKeyPressCallback func) this->keyEventSubscrivers.push_back(object);
{
RemoveFromList(this->callbackList, func);
} }
void Keyboard::RemoveOnKeyDownCallback (OnKeyDownCallback func) void Keyboard::RemoveKeyboardEvent(KeyboardEvent* object)
{ {
RemoveFromList(this->callbackList, func); 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::RemoveOnKeyReleaseCallback (OnKeyReleaseCallback func)
{
RemoveFromList(this->callbackList, func);
}
void Keyboard::operator+= (KeyboardEvent* object) void Keyboard::operator+= (KeyboardEvent* object)
{ {
if(ExistsInList(this->keyEventSubscrivers, object)) return; if(ExistsInList(this->keyEventSubscrivers, object)) return;
@ -218,6 +224,56 @@ void Keyboard::operator-= (KeyboardEvent* object)
} }
} }
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)
{
KeyboardCallbackList::CallbackData d;
d.keyPressCallback = func;
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnPress, tag);
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnPress, tag);
}
void Keyboard::AddOnKeyDownCallback (OnKeyDownCallback func, void* tag)
{
KeyboardCallbackList::CallbackData d;
d.keyDownCallback = func;
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnDown, tag);
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnDown, tag);
}
void Keyboard::AddOnKeyReleaseCallback (OnKeyReleaseCallback func, void* tag)
{
KeyboardCallbackList::CallbackData d;
d.keyReleaseCallback = func;
if(!this->callbackList) this->callbackList = new KeyboardCallbackList(d, KeyboardCallbackList::CallbackDataType_OnRelease, tag);
else AddToList(this->callbackList, d, KeyboardCallbackList::CallbackDataType_OnRelease, tag);
}
void Keyboard::RemoveOnKeyEventCallback (OnKeyEventCallback func)
{
Keyboard::KeyboardCallbackList::CallbackData temp;
temp.keyEventCallback = func;
RemoveFromList(this->callbackList, temp);
}
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);
}
void Keyboard::BindTextTarget( ::std::wstring *field ) void Keyboard::BindTextTarget( ::std::wstring *field )
{ {
this->textTarget = field; this->textTarget = field;

View File

@ -66,10 +66,10 @@ void Win32Keyboard::Deactivate ()
void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard) void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard)
{ {
if(!this->active) if(!this->active) return;
{
return; static Struct::KeyboardEventData keyboardEventData;
} memset(&keyboardEventData, 0, sizeof(Struct::KeyboardEventData));
bool isUp = (( keyboard.Flags & RI_KEY_BREAK) != 0); bool isUp = (( keyboard.Flags & RI_KEY_BREAK) != 0);
SAKI key = SAKI_Unknown; SAKI key = SAKI_Unknown;
@ -86,6 +86,11 @@ void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard)
this->keys[key].isDown = false; this->keys[key].isDown = false;
this->keys[key].isE0 = isE0; this->keys[key].isE0 = isE0;
this->keys[key].makecode = keyboard.MakeCode; this->keys[key].makecode = keyboard.MakeCode;
keyboardEventData.key = key;
keyboardEventData.sender = this;
keyboardEventData.state = Enum::ButtonState_Release;
InternalOnEvent(keyboardEventData);
} }
//The key is pressed. //The key is pressed.
else /*if (k.Flags == RI_KEY_MAKE || k.Flags == (RI_KEY_MAKE | RI_KEY_E0) || k.Flags == (RI_KEY_MAKE | RI_KEY_E1))*/ else /*if (k.Flags == RI_KEY_MAKE || k.Flags == (RI_KEY_MAKE | RI_KEY_E0) || k.Flags == (RI_KEY_MAKE | RI_KEY_E1))*/
@ -93,6 +98,11 @@ void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard)
if(this->keys[key].isDown) if(this->keys[key].isDown)
{ {
this->InternalOnKeyDown(key); this->InternalOnKeyDown(key);
keyboardEventData.key = key;
keyboardEventData.sender = this;
keyboardEventData.state = Enum::ButtonState_Down;
InternalOnEvent(keyboardEventData);
} }
else else
{ {
@ -100,6 +110,11 @@ void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard)
this->keys[key].isDown = true; this->keys[key].isDown = true;
this->keys[key].isE0 = isE0; this->keys[key].isE0 = isE0;
this->keys[key].makecode = keyboard.MakeCode; this->keys[key].makecode = keyboard.MakeCode;
keyboardEventData.key = key;
keyboardEventData.sender = this;
keyboardEventData.state = Enum::ButtonState_Press;
InternalOnEvent(keyboardEventData);
} }
} }
} }