Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/ChatInputHandler.cc @ 7660

Last change on this file since 7660 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 10.3 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"
[7284]30#include "util/ScopedSingletonManager.h"
[6788]31#include "core/CoreIncludes.h"
[6846]32#include "core/GUIManager.h"
33#include "core/CorePrereqs.h"
[7284]34#include "core/command/ConsoleCommand.h"
[6846]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
[7127]42namespace orxonox
[6776]43{
[6777]44  /* singleton */
[6788]45  ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false );
[6876]46
47  /* add commands to console */
[7284]48  SetConsoleCommand( "startchat", &ChatInputHandler::activate_static );
49  SetConsoleCommand( "startchat_small", &ChatInputHandler::activate_small_static );
[6777]50
[6776]51  /* constructor */
[6788]52  ChatInputHandler::ChatInputHandler()
[6776]53  {
[6777]54    /* register the object  */
55    RegisterObject(ChatInputHandler);
56
[6776]57    /* create necessary objects */
58    this->inpbuf = new InputBuffer();
[6876]59    this->disp_offset = 0;
[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
[7043]66    /* setup colors */
67    setupColors();
68
[6788]69    /* configure the input buffer */
[6791]70    configureInputBuffer();
[6788]71
72    this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic );
73    this->inputState->setKeyHandler(this->inpbuf);
[6776]74  }
75
[7043]76  /* configure input buffer, sub for the constructor */
[6776]77  void ChatInputHandler::configureInputBuffer()
78  {
[6910]79    /* INSTALL CALLBACKS */
[6846]80    /* input has changed */
[6788]81    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
[6846]82
83    /* add a line */
[6788]84    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false);
85    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false);
[6776]86
[6846]87    /* backspace */
[6788]88    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true);
[6890]89    //this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true);
[6776]90
[6846]91    /* exit the chatinputhandler thingy (tbd) */
[6788]92    this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape
[6776]93
[6846]94    /* delete character */
[6788]95    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete);
[6776]96
[6846]97    /* cursor movement */
[6788]98    this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight,     KeyCode::Right);
99    this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft,      KeyCode::Left);
100    this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd,       KeyCode::End);
101    this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome,      KeyCode::Home);
[6846]102
[6910]103    /* GET WINDOW POINTERS */
[6846]104    input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" );
[6870]105    inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" );
106
[6910]107    /* get pointer to the history window */
[6846]108    CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" );
[6910]109
110    /* cast it to a listbox */
[7127]111    lb_history = dynamic_cast<CEGUI::Listbox*>(history);
[6846]112
113    /* assert wee */
114    assert( lb_history );
[6776]115  }
116
[7043]117  /* setup the colors, sub for the constructor */
118  void ChatInputHandler::setupColors()
119  {
[7049]120    /* auto variables */
[7043]121    float red = 1.0, green = 0.5, blue = 0.5;
122    int i = 0;
[6777]123
[7049]124    // three loops: red tones, blue tones and green tones
[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 ] );
[7183]129      green += 0.2f, blue += 0.2f;
[7043]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 ] );
[7183]137      red += 0.2f, blue += 0.2f;
[7043]138    }
139
[7127]140    // blues
[7043]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 ] );
[7183]145      red += 0.2f, green += 0.2f;
[7043]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
[7127]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  {
[7049]186    /* sanity checks */
[7043]187    if( !tocolor )
188      COUT(2) << "Empty ListBoxTextItem given to "
189        "ChatInputhandler::sub_setcolor().\n";
190
191    /* "hash" the name */
192    int hash = 0;
193    for( int i = name.length(); i > 0; --i )
194      hash += name[i-1];
195    hash = hash % this->NumberOfColors;
196
197    /* set the color according to the hash */
198    tocolor->setTextColours( *(this->text_colors[ hash ]) );
199  }
200
201  /* handle incoming chat */
[7127]202  void ChatInputHandler::incomingChat(const std::string& message,
[6885]203    unsigned int senderID)
[6876]204  {
[7043]205    /* look up the actual name of the sender */
206    std::string text, name = "unknown";
[6885]207
[7043]208    /* setup player name info */
[6885]209    if (senderID != CLIENTID_UNKNOWN)
[7127]210    {
[6885]211       PlayerInfo* player = PlayerManager::getInstance().getClient(senderID);
212       if (player)
213         name = player->getName();
214    }
215
[7043]216    /* assemble the text */
217    text = name + ": " + message;
218
219    /* create item */
[6885]220    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( text );
[7043]221
222    /* setup colors */
223    sub_setcolor( toadd, name );
224
225    /* now add */
[6885]226    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
[7127]227    this->lb_history->ensureItemIsVisible(
[7043]228      dynamic_cast<CEGUI::ListboxItem*>(toadd) );
[6885]229
[7043]230    /* make sure the history handles it */
[6885]231    this->lb_history->handleUpdatedItemData();
[7127]232  }
[6885]233
234
[7043]235  /* sub for inputchanged */
[7127]236  void ChatInputHandler::sub_adjust_dispoffset( int maxlen,
237    int cursorpos,
[6885]238    int inplen )
239  {
[6876]240    /* already start offsetting 5 characters before end */
241    if( cursorpos+5 > maxlen )
[7127]242    {
[6876]243      /* always stay 5 characters ahead of end, looks better */
244      ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0;
245
246      /* enforce visibility of cursor */
247      (disp_offset > cursorpos ) ? disp_offset = 0 : 1;
248    }
[7127]249
[6876]250    /* make sure we don't die at substr */
251    if( inplen <= disp_offset ) disp_offset = 0;
252  }
253
[6776]254  /* callbacks for InputBuffer */
255  void ChatInputHandler::inputChanged()
256  {
[6846]257    /* update the cursor and the window */
258    std::string raw = this->inpbuf->get();
[6876]259    int cursorpos = this->inpbuf->getCursorPosition();
[7127]260
[6846]261    /* get string before cursor */
[6876]262    std::string left = raw.substr( 0, cursorpos );
[6837]263
[6846]264    /* see if there's a string after the cursor */
265    std::string right = "";
266    if( raw.length() >= left.length()+1 )
[6876]267      right = raw.substr( cursorpos );
[7127]268
[6846]269    /* set the text */
[6876]270    std::string assembled = "$ " + left + "|" + right;
271
[6879]272    if( this->fullchat )
[7127]273    {
[6879]274      /* adjust curser position - magic number 5 for font width */
[7183]275      sub_adjust_dispoffset( (int)(this->input->getUnclippedInnerRect().getWidth()/6),
[6879]276        cursorpos, assembled.length() );
277      this->input->setProperty( "Text", assembled.substr( disp_offset ) );
278    }
279    else
280    {
281      /* adjust curser position - magic number 5 for font width */
[7183]282      sub_adjust_dispoffset( (int)(this->inputonly->getUnclippedInnerRect().getWidth()/6),
[6879]283        cursorpos, assembled.length() );
284      this->inputonly->setProperty( "Text", assembled.substr( disp_offset) );
285    }
[6876]286
287    /* reset display offset */
288    disp_offset = 0;
[6776]289  }
290
291  void ChatInputHandler::addline()
292  {
293    /* actually do send what was input */
294    /* a) get the string out of the inputbuffer */
[6788]295    std::string msgtosend = this->inpbuf->get();
[6776]296
[6846]297    if( msgtosend.length() == 0 )
298    { this->deactivate();
299      return;
300    }
301
[6776]302    /* b) clear the input buffer */
[6788]303    if (this->inpbuf->getSize() > 0)
304      this->inpbuf->clear();
[6776]305
306    /* c) send the chat via some call */
[6777]307    Host::Chat( msgtosend );
[6776]308
[6885]309    /* d) stop listening to input - only if this is not fullchat */
[6879]310    if( !this->fullchat )
311      this->deactivate();
[6846]312
[6776]313  }
314
315  void ChatInputHandler::backspace()
[6846]316  { this->inpbuf->removeBehindCursor(); }
[6776]317
318  void ChatInputHandler::deleteChar()
[6846]319  { this->inpbuf->removeAtCursor(); }
[6776]320
321  void ChatInputHandler::cursorRight()
[6846]322  { this->inpbuf->increaseCursor(); }
[7127]323
[6776]324  void ChatInputHandler::cursorLeft()
[6846]325  { this->inpbuf->decreaseCursor(); }
[7127]326
[6776]327  void ChatInputHandler::cursorEnd()
[6846]328  { this->inpbuf->setCursorToEnd(); }
[6776]329
330  void ChatInputHandler::cursorHome()
[6846]331  { this->inpbuf->setCursorToBegin(); }
[6776]332
333  void ChatInputHandler::exit()
[6879]334  {
335    /* b) clear the input buffer */
336    if (this->inpbuf->getSize() > 0)
337      this->inpbuf->clear();
[6776]338
[6879]339    /* d) stop listening to input  */
340    this->deactivate();
341  }
342
[6776]343}
Note: See TracBrowser for help on using the repository browser.