Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/core/input/SimpleInputState.cc @ 3192

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

Another clean up in core/input

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