Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/input/InputState.cc @ 3288

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

Finally found a satisfying way to deal with interfaces that deliver information, but only upon virtual call.
The solution involves a static variable but any other (and uglier/hackier) solution will do so too.
I applied the method the the JoyStickQuantityListener so that the KeyBinder is now independent on the InputManager.

  • Property svn:eol-style set to native
File size: 3.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#include "InputState.h"
30#include "core/Functor.h"
31
32namespace orxonox
33{
34    InputState::InputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority)
35        : name_(name)
36        , bAlwaysGetsInput_(bAlwaysGetsInput)
37        , bTransparent_(bTransparent)
38        , bExpired_(true)
39        , handlers_(2)
40        , joyStickHandlerAll_(0)
41        , enterFunctor_(0)
42        , leaveFunctor_(0)
43    {
44        if (priority >= InputStatePriority::HighPriority || priority == InputStatePriority::Empty)
45            priority_ = priority;
46        else
47            priority_ = 0;
48
49        handlers_.resize(InputDeviceEnumerator::FirstJoyStick + JoyStickQuantityListener::getJoyStickList().size(), NULL);
50    }
51
52    bool InputState::isInputDeviceEnabled(unsigned int device)
53    {
54        if (device < handlers_.size())
55            return handlers_[device] != NULL;
56        else
57            return false;
58    }
59
60    void InputState::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList)
61    {
62        unsigned int oldSize = handlers_.size();
63        handlers_.resize(InputDeviceEnumerator::FirstJoyStick + joyStickList.size(), NULL);
64
65        for (unsigned int i = oldSize; i < handlers_.size(); ++i)
66            handlers_[i] = joyStickHandlerAll_;
67
68        bExpired_ = true;
69    }
70
71    /**
72    @brief
73        Adds a joy stick handler.
74    @param handler
75        Pointer to the handler object.
76    @param joyStickID
77        ID of the joy stick
78    @return
79        True if added, false otherwise.
80    */
81    bool InputState::setJoyStickHandler(InputHandler* handler, unsigned int joyStick)
82    {
83        unsigned device = joyStick + firstJoyStickIndex_s;
84        if (joyStick >= handlers_.size() - device)
85            return false;
86
87        handlers_[device] = handler;
88        bExpired_ = true;
89        return true;
90    }
91
92    /**
93    @brief
94        Adds a joy stick handler.
95    @param handler
96        Pointer to the handler object.
97    @return
98        True if added, false if handler already existed.
99    */
100    bool InputState::setJoyStickHandler(InputHandler* handler)
101    {
102        joyStickHandlerAll_ = handler;
103        for (unsigned int i = firstJoyStickIndex_s; i < handlers_.size(); ++i)
104            handlers_[i] = handler;
105        bExpired_ = true;
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 InputState::setHandler(InputHandler* handler)
118    {
119        setKeyHandler(handler);
120        setMouseHandler(handler);
121        return setJoyStickHandler(handler);
122    }
123
124    void InputState::entered()
125    {
126        if (enterFunctor_)
127            (*enterFunctor_)();
128           
129    }
130
131    void InputState::left()
132    {
133        if (leaveFunctor_)
134            (*leaveFunctor_)();
135    }
136}
Note: See TracBrowser for help on using the repository browser.