Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/input/SimpleInputState.cc @ 3270

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

Extracted joy stick related code from InputManager to a new JoyStick class in order to make the InputManger less of a monster class and to apply a little bit more OO.

  • Property svn:eol-style set to native
File size: 4.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 SimpleInputState class.
33*/
34
35#include "SimpleInputState.h"
36#include "core/Executor.h"
37
38namespace orxonox
39{
40    SimpleInputState::SimpleInputState()
41        : keyHandler_(0)
42        , mouseHandler_(0)
43        , joyStickHandlerAll_(0)
44    {
45    }
46
47    void SimpleInputState::numberOfJoySticksChanged(unsigned int n)
48    {
49        unsigned int oldSize = joyStickHandler_.size();
50        joyStickHandler_.resize(n);
51
52        if (n > oldSize)
53        {
54            // we have to add the handler in joyStickHandlerAll_ to the joyStickHandler_[>n]
55            for (unsigned int i = oldSize; i < n; ++i)
56            {
57                joyStickHandler_[i] = joyStickHandlerAll_;
58            }
59        }
60        update();
61    }
62
63    /**
64    @brief
65        Adds a joy stick handler.
66    @param handler
67        Pointer to the handler object.
68    @param joyStickID
69        ID of the joy stick
70    @return
71        True if added, false otherwise.
72    */
73    bool SimpleInputState::setJoyStickHandler(JoyStickHandler* handler, unsigned int joyStickID)
74    {
75        if (joyStickID >= joyStickHandler_.size())
76            return false;
77
78        joyStickHandler_[joyStickID] = handler;
79        update();
80        return true;
81    }
82
83    /**
84    @brief
85        Adds a joy stick handler.
86    @param handler
87        Pointer to the handler object.
88    @return
89        True if added, false if handler already existed.
90    */
91    bool SimpleInputState::setJoyStickHandler(JoyStickHandler* handler)
92    {
93        if (handler == joyStickHandlerAll_)
94            return false;
95
96        joyStickHandlerAll_ = handler;
97        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); ++iJoyStick)
98            setJoyStickHandler(handler, iJoyStick);
99        update();
100        return true;
101    }
102
103    /**
104    @brief
105        Adds a handler of any kind. dynamic_cast determines to which list it is added.
106    @param handler
107        Pointer to the handler object.
108    @return
109        True if added, false if handler already existed.
110    */
111    bool SimpleInputState::setHandler(InputHandler* handler)
112    {
113        setKeyHandler(dynamic_cast<KeyHandler*>(handler));
114        setMouseHandler(dynamic_cast<MouseHandler*>(handler));
115        return setJoyStickHandler(dynamic_cast<JoyStickHandler*>(handler));
116    }
117
118    void SimpleInputState::update()
119    {
120        // we can use a set to have a list of unique pointers (an object can implement all 3 handlers)
121        std::set<InputHandler*> tempSet;
122        if (keyHandler_)
123            tempSet.insert(keyHandler_);
124        if (mouseHandler_)
125            tempSet.insert(mouseHandler_);
126        for (unsigned int iJoyStick = 0; iJoyStick < joyStickHandler_.size(); iJoyStick++)
127            if (joyStickHandler_[iJoyStick])
128                tempSet.insert(joyStickHandler_[iJoyStick]);
129
130        // copy the content of the map back to the actual vector
131        allHandlers_.clear();
132        for (std::set<InputHandler*>::const_iterator itHandler = tempSet.begin();
133            itHandler != tempSet.end(); itHandler++)
134            allHandlers_.push_back(*itHandler);
135
136        // update the deviceEnabled options
137        setInputDeviceEnabled(InputDevice::Keyboard, (keyHandler_ != 0));
138        setInputDeviceEnabled(InputDevice::Mouse, (mouseHandler_ != 0));
139        for (unsigned int i = 0; i < joyStickHandler_.size(); ++i)
140            setInputDeviceEnabled(2 + i, (joyStickHandler_[i] != 0));
141
142        // inform InputManager that there might be changes in EMPTY_HANDLER situation
143        bHandlersChanged_ = true;
144    }
145
146    void SimpleInputState::onEnter()
147    {
148        if (executorOnEnter_)
149            (*executorOnEnter_)();
150    }
151
152    void SimpleInputState::onLeave()
153    {
154        if (executorOnLeave_)
155            (*executorOnLeave_)();
156    }
157}
Note: See TracBrowser for help on using the repository browser.