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