Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/core/InputHandler.cc @ 1268

Last change on this file since 1268 was 1268, checked in by rgrieder, 16 years ago
  • removed some debug output
File size: 14.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30 @file
31 @brief Implementation of the different input handlers.
32 */
33
34#include "InputHandler.h"
35#include "Debug.h"
36#include "ConfigValueIncludes.h"
37#include "CoreIncludes.h"
38#include "util/Convert.h"
39#include "core/CommandExecutor.h"
40
41namespace orxonox
42{
43  // ###############################
44  // ######     KeyBinder     ######
45  // ###############################
46
47  /**
48    @brief Constructor that does as little as necessary.
49  */
50  KeyBinder::KeyBinder()
51  {
52    RegisterObject(KeyBinder);
53    clearBindings();
54
55    std::string keyNames[] = {
56    "UNASSIGNED",
57    "ESCAPE",
58    "1",
59    "2",
60    "3",
61    "4",
62    "5",
63    "6",
64    "7",
65    "8",
66    "9",
67    "0",
68    "MINUS",
69    "EQUALS",
70    "BACK",
71    "TAB",
72    "Q",
73    "W",
74    "E",
75    "R",
76    "T",
77    "Y",
78    "U",
79    "I",
80    "O",
81    "P",
82    "LBRACKET",
83    "RBRACKET",
84    "RETURN",
85    "LCONTROL",
86    "A",
87    "S",
88    "D",
89    "F",
90    "G",
91    "H",
92    "J",
93    "K",
94    "L",
95    "SEMICOLON",
96    "APOSTROPHE",
97    "GRAVE",
98    "LSHIFT",
99    "BACKSLASH",
100    "Z",
101    "X",
102    "C",
103    "V",
104    "B",
105    "N",
106    "M",
107    "COMMA",
108    "PERIOD",
109    "SLASH",
110    "RSHIFT",
111    "MULTIPLY",
112    "LMENU",
113    "SPACE",
114    "CAPITAL",
115    "F1",
116    "F2",
117    "F3",
118    "F4",
119    "F5",
120    "F6",
121    "F7",
122    "F8",
123    "F9",
124    "F10",
125    "NUMLOCK",
126    "SCROLL",
127    "NUMPAD7",
128    "NUMPAD8",
129    "NUMPAD9",
130    "SUBTRACT",
131    "NUMPAD4",
132    "NUMPAD5",
133    "NUMPAD6",
134    "ADD",
135    "NUMPAD1",
136    "NUMPAD2",
137    "NUMPAD3",
138    "NUMPAD0",
139    "DECIMAL",
140    "","",
141    "OEM_102",
142    "F11",
143    "F12",
144    "","","","","","","","","","","",
145    "F13",
146    "F14",
147    "F15",
148    "","","","","","","","","","",
149    "KANA",
150    "","",
151    "ABNT_C1",
152    "","","","","",
153    "CONVERT",
154    "",
155    "NOCONVERT",
156    "",
157    "YEN",
158    "ABNT_C2",
159    "","","","","","","","","","","","","","",
160    "NUMPADEQUALS",
161    "","",
162    "PREVTRACK",
163    "AT",
164    "COLON",
165    "UNDERLINE",
166    "KANJI",
167    "STOP",
168    "AX",
169    "UNLABELED",
170    "NEXTTRACK",
171    "","",
172    "NUMPADENTER",
173    "RCONTROL",
174    "","",
175    "MUTE",
176    "CALCULATOR",
177    "PLAYPAUSE",
178    "",
179    "MEDIASTOP",
180    "","","","","","","","","",
181    "VOLUMEDOWN",
182    "",
183    "VOLUMEUP",
184    "",
185    "WEBHOME",
186    "NUMPADCOMMA",
187    "",
188    "DIVIDE",
189    "",
190    "SYSRQ",
191    "RMENU",
192    "","","","","","","","","","","","",
193    "PAUSE",
194    "",
195    "HOME",
196    "UP",
197    "PGUP",
198    "",
199    "LEFT",
200    "",
201    "RIGHT",
202    "",
203    "END",
204    "DOWN",
205    "PGDOWN",
206    "INSERT",
207    "DELETE",
208    "","","","","","","",
209    "LWIN",
210    "RWIN",
211    "APPS",
212    "POWER",
213    "SLEEP",
214    "","","",
215    "WAKE",
216    "",
217    "WEBSEARCH",
218    "WEBFAVORITES",
219    "WEBREFRESH",
220    "WEBSTOP",
221    "WEBFORWARD",
222    "WEBBACK",
223    "MYCOMPUTER",
224    "MAIL",
225    "MEDIASELECT"
226    };
227    for (int i = 0; i < numberOfKeys_s; i++)
228      keyNames_[i] = keyNames[i];
229
230    std::string mouseButtonNames[] = {
231      "MouseLeft", "MouseRight", "MouseMiddle",
232      "MouseButton3", "MouseButton4", "MouseButton5",
233      "MouseButton6", "MouseButton7" };
234    for (int i = 0; i < numberOfMouseButtons_s; i++)
235      mouseButtonNames_[i] = mouseButtonNames[i];
236
237    for (int i = 0; i < numberOfJoyStickButtons_s; i++)
238      joyStickButtonNames_[i] = "JoyStick" + getConvertedValue<int, std::string>(i);
239  }
240
241  /**
242    @brief Destructor
243  */
244  KeyBinder::~KeyBinder()
245  {
246  }
247
248  /**
249    @brief Loader for the key bindings, managed by config values.
250  */
251  void KeyBinder::setConfigValues()
252  {
253    ConfigValueContainer* cont;
254    std::string modes[] = {"P_", "R_", "H_"};
255
256    // keys
257    for (int i = 0; i < numberOfKeys_s; i++)
258    {
259      for (int j = 0; j < 3; j++)
260      {
261        cont = getIdentifier()->getConfigValueContainer(modes[j] + keyNames_[i]);
262        if (!cont)
263        {
264          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + keyNames_[i], "");
265          getIdentifier()->addConfigValueContainer(modes[j] + keyNames_[i], cont);
266        }
267        switch (j)
268        {
269          // note: the first element in the struct is the string, so the following pointer
270          //       arithmetic works.
271          case 0:
272            cont->getValue(&bindingsKeyPress_[i].commandStr);
273            break;
274          case 1:
275            cont->getValue(bindingsKeyRelease_ + i);
276            break;
277          case 2:
278            cont->getValue(bindingsKeyHold_ + i);
279        }
280      }
281    }
282
283    // mouse buttons
284    for (int i = 0; i < numberOfMouseButtons_s; i++)
285    {
286      for (int j = 0; j < 3; j++)
287      {
288        cont = getIdentifier()->getConfigValueContainer(modes[j] + mouseButtonNames_[i]);
289        if (!cont)
290        {
291          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + mouseButtonNames_[i], "");
292          getIdentifier()->addConfigValueContainer(modes[j] + mouseButtonNames_[i], cont);
293        }
294        switch (j)
295        {
296          case 0:
297            cont->getValue(bindingsMouseButtonPress_ + i);
298            break;
299          case 1:
300            cont->getValue(bindingsMouseButtonRelease_ + i);
301            break;
302          case 2:
303            cont->getValue(bindingsMouseButtonHold_ + i);
304        }
305      }
306    }
307
308    // joy stick buttons
309    for (int i = 0; i < numberOfJoyStickButtons_s; i++)
310    {
311      for (int j = 0; j < 3; j++)
312      {
313        cont = getIdentifier()->getConfigValueContainer(modes[j] + joyStickButtonNames_[i]);
314        if (!cont)
315        {
316          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + joyStickButtonNames_[i], "");
317          getIdentifier()->addConfigValueContainer(modes[j] + joyStickButtonNames_[i], cont);
318        }
319        switch (j)
320        {
321          case 0:
322            cont->getValue(bindingsJoyStickButtonPress_ + i);
323            break;
324          case 1:
325            cont->getValue(bindingsJoyStickButtonRelease_ + i);
326            break;
327          case 2:
328            cont->getValue(bindingsJoyStickButtonHold_ + i);
329        }
330      }
331    }
332  }
333
334  /**
335    @brief Overwrites all bindings with ""
336  */
337  void KeyBinder::clearBindings()
338  {
339    for (int i = 0; i < numberOfKeys_s; i++)
340    {
341      bindingsKeyPress_  [i].commandStr = "";
342      bindingsKeyRelease_[i].commandStr = "";
343      bindingsKeyHold_   [i].commandStr = "";
344    }
345    for (int i = 0; i < numberOfMouseButtons_s; i++)
346    {
347      bindingsMouseButtonPress_  [i] = "";
348      bindingsMouseButtonRelease_[i] = "";
349      bindingsMouseButtonHold_   [i] = "";
350    }
351    for (int i = 0; i < numberOfJoyStickButtons_s; i++)
352    {
353      bindingsJoyStickButtonPress_  [i] = "";
354      bindingsJoyStickButtonRelease_[i] = "";
355      bindingsJoyStickButtonHold_   [i] = "";
356    }
357  }
358
359  /**
360    @brief Loads the key and button bindings.
361    @return True if loading succeeded.
362  */
363  bool KeyBinder::loadBindings()
364  {
365    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings..." << std::endl;
366
367    ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini");
368    setConfigValues();
369
370    // evaluate the key bindings
371    // TODO: what if binding is invalid?
372    //for (int i = 0; i < numberOfKeys_s; i++)
373    //{
374    //  if (bindingsKeyPress_[i].commandStr != "")
375    //  {
376    //    bindingsKeyPress_[i].evaluation = CommandExecutor::evaluate(bindingsKeyPress_[i].commandStr);
377    //    bindingsKeyPress_[i].commandStr = bindingsKeyPress_[i].evaluation.getCommandString();
378    //  }
379    //}
380
381    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl;
382    return true;
383  }
384
385  bool KeyBinder::executeBinding(KeyBinding& binding)
386  {
387    if (binding.commandStr != "")
388    {
389      //if (binding.commandStr != binding.evaluation.getCommandString())
390      //{
391      //  // key binding has changed, reevaluate the command string.
392      //  binding.evaluation = CommandExecutor::evaluate(binding.commandStr);
393      //  binding.commandStr = binding.evaluation.getCommandString();
394      //}
395      COUT(ORX_DEBUG) << "Keybinding: Executing command: " << binding.commandStr << std::endl;
396      CommandExecutor::execute(binding.commandStr);
397    }
398
399    return true;
400  }
401
402
403  /**
404    @brief Event handler for the keyPressed Event.
405    @param e Event information
406  */
407  bool KeyBinder::keyPressed(const OIS::KeyEvent &e)
408  {
409    //COUT(ORX_DEBUG) << "Key: " << e.key << std::endl;
410    // find the appropriate key binding
411    executeBinding(bindingsKeyPress_[int(e.key)]);
412     
413    return true;
414  }
415
416  /**
417    @brief Event handler for the keyReleased Event.
418    @param e Event information
419  */
420  bool KeyBinder::keyReleased(const OIS::KeyEvent &e)
421  {
422    // find the appropriate key binding
423    executeBinding(bindingsKeyRelease_[int(e.key)]);
424
425    return true;
426  }
427
428  /**
429    @brief Event handler for the keyHeld Event.
430    @param e Event information
431  */
432  bool KeyBinder::keyHeld(const OIS::KeyEvent &e)
433  {
434    // find the appropriate key binding
435    executeBinding(bindingsKeyHold_[int(e.key)]);
436
437    return true;
438  }
439
440  /**
441    @brief Event handler for the mouseMoved Event.
442    @param e Event information
443  */
444  bool KeyBinder::mouseMoved(const OIS::MouseEvent &e)
445  {
446    return true;
447  }
448
449  /**
450    @brief Event handler for the mousePressed Event.
451    @param e Event information
452    @param id The ID of the mouse button
453  */
454  bool KeyBinder::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
455  {
456    // find the appropriate key binding
457    std::string cmdStr = bindingsMouseButtonPress_[int(id)];
458    if (cmdStr != "")
459    {
460      CommandExecutor::execute(cmdStr);
461      //COUT(3) << "Executing command: " << cmdStr << std::endl;
462    }
463
464    return true;
465  }
466
467  /**
468    @brief Event handler for the mouseReleased Event.
469    @param e Event information
470    @param id The ID of the mouse button
471  */
472  bool KeyBinder::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
473  {
474    // find the appropriate key binding
475    std::string cmdStr = bindingsMouseButtonRelease_[int(id)];
476    if (cmdStr != "")
477    {
478      CommandExecutor::execute(cmdStr);
479      //COUT(3) << "Executing command: " << cmdStr << std::endl;
480    }
481
482    return true;
483  }
484
485  /**
486    @brief Event handler for the mouseHeld Event.
487    @param e Event information
488    @param id The ID of the mouse button
489  */
490  bool KeyBinder::mouseHeld(const OIS::MouseEvent &e, OIS::MouseButtonID id)
491  {
492    // find the appropriate key binding
493    std::string cmdStr = bindingsMouseButtonHold_[int(id)];
494    if (cmdStr != "")
495    {
496      CommandExecutor::execute(cmdStr);
497      //COUT(3) << "Executing command: " << cmdStr << std::endl;
498    }
499
500    return true;
501  }
502
503  bool KeyBinder::buttonPressed(int joyStickID, const OIS::JoyStickEvent &arg, int button)
504  {
505    // find the appropriate key binding
506    std::string cmdStr = bindingsJoyStickButtonPress_[button];
507    if (cmdStr != "")
508    {
509      CommandExecutor::execute(cmdStr);
510      //COUT(3) << "Executing command: " << cmdStr << std::endl;
511    }
512
513    return true;
514  }
515
516  bool KeyBinder::buttonReleased(int joyStickID, const OIS::JoyStickEvent &arg, int button)
517  {
518    // find the appropriate key binding
519    std::string cmdStr = bindingsJoyStickButtonRelease_[button];
520    if (cmdStr != "")
521    {
522      CommandExecutor::execute(cmdStr);
523      //COUT(3) << "Executing command: " << cmdStr << std::endl;
524    }
525
526    return true;
527  }
528
529  bool KeyBinder::buttonHeld(int joyStickID, const OIS::JoyStickEvent &arg, int button)
530  {
531    // find the appropriate key binding
532    std::string cmdStr = bindingsJoyStickButtonHold_[button];
533    if (cmdStr != "")
534    {
535      CommandExecutor::execute(cmdStr);
536      //COUT(3) << "Executing command: " << cmdStr << std::endl;
537    }
538
539    return true;
540  }
541
542  bool KeyBinder::axisMoved(int joyStickID, const OIS::JoyStickEvent &arg, int axis)
543  {
544    return true;
545  }
546
547  bool KeyBinder::sliderMoved(int joyStickID, const OIS::JoyStickEvent &arg, int id)
548  {
549    return true;
550  }
551
552  bool KeyBinder::povMoved(int joyStickID, const OIS::JoyStickEvent &arg, int id)
553  {
554    return true;
555  }
556
557  bool KeyBinder::vector3Moved(int joyStickID, const OIS::JoyStickEvent &arg, int id)
558  {
559    return true;
560  }
561
562
563
564  // ###############################
565  // ###     GUIInputHandler     ###
566  // ###############################
567
568  ///**
569  //  @brief standard constructor
570  //*/
571  //GUIInputHandler::GUIInputHandler()
572  //{
573  //}
574
575  ///**
576  //  @brief Destructor
577  //*/
578  //GUIInputHandler::~GUIInputHandler()
579  //{
580  //}
581
582  ///**
583  //  @brief Event handler for the keyPressed Event.
584  //  @param e Event information
585  //*/
586  //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e)
587  //{
588    ////CEGUI::System::getSingleton().injectKeyDown( arg.key );
589    ////CEGUI::System::getSingleton().injectChar( arg.text );
590  //  return true;
591  //}
592
593  ///**
594  //  @brief Event handler for the keyReleased Event.
595  //  @param e Event information
596  //*/
597  //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e)
598  //{
599    ////CEGUI::System::getSingleton().injectKeyUp( arg.key );
600  //  return true;
601  //}
602
603  ///**
604  //  @brief Event handler for the mouseMoved Event.
605  //  @param e Event information
606  //*/
607  //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e)
608  //{
609    ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
610  //  return true;
611  //}
612
613  ///**
614  //  @brief Event handler for the mousePressed Event.
615  //  @param e Event information
616  //  @param id The ID of the mouse button
617  //*/
618  //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
619  //{
620    ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
621  //  return true;
622  //}
623
624  ///**
625  //  @brief Event handler for the mouseReleased Event.
626  //  @param e Event information
627  //  @param id The ID of the mouse button
628  //*/
629  //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
630  //{
631    ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
632  //  return true;
633  //}
634
635}
Note: See TracBrowser for help on using the repository browser.