| 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" |
|---|
| 30 | #include <core/ScopedSingletonManager.h> |
|---|
| 31 | #include "core/ConsoleCommand.h" |
|---|
| 32 | #include "core/CoreIncludes.h" |
|---|
| 33 | #include "core/GUIManager.h" |
|---|
| 34 | #include "core/CorePrereqs.h" |
|---|
| 35 | #include <CEGUIWindow.h> |
|---|
| 36 | #include <elements/CEGUIListbox.h> |
|---|
| 37 | #include <elements/CEGUIListboxItem.h> |
|---|
| 38 | #include <elements/CEGUIListboxTextItem.h> |
|---|
| 39 | #include <CEGUIWindowManager.h> |
|---|
| 40 | #include <string> |
|---|
| 41 | |
|---|
| 42 | namespace orxonox |
|---|
| 43 | { |
|---|
| 44 | /* singleton */ |
|---|
| 45 | ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false ); |
|---|
| 46 | |
|---|
| 47 | /* add commands to console */ |
|---|
| 48 | SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat", |
|---|
| 49 | true ); |
|---|
| 50 | SetConsoleCommandAlias( ChatInputHandler, activate_small_static, |
|---|
| 51 | "startchat_small", true ); |
|---|
| 52 | |
|---|
| 53 | /* constructor */ |
|---|
| 54 | ChatInputHandler::ChatInputHandler() |
|---|
| 55 | { |
|---|
| 56 | /* register the object */ |
|---|
| 57 | RegisterObject(ChatInputHandler); |
|---|
| 58 | |
|---|
| 59 | /* create necessary objects */ |
|---|
| 60 | this->inpbuf = new InputBuffer(); |
|---|
| 61 | this->disp_offset = 0; |
|---|
| 62 | assert( this->inpbuf != NULL ); |
|---|
| 63 | |
|---|
| 64 | /* generate chatbox ui and chatbox-inputonly ui */ |
|---|
| 65 | GUIManager::getInstance().loadGUI( "ChatBox" ); |
|---|
| 66 | GUIManager::getInstance().loadGUI( "ChatBox-inputonly" ); |
|---|
| 67 | |
|---|
| 68 | /* configure the input buffer */ |
|---|
| 69 | configureInputBuffer(); |
|---|
| 70 | |
|---|
| 71 | this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic ); |
|---|
| 72 | this->inputState->setKeyHandler(this->inpbuf); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void ChatInputHandler::configureInputBuffer() |
|---|
| 76 | { |
|---|
| 77 | /* INSTALL CALLBACKS */ |
|---|
| 78 | /* input has changed */ |
|---|
| 79 | this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true); |
|---|
| 80 | |
|---|
| 81 | /* add a line */ |
|---|
| 82 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\r', false); |
|---|
| 83 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\n', false); |
|---|
| 84 | |
|---|
| 85 | /* backspace */ |
|---|
| 86 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\b', true); |
|---|
| 87 | //this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\177', true); |
|---|
| 88 | |
|---|
| 89 | /* exit the chatinputhandler thingy (tbd) */ |
|---|
| 90 | this->inpbuf->registerListener(this, &ChatInputHandler::exit, '\033', true); // escape |
|---|
| 91 | |
|---|
| 92 | /* delete character */ |
|---|
| 93 | this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar, KeyCode::Delete); |
|---|
| 94 | |
|---|
| 95 | /* cursor movement */ |
|---|
| 96 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight, KeyCode::Right); |
|---|
| 97 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft, KeyCode::Left); |
|---|
| 98 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd, KeyCode::End); |
|---|
| 99 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome, KeyCode::Home); |
|---|
| 100 | |
|---|
| 101 | /* GET WINDOW POINTERS */ |
|---|
| 102 | input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" ); |
|---|
| 103 | inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" ); |
|---|
| 104 | |
|---|
| 105 | /* get pointer to the history window */ |
|---|
| 106 | CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" ); |
|---|
| 107 | |
|---|
| 108 | /* cast it to a listbox */ |
|---|
| 109 | lb_history = dynamic_cast<CEGUI::Listbox*>(history); |
|---|
| 110 | |
|---|
| 111 | /* assert wee */ |
|---|
| 112 | assert( lb_history ); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | /* activate, deactivate */ |
|---|
| 117 | void ChatInputHandler::activate_static() |
|---|
| 118 | { ChatInputHandler::getInstance().activate( true ); } |
|---|
| 119 | |
|---|
| 120 | void ChatInputHandler::activate_small_static() |
|---|
| 121 | { ChatInputHandler::getInstance().activate( false ); } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | void ChatInputHandler::activate( bool full ) |
|---|
| 127 | { |
|---|
| 128 | /* start listening */ |
|---|
| 129 | //COUT(0) << "chatinput activated." << std::endl; |
|---|
| 130 | InputManager::getInstance().enterState("chatinput"); |
|---|
| 131 | |
|---|
| 132 | /* MARK add spawning of chat widget stuff here.*/ |
|---|
| 133 | if( full ) |
|---|
| 134 | GUIManager::getInstance().showGUI( "ChatBox" ); |
|---|
| 135 | else |
|---|
| 136 | GUIManager::getInstance().showGUI( "ChatBox-inputonly" ); |
|---|
| 137 | |
|---|
| 138 | this->fullchat = full; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | void ChatInputHandler::deactivate() |
|---|
| 142 | { |
|---|
| 143 | /* stop listening */ |
|---|
| 144 | InputManager::getInstance().leaveState("chatinput"); |
|---|
| 145 | |
|---|
| 146 | /* un-spawning of chat widget stuff */ |
|---|
| 147 | GUIManager::getInstance().hideGUI( "ChatBox" ); |
|---|
| 148 | GUIManager::getInstance().hideGUI( "ChatBox-inputonly" ); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | void ChatInputHandler::incomingChat(const std::string& message, |
|---|
| 153 | unsigned int senderID) |
|---|
| 154 | { |
|---|
| 155 | /* --> a) look up the actual name of the sender */ |
|---|
| 156 | std::string text; |
|---|
| 157 | |
|---|
| 158 | if (senderID != CLIENTID_UNKNOWN) |
|---|
| 159 | { |
|---|
| 160 | std::string name = "unknown"; |
|---|
| 161 | PlayerInfo* player = PlayerManager::getInstance().getClient(senderID); |
|---|
| 162 | if (player) |
|---|
| 163 | name = player->getName(); |
|---|
| 164 | |
|---|
| 165 | text = name + ": " + message; |
|---|
| 166 | } |
|---|
| 167 | else |
|---|
| 168 | text = message; |
|---|
| 169 | |
|---|
| 170 | /* e) create item and add to history */ |
|---|
| 171 | CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( text ); |
|---|
| 172 | this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
|---|
| 173 | this->lb_history->ensureItemIsVisible( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
|---|
| 174 | |
|---|
| 175 | /* f) make sure the history handles it */ |
|---|
| 176 | this->lb_history->handleUpdatedItemData(); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | void ChatInputHandler::sub_adjust_dispoffset( int maxlen, |
|---|
| 181 | int cursorpos, |
|---|
| 182 | int inplen ) |
|---|
| 183 | { |
|---|
| 184 | /* already start offsetting 5 characters before end */ |
|---|
| 185 | if( cursorpos+5 > maxlen ) |
|---|
| 186 | { |
|---|
| 187 | /* always stay 5 characters ahead of end, looks better */ |
|---|
| 188 | ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0; |
|---|
| 189 | |
|---|
| 190 | /* enforce visibility of cursor */ |
|---|
| 191 | (disp_offset > cursorpos ) ? disp_offset = 0 : 1; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /* make sure we don't die at substr */ |
|---|
| 195 | if( inplen <= disp_offset ) disp_offset = 0; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /* callbacks for InputBuffer */ |
|---|
| 199 | void ChatInputHandler::inputChanged() |
|---|
| 200 | { |
|---|
| 201 | /* update the cursor and the window */ |
|---|
| 202 | std::string raw = this->inpbuf->get(); |
|---|
| 203 | int cursorpos = this->inpbuf->getCursorPosition(); |
|---|
| 204 | |
|---|
| 205 | /* get string before cursor */ |
|---|
| 206 | std::string left = raw.substr( 0, cursorpos ); |
|---|
| 207 | |
|---|
| 208 | /* see if there's a string after the cursor */ |
|---|
| 209 | std::string right = ""; |
|---|
| 210 | if( raw.length() >= left.length()+1 ) |
|---|
| 211 | right = raw.substr( cursorpos ); |
|---|
| 212 | |
|---|
| 213 | /* set the text */ |
|---|
| 214 | std::string assembled = "$ " + left + "|" + right; |
|---|
| 215 | |
|---|
| 216 | if( this->fullchat ) |
|---|
| 217 | { |
|---|
| 218 | /* adjust curser position - magic number 5 for font width */ |
|---|
| 219 | sub_adjust_dispoffset( (this->input->getUnclippedInnerRect().getWidth()/6), |
|---|
| 220 | cursorpos, assembled.length() ); |
|---|
| 221 | this->input->setProperty( "Text", assembled.substr( disp_offset ) ); |
|---|
| 222 | } |
|---|
| 223 | else |
|---|
| 224 | { |
|---|
| 225 | /* adjust curser position - magic number 5 for font width */ |
|---|
| 226 | sub_adjust_dispoffset( (this->inputonly->getUnclippedInnerRect().getWidth()/6), |
|---|
| 227 | cursorpos, assembled.length() ); |
|---|
| 228 | this->inputonly->setProperty( "Text", assembled.substr( disp_offset) ); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /* reset display offset */ |
|---|
| 232 | disp_offset = 0; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | void ChatInputHandler::addline() |
|---|
| 236 | { |
|---|
| 237 | /* actually do send what was input */ |
|---|
| 238 | /* a) get the string out of the inputbuffer */ |
|---|
| 239 | std::string msgtosend = this->inpbuf->get(); |
|---|
| 240 | |
|---|
| 241 | if( msgtosend.length() == 0 ) |
|---|
| 242 | { this->deactivate(); |
|---|
| 243 | return; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | /* b) clear the input buffer */ |
|---|
| 247 | if (this->inpbuf->getSize() > 0) |
|---|
| 248 | this->inpbuf->clear(); |
|---|
| 249 | |
|---|
| 250 | /* c) send the chat via some call */ |
|---|
| 251 | Host::Chat( msgtosend ); |
|---|
| 252 | |
|---|
| 253 | /* d) stop listening to input - only if this is not fullchat */ |
|---|
| 254 | if( !this->fullchat ) |
|---|
| 255 | this->deactivate(); |
|---|
| 256 | |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | void ChatInputHandler::backspace() |
|---|
| 260 | { this->inpbuf->removeBehindCursor(); } |
|---|
| 261 | |
|---|
| 262 | void ChatInputHandler::deleteChar() |
|---|
| 263 | { this->inpbuf->removeAtCursor(); } |
|---|
| 264 | |
|---|
| 265 | void ChatInputHandler::cursorRight() |
|---|
| 266 | { this->inpbuf->increaseCursor(); } |
|---|
| 267 | |
|---|
| 268 | void ChatInputHandler::cursorLeft() |
|---|
| 269 | { this->inpbuf->decreaseCursor(); } |
|---|
| 270 | |
|---|
| 271 | void ChatInputHandler::cursorEnd() |
|---|
| 272 | { this->inpbuf->setCursorToEnd(); } |
|---|
| 273 | |
|---|
| 274 | void ChatInputHandler::cursorHome() |
|---|
| 275 | { this->inpbuf->setCursorToBegin(); } |
|---|
| 276 | |
|---|
| 277 | void ChatInputHandler::exit() |
|---|
| 278 | { |
|---|
| 279 | /* b) clear the input buffer */ |
|---|
| 280 | if (this->inpbuf->getSize() > 0) |
|---|
| 281 | this->inpbuf->clear(); |
|---|
| 282 | |
|---|
| 283 | /* d) stop listening to input */ |
|---|
| 284 | this->deactivate(); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | } |
|---|