Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/input/ExtendedInputState.cc @ 1645

Last change on this file since 1645 was 1642, checked in by rgrieder, 16 years ago
  • changed the static interface of the InputManager to a member one with getInstance()
  • converted InputManager and InGameConsole to Ogre Singletons (c'tor and d'tor public, but assert in c'tor to prevent multiple instances at runtime)
  • added toluabind_orxonox files to tolua folder that contains a pimped version of tolua (I'll write a little cmake project soon to automate it; Currently that only works with msvc)
  • commented out Loader::unload() from Orxonox destructor because that deleted the ParticleSpawners, which were using a method of a sceneNode that belonged to a already destroyed SpaceShip. —> Results in a memory leak. Previously Loader::unload() was called for all BaseObjects (now calling unload(level_)). And since 'P' from ParticleSpawner comes before 'S' like SpaceShip, the order was correct.
  • Added factory feature for InputStates (can now be created by string if there is Factory entry for it)
  • Created factory entries for SimpleInputState and ExtendedInputState
  • Property svn:eol-style set to native
File size: 14.7 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
32    Implementation of the ExtendedInputState class.
33*/
34
35#include "ExtendedInputState.h"
36
37#include <assert.h>
38#include "core/Debug.h"
39#include "core/CoreIncludes.h"
40
41namespace orxonox
42{
43    CreateFactory(ExtendedInputState);
44
45    using namespace InputDevice;
46
47    ExtendedInputState::ExtendedInputState()
48    {
49        RegisterObject(ExtendedInputState);
50    }
51
52    void ExtendedInputState::numberOfJoySticksChanged(unsigned int n)
53    {
54        unsigned int oldSize = joyStickHandlers_.size();
55        joyStickHandlers_.resize(n);
56
57        if (oldSize > n)
58        {
59            // we have to add all the handlers in joyStickHandlersAll_ to the joyStickHandlers_[>n]
60            for (unsigned int j = 0; j < joyStickHandlersAll_.size(); ++j)
61            {
62                for (unsigned int i = oldSize; i < n; ++i)
63                {
64                    joyStickHandlers_[i].push_back(joyStickHandlersAll_[j]);
65                }
66            }
67        }
68        update();
69    }
70
71    void ExtendedInputState::keyPressed(const KeyEvent& evt)
72    {
73        for (unsigned int i = 0; i < keyHandlers_.size(); i++)
74            keyHandlers_[i]->keyPressed(evt);
75    }
76
77    void ExtendedInputState::keyReleased(const KeyEvent& evt)
78    {
79        for (unsigned int i = 0; i < keyHandlers_.size(); i++)
80            keyHandlers_[i]->keyReleased(evt);
81    }
82
83    void ExtendedInputState::keyHeld(const KeyEvent& evt)
84    {
85        for (unsigned int i = 0; i < keyHandlers_.size(); i++)
86            keyHandlers_[i]->keyHeld(evt);
87    }
88
89
90    void ExtendedInputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
91    {
92        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
93            mouseHandlers_[i]->mouseMoved(abs, rel, clippingSize);
94    }
95
96    void ExtendedInputState::mouseScrolled(int abs, int rel)
97    {
98        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
99            mouseHandlers_[i]->mouseScrolled(abs, rel);
100    }
101
102    void ExtendedInputState::mouseButtonPressed(MouseButton::Enum id)
103    {
104        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
105            mouseHandlers_[i]->mouseButtonPressed(id);
106    }
107
108    void ExtendedInputState::mouseButtonReleased(MouseButton::Enum id)
109    {
110        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
111            mouseHandlers_[i]->mouseButtonReleased(id);
112    }
113
114    void ExtendedInputState::mouseButtonHeld(MouseButton::Enum id)
115    {
116        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
117            mouseHandlers_[i]->mouseButtonHeld(id);
118    }
119
120
121    void ExtendedInputState::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value)
122    {
123        assert(joyStickID < joyStickHandlers_.size());
124        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
125            joyStickHandlers_[joyStickID][i]->joyStickAxisMoved(joyStickID, axis, value);
126    }
127
128    void ExtendedInputState::joyStickButtonPressed(unsigned int joyStickID, JoyStickButton::Enum id)
129    {
130        assert(joyStickID < joyStickHandlers_.size());
131        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
132            joyStickHandlers_[joyStickID][i]->joyStickButtonPressed(joyStickID, id);
133    }
134
135    void ExtendedInputState::joyStickButtonReleased(unsigned int joyStickID, JoyStickButton::Enum id)
136    {
137        assert(joyStickID < joyStickHandlers_.size());
138        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
139            joyStickHandlers_[joyStickID][i]->joyStickButtonReleased(joyStickID, id);
140    }
141
142    void ExtendedInputState::joyStickButtonHeld(unsigned int joyStickID, JoyStickButton::Enum id)
143    {
144        assert(joyStickID < joyStickHandlers_.size());
145        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
146            joyStickHandlers_[joyStickID][i]->joyStickButtonHeld(joyStickID, id);
147    }
148
149
150    /**
151    @brief
152        Adds a key handler.
153    @param handler
154        Pointer to the handler object.
155    @return
156        True if added, false if handler already existed.
157    */
158    bool ExtendedInputState::addKeyHandler(KeyHandler* handler)
159    {
160        if (!handler)
161            return false;
162
163        // see whether the handler already is in the list
164        for (std::vector<KeyHandler*>::iterator it = keyHandlers_.begin(); it != keyHandlers_.end(); it++)
165        {
166            if ((*it) == handler)
167            {
168                return false;
169            }
170        }
171        keyHandlers_.push_back(handler);
172        update();
173        return true;
174    }
175
176    /**
177    @brief
178        Removes a Key handler from the state.
179    @param handler
180        Pointer to the handler.
181    @return
182        True if removal was successful, false if handler was not found.
183    */
184    bool ExtendedInputState::removeKeyHandler(KeyHandler* handler)
185    {
186        if (!handler)
187            return false;
188
189        for (std::vector<KeyHandler*>::iterator it = keyHandlers_.begin(); it != keyHandlers_.end(); it++)
190        {
191            if ((*it) == handler)
192            {
193                keyHandlers_.erase(it);
194                update();
195                return true;
196            }
197        }
198        return false;
199    }
200
201
202    /**
203    @brief
204        Adds a mouse handler.
205    @param handler
206        Pointer to the handler object.
207    @return
208        True if added, false if handler already existed.
209    */
210    bool ExtendedInputState::addMouseHandler(MouseHandler* handler)
211    {
212        if (!handler)
213            return false;
214
215        // see whether the handler already is in the list
216        for (std::vector<MouseHandler*>::iterator it = mouseHandlers_.begin(); it != mouseHandlers_.end(); it++)
217        {
218            if ((*it) == handler)
219            {
220                return false;
221            }
222        }
223        mouseHandlers_.push_back(handler);
224        update();
225        return true;
226    }
227
228    /**
229    @brief
230        Removes a mouse handler from the state.
231    @param handler
232        Pointer to the handler.
233    @return
234        True if removal was successful, false if handler was not found.
235    */
236    bool ExtendedInputState::removeMouseHandler(MouseHandler* handler)
237    {
238        if (!handler)
239            return false;
240
241        for (std::vector<MouseHandler*>::iterator it = mouseHandlers_.begin(); it != mouseHandlers_.end(); it++)
242        {
243            if ((*it) == handler)
244            {
245                mouseHandlers_.erase(it);
246                update();
247                return true;
248            }
249        }
250        return false;
251    }
252
253
254    /**
255    @brief
256        Adds a joy stick handler.
257    @param handler
258        Pointer to the handler object.
259    @param joyStickID
260        ID of the joy stick
261    @return
262        True if added, false if handler already existed.
263    */
264    bool ExtendedInputState::addJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID)
265    {
266        if (!handler || joyStickID >= joyStickHandlers_.size())
267            return false;
268
269        // see whether the handler already is in the list
270        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlers_[joyStickID].begin();
271            it != joyStickHandlers_[joyStickID].end(); it++)
272        {
273            if ((*it) == handler)
274            {
275                return false;
276            }
277        }
278        joyStickHandlers_[joyStickID].push_back(handler);
279        update();
280        return true;
281    }
282
283    /**
284    @brief
285        Removes a joy stick handler from the state.
286    @param handler
287        Pointer to the handler.
288    @param joyStickID
289        ID of the joy stick
290    @return
291        True if removal was successful, false if handler was not found.
292    */
293    bool ExtendedInputState::removeJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID)
294    {
295        if (!handler || joyStickID >= joyStickHandlers_.size())
296            return false;
297
298        // remove it from the list of all-joystick handlers if present
299        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlersAll_.begin();
300            it != joyStickHandlersAll_.end(); it++)
301        {
302            if ((*it) == handler)
303            {
304                joyStickHandlersAll_.erase(it);
305            }
306        }
307
308        // remove handler from the list of seperate lists of handlers
309        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlers_[joyStickID].begin();
310            it != joyStickHandlers_[joyStickID].end(); it++)
311        {
312            if ((*it) == handler)
313            {
314                joyStickHandlers_[joyStickID].erase(it);
315                update();
316                return true;
317            }
318        }
319        return false;
320    }
321
322    /**
323    @brief
324        Adds a joy stick handler.
325    @param handler
326        Pointer to the handler object.
327    @return
328        True if added, false if handler already existed.
329    */
330    bool ExtendedInputState::addJoyStickHandler(JoyStickHandler* handler)
331    {
332        if (!handler)
333            return false;
334
335        // see whether the handler already is in the big list
336        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlersAll_.begin();
337            it != joyStickHandlersAll_.end(); it++)
338        {
339            if ((*it) == handler)
340            {
341                return false;
342            }
343        }
344        joyStickHandlersAll_.push_back(handler);
345        update();
346
347        // now, we have to add it to all the separate lists
348        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick)
349            addJoyStickHandler(handler, iJoyStick);
350        return true;
351    }
352
353    /**
354    @brief
355        Removes a joy stick handler from the state.
356    @param handler
357        Pointer to the handler.
358    @return
359        True if removal was successful, false if handler was not found.
360    */
361    bool ExtendedInputState::removeJoyStickHandler(JoyStickHandler* handler)
362    {
363        if (!handler)
364            return false;
365
366        bool success = false;
367        // remove from all lists in a loop (also removes it from the big list)
368        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick)
369            success |= removeJoyStickHandler(handler, iJoyStick);
370
371        return success;
372    }
373
374    void ExtendedInputState::removeAndDestroyAllHandlers()
375    {
376        for (std::vector<InputTickable*>::iterator it = allHandlers_.begin();
377            it != allHandlers_.end(); ++it)
378            delete *it;
379
380        allHandlers_.clear();
381        keyHandlers_.clear();
382        mouseHandlers_.clear();
383        joyStickHandlersAll_.clear();
384        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick)
385            joyStickHandlers_[iJoyStick].clear();
386        update();
387    }
388
389    /**
390    @brief
391        Adds a handler of any kind. dynamic_cast determines to which list it is added.
392    @param handler
393        Pointer to the handler object.
394    @return
395        True if added, false if handler already existed.
396    */
397    bool ExtendedInputState::addHandler(InputTickable* handler)
398    {
399        bool success = false;
400
401        success |= addKeyHandler(dynamic_cast<KeyHandler*>(handler));
402        success |= addMouseHandler(dynamic_cast<MouseHandler*>(handler));
403        success |= addJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler));
404
405        return success;
406    }
407
408    /**
409    @brief
410        Removes a handler from all lists.
411    @param handler
412        Pointer to the handler.
413    @return
414        True if removal was successful, false if handler was not found.
415    */
416    bool ExtendedInputState::removeHandler(InputTickable* handler)
417    {
418        bool success = false;
419
420        success |= removeKeyHandler(dynamic_cast<KeyHandler*>(handler));
421        success |= removeMouseHandler(dynamic_cast<MouseHandler*>(handler));
422        success |= removeJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler));
423
424        return success;
425    }
426
427    void ExtendedInputState::tickInput(float dt)
428    {
429        for (unsigned int i = 0; i < allHandlers_.size(); ++i)
430        {
431            allHandlers_[i]->tickInput(dt);
432        }
433    }
434
435    void ExtendedInputState::tickInput(float dt, unsigned int device)
436    {
437        switch (device)
438        {
439        case Keyboard:
440            for (unsigned int i = 0; i < keyHandlers_.size(); ++i)
441                keyHandlers_[i]->tickKey(dt);
442            break;
443
444        case Mouse:
445            for (unsigned int i = 0; i < mouseHandlers_.size(); ++i)
446                mouseHandlers_[i]->tickMouse(dt);
447            break;
448
449        default: // joy sticks
450            for (unsigned int i = 0; i < joyStickHandlers_[device - 2].size(); ++i)
451                joyStickHandlers_[device - 2][i]->tickJoyStick(dt, device - 2);
452            break;
453        }
454    }
455
456    void ExtendedInputState::update()
457    {
458        // we can use a set to have a list of unique pointers (an object can implement all 3 handlers)
459        std::set<InputTickable*> tempSet;
460        for (unsigned int iHandler = 0; iHandler < keyHandlers_.size(); iHandler++)
461            tempSet.insert(keyHandlers_[iHandler]);
462        for (unsigned int iHandler = 0; iHandler < mouseHandlers_.size(); iHandler++)
463            tempSet.insert(mouseHandlers_[iHandler]);
464        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); iJoyStick++)
465            for (unsigned int iHandler = 0; iHandler < joyStickHandlers_[iJoyStick].size(); iHandler++)
466                tempSet.insert(joyStickHandlers_[iJoyStick][iHandler]);
467
468        // copy the content of the map back to the actual vector
469        allHandlers_.clear();
470        for (std::set<InputTickable*>::const_iterator itHandler = tempSet.begin();
471            itHandler != tempSet.end(); itHandler++)
472            allHandlers_.push_back(*itHandler);
473
474        // update the deviceEnabled options
475        setInputDeviceEnabled(Keyboard, (keyHandlers_.size() != 0));
476        setInputDeviceEnabled(Mouse, (mouseHandlers_.size() != 0));
477        for (unsigned int i = 0; i < joyStickHandlers_.size(); ++i)
478            setInputDeviceEnabled(2 + i, (joyStickHandlers_[i].size() != 0));
479    }
480}
Note: See TracBrowser for help on using the repository browser.