Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/InputHandler.cc @ 936

Last change on this file since 936 was 934, checked in by rgrieder, 17 years ago
  • added debug output to the InputHandler
  • fixed singleton issues
  • cleaned up Main.cc (replaced WinMain by main)
File size: 7.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Reto Grieder
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/**
29 @file
30 @brief Implementation of a little Input handler that distributes everything
31        coming from OIS.
32 */
33
34#include "OrxonoxStableHeaders.h"
35
36#include "core/CoreIncludes.h"
37#include "core/Debug.h"
38#include "Orxonox.h"
39#include "InputEventListener.h"
40#include "InputHandler.h"
41
42namespace orxonox
43{
44  /**
45    @brief The reference to the singleton
46  */
47  InputHandler* InputHandler::singletonRef_s = 0;
48
49  /**
50    @brief Constructor only resets the pointer values to 0.
51  */
52  InputHandler::InputHandler() :
53      mouse_(0), keyboard_(0), inputSystem_(0)
54  {
55    //RegisterObject(InputHandler);
56  }
57
58  /**
59    @brief Destructor only called at the end of the program
60  */
61  InputHandler::~InputHandler()
62  {
63  }
64
65  /**
66    @brief The one instance of the InputHandler is stored in this function.
67    @return The pointer to the only instance of the InputHandler
68  */
69  InputHandler *InputHandler::getSingleton()
70  {
71    if (!singletonRef_s)
72      singletonRef_s = new InputHandler();
73    return singletonRef_s;
74    //static InputHandler theOnlyInstance;
75    //return &theOnlyInstance;
76  }
77
78  /**
79    @brief Creates the OIS::InputMananger, the keyboard and the mouse and
80           assigns the key bindings.
81    @param windowHnd The window handle of the render window
82    @param windowWidth The width of the render window
83    @param windowHeight The height of the render window
84  */
85  bool InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight)
86  {
87    if (!this->inputSystem_)
88    {
89      // Setup basic variables
90      OIS::ParamList paramList;
91      std::ostringstream windowHndStr;
92
93      // Fill parameter list
94      windowHndStr << (unsigned int)windowHnd;
95      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
96
97#if defined OIS_LINUX_PLATFORM
98      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
99#endif
100
101      try
102      {
103        // Create inputsystem
104        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
105        //if (getSoftDebugLevel() >= ORX_DEBUG)
106        //  orxonox::OutputHandler::getOutStream().setOutputLevel(4) << "asdfblah" << std::endl;
107        COUT(ORX_DEBUG) << "*** InputHandler: Created OIS input system" << std::endl;
108
109        // If possible create a buffered keyboard
110        if (inputSystem_->numKeyboards() > 0)
111        {
112          keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
113          keyboard_->setEventCallback(this);
114          COUT(ORX_DEBUG) << "*** InputHandler: Created OIS mouse" << std::endl;
115        }
116
117        // If possible create a buffered mouse
118        if (inputSystem_->numMice() > 0 )
119        {
120          mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
121          mouse_->setEventCallback(this);
122          COUT(ORX_DEBUG) << "*** InputHandler: Created OIS keyboard" << std::endl;
123
124          // Set mouse region
125          this->setWindowExtents(windowWidth, windowHeight);
126        }
127      }
128      catch (OIS::Exception ex)
129      {
130        // something went wrong with the initialisation
131        COUT(ORX_ERROR) << "Error: Failed creating an input system. Message: \"" << ex.eText << "\"" << std::endl;
132        this->inputSystem_ = 0;
133        return false;
134      }
135    }
136
137    COUT(ORX_DEBUG) << "*** InputHandler: Loading key bindings..." << std::endl;
138    // temporary solution: create event list
139    //InputEvent[] list = this->createEventList();
140    // load the key bindings
141    InputEvent empty = {0, false, 0, 0, 0};
142    for (int i = 0; i < this->numberOfKeys_; i++)
143      this->bindingsKeyPressed_[i] = empty;
144
145    //assign 'abort' to the escape key
146    this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
147    COUT(ORX_DEBUG) << "*** InputHandler: Loading done." << std::endl;
148
149    return true;
150  }
151
152  /**
153    @brief Destroys all the created input devices.
154  */
155  void InputHandler::destroyDevices()
156  {
157    COUT(ORX_DEBUG) << "*** InputHandler: Destroying InputHandler..." << std::endl;
158    if (this->mouse_)
159      this->inputSystem_->destroyInputObject(mouse_);
160    if (this->keyboard_)
161      this->inputSystem_->destroyInputObject(keyboard_);
162    if (this->inputSystem_)
163      OIS::InputManager::destroyInputSystem(this->inputSystem_);
164
165    this->mouse_         = 0;
166    this->keyboard_      = 0;
167    this->inputSystem_   = 0;
168    COUT(ORX_DEBUG) << "*** InputHandler: Destroying done." << std::endl;
169  }
170
171  /**
172    @brief Destroys the singleton.
173  */
174  void InputHandler::destroy()
175  {
176    if (singletonRef_s)
177      delete singletonRef_s;
178    singletonRef_s = 0;
179  }
180
181  /**
182    @brief Updates the InputHandler
183    @param dt Delta time
184  */
185  void InputHandler::tick(float dt)
186  {
187    // capture all the input. That calls the event handlers.
188    if (mouse_)
189      mouse_->capture();
190
191    if (keyboard_)
192      keyboard_->capture();
193  }
194
195  /**
196    @brief Adjusts the mouse window metrics.
197    This method has to be called every time the size of the window changes.
198    @param width The new width of the render window
199    @param height the new height of the render window
200  */
201  void InputHandler::setWindowExtents(int width, int height)
202  {
203    // Set mouse region (if window resizes, we should alter this to reflect as well)
204    const OIS::MouseState &mouseState = mouse_->getMouseState();
205    mouseState.width  = width;
206    mouseState.height = height;
207  }
208
209  /**
210    @brief Calls all the objects from classes that derive from InputEventListener.
211    @param evt The input event that occured.
212  */
213  inline void InputHandler::callListeners(orxonox::InputEvent &evt)
214  {
215    for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; )
216    {
217      if (it->bActive_)
218        (it++)->eventOccured(evt);
219      else
220        it++;
221    }
222  }
223
224  /**
225    @brief Event handler for the keyPressed Event.
226    @param e Event information
227  */
228  bool InputHandler::keyPressed(const OIS::KeyEvent &e)
229  {
230    callListeners(this->bindingsKeyPressed_[(int)e.key]);
231    return true;
232  }
233
234  /**
235    @brief Event handler for the keyReleased Event.
236    @param e Event information
237  */
238  bool InputHandler::keyReleased(const OIS::KeyEvent &e)
239  {
240    return true;
241  }
242
243  /**
244    @brief Event handler for the mouseMoved Event.
245    @param e Event information
246  */
247  bool InputHandler::mouseMoved(const OIS::MouseEvent &e)
248  {
249    return true;
250  }
251
252  /**
253    @brief Event handler for the mousePressed Event.
254    @param e Event information
255    @param id The ID of the mouse button
256  */
257  bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
258  {
259    return true;
260  }
261
262  /**
263    @brief Event handler for the mouseReleased Event.
264    @param e Event information
265    @param id The ID of the mouse button
266  */
267  bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
268  {
269    return true;
270  }
271
272}
Note: See TracBrowser for help on using the repository browser.