Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1215 was 1215, checked in by rgrieder, 16 years ago
  • SpaceShip is now a default listener for mouse input. still a hack..
  • several changes in InputManager.cc regarding joy sticks
  • Key bindings work, but not very advanced
File size: 14.5 KB
RevLine 
[971]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[971]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 */
[973]28
[971]29/**
30 @file
[973]31 @brief Implementation of the different input handlers.
[971]32 */
33
[1062]34#include "InputHandler.h"
[1022]35#include "Debug.h"
[1213]36#include "ConfigValueIncludes.h"
37#include "CoreIncludes.h"
[1022]38#include "util/Convert.h"
[1182]39#include "core/CommandExecutor.h"
[971]40
41namespace orxonox
42{
[973]43  // ###############################
[1203]44  // ######     KeyBinder     ######
[973]45  // ###############################
46
[971]47  /**
[1203]48    @brief Constructor that does as little as necessary.
[971]49  */
[1203]50  KeyBinder::KeyBinder()
[973]51  {
[1213]52    RegisterObject(KeyBinder);
[1203]53    clearBindings();
[1213]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);
[973]239  }
[971]240
[973]241  /**
242    @brief Destructor
243  */
[1203]244  KeyBinder::~KeyBinder()
[973]245  {
246  }
247
248  /**
[1213]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        {
[1215]269          // note: the first element in the struct is the string, so the following pointer
270          //       arithmetic works.
[1213]271          case 0:
[1215]272            cont->getValue(&bindingsKeyPress_[i].commandStr);
[1213]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  /**
[1203]335    @brief Overwrites all bindings with ""
[1022]336  */
[1203]337  void KeyBinder::clearBindings()
[1022]338  {
339    for (int i = 0; i < numberOfKeys_s; i++)
340    {
[1215]341      bindingsKeyPress_  [i].commandStr = "";
342      bindingsKeyRelease_[i].commandStr = "";
343      bindingsKeyHold_   [i].commandStr = "";
[1022]344    }
[1203]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
[1213]367    ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini");
368    setConfigValues();
[1203]369
[1215]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
[1203]381    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl;
[1022]382    return true;
383  }
384
[1215]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(3) << "Executing command: " << binding.commandStr << std::endl;
396      CommandExecutor::execute(binding.commandStr);
397    }
[1203]398
[1215]399    return true;
400  }
401
402
[1022]403  /**
[973]404    @brief Event handler for the keyPressed Event.
405    @param e Event information
406  */
[1203]407  bool KeyBinder::keyPressed(const OIS::KeyEvent &e)
[973]408  {
[1213]409    COUT(3) << "Key: " << e.key << std::endl;
[1182]410    // find the appropriate key binding
[1215]411    executeBinding(bindingsKeyPress_[int(e.key)]);
412     
[973]413    return true;
414  }
415
416  /**
417    @brief Event handler for the keyReleased Event.
418    @param e Event information
419  */
[1203]420  bool KeyBinder::keyReleased(const OIS::KeyEvent &e)
[973]421  {
[1203]422    // find the appropriate key binding
[1215]423    executeBinding(bindingsKeyRelease_[int(e.key)]);
[1182]424
[1203]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  {
[1022]434    // find the appropriate key binding
[1215]435    executeBinding(bindingsKeyHold_[int(e.key)]);
[1203]436
[973]437    return true;
438  }
439
440  /**
441    @brief Event handler for the mouseMoved Event.
442    @param e Event information
443  */
[1203]444  bool KeyBinder::mouseMoved(const OIS::MouseEvent &e)
[973]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  */
[1203]454  bool KeyBinder::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
[973]455  {
[1203]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
[973]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  */
[1203]472  bool KeyBinder::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
[973]473  {
[1203]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
[973]482    return true;
483  }
484
[1022]485  /**
[1203]486    @brief Event handler for the mouseHeld Event.
487    @param e Event information
488    @param id The ID of the mouse button
[1182]489  */
[1203]490  bool KeyBinder::mouseHeld(const OIS::MouseEvent &e, OIS::MouseButtonID id)
[1182]491  {
[1203]492    // find the appropriate key binding
493    std::string cmdStr = bindingsMouseButtonHold_[int(id)];
494    if (cmdStr != "")
[1182]495    {
[1203]496      CommandExecutor::execute(cmdStr);
497      COUT(3) << "Executing command: " << cmdStr << std::endl;
[1182]498    }
[1203]499
500    return true;
[1182]501  }
502
[1215]503  bool KeyBinder::buttonPressed(int joyStickID, const OIS::JoyStickEvent &arg, int button)
[1022]504  {
[1203]505    // find the appropriate key binding
506    std::string cmdStr = bindingsJoyStickButtonPress_[button];
507    if (cmdStr != "")
[1022]508    {
[1203]509      CommandExecutor::execute(cmdStr);
510      COUT(3) << "Executing command: " << cmdStr << std::endl;
[1022]511    }
[1203]512
513    return true;
514  }
515
[1215]516  bool KeyBinder::buttonReleased(int joyStickID, const OIS::JoyStickEvent &arg, int button)
[1203]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
[1215]529  bool KeyBinder::buttonHeld(int joyStickID, const OIS::JoyStickEvent &arg, int button)
[1203]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;
[1022]540  }
541
[1215]542  bool KeyBinder::axisMoved(int joyStickID, const OIS::JoyStickEvent &arg, int axis)
[1203]543  {
544    return true;
545  }
546
[1215]547  bool KeyBinder::sliderMoved(int joyStickID, const OIS::JoyStickEvent &arg, int id)
[1203]548  {
549    return true;
550  }
551
[1215]552  bool KeyBinder::povMoved(int joyStickID, const OIS::JoyStickEvent &arg, int id)
[1203]553  {
554    return true;
555  }
[1215]556
557  bool KeyBinder::vector3Moved(int joyStickID, const OIS::JoyStickEvent &arg, int id)
558  {
559    return true;
560  }
[1022]561
[1203]562
563
[973]564  // ###############################
[1203]565  // ###     GUIInputHandler     ###
[973]566  // ###############################
567
[1203]568  ///**
569  //  @brief standard constructor
570  //*/
571  //GUIInputHandler::GUIInputHandler()
572  //{
573  //}
[973]574
[1203]575  ///**
576  //  @brief Destructor
577  //*/
578  //GUIInputHandler::~GUIInputHandler()
579  //{
580  //}
[973]581
[1203]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  //}
[973]592
[1203]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  //}
[973]602
[1203]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  //}
[973]612
[1203]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  //}
[973]623
[1203]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  //}
[973]634
[971]635}
Note: See TracBrowser for help on using the repository browser.