| [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 ); |
|---|
| [6791] | 46 | SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat", |
|---|
| 47 | true ); |
|---|
| [6870] | 48 | SetConsoleCommandAlias( ChatInputHandler, activate_small_static, |
|---|
| 49 | "startchat_small", true ); |
|---|
| [6777] | 50 | |
|---|
| [6846] | 51 | |
|---|
| [6776] | 52 | /* constructor */ |
|---|
| [6788] | 53 | ChatInputHandler::ChatInputHandler() |
|---|
| [6776] | 54 | { |
|---|
| [6777] | 55 | /* register the object */ |
|---|
| 56 | RegisterObject(ChatInputHandler); |
|---|
| 57 | |
|---|
| [6776] | 58 | /* create necessary objects */ |
|---|
| 59 | this->inpbuf = new InputBuffer(); |
|---|
| [6846] | 60 | assert( this->inpbuf != NULL ); |
|---|
| [6776] | 61 | |
|---|
| [6870] | 62 | /* generate chatbox ui and chatbox-inputonly ui */ |
|---|
| [6846] | 63 | GUIManager::getInstance().loadGUI( "ChatBox" ); |
|---|
| [6870] | 64 | GUIManager::getInstance().loadGUI( "ChatBox-inputonly" ); |
|---|
| [6846] | 65 | |
|---|
| [6788] | 66 | /* configure the input buffer */ |
|---|
| [6791] | 67 | configureInputBuffer(); |
|---|
| [6788] | 68 | |
|---|
| 69 | this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic ); |
|---|
| 70 | this->inputState->setKeyHandler(this->inpbuf); |
|---|
| [6776] | 71 | } |
|---|
| 72 | |
|---|
| 73 | void ChatInputHandler::configureInputBuffer() |
|---|
| 74 | { |
|---|
| [6846] | 75 | /* input has changed */ |
|---|
| [6788] | 76 | this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true); |
|---|
| [6846] | 77 | |
|---|
| 78 | /* add a line */ |
|---|
| [6788] | 79 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\r', false); |
|---|
| 80 | this->inpbuf->registerListener(this, &ChatInputHandler::addline, '\n', false); |
|---|
| [6776] | 81 | |
|---|
| [6846] | 82 | /* backspace */ |
|---|
| [6788] | 83 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\b', true); |
|---|
| 84 | this->inpbuf->registerListener(this, &ChatInputHandler::backspace, '\177', true); |
|---|
| [6776] | 85 | |
|---|
| [6846] | 86 | /* exit the chatinputhandler thingy (tbd) */ |
|---|
| [6788] | 87 | this->inpbuf->registerListener(this, &ChatInputHandler::exit, '\033', true); // escape |
|---|
| [6776] | 88 | |
|---|
| [6846] | 89 | /* delete character */ |
|---|
| [6788] | 90 | this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar, KeyCode::Delete); |
|---|
| [6776] | 91 | |
|---|
| [6846] | 92 | /* cursor movement */ |
|---|
| [6788] | 93 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight, KeyCode::Right); |
|---|
| 94 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft, KeyCode::Left); |
|---|
| 95 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd, KeyCode::End); |
|---|
| 96 | this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome, KeyCode::Home); |
|---|
| [6846] | 97 | |
|---|
| 98 | /* get window pointers */ |
|---|
| 99 | input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" ); |
|---|
| [6870] | 100 | inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" ); |
|---|
| 101 | |
|---|
| [6846] | 102 | CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" ); |
|---|
| 103 | lb_history = dynamic_cast<CEGUI::Listbox*>(history); |
|---|
| 104 | |
|---|
| 105 | /* assert wee */ |
|---|
| 106 | assert( lb_history ); |
|---|
| [6776] | 107 | } |
|---|
| 108 | |
|---|
| [6777] | 109 | |
|---|
| 110 | /* activate, deactivate */ |
|---|
| [6791] | 111 | void ChatInputHandler::activate_static() |
|---|
| [6870] | 112 | { ChatInputHandler::getInstance().activate( true ); } |
|---|
| 113 | |
|---|
| 114 | void ChatInputHandler::activate_small_static() |
|---|
| 115 | { ChatInputHandler::getInstance().activate( false ); } |
|---|
| 116 | |
|---|
| 117 | void ChatInputHandler::activate( bool full ) |
|---|
| [6777] | 118 | { |
|---|
| 119 | /* start listening */ |
|---|
| [6846] | 120 | //COUT(0) << "chatinput activated." << std::endl; |
|---|
| [6788] | 121 | InputManager::getInstance().enterState("chatinput"); |
|---|
| [6846] | 122 | |
|---|
| 123 | /* MARK add spawning of chat widget stuff here.*/ |
|---|
| [6870] | 124 | if( full ) |
|---|
| 125 | GUIManager::getInstance().showGUI( "ChatBox" ); |
|---|
| 126 | else |
|---|
| 127 | GUIManager::getInstance().showGUI( "ChatBox-inputonly" ); |
|---|
| [6777] | 128 | } |
|---|
| 129 | |
|---|
| [6788] | 130 | void ChatInputHandler::deactivate() |
|---|
| [6777] | 131 | { |
|---|
| 132 | /* stop listening */ |
|---|
| [6788] | 133 | InputManager::getInstance().leaveState("chatinput"); |
|---|
| [6846] | 134 | |
|---|
| 135 | /* MARK add un-spawning of chat widget stuff here. */ |
|---|
| 136 | GUIManager::getInstance().hideGUI( "ChatBox" ); |
|---|
| [6870] | 137 | GUIManager::getInstance().hideGUI( "ChatBox-inputonly" ); |
|---|
| [6777] | 138 | } |
|---|
| 139 | |
|---|
| [6776] | 140 | /* callbacks for InputBuffer */ |
|---|
| 141 | void ChatInputHandler::inputChanged() |
|---|
| 142 | { |
|---|
| [6846] | 143 | /* update the cursor and the window */ |
|---|
| 144 | std::string raw = this->inpbuf->get(); |
|---|
| 145 | |
|---|
| 146 | /* get string before cursor */ |
|---|
| 147 | std::string left = raw.substr( 0, this->inpbuf->getCursorPosition() ); |
|---|
| [6837] | 148 | |
|---|
| [6846] | 149 | /* see if there's a string after the cursor */ |
|---|
| 150 | std::string right = ""; |
|---|
| 151 | if( raw.length() >= left.length()+1 ) |
|---|
| 152 | right = raw.substr( this->inpbuf->getCursorPosition() ); |
|---|
| 153 | |
|---|
| 154 | /* set the text */ |
|---|
| 155 | this->input->setProperty( "Text", left + "|" + right ); |
|---|
| [6870] | 156 | this->inputonly->setProperty( "Text", left + "|" + right ); |
|---|
| [6776] | 157 | } |
|---|
| 158 | |
|---|
| 159 | void ChatInputHandler::addline() |
|---|
| 160 | { |
|---|
| 161 | /* actually do send what was input */ |
|---|
| 162 | /* a) get the string out of the inputbuffer */ |
|---|
| [6788] | 163 | std::string msgtosend = this->inpbuf->get(); |
|---|
| [6776] | 164 | |
|---|
| [6846] | 165 | if( msgtosend.length() == 0 ) |
|---|
| 166 | { this->deactivate(); |
|---|
| 167 | return; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| [6776] | 170 | /* b) clear the input buffer */ |
|---|
| [6788] | 171 | if (this->inpbuf->getSize() > 0) |
|---|
| 172 | this->inpbuf->clear(); |
|---|
| [6776] | 173 | |
|---|
| 174 | /* c) send the chat via some call */ |
|---|
| [6777] | 175 | Host::Chat( msgtosend ); |
|---|
| [6776] | 176 | |
|---|
| 177 | /* d) stop listening to input */ |
|---|
| [6788] | 178 | this->deactivate(); |
|---|
| [6846] | 179 | |
|---|
| 180 | /* e) create item and add to history */ |
|---|
| 181 | CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( msgtosend ); |
|---|
| 182 | this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
|---|
| 183 | this->lb_history->ensureItemIsVisible( dynamic_cast<CEGUI::ListboxItem*>(toadd) ); |
|---|
| 184 | |
|---|
| 185 | /* f) make sure the history handles it */ |
|---|
| 186 | this->lb_history->handleUpdatedItemData(); |
|---|
| [6776] | 187 | } |
|---|
| 188 | |
|---|
| 189 | void ChatInputHandler::backspace() |
|---|
| [6846] | 190 | { this->inpbuf->removeBehindCursor(); } |
|---|
| [6776] | 191 | |
|---|
| 192 | void ChatInputHandler::deleteChar() |
|---|
| [6846] | 193 | { this->inpbuf->removeAtCursor(); } |
|---|
| [6776] | 194 | |
|---|
| 195 | void ChatInputHandler::cursorRight() |
|---|
| [6846] | 196 | { this->inpbuf->increaseCursor(); } |
|---|
| [6776] | 197 | |
|---|
| 198 | void ChatInputHandler::cursorLeft() |
|---|
| [6846] | 199 | { this->inpbuf->decreaseCursor(); } |
|---|
| [6776] | 200 | |
|---|
| 201 | void ChatInputHandler::cursorEnd() |
|---|
| [6846] | 202 | { this->inpbuf->setCursorToEnd(); } |
|---|
| [6776] | 203 | |
|---|
| 204 | void ChatInputHandler::cursorHome() |
|---|
| [6846] | 205 | { this->inpbuf->setCursorToBegin(); } |
|---|
| [6776] | 206 | |
|---|
| 207 | void ChatInputHandler::exit() |
|---|
| [6846] | 208 | { } |
|---|
| [6776] | 209 | |
|---|
| 210 | } |
|---|