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