1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Reto Grieder |
---|
23 | * Co-authors: |
---|
24 | * Some guy writing the example code from Ogre |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | @file |
---|
30 | @brief Implementation of a little Input handler that distributes everything |
---|
31 | coming from OIS. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "OrxonoxStableHeaders.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "Orxonox.h" |
---|
38 | #include "InputHandler.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | //using namespace OIS; |
---|
43 | |
---|
44 | /** |
---|
45 | @brief Constructor only resets the pointer values to 0. |
---|
46 | */ |
---|
47 | InputHandler::InputHandler() |
---|
48 | { |
---|
49 | /*if (orxonox::Identifier::isCreatingHierarchy() && !this->getParents()) |
---|
50 | this->createParents(); |
---|
51 | this->setIdentifier(orxonox::ClassManager<InputHandler>::getIdentifier()->registerClass(this->getParents(), "InputHandler", true)); |
---|
52 | if (orxonox::Identifier::isCreatingHierarchy() && this->getParents()) |
---|
53 | this->getParents()->insert(this->getParents()->end(), this->getIdentifier()); |
---|
54 | orxonox::ClassManager<InputHandler>::getIdentifier()->addObject(this);*/ |
---|
55 | |
---|
56 | //RegisterObject(InputHandler); |
---|
57 | |
---|
58 | this->mouse_ = 0; |
---|
59 | this->keyboard_ = 0; |
---|
60 | this->inputSystem_ = 0; |
---|
61 | |
---|
62 | //this->setConfigValues(); |
---|
63 | } |
---|
64 | |
---|
65 | void InputHandler::setConfigValues() |
---|
66 | { |
---|
67 | //SetConfigValue(codeFire_, 4).description("test value"); |
---|
68 | |
---|
69 | ConfigValueContainer *containercodeFire_ = new ConfigValueContainer("asdfblah", "codeFire_", 4); |
---|
70 | containercodeFire_->getValue(&codeFire_).description("test"); |
---|
71 | //containercodeFire_-> |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | @brief The one instance of the InputHandler is stored in this function. |
---|
76 | @return The pointer to the only instance of the InputHandler |
---|
77 | */ |
---|
78 | InputHandler* InputHandler::getSingleton() |
---|
79 | { |
---|
80 | static InputHandler theOnlyInstance; |
---|
81 | return &theOnlyInstance; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | @brief Creates the OIS::InputMananger, the keyboard and the mouse |
---|
86 | @param windowHnd The window handle of the render window |
---|
87 | @param windowWidth The width of the render window |
---|
88 | @param windowHeight The height of the render window |
---|
89 | */ |
---|
90 | void InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
---|
91 | { |
---|
92 | if (!inputSystem_) |
---|
93 | { |
---|
94 | // Setup basic variables |
---|
95 | OIS::ParamList paramList; |
---|
96 | std::ostringstream windowHndStr; |
---|
97 | |
---|
98 | // Fill parameter list |
---|
99 | windowHndStr << (unsigned int)windowHnd; |
---|
100 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
101 | |
---|
102 | // Create inputsystem |
---|
103 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
104 | |
---|
105 | // If possible create a buffered keyboard |
---|
106 | if (inputSystem_->numKeyboards() > 0) |
---|
107 | { |
---|
108 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
---|
109 | keyboard_->setEventCallback(this); |
---|
110 | } |
---|
111 | |
---|
112 | // If possible create a buffered mouse |
---|
113 | if (inputSystem_->numMice() > 0 ) |
---|
114 | { |
---|
115 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
116 | mouse_->setEventCallback(this); |
---|
117 | |
---|
118 | // Set mouse region |
---|
119 | this->setWindowExtents(windowWidth, windowHeight); |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | @brief Updates the InputHandler |
---|
126 | @param dt Delta time |
---|
127 | */ |
---|
128 | void InputHandler::tick(float dt) |
---|
129 | { |
---|
130 | // capture all the input. That calls the event handlers. |
---|
131 | if (mouse_) |
---|
132 | { |
---|
133 | mouse_->capture(); |
---|
134 | } |
---|
135 | |
---|
136 | if (keyboard_) |
---|
137 | { |
---|
138 | keyboard_->capture(); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | @brief Adjusts the mouse window metrics. |
---|
144 | This method has to be called every time the size of the window changes. |
---|
145 | @param width The new width of the render window |
---|
146 | @param height the new height of the render window |
---|
147 | */ |
---|
148 | void InputHandler::setWindowExtents(int width, int height) |
---|
149 | { |
---|
150 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
151 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
---|
152 | mouseState.width = width; |
---|
153 | mouseState.height = height; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | @brief Event handler for the keyPressed Event. |
---|
158 | @param e Event information |
---|
159 | */ |
---|
160 | bool InputHandler::keyPressed(const OIS::KeyEvent &e) |
---|
161 | { |
---|
162 | if (e.key == OIS::KC_ESCAPE) |
---|
163 | Orxonox::getSingleton()->abortRequest(); |
---|
164 | return true; |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | @brief Event handler for the keyReleased Event. |
---|
169 | @param e Event information |
---|
170 | */ |
---|
171 | bool InputHandler::keyReleased(const OIS::KeyEvent &e) |
---|
172 | { |
---|
173 | return true; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | @brief Event handler for the mouseMoved Event. |
---|
178 | @param e Event information |
---|
179 | */ |
---|
180 | bool InputHandler::mouseMoved(const OIS::MouseEvent &e) |
---|
181 | { |
---|
182 | return true; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | @brief Event handler for the mousePressed Event. |
---|
187 | @param e Event information |
---|
188 | @param id The ID of the mouse button |
---|
189 | */ |
---|
190 | bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
191 | { |
---|
192 | return true; |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | @brief Event handler for the mouseReleased Event. |
---|
197 | @param e Event information |
---|
198 | @param id The ID of the mouse button |
---|
199 | */ |
---|
200 | bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
201 | { |
---|
202 | return true; |
---|
203 | } |
---|
204 | |
---|
205 | } |
---|