Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lodfinal/src/core/input/ExtendedInputState.cc @ 2917

Last change on this file since 2917 was 1887, checked in by rgrieder, 16 years ago

FIRST THINGS FIRST: Delete or rename your keybindings.ini (def_keybindings.ini already has the most important bindings) or else you won't be able to do anything!

Changes:

  • Multiple joy stick support should now fully work with KeyBinder too (only tested with 0/1 joystick)
  • Reloading the OIS Devices now works with KeyBinder too
  • Modified ConfigValueContainer to accept arbitrary section names
  • added tkeybind to temporary bind a command to a key
  • Fixed dlleport issue in ArgumentCompletionFunctions.h

Internal changes:

  • General cleanup in initialisation of KeyBinder
  • All names of keys/buttons/axes are now statically saved in InputInterfaces.h
  • Move a magic value in KeyBinder to a configValue (MouseWheelStepSize_)
  • Separated ConfigValues from Keybinding ConfigValueContainer in KeyBinder (looks much nicer now ;))
  • Moved some performance critical small function to the inline section
  • Removed the ugly keybind function construct from the InputManager
  • More 'harmonising' work in KeyBinder
  • Property svn:eol-style set to native
File size: 14.1 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 "util/Debug.h"
39
40namespace orxonox
41{
42    using namespace InputDevice;
43
44    void ExtendedInputState::numberOfJoySticksChanged(unsigned int n)
45    {
46        unsigned int oldSize = joyStickHandlers_.size();
47        joyStickHandlers_.resize(n);
48
49        if (oldSize > n)
50        {
51            // we have to add all the handlers in joyStickHandlersAll_ to the joyStickHandlers_[>n]
52            for (unsigned int j = 0; j < joyStickHandlersAll_.size(); ++j)
53            {
54                for (unsigned int i = oldSize; i < n; ++i)
55                {
56                    joyStickHandlers_[i].push_back(joyStickHandlersAll_[j]);
57                }
58            }
59        }
60        update();
61    }
62
63    void ExtendedInputState::keyPressed(const KeyEvent& evt)
64    {
65        for (unsigned int i = 0; i < keyHandlers_.size(); i++)
66            keyHandlers_[i]->keyPressed(evt);
67    }
68
69    void ExtendedInputState::keyReleased(const KeyEvent& evt)
70    {
71        for (unsigned int i = 0; i < keyHandlers_.size(); i++)
72            keyHandlers_[i]->keyReleased(evt);
73    }
74
75    void ExtendedInputState::keyHeld(const KeyEvent& evt)
76    {
77        for (unsigned int i = 0; i < keyHandlers_.size(); i++)
78            keyHandlers_[i]->keyHeld(evt);
79    }
80
81
82    void ExtendedInputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
83    {
84        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
85            mouseHandlers_[i]->mouseMoved(abs, rel, clippingSize);
86    }
87
88    void ExtendedInputState::mouseScrolled(int abs, int rel)
89    {
90        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
91            mouseHandlers_[i]->mouseScrolled(abs, rel);
92    }
93
94    void ExtendedInputState::mouseButtonPressed(MouseButtonCode::ByEnum id)
95    {
96        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
97            mouseHandlers_[i]->mouseButtonPressed(id);
98    }
99
100    void ExtendedInputState::mouseButtonReleased(MouseButtonCode::ByEnum id)
101    {
102        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
103            mouseHandlers_[i]->mouseButtonReleased(id);
104    }
105
106    void ExtendedInputState::mouseButtonHeld(MouseButtonCode::ByEnum id)
107    {
108        for (unsigned int i = 0; i < mouseHandlers_.size(); i++)
109            mouseHandlers_[i]->mouseButtonHeld(id);
110    }
111
112
113    void ExtendedInputState::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value)
114    {
115        assert(joyStickID < joyStickHandlers_.size());
116        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
117            joyStickHandlers_[joyStickID][i]->joyStickAxisMoved(joyStickID, axis, value);
118    }
119
120    void ExtendedInputState::joyStickButtonPressed(unsigned int joyStickID, JoyStickButtonCode::ByEnum id)
121    {
122        assert(joyStickID < joyStickHandlers_.size());
123        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
124            joyStickHandlers_[joyStickID][i]->joyStickButtonPressed(joyStickID, id);
125    }
126
127    void ExtendedInputState::joyStickButtonReleased(unsigned int joyStickID, JoyStickButtonCode::ByEnum id)
128    {
129        assert(joyStickID < joyStickHandlers_.size());
130        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
131            joyStickHandlers_[joyStickID][i]->joyStickButtonReleased(joyStickID, id);
132    }
133
134    void ExtendedInputState::joyStickButtonHeld(unsigned int joyStickID, JoyStickButtonCode::ByEnum id)
135    {
136        assert(joyStickID < joyStickHandlers_.size());
137        for (unsigned int i = 0; i < joyStickHandlers_[joyStickID].size(); i++)
138            joyStickHandlers_[joyStickID][i]->joyStickButtonHeld(joyStickID, id);
139    }
140
141
142    /**
143    @brief
144        Adds a key handler.
145    @param handler
146        Pointer to the handler object.
147    @return
148        True if added, false if handler already existed.
149    */
150    bool ExtendedInputState::addKeyHandler(KeyHandler* handler)
151    {
152        if (!handler)
153            return false;
154
155        // see whether the handler already is in the list
156        for (std::vector<KeyHandler*>::iterator it = keyHandlers_.begin(); it != keyHandlers_.end(); it++)
157        {
158            if ((*it) == handler)
159            {
160                return false;
161            }
162        }
163        keyHandlers_.push_back(handler);
164        update();
165        return true;
166    }
167
168    /**
169    @brief
170        Removes a Key handler from the state.
171    @param handler
172        Pointer to the handler.
173    @return
174        True if removal was successful, false if handler was not found.
175    */
176    bool ExtendedInputState::removeKeyHandler(KeyHandler* handler)
177    {
178        if (!handler)
179            return false;
180
181        for (std::vector<KeyHandler*>::iterator it = keyHandlers_.begin(); it != keyHandlers_.end(); it++)
182        {
183            if ((*it) == handler)
184            {
185                keyHandlers_.erase(it);
186                update();
187                return true;
188            }
189        }
190        return false;
191    }
192
193
194    /**
195    @brief
196        Adds a mouse handler.
197    @param handler
198        Pointer to the handler object.
199    @return
200        True if added, false if handler already existed.
201    */
202    bool ExtendedInputState::addMouseHandler(MouseHandler* handler)
203    {
204        if (!handler)
205            return false;
206
207        // see whether the handler already is in the list
208        for (std::vector<MouseHandler*>::iterator it = mouseHandlers_.begin(); it != mouseHandlers_.end(); it++)
209        {
210            if ((*it) == handler)
211            {
212                return false;
213            }
214        }
215        mouseHandlers_.push_back(handler);
216        update();
217        return true;
218    }
219
220    /**
221    @brief
222        Removes a mouse handler from the state.
223    @param handler
224        Pointer to the handler.
225    @return
226        True if removal was successful, false if handler was not found.
227    */
228    bool ExtendedInputState::removeMouseHandler(MouseHandler* handler)
229    {
230        if (!handler)
231            return false;
232
233        for (std::vector<MouseHandler*>::iterator it = mouseHandlers_.begin(); it != mouseHandlers_.end(); it++)
234        {
235            if ((*it) == handler)
236            {
237                mouseHandlers_.erase(it);
238                update();
239                return true;
240            }
241        }
242        return false;
243    }
244
245
246    /**
247    @brief
248        Adds a joy stick handler.
249    @param handler
250        Pointer to the handler object.
251    @param joyStickID
252        ID of the joy stick
253    @return
254        True if added, false if handler already existed.
255    */
256    bool ExtendedInputState::addJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID)
257    {
258        if (!handler || joyStickID >= joyStickHandlers_.size())
259            return false;
260
261        // see whether the handler already is in the list
262        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlers_[joyStickID].begin();
263            it != joyStickHandlers_[joyStickID].end(); it++)
264        {
265            if ((*it) == handler)
266            {
267                return false;
268            }
269        }
270        joyStickHandlers_[joyStickID].push_back(handler);
271        update();
272        return true;
273    }
274
275    /**
276    @brief
277        Removes a joy stick handler from the state.
278    @param handler
279        Pointer to the handler.
280    @param joyStickID
281        ID of the joy stick
282    @return
283        True if removal was successful, false if handler was not found.
284    */
285    bool ExtendedInputState::removeJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID)
286    {
287        if (!handler || joyStickID >= joyStickHandlers_.size())
288            return false;
289
290        // remove it from the list of all-joystick handlers if present
291        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlersAll_.begin();
292            it != joyStickHandlersAll_.end(); it++)
293        {
294            if ((*it) == handler)
295            {
296                joyStickHandlersAll_.erase(it);
297            }
298        }
299
300        // remove handler from the list of seperate lists of handlers
301        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlers_[joyStickID].begin();
302            it != joyStickHandlers_[joyStickID].end(); it++)
303        {
304            if ((*it) == handler)
305            {
306                joyStickHandlers_[joyStickID].erase(it);
307                update();
308                return true;
309            }
310        }
311        return false;
312    }
313
314    /**
315    @brief
316        Adds a joy stick handler.
317    @param handler
318        Pointer to the handler object.
319    @return
320        True if added, false if handler already existed.
321    */
322    bool ExtendedInputState::addJoyStickHandler(JoyStickHandler* handler)
323    {
324        if (!handler)
325            return false;
326
327        // see whether the handler already is in the big list
328        for (std::vector<JoyStickHandler*>::iterator it = joyStickHandlersAll_.begin();
329            it != joyStickHandlersAll_.end(); it++)
330        {
331            if ((*it) == handler)
332            {
333                return false;
334            }
335        }
336        joyStickHandlersAll_.push_back(handler);
337        update();
338
339        // now, we have to add it to all the separate lists
340        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick)
341            addJoyStickHandler(handler, iJoyStick);
342        return true;
343    }
344
345    /**
346    @brief
347        Removes a joy stick handler from the state.
348    @param handler
349        Pointer to the handler.
350    @return
351        True if removal was successful, false if handler was not found.
352    */
353    bool ExtendedInputState::removeJoyStickHandler(JoyStickHandler* handler)
354    {
355        if (!handler)
356            return false;
357
358        bool success = false;
359        // remove from all lists in a loop (also removes it from the big list)
360        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); ++iJoyStick)
361            success |= removeJoyStickHandler(handler, iJoyStick);
362
363        return success;
364    }
365
366    /**
367    @brief
368        Adds a handler of any kind. dynamic_cast determines to which list it is added.
369    @param handler
370        Pointer to the handler object.
371    @return
372        True if added, false if handler already existed.
373    */
374    bool ExtendedInputState::addHandler(InputHandler* handler)
375    {
376        bool success = false;
377
378        success |= addKeyHandler(dynamic_cast<KeyHandler*>(handler));
379        success |= addMouseHandler(dynamic_cast<MouseHandler*>(handler));
380        success |= addJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler));
381
382        return success;
383    }
384
385    /**
386    @brief
387        Removes a handler from all lists.
388    @param handler
389        Pointer to the handler.
390    @return
391        True if removal was successful, false if handler was not found.
392    */
393    bool ExtendedInputState::removeHandler(InputHandler* handler)
394    {
395        bool success = false;
396
397        success |= removeKeyHandler(dynamic_cast<KeyHandler*>(handler));
398        success |= removeMouseHandler(dynamic_cast<MouseHandler*>(handler));
399        success |= removeJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler));
400
401        return success;
402    }
403
404    void ExtendedInputState::tickInput(float dt)
405    {
406        for (unsigned int i = 0; i < allHandlers_.size(); ++i)
407        {
408            allHandlers_[i]->tickInput(dt);
409        }
410    }
411
412    void ExtendedInputState::tickInput(float dt, unsigned int device)
413    {
414        switch (device)
415        {
416        case Keyboard:
417            for (unsigned int i = 0; i < keyHandlers_.size(); ++i)
418                keyHandlers_[i]->tickKey(dt);
419            break;
420
421        case Mouse:
422            for (unsigned int i = 0; i < mouseHandlers_.size(); ++i)
423                mouseHandlers_[i]->tickMouse(dt);
424            break;
425
426        default: // joy sticks
427            for (unsigned int i = 0; i < joyStickHandlers_[device - 2].size(); ++i)
428                joyStickHandlers_[device - 2][i]->tickJoyStick(dt, device - 2);
429            break;
430        }
431    }
432
433    void ExtendedInputState::update()
434    {
435        // we can use a set to have a list of unique pointers (an object can implement all 3 handlers)
436        std::set<InputHandler*> tempSet;
437        for (unsigned int iHandler = 0; iHandler < keyHandlers_.size(); iHandler++)
438            tempSet.insert(keyHandlers_[iHandler]);
439        for (unsigned int iHandler = 0; iHandler < mouseHandlers_.size(); iHandler++)
440            tempSet.insert(mouseHandlers_[iHandler]);
441        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandlers_.size(); iJoyStick++)
442            for (unsigned int iHandler = 0; iHandler < joyStickHandlers_[iJoyStick].size(); iHandler++)
443                tempSet.insert(joyStickHandlers_[iJoyStick][iHandler]);
444
445        // copy the content of the map back to the actual vector
446        allHandlers_.clear();
447        for (std::set<InputHandler*>::const_iterator itHandler = tempSet.begin();
448            itHandler != tempSet.end(); itHandler++)
449            allHandlers_.push_back(*itHandler);
450
451        // update the deviceEnabled options
452        setInputDeviceEnabled(Keyboard, (keyHandlers_.size() != 0));
453        setInputDeviceEnabled(Mouse, (mouseHandlers_.size() != 0));
454        for (unsigned int i = 0; i < joyStickHandlers_.size(); ++i)
455            setInputDeviceEnabled(2 + i, (joyStickHandlers_[i].size() != 0));
456
457        this->bHandlersChanged_ = true;
458    }
459}
Note: See TracBrowser for help on using the repository browser.