[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> |
---|
[6934] | 36 | #include <elements/CEGUIListbox.h> |
---|
| 37 | #include <elements/CEGUIListboxItem.h> |
---|
| 38 | #include <elements/CEGUIListboxTextItem.h> |
---|
[6846] | 39 | #include <CEGUIWindowManager.h> |
---|
[6788] | 40 | #include <string> |
---|
[6776] | 41 | |
---|
[7127] | 42 | namespace orxonox |
---|
[6776] | 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 ); |
---|
[7127] | 50 | SetConsoleCommandAlias( ChatInputHandler, activate_small_static, |
---|
[6870] | 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 | |
---|
[7043] | 68 | /* setup colors */ |
---|
| 69 | setupColors(); |
---|
| 70 | |
---|
[6788] | 71 | /* configure the input buffer */ |
---|
[6791] | 72 | configureInputBuffer(); |
---|
[6788] | 73 | |
---|
| 74 | this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic ); |
---|
| 75 | this->inputState->setKeyHandler(this->inpbuf); |
---|
[6776] | 76 | } |
---|
| 77 | |
---|
[7043] | 78 | /* configure input buffer, sub for the constructor */ |
---|
[6776] | 79 | void ChatInputHandler::configureInputBuffer() |
---|
| 80 | { |
---|
[6910] | 81 | /* INSTALL CALLBACKS */ |
---|
[6846] | 82 | /* input has changed */ |
---|
[6788] | 83 | this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true); |
---|
[6846] | 84 | |
---|
| 85 | /* add a line */ |
---|
[6788] | 86 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\r', false); |
---|
| 87 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\n', false); |
---|
[6776] | 88 | |
---|
[6846] | 89 | /* backspace */ |
---|
[6788] | 90 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\b', true); |
---|
[6890] | 91 | //this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\177', true); |
---|
[6776] | 92 | |
---|
[6846] | 93 | /* exit the chatinputhandler thingy (tbd) */ |
---|
[6788] | 94 | this->inpbuf->registerListener(this, &ChatInputHandler::exit, '\033', true); // escape |
---|
[6776] | 95 | |
---|
[6846] | 96 | /* delete character */ |
---|
[6788] | 97 | this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar, KeyCode::Delete); |
---|
[6776] | 98 | |
---|
[6846] | 99 | /* cursor movement */ |
---|
[6788] | 100 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight, KeyCode::Right); |
---|
| 101 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft, KeyCode::Left); |
---|
| 102 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd, KeyCode::End); |
---|
| 103 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome, KeyCode::Home); |
---|
[6846] | 104 | |
---|
[6910] | 105 | /* GET WINDOW POINTERS */ |
---|
[6846] | 106 | input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" ); |
---|
[6870] | 107 | inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" ); |
---|
| 108 | |
---|
[6910] | 109 | /* get pointer to the history window */ |
---|
[6846] | 110 | CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" ); |
---|
[6910] | 111 | |
---|
| 112 | /* cast it to a listbox */ |
---|
[7127] | 113 | lb_history = dynamic_cast<CEGUI::Listbox*>(history); |
---|
[6846] | 114 | |
---|
| 115 | /* assert wee */ |
---|
| 116 | assert( lb_history ); |
---|
[6776] | 117 | } |
---|
| 118 | |
---|
[7043] | 119 | /* setup the colors, sub for the constructor */ |
---|
| 120 | void ChatInputHandler::setupColors() |
---|
| 121 | { |
---|
[7049] | 122 | /* auto variables */ |
---|
[7043] | 123 | float red = 1.0, green = 0.5, blue = 0.5; |
---|
| 124 | int i = 0; |
---|
[6777] | 125 | |
---|
[7049] | 126 | // three loops: red tones, blue tones and green tones |
---|
[7043] | 127 | // reds |
---|
| 128 | for( i = 0; i < NumberOfColors/3; ++i ) |
---|
| 129 | { this->text_colors[ i ] = new CEGUI::colour( red, green, blue ); |
---|
| 130 | assert( this->text_colors[ i ] ); |
---|
[7183] | 131 | green += 0.2f, blue += 0.2f; |
---|
[7043] | 132 | } |
---|
| 133 | |
---|
| 134 | // greens |
---|
| 135 | red = 0.5, green = 1, blue = 0.5; |
---|
| 136 | for( ; i < NumberOfColors*2/3; ++i ) |
---|
| 137 | { this->text_colors[ i ] = new CEGUI::colour( red, green, blue ); |
---|
| 138 | assert( this->text_colors[ i ] ); |
---|
[7183] | 139 | red += 0.2f, blue += 0.2f; |
---|
[7043] | 140 | } |
---|
| 141 | |
---|
[7127] | 142 | // blues |
---|
[7043] | 143 | red = 0.5, green = 0.5, blue = 1; |
---|
| 144 | for( ; i < NumberOfColors; ++i ) |
---|
| 145 | { this->text_colors[ i ] = new CEGUI::colour( red, green, blue ); |
---|
| 146 | assert( this->text_colors[ i ] ); |
---|
[7183] | 147 | red += 0.2f, green += 0.2f; |
---|
[7043] | 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
[6777] | 152 | /* activate, deactivate */ |
---|
[6791] | 153 | void ChatInputHandler::activate_static() |
---|
[6870] | 154 | { ChatInputHandler::getInstance().activate( true ); } |
---|
| 155 | |
---|
| 156 | void ChatInputHandler::activate_small_static() |
---|
| 157 | { ChatInputHandler::getInstance().activate( false ); } |
---|
| 158 | |
---|
| 159 | void ChatInputHandler::activate( bool full ) |
---|
[6777] | 160 | { |
---|
| 161 | /* start listening */ |
---|
[6788] | 162 | InputManager::getInstance().enterState("chatinput"); |
---|
[6846] | 163 | |
---|
| 164 | /* MARK add spawning of chat widget stuff here.*/ |
---|
[6870] | 165 | if( full ) |
---|
| 166 | GUIManager::getInstance().showGUI( "ChatBox" ); |
---|
| 167 | else |
---|
| 168 | GUIManager::getInstance().showGUI( "ChatBox-inputonly" ); |
---|
[6879] | 169 | |
---|
| 170 | this->fullchat = full; |
---|
[6777] | 171 | } |
---|
| 172 | |
---|
[7127] | 173 | void ChatInputHandler::deactivate() |
---|
[6777] | 174 | { |
---|
| 175 | /* stop listening */ |
---|
[6788] | 176 | InputManager::getInstance().leaveState("chatinput"); |
---|
[6846] | 177 | |
---|
[6876] | 178 | /* un-spawning of chat widget stuff */ |
---|
[6846] | 179 | GUIManager::getInstance().hideGUI( "ChatBox" ); |
---|
[6870] | 180 | GUIManager::getInstance().hideGUI( "ChatBox-inputonly" ); |
---|
[6777] | 181 | } |
---|
| 182 | |
---|
[6885] | 183 | |
---|
[7043] | 184 | /* subs for incomingChat */ |
---|
| 185 | void ChatInputHandler::sub_setcolor( CEGUI::ListboxTextItem *tocolor, |
---|
| 186 | std::string name ) |
---|
| 187 | { |
---|
[7049] | 188 | /* sanity checks */ |
---|
[7043] | 189 | if( !tocolor ) |
---|
| 190 | COUT(2) << "Empty ListBoxTextItem given to " |
---|
| 191 | "ChatInputhandler::sub_setcolor().\n"; |
---|
| 192 | |
---|
| 193 | /* "hash" the name */ |
---|
| 194 | int hash = 0; |
---|
| 195 | for( int i = name.length(); i > 0; --i ) |
---|
| 196 | hash += name[i-1]; |
---|
| 197 | hash = hash % this->NumberOfColors; |
---|
| 198 | |
---|
| 199 | /* set the color according to the hash */ |
---|
| 200 | tocolor->setTextColours( *(this->text_colors[ hash ]) ); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /* handle incoming chat */ |
---|
[7127] | 204 | void ChatInputHandler::incomingChat(const std::string& message, |
---|
[6885] | 205 | unsigned int senderID) |
---|
[6876] | 206 | { |
---|
[7043] | 207 | /* look up the actual name of the sender */ |
---|
| 208 | std::string text, name = "unknown"; |
---|
[6885] | 209 | |
---|
[7043] | 210 | /* setup player name info */ |
---|
[6885] | 211 | if (senderID != CLIENTID_UNKNOWN) |
---|
[7127] | 212 | { |
---|
[6885] | 213 | PlayerInfo* player = PlayerManager::getInstance().getClient(senderID); |
---|
| 214 | if (player) |
---|
| 215 | name = player->getName(); |
---|
| 216 | } |
---|
| 217 | |
---|
[7043] | 218 | /* assemble the text */ |
---|
| 219 | text = name + ": " + message; |
---|
| 220 | |
---|
| 221 | /* create item */ |
---|
[6885] | 222 | CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( text ); |
---|
[7043] | 223 | |
---|
| 224 | /* setup colors */ |
---|
| 225 | sub_setcolor( toadd, name ); |
---|
| 226 | |
---|
| 227 | /* now add */ |
---|
[6885] | 228 | this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
---|
[7127] | 229 | this->lb_history->ensureItemIsVisible( |
---|
[7043] | 230 | dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
---|
[6885] | 231 | |
---|
[7043] | 232 | /* make sure the history handles it */ |
---|
[6885] | 233 | this->lb_history->handleUpdatedItemData(); |
---|
[7127] | 234 | } |
---|
[6885] | 235 | |
---|
| 236 | |
---|
[7043] | 237 | /* sub for inputchanged */ |
---|
[7127] | 238 | void ChatInputHandler::sub_adjust_dispoffset( int maxlen, |
---|
| 239 | int cursorpos, |
---|
[6885] | 240 | int inplen ) |
---|
| 241 | { |
---|
[6876] | 242 | /* already start offsetting 5 characters before end */ |
---|
| 243 | if( cursorpos+5 > maxlen ) |
---|
[7127] | 244 | { |
---|
[6876] | 245 | /* always stay 5 characters ahead of end, looks better */ |
---|
| 246 | ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0; |
---|
| 247 | |
---|
| 248 | /* enforce visibility of cursor */ |
---|
| 249 | (disp_offset > cursorpos ) ? disp_offset = 0 : 1; |
---|
| 250 | } |
---|
[7127] | 251 | |
---|
[6876] | 252 | /* make sure we don't die at substr */ |
---|
| 253 | if( inplen <= disp_offset ) disp_offset = 0; |
---|
| 254 | } |
---|
| 255 | |
---|
[6776] | 256 | /* callbacks for InputBuffer */ |
---|
| 257 | void ChatInputHandler::inputChanged() |
---|
| 258 | { |
---|
[6846] | 259 | /* update the cursor and the window */ |
---|
| 260 | std::string raw = this->inpbuf->get(); |
---|
[6876] | 261 | int cursorpos = this->inpbuf->getCursorPosition(); |
---|
[7127] | 262 | |
---|
[6846] | 263 | /* get string before cursor */ |
---|
[6876] | 264 | std::string left = raw.substr( 0, cursorpos ); |
---|
[6837] | 265 | |
---|
[6846] | 266 | /* see if there's a string after the cursor */ |
---|
| 267 | std::string right = ""; |
---|
| 268 | if( raw.length() >= left.length()+1 ) |
---|
[6876] | 269 | right = raw.substr( cursorpos ); |
---|
[7127] | 270 | |
---|
[6846] | 271 | /* set the text */ |
---|
[6876] | 272 | std::string assembled = "$ " + left + "|" + right; |
---|
| 273 | |
---|
[6879] | 274 | if( this->fullchat ) |
---|
[7127] | 275 | { |
---|
[6879] | 276 | /* adjust curser position - magic number 5 for font width */ |
---|
[7183] | 277 | sub_adjust_dispoffset( (int)(this->input->getUnclippedInnerRect().getWidth()/6), |
---|
[6879] | 278 | cursorpos, assembled.length() ); |
---|
| 279 | this->input->setProperty( "Text", assembled.substr( disp_offset ) ); |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | { |
---|
| 283 | /* adjust curser position - magic number 5 for font width */ |
---|
[7183] | 284 | sub_adjust_dispoffset( (int)(this->inputonly->getUnclippedInnerRect().getWidth()/6), |
---|
[6879] | 285 | cursorpos, assembled.length() ); |
---|
| 286 | this->inputonly->setProperty( "Text", assembled.substr( disp_offset) ); |
---|
| 287 | } |
---|
[6876] | 288 | |
---|
| 289 | /* reset display offset */ |
---|
| 290 | disp_offset = 0; |
---|
[6776] | 291 | } |
---|
| 292 | |
---|
| 293 | void ChatInputHandler::addline() |
---|
| 294 | { |
---|
| 295 | /* actually do send what was input */ |
---|
| 296 | /* a) get the string out of the inputbuffer */ |
---|
[6788] | 297 | std::string msgtosend = this->inpbuf->get(); |
---|
[6776] | 298 | |
---|
[6846] | 299 | if( msgtosend.length() == 0 ) |
---|
| 300 | { this->deactivate(); |
---|
| 301 | return; |
---|
| 302 | } |
---|
| 303 | |
---|
[6776] | 304 | /* b) clear the input buffer */ |
---|
[6788] | 305 | if (this->inpbuf->getSize() > 0) |
---|
| 306 | this->inpbuf->clear(); |
---|
[6776] | 307 | |
---|
| 308 | /* c) send the chat via some call */ |
---|
[6777] | 309 | Host::Chat( msgtosend ); |
---|
[6776] | 310 | |
---|
[6885] | 311 | /* d) stop listening to input - only if this is not fullchat */ |
---|
[6879] | 312 | if( !this->fullchat ) |
---|
| 313 | this->deactivate(); |
---|
[6846] | 314 | |
---|
[6776] | 315 | } |
---|
| 316 | |
---|
| 317 | void ChatInputHandler::backspace() |
---|
[6846] | 318 | { this->inpbuf->removeBehindCursor(); } |
---|
[6776] | 319 | |
---|
| 320 | void ChatInputHandler::deleteChar() |
---|
[6846] | 321 | { this->inpbuf->removeAtCursor(); } |
---|
[6776] | 322 | |
---|
| 323 | void ChatInputHandler::cursorRight() |
---|
[6846] | 324 | { this->inpbuf->increaseCursor(); } |
---|
[7127] | 325 | |
---|
[6776] | 326 | void ChatInputHandler::cursorLeft() |
---|
[6846] | 327 | { this->inpbuf->decreaseCursor(); } |
---|
[7127] | 328 | |
---|
[6776] | 329 | void ChatInputHandler::cursorEnd() |
---|
[6846] | 330 | { this->inpbuf->setCursorToEnd(); } |
---|
[6776] | 331 | |
---|
| 332 | void ChatInputHandler::cursorHome() |
---|
[6846] | 333 | { this->inpbuf->setCursorToBegin(); } |
---|
[6776] | 334 | |
---|
| 335 | void ChatInputHandler::exit() |
---|
[6879] | 336 | { |
---|
| 337 | /* b) clear the input buffer */ |
---|
| 338 | if (this->inpbuf->getSize() > 0) |
---|
| 339 | this->inpbuf->clear(); |
---|
[6776] | 340 | |
---|
[6879] | 341 | /* d) stop listening to input */ |
---|
| 342 | this->deactivate(); |
---|
| 343 | } |
---|
| 344 | |
---|
[6776] | 345 | } |
---|