Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/input/SimpleInputState.cc @ 1881

Last change on this file since 1881 was 1881, checked in by rgrieder, 15 years ago

Changed initialisation of internally handled InputStates and InputHandlers.

  • Property svn:eol-style set to native
File size: 7.5 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 "util/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    /**
187    @brief
188        Adds a handler of any kind. dynamic_cast determines to which list it is added.
189    @param handler
190        Pointer to the handler object.
191    @return
192        True if added, false if handler already existed.
193    */
194    bool SimpleInputState::setHandler(InputHandler* handler)
195    {
196        setKeyHandler(dynamic_cast<KeyHandler*>(handler));
197        setMouseHandler(dynamic_cast<MouseHandler*>(handler));
198        return setJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler));
199    }
200
201    void SimpleInputState::tickInput(float dt)
202    {
203        for (unsigned int i = 0; i < allHandlers_.size(); ++i)
204        {
205            allHandlers_[i]->tickInput(dt);
206        }
207    }
208
209    void SimpleInputState::tickInput(float dt, unsigned int device)
210    {
211        switch (device)
212        {
213        case Keyboard:
214            if (keyHandler_)
215                keyHandler_->tickKey(dt);
216            break;
217
218        case Mouse:
219            if (mouseHandler_)
220                mouseHandler_->tickMouse(dt);
221            break;
222
223        default: // joy sticks
224            if (joyStickHandler_[device - 2])
225                joyStickHandler_[device - 2]->tickJoyStick(dt, device - 2);
226            break;
227        }
228    }
229
230    void SimpleInputState::update()
231    {
232        // we can use a set to have a list of unique pointers (an object can implement all 3 handlers)
233        std::set<InputHandler*> tempSet;
234        if (keyHandler_)
235            tempSet.insert(keyHandler_);
236        if (mouseHandler_)
237            tempSet.insert(mouseHandler_);
238        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); iJoyStick++)
239            if (joyStickHandler_[iJoyStick])
240                tempSet.insert(joyStickHandler_[iJoyStick]);
241
242        // copy the content of the map back to the actual vector
243        allHandlers_.clear();
244        for (std::set<InputHandler*>::const_iterator itHandler = tempSet.begin();
245            itHandler != tempSet.end(); itHandler++)
246            allHandlers_.push_back(*itHandler);
247
248        // update the deviceEnabled options
249        setInputDeviceEnabled(Keyboard, (keyHandler_ != 0));
250        setInputDeviceEnabled(Mouse, (mouseHandler_ != 0));
251        for (unsigned int i = 0; i < joyStickHandler_.size(); ++i)
252            setInputDeviceEnabled(2 + i, (joyStickHandler_[i] != 0));
253
254        // inform InputManager that there might be changes in EMPTY_HANDLER situation
255        bHandlersChanged_ = true;
256    }
257}
Note: See TracBrowser for help on using the repository browser.