Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Keyboard.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "Keyboard.h"
22 
23 #include "KeyboardListener.h"
24 
25 #include <algorithm>
26 #include <assert.h>
27 
28 #include "platform/Environment.h"
29 
30 using namespace HFCore;
31 
32 #include "debug/CrtDbgNew.h"
33 
35 {
36 }
37 
39 {
40 }
41 
43 {
44  assert(std::find(mListeners.begin(), mListeners.end(), listener) == mListeners.end());
45  mListeners.push_back(listener);
46 }
47 
49 {
50  assert(std::find(mListeners.begin(), mListeners.end(), listener) != mListeners.end());
51  mListeners.erase(std::find(mListeners.begin(), mListeners.end(), listener));
52 }
53 
55 {
56  envDebugLog("Keyboard::fireKeyDownEvent(): unicode=%c key=0x%02x\r\n",
57  kb.unicode, kb.key);
58 
59  if (mListeners.size() > 0 && isEnabled())
60  {
61  uint32_hf numListeners = mListeners.size();
62  for (uint32_hf i = 0; i < numListeners; i++)
63  {
64  kb.self = this;
65  mListeners[i]->keyDown(kb);
66  }
67  }
68 }
69 
71 {
72  envDebugLog("Keyboard::fireKeyUpEvent(): unicode=%c key=0x%02x\r\n",
73  kb.unicode, kb.key);
74 
75  if (mListeners.size() > 0 && isEnabled())
76  {
77  uint32_hf numListeners = mListeners.size();
78  for (uint32_hf i = 0; i < numListeners; i++)
79  {
80  kb.self = this;
81  mListeners[i]->keyUp(kb);
82  }
83  }
84 }
unsigned int uint32_hf
A type definition for unsigned int.
Definition: HFDataTypes.h:354
Interface for KeyboardListener callback methods.
virtual ~Keyboard()
Destructor.
Definition: Keyboard.cpp:38
Keyboard()
Constructor.
Definition: Keyboard.cpp:34
Interface for input type Keyboard.
Interface for KeyboardListener callback methods.
void addListener(KeyboardListener *listener)
Add a new listener class to receive KeyboardListener callbacks.
Definition: Keyboard.cpp:42
wchar_t unicode
Unicode character if any.
Definition: Keyboard.h:108
std::vector< KeyboardListener * > mListeners
Vector holding KeyboardListener classes.
Definition: Keyboard.h:163
A structure that contains Keyboard event information.
Definition: Keyboard.h:103
Key key
Key enum value if any.
Definition: Keyboard.h:109
virtual bool isEnabled()
Check whether controller is enabled.
Definition: Controller.cpp:52
Keyboard * self
Pointer to Keyboard.
Definition: Keyboard.h:105
Definition: Actor.h:34
Singleton interface for platform specific things.
void removeListener(KeyboardListener *listener)
Remove a listener class from receiving further KeyboardListener callbacks.
Definition: Keyboard.cpp:48
#define envDebugLog(format, args...)
Shortcut macro for logging to &#39;debug.log&#39;.
Definition: Environment.h:2364
void fireKeyUpEvent(KeyboardInfo kb)
Called internally when a Keyboard keyUp event occurs. It calls KeyboardListener::keyUp().
Definition: Keyboard.cpp:70
void fireKeyDownEvent(KeyboardInfo kb)
Called internally when a Keyboard keyDown event occurs. It calls KeyboardListener::keyDown().
Definition: Keyboard.cpp:54