[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 | |
---|
| 119 | void ChatInputHandler::activate( bool full ) |
---|
[6777] | 120 | { |
---|
| 121 | /* start listening */ |
---|
[6846] | 122 | //COUT(0) << "chatinput activated." << std::endl; |
---|
[6788] | 123 | InputManager::getInstance().enterState("chatinput"); |
---|
[6846] | 124 | |
---|
| 125 | /* MARK add spawning of chat widget stuff here.*/ |
---|
[6870] | 126 | if( full ) |
---|
| 127 | GUIManager::getInstance().showGUI( "ChatBox" ); |
---|
| 128 | else |
---|
| 129 | GUIManager::getInstance().showGUI( "ChatBox-inputonly" ); |
---|
[6777] | 130 | } |
---|
| 131 | |
---|
[6788] | 132 | void ChatInputHandler::deactivate() |
---|
[6777] | 133 | { |
---|
| 134 | /* stop listening */ |
---|
[6788] | 135 | InputManager::getInstance().leaveState("chatinput"); |
---|
[6846] | 136 | |
---|
[6876] | 137 | /* un-spawning of chat widget stuff */ |
---|
[6846] | 138 | GUIManager::getInstance().hideGUI( "ChatBox" ); |
---|
[6870] | 139 | GUIManager::getInstance().hideGUI( "ChatBox-inputonly" ); |
---|
[6777] | 140 | } |
---|
| 141 | |
---|
[6876] | 142 | void ChatInputHandler::sub_adjust_dispoffset( int maxlen, int cursorpos, int inplen ) |
---|
| 143 | { |
---|
| 144 | /* already start offsetting 5 characters before end */ |
---|
| 145 | if( cursorpos+5 > maxlen ) |
---|
| 146 | { |
---|
| 147 | /* always stay 5 characters ahead of end, looks better */ |
---|
| 148 | ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0; |
---|
| 149 | |
---|
| 150 | /* enforce visibility of cursor */ |
---|
| 151 | (disp_offset > cursorpos ) ? disp_offset = 0 : 1; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /* make sure we don't die at substr */ |
---|
| 155 | if( inplen <= disp_offset ) disp_offset = 0; |
---|
| 156 | } |
---|
| 157 | |
---|
[6776] | 158 | /* callbacks for InputBuffer */ |
---|
| 159 | void ChatInputHandler::inputChanged() |
---|
| 160 | { |
---|
[6846] | 161 | /* update the cursor and the window */ |
---|
| 162 | std::string raw = this->inpbuf->get(); |
---|
[6876] | 163 | int cursorpos = this->inpbuf->getCursorPosition(); |
---|
[6846] | 164 | |
---|
| 165 | /* get string before cursor */ |
---|
[6876] | 166 | std::string left = raw.substr( 0, cursorpos ); |
---|
[6837] | 167 | |
---|
[6846] | 168 | /* see if there's a string after the cursor */ |
---|
| 169 | std::string right = ""; |
---|
| 170 | if( raw.length() >= left.length()+1 ) |
---|
[6876] | 171 | right = raw.substr( cursorpos ); |
---|
[6846] | 172 | |
---|
| 173 | /* set the text */ |
---|
[6876] | 174 | std::string assembled = "$ " + left + "|" + right; |
---|
| 175 | |
---|
| 176 | /* adjust curser position - magic number 5 for font width */ |
---|
| 177 | sub_adjust_dispoffset( (this->input->getUnclippedInnerRect().getWidth()/6), |
---|
| 178 | cursorpos, assembled.length() ); |
---|
| 179 | this->input->setProperty( "Text", assembled.substr( disp_offset ) ); |
---|
| 180 | |
---|
| 181 | /* reset display offset */ |
---|
| 182 | disp_offset = 0; |
---|
| 183 | |
---|
| 184 | /* adjust curser position - magic number 5 for font width */ |
---|
| 185 | sub_adjust_dispoffset( (this->inputonly->getUnclippedInnerRect().getWidth()/6), |
---|
| 186 | cursorpos, assembled.length() ); |
---|
| 187 | this->inputonly->setProperty( "Text", assembled.substr( disp_offset) ); |
---|
| 188 | |
---|
| 189 | /* reset display offset */ |
---|
| 190 | disp_offset = 0; |
---|
[6776] | 191 | } |
---|
| 192 | |
---|
| 193 | void ChatInputHandler::addline() |
---|
| 194 | { |
---|
| 195 | /* actually do send what was input */ |
---|
| 196 | /* a) get the string out of the inputbuffer */ |
---|
[6788] | 197 | std::string msgtosend = this->inpbuf->get(); |
---|
[6776] | 198 | |
---|
[6846] | 199 | if( msgtosend.length() == 0 ) |
---|
| 200 | { this->deactivate(); |
---|
| 201 | return; |
---|
| 202 | } |
---|
| 203 | |
---|
[6776] | 204 | /* b) clear the input buffer */ |
---|
[6788] | 205 | if (this->inpbuf->getSize() > 0) |
---|
| 206 | this->inpbuf->clear(); |
---|
[6776] | 207 | |
---|
| 208 | /* c) send the chat via some call */ |
---|
[6777] | 209 | Host::Chat( msgtosend ); |
---|
[6776] | 210 | |
---|
| 211 | /* d) stop listening to input */ |
---|
[6788] | 212 | this->deactivate(); |
---|
[6846] | 213 | |
---|
| 214 | /* e) create item and add to history */ |
---|
| 215 | CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( msgtosend ); |
---|
| 216 | this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
---|
| 217 | this->lb_history->ensureItemIsVisible( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
---|
| 218 | |
---|
| 219 | /* f) make sure the history handles it */ |
---|
| 220 | this->lb_history->handleUpdatedItemData(); |
---|
[6776] | 221 | } |
---|
| 222 | |
---|
| 223 | void ChatInputHandler::backspace() |
---|
[6846] | 224 | { this->inpbuf->removeBehindCursor(); } |
---|
[6776] | 225 | |
---|
| 226 | void ChatInputHandler::deleteChar() |
---|
[6846] | 227 | { this->inpbuf->removeAtCursor(); } |
---|
[6776] | 228 | |
---|
| 229 | void ChatInputHandler::cursorRight() |
---|
[6846] | 230 | { this->inpbuf->increaseCursor(); } |
---|
[6776] | 231 | |
---|
| 232 | void ChatInputHandler::cursorLeft() |
---|
[6846] | 233 | { this->inpbuf->decreaseCursor(); } |
---|
[6776] | 234 | |
---|
| 235 | void ChatInputHandler::cursorEnd() |
---|
[6846] | 236 | { this->inpbuf->setCursorToEnd(); } |
---|
[6776] | 237 | |
---|
| 238 | void ChatInputHandler::cursorHome() |
---|
[6846] | 239 | { this->inpbuf->setCursorToBegin(); } |
---|
[6776] | 240 | |
---|
| 241 | void ChatInputHandler::exit() |
---|
[6846] | 242 | { } |
---|
[6776] | 243 | |
---|
| 244 | } |
---|