Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/input/SimpleInputState.cc @ 1647

Last change on this file since 1647 was 1642, checked in by rgrieder, 17 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: 8.0 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 SimpleInputState class.
33*/
34
35#include "SimpleInputState.h"
36
37#include <assert.h>
38#include "core/Debug.h"
39#include "core/Executor.h"
40#include "core/CoreIncludes.h"
41
42namespace orxonox
43{
44    CreateFactory(SimpleInputState);
45
46    using namespace InputDevice;
47
48    SimpleInputState::SimpleInputState()
49        : keyHandler_(0)
50        , mouseHandler_(0)
51        , joyStickHandlerAll_(0)
52    {
53        RegisterObject(SimpleInputState);
54    }
55
56    void SimpleInputState::numberOfJoySticksChanged(unsigned int n)
57    {
58        unsigned int oldSize = joyStickHandler_.size();
59        joyStickHandler_.resize(n);
60
61        if (oldSize > n)
62        {
63            // we have to add the handler in joyStickHandlerAll_ to the joyStickHandler_[>n]
64            for (unsigned int i = oldSize; i < n; ++i)
65            {
66                joyStickHandler_[i] = joyStickHandlerAll_;
67            }
68        }
69        update();
70    }
71
72    void SimpleInputState::keyPressed(const KeyEvent& evt)
73    {
74        if (keyHandler_)
75            keyHandler_->keyPressed(evt);
76    }
77
78    void SimpleInputState::keyReleased(const KeyEvent& evt)
79    {
80        if (keyHandler_)
81            keyHandler_->keyReleased(evt);
82    }
83
84    void SimpleInputState::keyHeld(const KeyEvent& evt)
85    {
86        if (keyHandler_)
87            keyHandler_->keyHeld(evt);
88    }
89
90
91    void SimpleInputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
92    {
93        if (mouseHandler_)
94            mouseHandler_->mouseMoved(abs, rel, clippingSize);
95    }
96
97    void SimpleInputState::mouseScrolled(int abs, int rel)
98    {
99        if (mouseHandler_)
100            mouseHandler_->mouseScrolled(abs, rel);
101    }
102
103    void SimpleInputState::mouseButtonPressed(MouseButton::Enum id)
104    {
105        if (mouseHandler_)
106            mouseHandler_->mouseButtonPressed(id);
107    }
108
109    void SimpleInputState::mouseButtonReleased(MouseButton::Enum id)
110    {
111        if (mouseHandler_)
112            mouseHandler_->mouseButtonReleased(id);
113    }
114
115    void SimpleInputState::mouseButtonHeld(MouseButton::Enum id)
116    {
117        if (mouseHandler_)
118            mouseHandler_->mouseButtonHeld(id);
119    }
120
121
122    void SimpleInputState::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value)
123    {
124        assert(joyStickID < joyStickHandler_.size());
125        if (joyStickHandler_[joyStickID])
126            joyStickHandler_[joyStickID]->joyStickAxisMoved(joyStickID, axis, value);
127    }
128
129    void SimpleInputState::joyStickButtonPressed(unsigned int joyStickID, JoyStickButton::Enum id)
130    {
131        assert(joyStickID < joyStickHandler_.size());
132        if (joyStickHandler_[joyStickID])
133            joyStickHandler_[joyStickID]->joyStickButtonPressed(joyStickID, id);
134    }
135
136    void SimpleInputState::joyStickButtonReleased(unsigned int joyStickID, JoyStickButton::Enum id)
137    {
138        assert(joyStickID < joyStickHandler_.size());
139        if (joyStickHandler_[joyStickID])
140            joyStickHandler_[joyStickID]->joyStickButtonReleased(joyStickID, id);
141    }
142
143    void SimpleInputState::joyStickButtonHeld(unsigned int joyStickID, JoyStickButton::Enum id)
144    {
145        assert(joyStickID < joyStickHandler_.size());
146        if (joyStickHandler_[joyStickID])
147            joyStickHandler_[joyStickID]->joyStickButtonHeld(joyStickID, id);
148    }
149
150    /**
151    @brief
152        Adds a joy stick handler.
153    @param handler
154        Pointer to the handler object.
155    @param joyStickID
156        ID of the joy stick
157    @return
158        True if added, false otherwise.
159    */
160    bool SimpleInputState::setJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID)
161    {
162        if (joyStickID >= joyStickHandler_.size())
163            return false;
164
165        joyStickHandler_[joyStickID] = handler;
166        update();
167        return true;
168    }
169
170    /**
171    @brief
172        Adds a joy stick handler.
173    @param handler
174        Pointer to the handler object.
175    @return
176        True if added, false if handler already existed.
177    */
178    bool SimpleInputState::setJoyStickHandler(JoyStickHandler* handler)
179    {
180        if (handler == joyStickHandlerAll_)
181            return false;
182
183        joyStickHandlerAll_ = handler;
184        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); ++iJoyStick)
185            setJoyStickHandler(handler, iJoyStick);
186        update();
187        return true;
188    }
189
190    void SimpleInputState::removeAndDestroyAllHandlers()
191    {
192        for (std::vector<InputTickable*>::iterator it = allHandlers_.begin();
193            it != allHandlers_.end(); ++it)
194            delete *it;
195
196        allHandlers_.clear();
197        keyHandler_ = 0;
198        mouseHandler_ = 0;
199        joyStickHandlerAll_ = 0;
200        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); ++iJoyStick)
201            joyStickHandler_[iJoyStick] = 0;
202
203        update();
204    }
205
206    /**
207    @brief
208        Adds a handler of any kind. dynamic_cast determines to which list it is added.
209    @param handler
210        Pointer to the handler object.
211    @return
212        True if added, false if handler already existed.
213    */
214    bool SimpleInputState::setHandler(InputTickable* handler)
215    {
216        setKeyHandler(dynamic_cast<KeyHandler*>(handler));
217        setMouseHandler(dynamic_cast<MouseHandler*>(handler));
218        return setJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler));
219    }
220
221    void SimpleInputState::tickInput(float dt)
222    {
223        for (unsigned int i = 0; i < allHandlers_.size(); ++i)
224        {
225            allHandlers_[i]->tickInput(dt);
226        }
227    }
228
229    void SimpleInputState::tickInput(float dt, unsigned int device)
230    {
231        switch (device)
232        {
233        case Keyboard:
234            if (keyHandler_)
235                keyHandler_->tickKey(dt);
236            break;
237
238        case Mouse:
239            if (mouseHandler_)
240                mouseHandler_->tickMouse(dt);
241            break;
242
243        default: // joy sticks
244            if (joyStickHandler_[device - 2])
245                joyStickHandler_[device - 2]->tickJoyStick(dt, device - 2);
246            break;
247        }
248    }
249
250    void SimpleInputState::update()
251    {
252        // we can use a set to have a list of unique pointers (an object can implement all 3 handlers)
253        std::set<InputTickable*> tempSet;
254        if (keyHandler_)
255            tempSet.insert(keyHandler_);
256        if (mouseHandler_)
257            tempSet.insert(mouseHandler_);
258        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); iJoyStick++)
259            if (joyStickHandler_[iJoyStick])
260                tempSet.insert(joyStickHandler_[iJoyStick]);
261
262        // copy the content of the map back to the actual vector
263        allHandlers_.clear();
264        for (std::set<InputTickable*>::const_iterator itHandler = tempSet.begin();
265            itHandler != tempSet.end(); itHandler++)
266            allHandlers_.push_back(*itHandler);
267
268        // update the deviceEnabled options
269        setInputDeviceEnabled(Keyboard, (keyHandler_ != 0));
270        setInputDeviceEnabled(Mouse, (mouseHandler_ != 0));
271        for (unsigned int i = 0; i < joyStickHandler_.size(); ++i)
272            setInputDeviceEnabled(2 + i, (joyStickHandler_[i] != 0));
273    }
274}
Note: See TracBrowser for help on using the repository browser.