| 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" | 
|---|
| 30 | #include <core/ScopedSingletonManager.h> | 
|---|
| 31 | #include "core/ConsoleCommand.h" | 
|---|
| 32 | #include "core/CoreIncludes.h" | 
|---|
| 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> | 
|---|
| 40 | #include <string> | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox  | 
|---|
| 43 | { | 
|---|
| 44 |   /* singleton */ | 
|---|
| 45 |   ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false ); | 
|---|
| 46 |   SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat", | 
|---|
| 47 |     true ); | 
|---|
| 48 |   SetConsoleCommandAlias( ChatInputHandler, activate_small_static,  | 
|---|
| 49 |     "startchat_small", true ); | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 |   /* constructor */ | 
|---|
| 53 |   ChatInputHandler::ChatInputHandler() | 
|---|
| 54 |   { | 
|---|
| 55 |     /* register the object  */ | 
|---|
| 56 |     RegisterObject(ChatInputHandler); | 
|---|
| 57 |  | 
|---|
| 58 |     /* create necessary objects */ | 
|---|
| 59 |     this->inpbuf = new InputBuffer(); | 
|---|
| 60 |     assert( this->inpbuf != NULL ); | 
|---|
| 61 |  | 
|---|
| 62 |     /* generate chatbox ui and chatbox-inputonly ui */ | 
|---|
| 63 |     GUIManager::getInstance().loadGUI( "ChatBox" ); | 
|---|
| 64 |     GUIManager::getInstance().loadGUI( "ChatBox-inputonly" ); | 
|---|
| 65 |  | 
|---|
| 66 |     /* configure the input buffer */ | 
|---|
| 67 |     configureInputBuffer(); | 
|---|
| 68 |  | 
|---|
| 69 |     this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic ); | 
|---|
| 70 |     this->inputState->setKeyHandler(this->inpbuf); | 
|---|
| 71 |   } | 
|---|
| 72 |  | 
|---|
| 73 |   void ChatInputHandler::configureInputBuffer() | 
|---|
| 74 |   { | 
|---|
| 75 |     /* input has changed */ | 
|---|
| 76 |     this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true); | 
|---|
| 77 |  | 
|---|
| 78 |     /* add a line */ | 
|---|
| 79 |     this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false); | 
|---|
| 80 |     this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false); | 
|---|
| 81 |  | 
|---|
| 82 |     /* backspace */ | 
|---|
| 83 |     this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true); | 
|---|
| 84 |     this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true); | 
|---|
| 85 |  | 
|---|
| 86 |     /* exit the chatinputhandler thingy (tbd) */ | 
|---|
| 87 |     this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape | 
|---|
| 88 |  | 
|---|
| 89 |     /* delete character */ | 
|---|
| 90 |     this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete); | 
|---|
| 91 |  | 
|---|
| 92 |     /* cursor movement */ | 
|---|
| 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); | 
|---|
| 97 |  | 
|---|
| 98 |     /* get window pointers */ | 
|---|
| 99 |     input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" ); | 
|---|
| 100 |     inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" ); | 
|---|
| 101 |  | 
|---|
| 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 ); | 
|---|
| 107 |   } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 |   /* activate, deactivate */ | 
|---|
| 111 |   void ChatInputHandler::activate_static() | 
|---|
| 112 |   { ChatInputHandler::getInstance().activate( true ); } | 
|---|
| 113 |  | 
|---|
| 114 |   void ChatInputHandler::activate_small_static() | 
|---|
| 115 |   { ChatInputHandler::getInstance().activate( false ); } | 
|---|
| 116 |  | 
|---|
| 117 |   void ChatInputHandler::activate( bool full ) | 
|---|
| 118 |   { | 
|---|
| 119 |     /* start listening */ | 
|---|
| 120 |     //COUT(0) << "chatinput activated." << std::endl; | 
|---|
| 121 |     InputManager::getInstance().enterState("chatinput"); | 
|---|
| 122 |  | 
|---|
| 123 |     /* MARK add spawning of chat widget stuff here.*/ | 
|---|
| 124 |     if( full ) | 
|---|
| 125 |       GUIManager::getInstance().showGUI( "ChatBox" ); | 
|---|
| 126 |     else | 
|---|
| 127 |       GUIManager::getInstance().showGUI( "ChatBox-inputonly" ); | 
|---|
| 128 |   } | 
|---|
| 129 |  | 
|---|
| 130 |   void ChatInputHandler::deactivate()  | 
|---|
| 131 |   { | 
|---|
| 132 |     /* stop listening */ | 
|---|
| 133 |     InputManager::getInstance().leaveState("chatinput"); | 
|---|
| 134 |  | 
|---|
| 135 |     /* MARK add un-spawning of chat widget stuff here. */ | 
|---|
| 136 |     GUIManager::getInstance().hideGUI( "ChatBox" ); | 
|---|
| 137 |     GUIManager::getInstance().hideGUI( "ChatBox-inputonly" ); | 
|---|
| 138 |   } | 
|---|
| 139 |  | 
|---|
| 140 |   /* callbacks for InputBuffer */ | 
|---|
| 141 |   void ChatInputHandler::inputChanged() | 
|---|
| 142 |   { | 
|---|
| 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() ); | 
|---|
| 148 |  | 
|---|
| 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 ); | 
|---|
| 156 |     this->inputonly->setProperty( "Text", left + "|" + right ); | 
|---|
| 157 |   } | 
|---|
| 158 |  | 
|---|
| 159 |   void ChatInputHandler::addline() | 
|---|
| 160 |   { | 
|---|
| 161 |     /* actually do send what was input */ | 
|---|
| 162 |     /* a) get the string out of the inputbuffer */ | 
|---|
| 163 |     std::string msgtosend = this->inpbuf->get(); | 
|---|
| 164 |  | 
|---|
| 165 |     if( msgtosend.length() == 0 ) | 
|---|
| 166 |     { this->deactivate(); | 
|---|
| 167 |       return; | 
|---|
| 168 |     } | 
|---|
| 169 |  | 
|---|
| 170 |     /* b) clear the input buffer */ | 
|---|
| 171 |     if (this->inpbuf->getSize() > 0) | 
|---|
| 172 |       this->inpbuf->clear(); | 
|---|
| 173 |  | 
|---|
| 174 |     /* c) send the chat via some call */ | 
|---|
| 175 |     Host::Chat( msgtosend ); | 
|---|
| 176 |  | 
|---|
| 177 |     /* d) stop listening to input  */ | 
|---|
| 178 |     this->deactivate(); | 
|---|
| 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(); | 
|---|
| 187 |   } | 
|---|
| 188 |  | 
|---|
| 189 |   void ChatInputHandler::backspace() | 
|---|
| 190 |   { this->inpbuf->removeBehindCursor(); } | 
|---|
| 191 |  | 
|---|
| 192 |   void ChatInputHandler::deleteChar() | 
|---|
| 193 |   { this->inpbuf->removeAtCursor(); } | 
|---|
| 194 |  | 
|---|
| 195 |   void ChatInputHandler::cursorRight() | 
|---|
| 196 |   { this->inpbuf->increaseCursor(); } | 
|---|
| 197 |    | 
|---|
| 198 |   void ChatInputHandler::cursorLeft() | 
|---|
| 199 |   { this->inpbuf->decreaseCursor(); } | 
|---|
| 200 |    | 
|---|
| 201 |   void ChatInputHandler::cursorEnd() | 
|---|
| 202 |   { this->inpbuf->setCursorToEnd(); } | 
|---|
| 203 |  | 
|---|
| 204 |   void ChatInputHandler::cursorHome() | 
|---|
| 205 |   { this->inpbuf->setCursorToBegin(); } | 
|---|
| 206 |  | 
|---|
| 207 |   void ChatInputHandler::exit() | 
|---|
| 208 |   { } | 
|---|
| 209 |  | 
|---|
| 210 | } | 
|---|