Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/core/InputHandler.cc @ 1213

Last change on this file since 1213 was 1213, checked in by rgrieder, 16 years ago
  • Key bindings can now be stored in keybindings.ini
  • doesn't work for move movement yet, but for the buttons.
File size: 13.6 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          case 0:
270            cont->getValue(bindingsKeyPress_ + i);
271            break;
272          case 1:
273            cont->getValue(bindingsKeyRelease_ + i);
274            break;
275          case 2:
276            cont->getValue(bindingsKeyHold_ + i);
277        }
278      }
279    }
280
281    // mouse buttons
282    for (int i = 0; i < numberOfMouseButtons_s; i++)
283    {
284      for (int j = 0; j < 3; j++)
285      {
286        cont = getIdentifier()->getConfigValueContainer(modes[j] + mouseButtonNames_[i]);
287        if (!cont)
288        {
289          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + mouseButtonNames_[i], "");
290          getIdentifier()->addConfigValueContainer(modes[j] + mouseButtonNames_[i], cont);
291        }
292        switch (j)
293        {
294          case 0:
295            cont->getValue(bindingsMouseButtonPress_ + i);
296            break;
297          case 1:
298            cont->getValue(bindingsMouseButtonRelease_ + i);
299            break;
300          case 2:
301            cont->getValue(bindingsMouseButtonHold_ + i);
302        }
303      }
304    }
305
306    // joy stick buttons
307    for (int i = 0; i < numberOfJoyStickButtons_s; i++)
308    {
309      for (int j = 0; j < 3; j++)
310      {
311        cont = getIdentifier()->getConfigValueContainer(modes[j] + joyStickButtonNames_[i]);
312        if (!cont)
313        {
314          cont = new ConfigValueContainer(CFT_Keybindings, getIdentifier(), modes[j] + joyStickButtonNames_[i], "");
315          getIdentifier()->addConfigValueContainer(modes[j] + joyStickButtonNames_[i], cont);
316        }
317        switch (j)
318        {
319          case 0:
320            cont->getValue(bindingsJoyStickButtonPress_ + i);
321            break;
322          case 1:
323            cont->getValue(bindingsJoyStickButtonRelease_ + i);
324            break;
325          case 2:
326            cont->getValue(bindingsJoyStickButtonHold_ + i);
327        }
328      }
329    }
330  }
331
332  /**
333    @brief Overwrites all bindings with ""
334  */
335  void KeyBinder::clearBindings()
336  {
337    for (int i = 0; i < numberOfKeys_s; i++)
338    {
339      bindingsKeyPress_  [i] = "";
340      bindingsKeyRelease_[i] = "";
341      bindingsKeyHold_   [i] = "";
342    }
343    for (int i = 0; i < numberOfMouseButtons_s; i++)
344    {
345      bindingsMouseButtonPress_  [i] = "";
346      bindingsMouseButtonRelease_[i] = "";
347      bindingsMouseButtonHold_   [i] = "";
348    }
349    for (int i = 0; i < numberOfJoyStickButtons_s; i++)
350    {
351      bindingsJoyStickButtonPress_  [i] = "";
352      bindingsJoyStickButtonRelease_[i] = "";
353      bindingsJoyStickButtonHold_   [i] = "";
354    }
355  }
356
357  /**
358    @brief Loads the key and button bindings.
359    @return True if loading succeeded.
360  */
361  bool KeyBinder::loadBindings()
362  {
363    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings..." << std::endl;
364
365    ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini");
366    setConfigValues();
367
368    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl;
369    return true;
370  }
371
372
373  /**
374    @brief Event handler for the keyPressed Event.
375    @param e Event information
376  */
377  bool KeyBinder::keyPressed(const OIS::KeyEvent &e)
378  {
379    COUT(3) << "Key: " << e.key << std::endl;
380    // find the appropriate key binding
381    std::string cmdStr = bindingsKeyPress_[int(e.key)];
382    if (cmdStr != "")
383    {
384      CommandExecutor::execute(cmdStr);
385      COUT(3) << "Executing command: " << cmdStr << std::endl;
386    }
387   
388    return true;
389  }
390
391  /**
392    @brief Event handler for the keyReleased Event.
393    @param e Event information
394  */
395  bool KeyBinder::keyReleased(const OIS::KeyEvent &e)
396  {
397    // find the appropriate key binding
398    std::string cmdStr = bindingsKeyRelease_[int(e.key)];
399    if (cmdStr != "")
400    {
401      CommandExecutor::execute(cmdStr);
402      COUT(3) << "Executing command: " << cmdStr << std::endl;
403    }
404
405    return true;
406  }
407
408  /**
409    @brief Event handler for the keyHeld Event.
410    @param e Event information
411  */
412  bool KeyBinder::keyHeld(const OIS::KeyEvent &e)
413  {
414    // find the appropriate key binding
415    std::string cmdStr = bindingsKeyHold_[int(e.key)];
416    if (cmdStr != "")
417    {
418      CommandExecutor::execute(cmdStr);
419      COUT(3) << "Executing command: " << cmdStr << std::endl;
420    }
421
422    return true;
423  }
424
425  /**
426    @brief Event handler for the mouseMoved Event.
427    @param e Event information
428  */
429  bool KeyBinder::mouseMoved(const OIS::MouseEvent &e)
430  {
431    return true;
432  }
433
434  /**
435    @brief Event handler for the mousePressed Event.
436    @param e Event information
437    @param id The ID of the mouse button
438  */
439  bool KeyBinder::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
440  {
441    // find the appropriate key binding
442    std::string cmdStr = bindingsMouseButtonPress_[int(id)];
443    if (cmdStr != "")
444    {
445      CommandExecutor::execute(cmdStr);
446      COUT(3) << "Executing command: " << cmdStr << std::endl;
447    }
448
449    return true;
450  }
451
452  /**
453    @brief Event handler for the mouseReleased Event.
454    @param e Event information
455    @param id The ID of the mouse button
456  */
457  bool KeyBinder::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
458  {
459    // find the appropriate key binding
460    std::string cmdStr = bindingsMouseButtonRelease_[int(id)];
461    if (cmdStr != "")
462    {
463      CommandExecutor::execute(cmdStr);
464      COUT(3) << "Executing command: " << cmdStr << std::endl;
465    }
466
467    return true;
468  }
469
470  /**
471    @brief Event handler for the mouseHeld Event.
472    @param e Event information
473    @param id The ID of the mouse button
474  */
475  bool KeyBinder::mouseHeld(const OIS::MouseEvent &e, OIS::MouseButtonID id)
476  {
477    // find the appropriate key binding
478    std::string cmdStr = bindingsMouseButtonHold_[int(id)];
479    if (cmdStr != "")
480    {
481      CommandExecutor::execute(cmdStr);
482      COUT(3) << "Executing command: " << cmdStr << std::endl;
483    }
484
485    return true;
486  }
487
488  bool KeyBinder::buttonPressed(const OIS::JoyStickEvent &arg, int button)
489  {
490    // find the appropriate key binding
491    std::string cmdStr = bindingsJoyStickButtonPress_[button];
492    if (cmdStr != "")
493    {
494      CommandExecutor::execute(cmdStr);
495      COUT(3) << "Executing command: " << cmdStr << std::endl;
496    }
497
498    return true;
499  }
500
501  bool KeyBinder::buttonReleased(const OIS::JoyStickEvent &arg, int button)
502  {
503    // find the appropriate key binding
504    std::string cmdStr = bindingsJoyStickButtonRelease_[button];
505    if (cmdStr != "")
506    {
507      CommandExecutor::execute(cmdStr);
508      COUT(3) << "Executing command: " << cmdStr << std::endl;
509    }
510
511    return true;
512  }
513
514  bool KeyBinder::buttonHeld(const OIS::JoyStickEvent &arg, int button)
515  {
516    // find the appropriate key binding
517    std::string cmdStr = bindingsJoyStickButtonHold_[button];
518    if (cmdStr != "")
519    {
520      CommandExecutor::execute(cmdStr);
521      COUT(3) << "Executing command: " << cmdStr << std::endl;
522    }
523
524    return true;
525  }
526
527  bool KeyBinder::axisMoved(const OIS::JoyStickEvent &arg, int axis)
528  {
529    return true;
530  }
531
532  bool KeyBinder::sliderMoved(const OIS::JoyStickEvent &arg, int id)
533  {
534    return true;
535  }
536
537  bool KeyBinder::povMoved(const OIS::JoyStickEvent &arg, int id)
538  {
539    return true;
540  }
541
542
543
544  // ###############################
545  // ###     GUIInputHandler     ###
546  // ###############################
547
548  ///**
549  //  @brief standard constructor
550  //*/
551  //GUIInputHandler::GUIInputHandler()
552  //{
553  //}
554
555  ///**
556  //  @brief Destructor
557  //*/
558  //GUIInputHandler::~GUIInputHandler()
559  //{
560  //}
561
562  ///**
563  //  @brief Event handler for the keyPressed Event.
564  //  @param e Event information
565  //*/
566  //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e)
567  //{
568                ////CEGUI::System::getSingleton().injectKeyDown( arg.key );
569                ////CEGUI::System::getSingleton().injectChar( arg.text );
570  //  return true;
571  //}
572
573  ///**
574  //  @brief Event handler for the keyReleased Event.
575  //  @param e Event information
576  //*/
577  //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e)
578  //{
579                ////CEGUI::System::getSingleton().injectKeyUp( arg.key );
580  //  return true;
581  //}
582
583  ///**
584  //  @brief Event handler for the mouseMoved Event.
585  //  @param e Event information
586  //*/
587  //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e)
588  //{
589                ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
590  //  return true;
591  //}
592
593  ///**
594  //  @brief Event handler for the mousePressed Event.
595  //  @param e Event information
596  //  @param id The ID of the mouse button
597  //*/
598  //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
599  //{
600                ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
601  //  return true;
602  //}
603
604  ///**
605  //  @brief Event handler for the mouseReleased Event.
606  //  @param e Event information
607  //  @param id The ID of the mouse button
608  //*/
609  //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
610  //{
611                ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
612  //  return true;
613  //}
614
615}
Note: See TracBrowser for help on using the repository browser.