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