[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" |
---|
[6846] | 33 | #include "core/GUIManager.h" |
---|
| 34 | #include "core/CorePrereqs.h" |
---|
| 35 | #include <CEGUIWindow.h> |
---|
| 36 | #include <CEGUI/elements/CEGUIListbox.h> |
---|
| 37 | #include <CEGUI/elements/CEGUIListboxItem.h> |
---|
| 38 | #include <CEGUI/elements/CEGUIListboxTextItem.h> |
---|
| 39 | #include <CEGUIWindowManager.h> |
---|
[6788] | 40 | #include <string> |
---|
[6776] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[6777] | 44 | /* singleton */ |
---|
[6788] | 45 | ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false ); |
---|
[6791] | 46 | SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat", |
---|
| 47 | true ); |
---|
[6777] | 48 | |
---|
[6846] | 49 | |
---|
[6776] | 50 | /* constructor */ |
---|
[6788] | 51 | ChatInputHandler::ChatInputHandler() |
---|
[6776] | 52 | { |
---|
[6777] | 53 | /* register the object */ |
---|
| 54 | RegisterObject(ChatInputHandler); |
---|
| 55 | |
---|
[6776] | 56 | /* create necessary objects */ |
---|
| 57 | this->inpbuf = new InputBuffer(); |
---|
[6846] | 58 | assert( this->inpbuf != NULL ); |
---|
[6776] | 59 | |
---|
[6846] | 60 | /* MARK add generation of ChatBox thingy here */ |
---|
| 61 | GUIManager::getInstance().loadGUI( "ChatBox" ); |
---|
| 62 | |
---|
[6788] | 63 | /* configure the input buffer */ |
---|
[6791] | 64 | configureInputBuffer(); |
---|
[6788] | 65 | |
---|
| 66 | this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic ); |
---|
| 67 | this->inputState->setKeyHandler(this->inpbuf); |
---|
| 68 | |
---|
[6791] | 69 | //[> set console shortcut <] |
---|
| 70 | //this->getIdentifier()->addConsoleCommand(createConsoleCommand(createFunctor( |
---|
| 71 | //&ChatInputHandler::activate, this), "startchat"), false); |
---|
[6776] | 72 | } |
---|
| 73 | |
---|
| 74 | void ChatInputHandler::configureInputBuffer() |
---|
| 75 | { |
---|
[6846] | 76 | /* input has changed */ |
---|
[6788] | 77 | this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true); |
---|
[6846] | 78 | |
---|
| 79 | /* add a line */ |
---|
[6788] | 80 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\r', false); |
---|
| 81 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\n', false); |
---|
[6776] | 82 | |
---|
[6846] | 83 | /* backspace */ |
---|
[6788] | 84 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\b', true); |
---|
| 85 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\177', true); |
---|
[6776] | 86 | |
---|
[6846] | 87 | /* exit the chatinputhandler thingy (tbd) */ |
---|
[6788] | 88 | this->inpbuf->registerListener(this, &ChatInputHandler::exit, '\033', true); // escape |
---|
[6776] | 89 | |
---|
[6846] | 90 | /* delete character */ |
---|
[6788] | 91 | this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar, KeyCode::Delete); |
---|
[6776] | 92 | |
---|
[6846] | 93 | /* cursor movement */ |
---|
[6788] | 94 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight, KeyCode::Right); |
---|
| 95 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft, KeyCode::Left); |
---|
| 96 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd, KeyCode::End); |
---|
| 97 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome, KeyCode::Home); |
---|
[6846] | 98 | |
---|
| 99 | /* get window pointers */ |
---|
| 100 | input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" ); |
---|
| 101 | CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" ); |
---|
| 102 | lb_history = dynamic_cast<CEGUI::Listbox*>(history); |
---|
| 103 | |
---|
| 104 | /* assert wee */ |
---|
| 105 | assert( lb_history ); |
---|
[6776] | 106 | } |
---|
| 107 | |
---|
[6777] | 108 | |
---|
| 109 | /* activate, deactivate */ |
---|
[6791] | 110 | void ChatInputHandler::activate_static() |
---|
| 111 | { ChatInputHandler::getInstance().activate(); } |
---|
| 112 | |
---|
[6788] | 113 | void ChatInputHandler::activate() |
---|
[6777] | 114 | { |
---|
| 115 | /* start listening */ |
---|
[6846] | 116 | //COUT(0) << "chatinput activated." << std::endl; |
---|
[6788] | 117 | InputManager::getInstance().enterState("chatinput"); |
---|
[6846] | 118 | |
---|
| 119 | /* MARK add spawning of chat widget stuff here.*/ |
---|
| 120 | GUIManager::getInstance().showGUI( "ChatBox" ); |
---|
[6777] | 121 | } |
---|
| 122 | |
---|
[6788] | 123 | void ChatInputHandler::deactivate() |
---|
[6777] | 124 | { |
---|
| 125 | /* stop listening */ |
---|
[6788] | 126 | InputManager::getInstance().leaveState("chatinput"); |
---|
[6846] | 127 | |
---|
| 128 | /* MARK add un-spawning of chat widget stuff here. */ |
---|
| 129 | GUIManager::getInstance().hideGUI( "ChatBox" ); |
---|
[6777] | 130 | } |
---|
| 131 | |
---|
[6776] | 132 | /* callbacks for InputBuffer */ |
---|
| 133 | void ChatInputHandler::inputChanged() |
---|
| 134 | { |
---|
[6846] | 135 | /* update the cursor and the window */ |
---|
| 136 | std::string raw = this->inpbuf->get(); |
---|
| 137 | |
---|
| 138 | /* get string before cursor */ |
---|
| 139 | std::string left = raw.substr( 0, this->inpbuf->getCursorPosition() ); |
---|
[6837] | 140 | |
---|
[6846] | 141 | /* see if there's a string after the cursor */ |
---|
| 142 | std::string right = ""; |
---|
| 143 | if( raw.length() >= left.length()+1 ) |
---|
| 144 | right = raw.substr( this->inpbuf->getCursorPosition() ); |
---|
| 145 | |
---|
| 146 | /* set the text */ |
---|
| 147 | this->input->setProperty( "Text", left + "|" + right ); |
---|
[6776] | 148 | } |
---|
| 149 | |
---|
| 150 | void ChatInputHandler::addline() |
---|
| 151 | { |
---|
| 152 | /* actually do send what was input */ |
---|
| 153 | /* a) get the string out of the inputbuffer */ |
---|
[6788] | 154 | std::string msgtosend = this->inpbuf->get(); |
---|
[6776] | 155 | |
---|
[6846] | 156 | if( msgtosend.length() == 0 ) |
---|
| 157 | { this->deactivate(); |
---|
| 158 | return; |
---|
| 159 | } |
---|
| 160 | |
---|
[6776] | 161 | /* b) clear the input buffer */ |
---|
[6788] | 162 | if (this->inpbuf->getSize() > 0) |
---|
| 163 | this->inpbuf->clear(); |
---|
[6776] | 164 | |
---|
| 165 | /* c) send the chat via some call */ |
---|
[6777] | 166 | Host::Chat( msgtosend ); |
---|
[6776] | 167 | |
---|
| 168 | /* d) stop listening to input */ |
---|
[6788] | 169 | this->deactivate(); |
---|
[6846] | 170 | |
---|
| 171 | /* e) create item and add to history */ |
---|
| 172 | CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( msgtosend ); |
---|
| 173 | this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
---|
| 174 | this->lb_history->ensureItemIsVisible( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
---|
| 175 | |
---|
| 176 | /* f) make sure the history handles it */ |
---|
| 177 | this->lb_history->handleUpdatedItemData(); |
---|
[6776] | 178 | } |
---|
| 179 | |
---|
| 180 | void ChatInputHandler::backspace() |
---|
[6846] | 181 | { this->inpbuf->removeBehindCursor(); } |
---|
[6776] | 182 | |
---|
| 183 | void ChatInputHandler::deleteChar() |
---|
[6846] | 184 | { this->inpbuf->removeAtCursor(); } |
---|
[6776] | 185 | |
---|
| 186 | void ChatInputHandler::cursorRight() |
---|
[6846] | 187 | { this->inpbuf->increaseCursor(); } |
---|
[6776] | 188 | |
---|
| 189 | void ChatInputHandler::cursorLeft() |
---|
[6846] | 190 | { this->inpbuf->decreaseCursor(); } |
---|
[6776] | 191 | |
---|
| 192 | void ChatInputHandler::cursorEnd() |
---|
[6846] | 193 | { this->inpbuf->setCursorToEnd(); } |
---|
[6776] | 194 | |
---|
| 195 | void ChatInputHandler::cursorHome() |
---|
[6846] | 196 | { this->inpbuf->setCursorToBegin(); } |
---|
[6776] | 197 | |
---|
| 198 | void ChatInputHandler::exit() |
---|
[6846] | 199 | { } |
---|
[6776] | 200 | |
---|
| 201 | } |
---|