Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/ChatInputHandler.cc @ 7028

Last change on this file since 7028 was 6934, checked in by cdaniel, 15 years ago

Fixed Include file destination (For Visual Studio under Windows)

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