[6776] | 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 | * Sandro 'smerkli' Merkli |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ChatInputHandler.h" |
---|
[6788] | 30 | #include <core/ScopedSingletonManager.h> |
---|
| 31 | #include "core/ConsoleCommand.h" |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include <string> |
---|
[6776] | 34 | |
---|
| 35 | namespace orxonox |
---|
| 36 | { |
---|
[6777] | 37 | /* singleton */ |
---|
[6788] | 38 | ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false ); |
---|
[6791] | 39 | SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat", |
---|
| 40 | true ); |
---|
[6777] | 41 | |
---|
[6776] | 42 | /* constructor */ |
---|
[6788] | 43 | ChatInputHandler::ChatInputHandler() |
---|
[6776] | 44 | { |
---|
[6777] | 45 | /* register the object */ |
---|
| 46 | RegisterObject(ChatInputHandler); |
---|
| 47 | |
---|
[6776] | 48 | /* create necessary objects */ |
---|
| 49 | this->inpbuf = new InputBuffer(); |
---|
| 50 | |
---|
[6788] | 51 | /* configure the input buffer */ |
---|
[6791] | 52 | configureInputBuffer(); |
---|
[6788] | 53 | |
---|
| 54 | this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic ); |
---|
| 55 | this->inputState->setKeyHandler(this->inpbuf); |
---|
| 56 | |
---|
[6791] | 57 | //[> set console shortcut <] |
---|
| 58 | //this->getIdentifier()->addConsoleCommand(createConsoleCommand(createFunctor( |
---|
| 59 | //&ChatInputHandler::activate, this), "startchat"), false); |
---|
[6776] | 60 | } |
---|
| 61 | |
---|
| 62 | void ChatInputHandler::configureInputBuffer() |
---|
| 63 | { |
---|
| 64 | /* input has changed */ |
---|
[6788] | 65 | this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true); |
---|
[6776] | 66 | |
---|
| 67 | /* add a line */ |
---|
[6788] | 68 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\r', false); |
---|
| 69 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\n', false); |
---|
[6776] | 70 | |
---|
| 71 | /* backspace */ |
---|
[6788] | 72 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\b', true); |
---|
| 73 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\177', true); |
---|
[6776] | 74 | |
---|
| 75 | /* exit the chatinputhandler thingy (tbd) */ |
---|
[6788] | 76 | this->inpbuf->registerListener(this, &ChatInputHandler::exit, '\033', true); // escape |
---|
[6776] | 77 | |
---|
| 78 | /* delete character */ |
---|
[6788] | 79 | this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar, KeyCode::Delete); |
---|
[6776] | 80 | |
---|
| 81 | /* cursor movement */ |
---|
[6788] | 82 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight, KeyCode::Right); |
---|
| 83 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft, KeyCode::Left); |
---|
| 84 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd, KeyCode::End); |
---|
| 85 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome, KeyCode::Home); |
---|
[6776] | 86 | } |
---|
| 87 | |
---|
[6777] | 88 | |
---|
| 89 | /* activate, deactivate */ |
---|
[6791] | 90 | void ChatInputHandler::activate_static() |
---|
| 91 | { ChatInputHandler::getInstance().activate(); } |
---|
| 92 | |
---|
[6788] | 93 | void ChatInputHandler::activate() |
---|
[6777] | 94 | { |
---|
| 95 | /* start listening */ |
---|
[6788] | 96 | COUT(0) << "chatinput activated." << std::endl; |
---|
| 97 | InputManager::getInstance().enterState("chatinput"); |
---|
[6777] | 98 | } |
---|
| 99 | |
---|
[6788] | 100 | void ChatInputHandler::deactivate() |
---|
[6777] | 101 | { |
---|
| 102 | /* stop listening */ |
---|
[6788] | 103 | InputManager::getInstance().leaveState("chatinput"); |
---|
[6777] | 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
[6776] | 108 | /* callbacks for InputBuffer */ |
---|
| 109 | void ChatInputHandler::inputChanged() |
---|
| 110 | { |
---|
| 111 | //this->updateListeners<&ShellListener::inputChanged>(); |
---|
| 112 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void ChatInputHandler::addline() |
---|
| 116 | { |
---|
| 117 | /* MARK MARK */ |
---|
| 118 | |
---|
| 119 | /* actually do send what was input */ |
---|
| 120 | /* a) get the string out of the inputbuffer */ |
---|
[6788] | 121 | std::string msgtosend = this->inpbuf->get(); |
---|
[6776] | 122 | |
---|
| 123 | /* b) clear the input buffer */ |
---|
[6788] | 124 | if (this->inpbuf->getSize() > 0) |
---|
| 125 | this->inpbuf->clear(); |
---|
[6776] | 126 | |
---|
| 127 | /* c) send the chat via some call */ |
---|
[6777] | 128 | Host::Chat( msgtosend ); |
---|
[6776] | 129 | |
---|
| 130 | /* d) stop listening to input */ |
---|
[6788] | 131 | this->deactivate(); |
---|
[6776] | 132 | } |
---|
| 133 | |
---|
| 134 | void ChatInputHandler::backspace() |
---|
| 135 | { |
---|
[6788] | 136 | this->inpbuf->removeBehindCursor(); |
---|
[6776] | 137 | //this->updateListeners<&ShellListener::inputChanged>(); |
---|
| 138 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void ChatInputHandler::deleteChar() |
---|
| 142 | { |
---|
[6788] | 143 | this->inpbuf->removeAtCursor(); |
---|
[6776] | 144 | //this->updateListeners<&ShellListener::inputChanged>(); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void ChatInputHandler::cursorRight() |
---|
| 148 | { |
---|
[6788] | 149 | this->inpbuf->increaseCursor(); |
---|
[6776] | 150 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void ChatInputHandler::cursorLeft() |
---|
| 154 | { |
---|
[6788] | 155 | this->inpbuf->decreaseCursor(); |
---|
[6776] | 156 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | void ChatInputHandler::cursorEnd() |
---|
| 160 | { |
---|
[6788] | 161 | this->inpbuf->setCursorToEnd(); |
---|
[6776] | 162 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void ChatInputHandler::cursorHome() |
---|
| 166 | { |
---|
[6788] | 167 | this->inpbuf->setCursorToBegin(); |
---|
[6776] | 168 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void ChatInputHandler::exit() |
---|
| 172 | { |
---|
[6788] | 173 | //if (this->inpbuf->getSize() > 0) |
---|
[6776] | 174 | //{ |
---|
| 175 | //this->clearInput(); |
---|
| 176 | //return; |
---|
| 177 | //} |
---|
| 178 | |
---|
| 179 | //this->clearInput(); |
---|
| 180 | //this->scrollPosition_ = 0; |
---|
| 181 | //this->scrollIterator_ = this->outputLines_.begin(); |
---|
| 182 | |
---|
| 183 | //this->updateListeners<&ShellListener::exit>(); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | } |
---|