comparison ext/guichan-0.8.2/src/hge/hgeinput.cpp @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 /* _______ __ __ __ ______ __ __ _______ __ __
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8 *
9 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
10 *
11 *
12 * Per Larsson a.k.a finalman
13 * Olof Naessén a.k.a jansem/yakslem
14 *
15 * Visit: http://guichan.sourceforge.net
16 *
17 * License: (BSD)
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * 3. Neither the name of Guichan nor the names of its contributors may
28 * be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /*
45 * For comments regarding functions please see the header file.
46 */
47
48 #include "guichan/hge/hgeinput.hpp"
49 #include "guichan/exception.hpp"
50
51 namespace gcn
52 {
53 HGE *HGEInput::mHGE = NULL;
54
55 HGEInput::HGEInput()
56 {
57 mHGE = hgeCreate(HGE_VERSION);
58
59 mMouseX = 0;
60 mMouseY = 0;
61
62 mLeftMouseButtonDown = false;
63 mRightMouseButtonDown = false;
64 mMiddleMouseButtonDown = false;
65 }
66
67 HGEInput::~HGEInput()
68 {
69 mHGE->Release();
70 }
71
72 bool HGEInput::isKeyQueueEmpty()
73 {
74 return mKeyInputQueue.empty();
75 }
76
77 bool HGEInput::isMouseQueueEmpty()
78 {
79 return mMouseInputQueue.empty();
80 }
81
82 KeyInput HGEInput::dequeueKeyInput()
83 {
84 if (isKeyQueueEmpty())
85 {
86 throw GCN_EXCEPTION("Key queue is empty.");
87 }
88
89 KeyInput keyInput;
90
91 keyInput = mKeyInputQueue.front();
92 mKeyInputQueue.pop();
93
94 return keyInput;
95
96 }
97
98 MouseInput HGEInput::dequeueMouseInput()
99 {
100 if (isMouseQueueEmpty())
101 {
102 throw GCN_EXCEPTION("Mouse queue is empty.");
103 }
104
105 MouseInput mouseInput;
106
107 mouseInput = mMouseInputQueue.front();
108 mMouseInputQueue.pop();
109
110 return mouseInput;
111 }
112
113 void HGEInput::_pollInput()
114 {
115 hgeInputEvent ie;
116
117 mHGE->Input_GetEvent(&ie);
118
119 pollMouseInput();
120 pollKeyInput(ie);
121 }
122
123 void HGEInput::pollMouseInput()
124 {
125 float curMouseX, curMouseY;
126 int mouseWheel;
127
128 bool leftBtn, rightBtn, centerBtn;
129
130 mHGE->Input_GetMousePos(&curMouseX, &curMouseY);
131 mouseWheel = mHGE->Input_GetMouseWheel();
132
133 leftBtn = mHGE->Input_GetKeyState(HGEK_LBUTTON);
134 rightBtn = mHGE->Input_GetKeyState(HGEK_RBUTTON);
135 centerBtn = mHGE->Input_GetKeyState(HGEK_MBUTTON);
136
137 // Check mouse movement
138 if (mMouseX != curMouseX
139 || mMouseY != curMouseY )
140 {
141 mMouseInputQueue.push(MouseInput(MouseInput::EMPTY,
142 MouseInput::MOVED,
143 curMouseX,
144 curMouseY,
145 0));
146
147 mMouseX = curMouseX;
148 mMouseY = curMouseY;
149 }
150
151 // Check mouse wheel
152 if (mouseWheel > 0)
153 {
154 mMouseInputQueue.push(MouseInput(MouseInput::EMPTY,
155 MouseInput::WHEEL_MOVED_UP,
156 curMouseX,
157 curMouseY,
158 0));
159 }
160 else if (mouseWheel < 0)
161 {
162 mMouseInputQueue.push(MouseInput(MouseInput::EMPTY,
163 MouseInput::WHEEL_MOVED_DOWN,
164 curMouseX,
165 curMouseY,
166 0));
167 }
168
169 //check mouse buttons
170 if (!mLeftMouseButtonDown && leftBtn)
171 {
172 mMouseInputQueue.push(MouseInput(MouseInput::LEFT,
173 MouseInput::PRESSED,
174 curMouseX,
175 curMouseY,
176 0));
177 }
178 else if (mLeftMouseButtonDown && !leftBtn )
179 {
180 mMouseInputQueue.push(MouseInput(MouseInput::LEFT,
181 MouseInput::RELEASED,
182 curMouseX,
183 curMouseY,
184 0));
185 }
186 else if (!mRightMouseButtonDown && rightBtn)
187 {
188 mMouseInputQueue.push(MouseInput(MouseInput::RIGHT,
189 MouseInput::PRESSED,
190 curMouseX,
191 curMouseY,
192 0));
193 }
194 else if (mRightMouseButtonDown && !rightBtn)
195 {
196 mMouseInputQueue.push(MouseInput(MouseInput::RIGHT,
197 MouseInput::RELEASED,
198 curMouseX,
199 curMouseY,
200 0));
201 }
202 else if (!mMiddleMouseButtonDown && centerBtn)
203 {
204 mMouseInputQueue.push(MouseInput(MouseInput::MIDDLE,
205 MouseInput::PRESSED,
206 curMouseX,
207 curMouseY,
208 0));
209 }
210 else if (mMiddleMouseButtonDown && !centerBtn)
211 {
212 mMouseInputQueue.push(MouseInput(MouseInput::MIDDLE,
213 MouseInput::RELEASED,
214 curMouseX,
215 curMouseY,
216 0));
217 }
218
219 mLeftMouseButtonDown = leftBtn;
220 mRightMouseButtonDown = rightBtn;
221 mMiddleMouseButtonDown = centerBtn;
222 }
223
224 void HGEInput::pollKeyInput(hgeInputEvent &ki)
225 {
226 if (ki.type != INPUT_KEYDOWN
227 && ki.type != INPUT_KEYUP )
228 {
229 return ;
230 }
231
232 if (ki.type == INPUT_KEYDOWN)
233 {
234 Key keyObj = convertToKey(ki.key, ki.chr);
235
236 KeyInput keyInput(keyObj, KeyInput::PRESSED);
237
238 keyInput.setNumericPad(isNumericPad(ki.key));
239
240 keyInput.setShiftPressed(ki.flags & HGEINP_SHIFT);
241 keyInput.setAltPressed(ki.flags & HGEINP_ALT);
242 keyInput.setControlPressed(ki.flags & HGEINP_CTRL);
243
244 mKeyInputQueue.push(keyInput);
245 }
246
247 if (ki.type == INPUT_KEYUP)
248 {
249 Key keyObj = convertToKey(ki.key, ki.chr);
250
251 KeyInput keyInput(keyObj, KeyInput::RELEASED);
252
253 keyInput.setNumericPad(isNumericPad(ki.key));
254
255 keyInput.setShiftPressed(ki.flags & HGEINP_SHIFT);
256 keyInput.setAltPressed(ki.flags & HGEINP_ALT);
257 keyInput.setControlPressed(ki.flags & HGEINP_CTRL);
258
259 mKeyInputQueue.push(keyInput);
260 }
261 }
262
263 Key HGEInput::convertToKey(int key, int chr)
264 {
265 int keysym;
266
267 switch(key)
268 {
269 case HGEK_TAB:
270 keysym = Key::TAB;
271 break;
272 case HGEK_ESCAPE:
273 keysym = Key::ESCAPE;
274 break;
275 case HGEK_ALT:
276 keysym = Key::LEFT_ALT;
277 break;
278 /*
279 Unsupported by HGE.
280 case HGEK_ALT:
281 keysym = Key::RIGHT_ALT;
282 break;
283 */
284 case HGEK_SHIFT:
285 keysym = Key::LEFT_SHIFT;
286 break;
287 /*
288 Unsuppored by HGE.
289 case HGEK_SHIFT:
290 keysym = Key::RIGHT_SHIFT;
291 break;
292 */
293 case HGEK_CTRL:
294 keysym = Key::LEFT_CONTROL;
295 break;
296 /*
297 Unsupported by HGE.
298 case HGEK_CTRL:
299 keysym = Key::RIGHT_CONTROL;
300 break;
301 */
302 case HGEK_LWIN:
303 keysym = Key::LEFT_META;
304 break;
305 case HGEK_RWIN:
306 keysym = Key::RIGHT_META;
307 break;
308 case HGEK_INSERT:
309 keysym = Key::INSERT;
310 break;
311 case HGEK_HOME:
312 keysym = Key::HOME;
313 break;
314 case HGEK_PGUP:
315 keysym = Key::PAGE_UP;
316 break;
317 case HGEK_PGDN:
318 keysym = Key::PAGE_DOWN;
319 break;
320 case HGEK_DELETE:
321 keysym = Key::DELETE;
322 break;
323 /*
324 Unsupported by HGE.
325 case HGEK_BACKSPACE:
326 keysym = Key::DELETE;
327 pad = true;
328 break;
329 */
330 case HGEK_END:
331 keysym = Key::END;
332 break;
333 case HGEK_CAPSLOCK:
334 keysym = Key::CAPS_LOCK;
335 break;
336 case HGEK_BACKSPACE:
337 keysym = Key::BACKSPACE;
338 break;
339 case HGEK_F1:
340 keysym = Key::F1;
341 break;
342 case HGEK_F2:
343 keysym = Key::F2;
344 break;
345 case HGEK_F3:
346 keysym = Key::F3;
347 break;
348 case HGEK_F4:
349 keysym = Key::F4;
350 break;
351 case HGEK_F5:
352 keysym = Key::F5;
353 break;
354 case HGEK_F6:
355 keysym = Key::F6;
356 break;
357 case HGEK_F7:
358 keysym = Key::F7;
359 break;
360 case HGEK_F8:
361 keysym = Key::F8;
362 break;
363 case HGEK_F9:
364 keysym = Key::F9;
365 break;
366 case HGEK_F10:
367 keysym = Key::F10;
368 break;
369 case HGEK_F11:
370 keysym = Key::F11;
371 break;
372 case HGEK_F12:
373 keysym = Key::F12;
374 break;
375 /*
376 Unsupported by HGE
377 case HGEK_PRTSCR:
378 keysym = Key::PRINT_SCREEN;
379 break;
380 */
381 case HGEK_PAUSE:
382 keysym = Key::PAUSE;
383 break;
384 case HGEK_SCROLLLOCK:
385 keysym = Key::SCROLL_LOCK;
386 break;
387 case HGEK_NUMLOCK:
388 keysym = Key::NUM_LOCK;
389 break;
390 case HGEK_LEFT:
391 keysym = Key::LEFT;
392 break;
393 case HGEK_RIGHT:
394 keysym = Key::RIGHT;
395 break;
396 case HGEK_UP:
397 keysym = Key::UP;
398 break;
399 case HGEK_DOWN:
400 keysym = Key::DOWN;
401 break;
402 case HGEK_ENTER:
403 keysym = Key::ENTER;
404 break;
405 default:
406 keysym = chr;
407 }
408
409 Key k = Key(keysym);
410
411 return k;
412 }
413
414 bool HGEInput::isNumericPad(int keyCode)
415 {
416 switch(keyCode)
417 {
418 case HGEK_NUMPAD0:
419 case HGEK_NUMPAD1:
420 case HGEK_NUMPAD2:
421 case HGEK_NUMPAD3:
422 case HGEK_NUMPAD4:
423 case HGEK_NUMPAD5:
424 case HGEK_NUMPAD6:
425 case HGEK_NUMPAD7:
426 case HGEK_NUMPAD8:
427 case HGEK_NUMPAD9:
428 return true;
429 default:
430 return false;
431 }
432 }
433 }