[1526] | 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 "KeyDetector.h" |
---|
[3196] | 30 | |
---|
[5929] | 31 | #include "core/ConsoleCommand.h" |
---|
[1526] | 32 | #include "core/CoreIncludes.h" |
---|
[5929] | 33 | #include "core/ScopedSingletonManager.h" |
---|
[1526] | 34 | #include "Button.h" |
---|
[5929] | 35 | #include "InputManager.h" |
---|
| 36 | #include "InputState.h" |
---|
[1526] | 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[5929] | 40 | std::string KeyDetector::callbackCommand_s = "KeyDetectorKeyPressed"; |
---|
| 41 | KeyDetector* KeyDetector::singletonPtr_s = 0; |
---|
| 42 | ManageScopedSingleton(KeyDetector, ScopeID::Graphics, false); |
---|
| 43 | |
---|
[1755] | 44 | KeyDetector::KeyDetector() |
---|
[5929] | 45 | : KeyBinder("") |
---|
[1755] | 46 | { |
---|
| 47 | RegisterObject(KeyDetector); |
---|
[5929] | 48 | |
---|
| 49 | CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&KeyDetector::callback, this), callbackCommand_s)); |
---|
| 50 | this->assignCommands(); |
---|
| 51 | |
---|
| 52 | inputState_ = InputManager::getInstance().createInputState("detector", false, false, InputStatePriority::Detector); |
---|
| 53 | // Create a callback to avoid buttonHeld events after the key has been detected |
---|
| 54 | inputState_->setLeaveFunctor(createFunctor(&InputManager::clearBuffers, &InputManager::getInstance())); |
---|
| 55 | inputState_->setHandler(this); |
---|
[1755] | 56 | } |
---|
[1526] | 57 | |
---|
[1755] | 58 | KeyDetector::~KeyDetector() |
---|
| 59 | { |
---|
[5929] | 60 | inputState_->setHandler(NULL); |
---|
| 61 | InputManager::getInstance().destroyState("detector"); |
---|
[1755] | 62 | } |
---|
[1526] | 63 | |
---|
[5929] | 64 | void KeyDetector::assignCommands() |
---|
[1755] | 65 | { |
---|
[5929] | 66 | // Assign every button/axis the same command, but with its name as argument |
---|
[1755] | 67 | clearBindings(); |
---|
[1887] | 68 | for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it) |
---|
| 69 | { |
---|
[5929] | 70 | it->second->bindingString_ = callbackCommand_s + " " + it->second->groupName_ + "." + it->second->name_; |
---|
[1887] | 71 | it->second->parse(); |
---|
| 72 | } |
---|
[1755] | 73 | } |
---|
[1526] | 74 | |
---|
[5929] | 75 | void KeyDetector::callback(const std::string& name) |
---|
| 76 | { |
---|
| 77 | // Call the registered function |
---|
| 78 | if (this->callbackFunction_) |
---|
| 79 | (*this->callbackFunction_)(name); |
---|
| 80 | } |
---|
| 81 | |
---|
[3327] | 82 | void KeyDetector::JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList) |
---|
[1755] | 83 | { |
---|
[3327] | 84 | KeyBinder::JoyStickQuantityChanged(joyStickList); |
---|
[5929] | 85 | this->assignCommands(); |
---|
[1755] | 86 | } |
---|
[1526] | 87 | } |
---|