Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/orxonox/ChatInputHandler.cc @ 8000

Last change on this file since 8000 was 8000, checked in by rgrieder, 13 years ago

Destructors can sometimes be very useful for clean up ;)

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