Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7043 was 7043, checked in by smerkli, 14 years ago
  • Property svn:eol-style set to native
File size: 10.2 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
[7043]68    /* setup colors */
69    setupColors();
70
[6788]71    /* configure the input buffer */
[6791]72    configureInputBuffer();
[6788]73
74    this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic );
75    this->inputState->setKeyHandler(this->inpbuf);
[6776]76  }
77
[7043]78  /* configure input buffer, sub for the constructor */
[6776]79  void ChatInputHandler::configureInputBuffer()
80  {
[6910]81    /* INSTALL CALLBACKS */
[6846]82    /* input has changed */
[6788]83    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
[6846]84
85    /* add a line */
[6788]86    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false);
87    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false);
[6776]88
[6846]89    /* backspace */
[6788]90    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true);
[6890]91    //this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true);
[6776]92
[6846]93    /* exit the chatinputhandler thingy (tbd) */
[6788]94    this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape
[6776]95
[6846]96    /* delete character */
[6788]97    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete);
[6776]98
[6846]99    /* cursor movement */
[6788]100    this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight,     KeyCode::Right);
101    this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft,      KeyCode::Left);
102    this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd,       KeyCode::End);
103    this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome,      KeyCode::Home);
[6846]104
[6910]105    /* GET WINDOW POINTERS */
[6846]106    input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" );
[6870]107    inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" );
108
[6910]109    /* get pointer to the history window */
[6846]110    CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" );
[6910]111
112    /* cast it to a listbox */
[6846]113    lb_history = dynamic_cast<CEGUI::Listbox*>(history); 
114
115    /* assert wee */
116    assert( lb_history );
[6776]117  }
118
[7043]119  /* setup the colors, sub for the constructor */
120  void ChatInputHandler::setupColors()
121  {
122    float red = 1.0, green = 0.5, blue = 0.5;
123    int i = 0;
[6777]124
[7043]125    // reds
126    for( i = 0; i < NumberOfColors/3; ++i )
127    { this->text_colors[ i ] = new CEGUI::colour( red, green, blue );
128      assert( this->text_colors[ i ] );
129      green += 0.2, blue += 0.2;
130    }
131
132    // greens
133    red = 0.5, green = 1, blue = 0.5;
134    for( ; i < NumberOfColors*2/3; ++i )
135    { this->text_colors[ i ] = new CEGUI::colour( red, green, blue );
136      assert( this->text_colors[ i ] );
137      red += 0.2, blue += 0.2;
138    }
139
140    // blues
141    red = 0.5, green = 0.5, blue = 1;
142    for( ; i < NumberOfColors; ++i )
143    { this->text_colors[ i ] = new CEGUI::colour( red, green, blue );
144      assert( this->text_colors[ i ] );
145      red += 0.2, green += 0.2;
146    }
147  }
148
149
[6777]150  /* activate, deactivate */
[6791]151  void ChatInputHandler::activate_static()
[6870]152  { ChatInputHandler::getInstance().activate( true ); }
153
154  void ChatInputHandler::activate_small_static()
155  { ChatInputHandler::getInstance().activate( false ); }
156
157  void ChatInputHandler::activate( bool full )
[6777]158  {
159    /* start listening */
[6788]160    InputManager::getInstance().enterState("chatinput");
[6846]161
162    /* MARK add spawning of chat widget stuff here.*/
[6870]163    if( full )
164      GUIManager::getInstance().showGUI( "ChatBox" );
165    else
166      GUIManager::getInstance().showGUI( "ChatBox-inputonly" );
[6879]167
168    this->fullchat = full;
[6777]169  }
170
[6788]171  void ChatInputHandler::deactivate() 
[6777]172  {
173    /* stop listening */
[6788]174    InputManager::getInstance().leaveState("chatinput");
[6846]175
[6876]176    /* un-spawning of chat widget stuff */
[6846]177    GUIManager::getInstance().hideGUI( "ChatBox" );
[6870]178    GUIManager::getInstance().hideGUI( "ChatBox-inputonly" );
[6777]179  }
180
[6885]181
[7043]182  /* subs for incomingChat */
183  void ChatInputHandler::sub_setcolor( CEGUI::ListboxTextItem *tocolor,
184    std::string name )
185  {
186    if( !tocolor )
187      COUT(2) << "Empty ListBoxTextItem given to "
188        "ChatInputhandler::sub_setcolor().\n";
189
190    /* "hash" the name */
191    int hash = 0;
192    for( int i = name.length(); i > 0; --i )
193      hash += name[i-1];
194    hash = hash % this->NumberOfColors;
195
196    /* set the color according to the hash */
197    tocolor->setTextColours( *(this->text_colors[ hash ]) );
198  }
199
200  /* handle incoming chat */
[6885]201  void ChatInputHandler::incomingChat(const std::string& message, 
202    unsigned int senderID)
[6876]203  {
[7043]204    /* look up the actual name of the sender */
205    std::string text, name = "unknown";
[6885]206
[7043]207    /* setup player name info */
[6885]208    if (senderID != CLIENTID_UNKNOWN)
[7043]209    { 
[6885]210       PlayerInfo* player = PlayerManager::getInstance().getClient(senderID);
211       if (player)
212         name = player->getName();
213    }
214
[7043]215    /* assemble the text */
216    text = name + ": " + message;
217
218    /* create item */
[6885]219    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( text );
[7043]220
221    /* setup colors */
222    sub_setcolor( toadd, name );
223
224    /* now add */
[6885]225    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
[7043]226    this->lb_history->ensureItemIsVisible( 
227      dynamic_cast<CEGUI::ListboxItem*>(toadd) );
[6885]228
[7043]229    /* make sure the history handles it */
[6885]230    this->lb_history->handleUpdatedItemData();
231  } 
232
233
[7043]234  /* sub for inputchanged */
[6885]235  void ChatInputHandler::sub_adjust_dispoffset( int maxlen, 
236    int cursorpos, 
237    int inplen )
238  {
[6876]239    /* already start offsetting 5 characters before end */
240    if( cursorpos+5 > maxlen )
241    { 
242      /* always stay 5 characters ahead of end, looks better */
243      ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0;
244
245      /* enforce visibility of cursor */
246      (disp_offset > cursorpos ) ? disp_offset = 0 : 1;
247    }
248     
249    /* make sure we don't die at substr */
250    if( inplen <= disp_offset ) disp_offset = 0;
251  }
252
[6776]253  /* callbacks for InputBuffer */
254  void ChatInputHandler::inputChanged()
255  {
[6846]256    /* update the cursor and the window */
257    std::string raw = this->inpbuf->get();
[6876]258    int cursorpos = this->inpbuf->getCursorPosition();
[6846]259   
260    /* get string before cursor */
[6876]261    std::string left = raw.substr( 0, cursorpos );
[6837]262
[6846]263    /* see if there's a string after the cursor */
264    std::string right = "";
265    if( raw.length() >= left.length()+1 )
[6876]266      right = raw.substr( cursorpos );
[6846]267     
268    /* set the text */
[6876]269    std::string assembled = "$ " + left + "|" + right;
270
[6879]271    if( this->fullchat )
272    { 
273      /* adjust curser position - magic number 5 for font width */
274      sub_adjust_dispoffset( (this->input->getUnclippedInnerRect().getWidth()/6), 
275        cursorpos, assembled.length() );
276      this->input->setProperty( "Text", assembled.substr( disp_offset ) );
277    }
278    else
279    {
280      /* adjust curser position - magic number 5 for font width */
281      sub_adjust_dispoffset( (this->inputonly->getUnclippedInnerRect().getWidth()/6), 
282        cursorpos, assembled.length() );
283      this->inputonly->setProperty( "Text", assembled.substr( disp_offset) );
284    }
[6876]285
286    /* reset display offset */
287    disp_offset = 0;
[6776]288  }
289
290  void ChatInputHandler::addline()
291  {
292    /* actually do send what was input */
293    /* a) get the string out of the inputbuffer */
[6788]294    std::string msgtosend = this->inpbuf->get();
[6776]295
[6846]296    if( msgtosend.length() == 0 )
297    { this->deactivate();
298      return;
299    }
300
[6776]301    /* b) clear the input buffer */
[6788]302    if (this->inpbuf->getSize() > 0)
303      this->inpbuf->clear();
[6776]304
305    /* c) send the chat via some call */
[6777]306    Host::Chat( msgtosend );
[6776]307
[6885]308    /* d) stop listening to input - only if this is not fullchat */
[6879]309    if( !this->fullchat )
310      this->deactivate();
[6846]311
[6776]312  }
313
314  void ChatInputHandler::backspace()
[6846]315  { this->inpbuf->removeBehindCursor(); }
[6776]316
317  void ChatInputHandler::deleteChar()
[6846]318  { this->inpbuf->removeAtCursor(); }
[6776]319
320  void ChatInputHandler::cursorRight()
[6846]321  { this->inpbuf->increaseCursor(); }
[6776]322 
323  void ChatInputHandler::cursorLeft()
[6846]324  { this->inpbuf->decreaseCursor(); }
[6776]325 
326  void ChatInputHandler::cursorEnd()
[6846]327  { this->inpbuf->setCursorToEnd(); }
[6776]328
329  void ChatInputHandler::cursorHome()
[6846]330  { this->inpbuf->setCursorToBegin(); }
[6776]331
332  void ChatInputHandler::exit()
[6879]333  {
334    /* b) clear the input buffer */
335    if (this->inpbuf->getSize() > 0)
336      this->inpbuf->clear();
[6776]337
[6879]338    /* d) stop listening to input  */
339    this->deactivate();
340  }
341
[6776]342}
Note: See TracBrowser for help on using the repository browser.