-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInput.cpp
More file actions
255 lines (205 loc) · 6.38 KB
/
Input.cpp
File metadata and controls
255 lines (205 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Input.cpp
* Prototyp
*
* Created by Christian Pehle on 24.08.09.
* Copyright 2009 Universität Heidelberg. All rights reserved.
*
*/
#include "Input.h"
#include <Ogre/OgreStringConverter.h>
namespace Orbifold {
InputHandler::InputHandler() {
this->inputsystem = 0;
this->mouse = 0;
this->keyboard = 0;
}
InputHandler::~InputHandler() {
if(this->instance) {
delete this->instance;
}
if(this->inputsystem) {
if(this->mouse)
this->inputsystem->destroyInputObject(this->mouse);
if(this->keyboard)
this->inputsystem->destroyInputObject(this->keyboard);
this->inputsystem->destroyInputSystem(this->inputsystem);
}
this->keyListeners.clear();
this->mouseListeners.clear();
}
InputHandler* InputHandler::instance = 0;
InputHandler* InputHandler::getSingleton() {
if(!instance)
instance = new InputHandler();
return instance;
}
// InputHandler::initialise : Set up a buffered Mouse and Keyboard.
void InputHandler::initialise(Ogre::RenderWindow *window) {
InputHandler* hdl = InputHandler::getSingleton();
// get window handle
unsigned long hWnd;
window->getCustomAttribute("WINDOW", &hWnd);
// we got the window handle, insert it into a parameter list
OIS::ParamList pl;
pl.insert(OIS::ParamList::value_type("WINDOW", Ogre::StringConverter::toString(hWnd)));
// create the input system
hdl->inputsystem = OIS::InputManager::createInputSystem(pl);
// if possible create a buffered mouse
hdl->initMouse(window);
hdl->initKeyboard();
// and create a buffered keyboard
}
void InputHandler::initMouse(Ogre::RenderWindow *window){
if (this->inputsystem->getNumberOfDevices(OIS::OISMouse) > 0) {
this->mouse = static_cast<OIS::Mouse*>(this->inputsystem->createInputObject(OIS::OISMouse, true));
this->mouse->setEventCallback(this);
// get window metrics and set initial mouse region
unsigned int width, height, depth;
int left, top;
window->getMetrics(width, height, depth, left, top);
this->updateWindowDimensions(width, height);
}
}
void InputHandler::initKeyboard() {
if ( this->inputsystem->getNumberOfDevices(OIS::OISKeyboard) > 0) {
this->keyboard = static_cast<OIS::Keyboard*>(this->inputsystem->createInputObject(OIS::OISKeyboard, true));
this->keyboard->setEventCallback(this);
}
}
void InputHandler::capture() {
this->mouse->capture();
this->keyboard->capture();
}
void InputHandler::updateWindowDimensions(int width, int height){
const OIS::MouseState &ms = this->mouse->getMouseState();
ms.width = width;
ms.height = height;
}
// Manage Listeners.
void InputHandler::addKeyListener(OIS::KeyListener *keyListener, const std::string& instanceName) {
if(this->keyboard) {
// Check for duplicate items
itKeyListener = keyListeners.find(instanceName);
if(itKeyListener == keyListeners.end()) {
keyListeners[instanceName] = keyListener;
}
else {
// Duplicate Item
}
}
}
void InputHandler::addMouseListener(OIS::MouseListener *mouseListener, const std::string& instanceName) {
if(this->mouse) {
// Check for duplicate items
itMouseListener = mouseListeners.find(instanceName);
if(itMouseListener == mouseListeners.end()) {
mouseListeners[instanceName] = mouseListener;
}
else {
// Duplicate Item
}
}
}
void InputHandler::removeKeyListener(const std::string& instanceName) {
// Check if item exists
itKeyListener = keyListeners.find(instanceName);
if( itKeyListener != keyListeners.end()) {
keyListeners.erase(itKeyListener);
}
else {
// Doesn't Exist
}
}
void InputHandler::removeMouseListener( const std::string& instanceName ) {
// Check if item exists
itMouseListener = mouseListeners.find(instanceName);
if(itMouseListener != mouseListeners.end()) {
mouseListeners.erase(itMouseListener);
}
else {
// Doesn't Exist
}
}
void InputHandler::removeKeyListener(OIS::KeyListener *keyListener) {
itKeyListener = keyListeners.begin();
itKeyListenerEnd = keyListeners.end();
for(; itKeyListener != itKeyListenerEnd; ++itKeyListener ) {
if( itKeyListener->second == keyListener ) {
keyListeners.erase( itKeyListener );
break;
}
}
}
void InputHandler::removeMouseListener(OIS::MouseListener *mouseListener) {
itMouseListener = mouseListeners.begin();
itMouseListenerEnd = mouseListeners.end();
for(; itMouseListener != itMouseListenerEnd; ++itMouseListener) {
if(itMouseListener->second == mouseListener) {
mouseListeners.erase(itMouseListener);
break;
}
}
}
void InputHandler::removeAllKeyListeners() {
keyListeners.clear();
}
void InputHandler::removeAllMouseListeners() {
mouseListeners.clear();
}
void InputHandler::removeAllListeners() {
this->removeAllKeyListeners();
this->removeAllMouseListeners();
}
OIS::Mouse* InputHandler::getMouse() {
return this->mouse;
}
OIS::Keyboard* InputHandler::getKeyboard() {
return this->keyboard;
}
bool InputHandler::keyPressed(const OIS::KeyEvent &e) {
itKeyListener = keyListeners.begin();
itKeyListenerEnd = keyListeners.end();
for(; itKeyListener != itKeyListenerEnd; ++itKeyListener) {
if(!itKeyListener->second->keyPressed(e))
break;
}
return true;
}
bool InputHandler::keyReleased(const OIS::KeyEvent &e) {
itKeyListener = keyListeners.begin();
itKeyListenerEnd = keyListeners.end();
for(; itKeyListener != itKeyListenerEnd; ++itKeyListener ) {
if(!itKeyListener->second->keyReleased(e))
break;
}
return true;
}
bool InputHandler::mouseMoved(const OIS::MouseEvent &e) {
itMouseListener = mouseListeners.begin();
itMouseListenerEnd = mouseListeners.end();
for(; itMouseListener != itMouseListenerEnd; ++itMouseListener) {
if(!itMouseListener->second->mouseMoved(e))
break;
}
return true;
}
bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) {
itMouseListener = mouseListeners.begin();
itMouseListenerEnd = mouseListeners.end();
for(; itMouseListener != itMouseListenerEnd; ++itMouseListener) {
if(!itMouseListener->second->mousePressed(e, id))
break;
}
return true;
}
bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) {
itMouseListener = mouseListeners.begin();
itMouseListenerEnd = mouseListeners.end();
for(; itMouseListener != itMouseListenerEnd; ++itMouseListener) {
if(!itMouseListener->second->mouseReleased(e, id))
break;
}
return true;
}
}