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 <OIS/OIS.h> |
---|
37 | |
---|
38 | #include "core/CoreIncludes.h" |
---|
39 | #include "Orxonox.h" |
---|
40 | #include "InputHandler.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | //using namespace OIS; |
---|
45 | |
---|
46 | /** |
---|
47 | @brief Constructor only resets the pointer values to 0. |
---|
48 | */ |
---|
49 | InputHandler::InputHandler() |
---|
50 | { |
---|
51 | /*if (orxonox::Identifier::isCreatingHierarchy() && !this->getParents()) |
---|
52 | this->createParents(); |
---|
53 | this->setIdentifier(orxonox::ClassManager<InputHandler>::getIdentifier()->registerClass(this->getParents(), "InputHandler", true)); |
---|
54 | if (orxonox::Identifier::isCreatingHierarchy() && this->getParents()) |
---|
55 | this->getParents()->insert(this->getParents()->end(), this->getIdentifier()); |
---|
56 | orxonox::ClassManager<InputHandler>::getIdentifier()->addObject(this);*/ |
---|
57 | |
---|
58 | //RegisterObject(InputHandler); |
---|
59 | |
---|
60 | this->mouse_ = 0; |
---|
61 | this->keyboard_ = 0; |
---|
62 | this->inputSystem_ = 0; |
---|
63 | |
---|
64 | //this->setConfigValues(); |
---|
65 | } |
---|
66 | |
---|
67 | void InputHandler::setConfigValues() |
---|
68 | { |
---|
69 | //SetConfigValue(codeFire_, 4).description("test value"); |
---|
70 | |
---|
71 | ConfigValueContainer *containercodeFire_ = new ConfigValueContainer("asdfblah", "codeFire_", 4); |
---|
72 | containercodeFire_->getValue(&codeFire_).description("test"); |
---|
73 | //containercodeFire_-> |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | @brief The one instance of the InputHandler is stored in this function. |
---|
78 | @return The pointer to the only instance of the InputHandler |
---|
79 | */ |
---|
80 | InputHandler* InputHandler::getSingleton() |
---|
81 | { |
---|
82 | static InputHandler theOnlyInstance; |
---|
83 | return &theOnlyInstance; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | @brief Creates the OIS::InputMananger, the keyboard and the mouse |
---|
88 | @param windowHnd The window handle of the render window |
---|
89 | @param windowWidth The width of the render window |
---|
90 | @param windowHeight The height of the render window |
---|
91 | */ |
---|
92 | void InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
---|
93 | { |
---|
94 | if (!inputSystem_) |
---|
95 | { |
---|
96 | // Setup basic variables |
---|
97 | OIS::ParamList paramList; |
---|
98 | std::ostringstream windowHndStr; |
---|
99 | |
---|
100 | // Fill parameter list |
---|
101 | windowHndStr << (unsigned int)windowHnd; |
---|
102 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
103 | |
---|
104 | // Create inputsystem |
---|
105 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
106 | |
---|
107 | // If possible create a buffered keyboard |
---|
108 | if (inputSystem_->numKeyboards() > 0) |
---|
109 | { |
---|
110 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
---|
111 | keyboard_->setEventCallback(this); |
---|
112 | } |
---|
113 | |
---|
114 | // If possible create a buffered mouse |
---|
115 | if (inputSystem_->numMice() > 0 ) |
---|
116 | { |
---|
117 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
118 | mouse_->setEventCallback(this); |
---|
119 | |
---|
120 | // Set mouse region |
---|
121 | this->setWindowExtents(windowWidth, windowHeight); |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | @brief Updates the InputHandler |
---|
128 | @param dt Delta time |
---|
129 | */ |
---|
130 | void InputHandler::tick(float dt) |
---|
131 | { |
---|
132 | // capture all the input. That calls the event handlers. |
---|
133 | if (mouse_) |
---|
134 | { |
---|
135 | mouse_->capture(); |
---|
136 | } |
---|
137 | |
---|
138 | if (keyboard_) |
---|
139 | { |
---|
140 | keyboard_->capture(); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | @brief Adjusts the mouse window metrics. |
---|
146 | This method has to be called every time the size of the window changes. |
---|
147 | @param width The new width of the render window |
---|
148 | @param height the new height of the render window |
---|
149 | */ |
---|
150 | void InputHandler::setWindowExtents(int width, int height) |
---|
151 | { |
---|
152 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
153 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
---|
154 | mouseState.width = width; |
---|
155 | mouseState.height = height; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | @brief Event handler for the keyPressed Event. |
---|
160 | @param e Event information |
---|
161 | */ |
---|
162 | bool InputHandler::keyPressed(const OIS::KeyEvent &e) |
---|
163 | { |
---|
164 | if (e.key == OIS::KC_ESCAPE) |
---|
165 | Orxonox::getSingleton()->abortRequest(); |
---|
166 | return true; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | @brief Event handler for the keyReleased Event. |
---|
171 | @param e Event information |
---|
172 | */ |
---|
173 | bool InputHandler::keyReleased(const OIS::KeyEvent &e) |
---|
174 | { |
---|
175 | return true; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | @brief Event handler for the mouseMoved Event. |
---|
180 | @param e Event information |
---|
181 | */ |
---|
182 | bool InputHandler::mouseMoved(const OIS::MouseEvent &e) |
---|
183 | { |
---|
184 | return true; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | @brief Event handler for the mousePressed Event. |
---|
189 | @param e Event information |
---|
190 | @param id The ID of the mouse button |
---|
191 | */ |
---|
192 | bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
193 | { |
---|
194 | return true; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | @brief Event handler for the mouseReleased Event. |
---|
199 | @param e Event information |
---|
200 | @param id The ID of the mouse button |
---|
201 | */ |
---|
202 | bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) |
---|
203 | { |
---|
204 | return true; |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|