Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1997 was 1659, checked in by rgrieder, 16 years ago

reload feature tested and works under windows, but only with one joy stick. Will have to test it with multiple ones when that is properly supported.

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