Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1259 was 1259, checked in by rgrieder, 17 years ago

many minor changes, but it's more of a 'svn save' since I'm gonna start rewriting parts of the keybinder

File size: 14.7 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"
[1236]35#include "util/Convert.h"
[1022]36#include "Debug.h"
[1219]37#include "ConfigValueIncludes.h"
38#include "CoreIncludes.h"
[1236]39#include "CommandExecutor.h"
[971]40
41namespace orxonox
42{
[973]43  // ###############################
[1219]44  // ######     KeyBinder     ######
[973]45  // ###############################
46
[971]47  /**
[1219]48    @brief Constructor that does as little as necessary.
[971]49  */
[1219]50  KeyBinder::KeyBinder()
[973]51  {
[1219]52    RegisterObject(KeyBinder);
53    clearBindings();
54
55    std::string keyNames[] = {
56    "UNASSIGNED",
[1237]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",
[1219]140    "","",
[1237]141    "OEM_102",
142    "F11",
143    "F12",
[1219]144    "","","","","","","","","","","",
[1237]145    "F13",
146    "F14",
147    "F15",
[1219]148    "","","","","","","","","","",
[1237]149    "KANA",
[1219]150    "","",
[1237]151    "ABNT_C1",
[1219]152    "","","","","",
[1237]153    "CONVERT",
[1219]154    "",
[1237]155    "NOCONVERT",
[1219]156    "",
[1237]157    "YEN",
158    "ABNT_C2",
[1219]159    "","","","","","","","","","","","","","",
[1237]160    "NUMPADEQUALS",
[1219]161    "","",
[1237]162    "PREVTRACK",
163    "AT",
164    "COLON",
165    "UNDERLINE",
166    "KANJI",
167    "STOP",
168    "AX",
169    "UNLABELED",
170    "NEXTTRACK",
[1219]171    "","",
[1237]172    "NUMPADENTER",
173    "RCONTROL",
[1219]174    "","",
[1237]175    "MUTE",
176    "CALCULATOR",
177    "PLAYPAUSE",
[1219]178    "",
[1237]179    "MEDIASTOP",
[1219]180    "","","","","","","","","",
[1237]181    "VOLUMEDOWN",
[1219]182    "",
[1237]183    "VOLUMEUP",
[1219]184    "",
[1237]185    "WEBHOME",
186    "NUMPADCOMMA",
[1219]187    "",
[1237]188    "DIVIDE",
[1219]189    "",
[1237]190    "SYSRQ",
191    "RMENU",
[1219]192    "","","","","","","","","","","","",
[1237]193    "PAUSE",
[1219]194    "",
[1237]195    "HOME",
196    "UP",
197    "PGUP",
[1219]198    "",
[1237]199    "LEFT",
[1219]200    "",
[1237]201    "RIGHT",
[1219]202    "",
[1237]203    "END",
204    "DOWN",
205    "PGDOWN",
206    "INSERT",
207    "DELETE",
[1219]208    "","","","","","","",
[1237]209    "LWIN",
210    "RWIN",
211    "APPS",
212    "POWER",
213    "SLEEP",
[1219]214    "","","",
[1237]215    "WAKE",
[1219]216    "",
[1237]217    "WEBSEARCH",
218    "WEBFAVORITES",
219    "WEBREFRESH",
220    "WEBSTOP",
221    "WEBFORWARD",
222    "WEBBACK",
223    "MYCOMPUTER",
224    "MAIL",
225    "MEDIASELECT"
[1219]226    };
227    for (int i = 0; i < numberOfKeys_s; i++)
228      keyNames_[i] = keyNames[i];
229
230    std::string mouseButtonNames[] = {
[1259]231      "MouseLeft", "MouseRight", "MouseMiddle",
232      "MouseButton3", "MouseButton4", "MouseButton5",
233      "MouseButton6", "MouseButton7" };
[1219]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  */
[1219]244  KeyBinder::~KeyBinder()
[973]245  {
246  }
247
248  /**
[1219]249    @brief Loader for the key bindings, managed by config values.
[1022]250  */
[1219]251  void KeyBinder::setConfigValues()
[1022]252  {
[1219]253    ConfigValueContainer* cont;
254    std::string modes[] = {"P_", "R_", "H_"};
255
256    // keys
[1022]257    for (int i = 0; i < numberOfKeys_s; i++)
258    {
[1219]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].commandStr);
271            break;
272          case 1:
[1236]273            cont->getValue(&bindingsKeyRelease_[i].commandStr);
[1219]274            break;
275          case 2:
[1236]276            cont->getValue(&bindingsKeyHold_[i].commandStr);
[1219]277        }
278      }
[1022]279    }
[1219]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:
[1236]295            cont->getValue(&bindingsMouseButtonPress_[i].commandStr);
[1219]296            break;
297          case 1:
[1236]298            cont->getValue(&bindingsMouseButtonRelease_[i].commandStr);
[1219]299            break;
300          case 2:
[1236]301            cont->getValue(&bindingsMouseButtonHold_[i].commandStr);
[1219]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:
[1236]320            cont->getValue(&bindingsJoyStickButtonPress_[i].commandStr);
[1219]321            break;
322          case 1:
[1236]323            cont->getValue(&bindingsJoyStickButtonRelease_[i].commandStr);
[1219]324            break;
325          case 2:
[1236]326            cont->getValue(&bindingsJoyStickButtonHold_[i].commandStr);
[1219]327        }
328      }
329    }
[1022]330  }
331
332  /**
[1219]333    @brief Overwrites all bindings with ""
[973]334  */
[1219]335  void KeyBinder::clearBindings()
[973]336  {
[1219]337    for (int i = 0; i < numberOfKeys_s; i++)
[1022]338    {
[1219]339      bindingsKeyPress_  [i].commandStr = "";
340      bindingsKeyRelease_[i].commandStr = "";
341      bindingsKeyHold_   [i].commandStr = "";
[1022]342    }
[1219]343    for (int i = 0; i < numberOfMouseButtons_s; i++)
[1066]344    {
[1236]345      bindingsMouseButtonPress_  [i].commandStr = "";
346      bindingsMouseButtonRelease_[i].commandStr = "";
347      bindingsMouseButtonHold_   [i].commandStr = "";
[1066]348    }
[1219]349    for (int i = 0; i < numberOfJoyStickButtons_s; i++)
[1022]350    {
[1236]351      bindingsJoyStickButtonPress_  [i].commandStr = "";
352      bindingsJoyStickButtonRelease_[i].commandStr = "";
353      bindingsJoyStickButtonHold_   [i].commandStr = "";
[1022]354    }
[973]355  }
356
357  /**
[1219]358    @brief Loads the key and button bindings.
359    @return True if loading succeeded.
[973]360  */
[1219]361  bool KeyBinder::loadBindings()
[973]362  {
[1219]363    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings..." << std::endl;
364
365    ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini");
366    setConfigValues();
367
368    // evaluate the key bindings
369    // TODO: what if binding is invalid?
[1236]370    for (int i = 0; i < numberOfKeys_s; i++)
371    {
372      if (bindingsKeyPress_[i].commandStr != "")
373      {
374        bindingsKeyPress_[i].evaluation = CommandExecutor::evaluate(bindingsKeyPress_[i].commandStr);
375        bindingsKeyPress_[i].commandStr = bindingsKeyPress_[i].evaluation.getCommandString();
376      }
377    }
[1219]378
379    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl;
[973]380    return true;
381  }
382
[1259]383  bool KeyBinder::executeSimpleBinding(KeyBinding& binding)
[973]384  {
[1219]385    if (binding.commandStr != "")
386    {
[1236]387      if (binding.commandStr != binding.evaluation.getCommandString())
388      {
389        // key binding has changed, reevaluate the command string.
390        binding.evaluation = CommandExecutor::evaluate(binding.commandStr);
391        binding.commandStr = binding.evaluation.getCommandString();
392      }
[1219]393      COUT(3) << "Executing command: " << binding.commandStr << std::endl;
394      CommandExecutor::execute(binding.commandStr);
395    }
396
[973]397    return true;
398  }
399
[1219]400
[973]401  /**
[1219]402    @brief Event handler for the keyPressed Event.
[973]403    @param e Event information
404  */
[1236]405  bool KeyBinder::keyPressed(const KeyEvent& evt)
[973]406  {
[1219]407    // find the appropriate key binding
[1259]408    executeSimpleBinding(bindingsKeyPress_[int(evt.key)]);
[1236]409
[973]410    return true;
411  }
412
413  /**
[1219]414    @brief Event handler for the keyReleased Event.
[973]415    @param e Event information
416  */
[1236]417  bool KeyBinder::keyReleased(const KeyEvent& evt)
[973]418  {
[1219]419    // find the appropriate key binding
[1259]420    executeSimpleBinding(bindingsKeyRelease_[int(evt.key)]);
[1219]421
[973]422    return true;
423  }
424
[1022]425  /**
[1219]426    @brief Event handler for the keyHeld Event.
[1236]427    @param e Mouse state information
[1022]428  */
[1236]429  bool KeyBinder::keyHeld(const KeyEvent& evt)
[1022]430  {
[1219]431    // find the appropriate key binding
[1259]432    executeSimpleBinding(bindingsKeyHold_[int(evt.key)]);
[1022]433
[1219]434    return true;
[973]435  }
436
437  /**
[1219]438    @brief Event handler for the mouseMoved Event.
[1236]439    @param e Mouse state information
[973]440  */
[1236]441  bool KeyBinder::mouseMoved(const MouseState &evt)
[973]442  {
[1259]443    /*if (bindingMouseMoved_.commandStr != "")
444    {
445      if (bindingMouseMoved_.commandStr != bindingMouseMoved_.evaluation.getCommandString())
446      {
447        // key binding has changed, reevaluate the command string.
448        bindingMouseMoved_.evaluation = CommandExecutor::evaluate(bindingMouseMoved_.commandStr);
449        bindingMouseMoved_.commandStr = bindingMouseMoved_.evaluation.getCommandString();
450      }
451      COUT(3) << "Executing command: " << bindingMouseMoved_.commandStr << std::endl;
452
453      bindingMouseMoved_.evaluation.setEvaluatedParameter(
454      CommandExecutor::execute(bindingMouseMoved_.commandStr);
455    }*/
456
[973]457    return true;
458  }
459
460  /**
[1259]461    @brief Event handler for the mouseScrolled Event.
[1236]462    @param e Mouse state information
463  */
[1259]464  bool KeyBinder::mouseScrolled(const MouseState &evt)
[1236]465  {
466    return true;
467  }
468
469  /**
[1219]470    @brief Event handler for the mousePressed Event.
[973]471    @param e Event information
[1219]472    @param id The ID of the mouse button
[973]473  */
[1236]474  bool KeyBinder::mouseButtonPressed(const MouseState& state, MouseButton::Enum id)
[973]475  {
[1219]476    // find the appropriate key binding
[1259]477    executeSimpleBinding(bindingsMouseButtonPress_[int(id)]);
[1219]478
[973]479    return true;
480  }
481
482  /**
[1219]483    @brief Event handler for the mouseReleased Event.
[973]484    @param e Event information
[1219]485    @param id The ID of the mouse button
[973]486  */
[1236]487  bool KeyBinder::mouseButtonReleased(const MouseState& state, MouseButton::Enum id)
[973]488  {
[1219]489    // find the appropriate key binding
[1259]490    executeSimpleBinding(bindingsMouseButtonRelease_[int(id)]);
[1219]491
[973]492    return true;
493  }
494
495  /**
[1219]496    @brief Event handler for the mouseHeld Event.
[973]497    @param e Event information
498    @param id The ID of the mouse button
499  */
[1236]500  bool KeyBinder::mouseButtonHeld(const MouseState& state, MouseButton::Enum id)
[973]501  {
[1219]502    // find the appropriate key binding
[1259]503    executeSimpleBinding(bindingsMouseButtonHold_[int(id)]);
[1219]504
[973]505    return true;
506  }
507
[1236]508  bool KeyBinder::joyStickButtonPressed(const JoyStickState& state, int button)
[973]509  {
[1219]510    // find the appropriate key binding
[1259]511    executeSimpleBinding(bindingsJoyStickButtonPress_[button]);
[1219]512
[973]513    return true;
[1219]514  }
515
[1236]516  bool KeyBinder::joyStickButtonReleased(const JoyStickState& state, int button)
[1219]517  {
518    // find the appropriate key binding
[1259]519    executeSimpleBinding(bindingsJoyStickButtonRelease_[button]);
[1219]520
521    return true;
522  }
523
[1236]524  bool KeyBinder::joyStickButtonHeld(const JoyStickState& state, int button)
[1219]525  {
526    // find the appropriate key binding
[1259]527    executeSimpleBinding(bindingsJoyStickButtonHold_[button]);
[1219]528
529    return true;
[973]530  }
531
[1236]532  bool KeyBinder::joyStickAxisMoved(const JoyStickState& state, int axis)
[1219]533  {
534    return true;
535  }
536
[1236]537  bool KeyBinder::joyStickSliderMoved(const JoyStickState& state, int index)
[1219]538  {
539    return true;
540  }
541
[1236]542  bool KeyBinder::joyStickPovMoved(const JoyStickState& state, int index)
[1219]543  {
544    return true;
545  }
546
[1236]547  bool KeyBinder::joyStickVector3Moved(const JoyStickState& state, int index)
[1219]548  {
549    return true;
550  }
551
552
553
554  // ###############################
555  // ###     GUIInputHandler     ###
556  // ###############################
557
558  ///**
559  //  @brief standard constructor
560  //*/
561  //GUIInputHandler::GUIInputHandler()
562  //{
563  //}
564
565  ///**
566  //  @brief Destructor
567  //*/
568  //GUIInputHandler::~GUIInputHandler()
569  //{
570  //}
571
572  ///**
573  //  @brief Event handler for the keyPressed Event.
574  //  @param e Event information
575  //*/
576  //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e)
577  //{
[1237]578    ////CEGUI::System::getSingleton().injectKeyDown( arg.key );
579    ////CEGUI::System::getSingleton().injectChar( arg.text );
[1219]580  //  return true;
581  //}
582
583  ///**
584  //  @brief Event handler for the keyReleased Event.
585  //  @param e Event information
586  //*/
587  //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e)
588  //{
[1237]589    ////CEGUI::System::getSingleton().injectKeyUp( arg.key );
[1219]590  //  return true;
591  //}
592
593  ///**
594  //  @brief Event handler for the mouseMoved Event.
595  //  @param e Event information
596  //*/
597  //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e)
598  //{
[1237]599    ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
[1219]600  //  return true;
601  //}
602
603  ///**
604  //  @brief Event handler for the mousePressed Event.
605  //  @param e Event information
606  //  @param id The ID of the mouse button
607  //*/
[1236]608  //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButton id)
[1219]609  //{
[1237]610    ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
[1219]611  //  return true;
612  //}
613
614  ///**
615  //  @brief Event handler for the mouseReleased Event.
616  //  @param e Event information
617  //  @param id The ID of the mouse button
618  //*/
[1236]619  //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButton id)
[1219]620  //{
[1237]621    ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
[1219]622  //  return true;
623  //}
624
[971]625}
Note: See TracBrowser for help on using the repository browser.