Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound6/src/orxonox/ChatInputHandler.cc @ 8784

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

Destructors can sometimes be very useful for clean up ;)

  • Property svn:eol-style set to native
File size: 10.3 KB
Line 
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 "util/ScopedSingletonManager.h"
31#include "core/CoreIncludes.h"
32#include "core/GUIManager.h"
33#include "core/CorePrereqs.h"
34#include "core/command/ConsoleCommand.h"
35#include <CEGUIWindow.h>
36#include <elements/CEGUIListbox.h>
37#include <elements/CEGUIListboxItem.h>
38#include <elements/CEGUIListboxTextItem.h>
39#include <CEGUIWindowManager.h>
40#include <string>
41
42namespace orxonox
43{
44  /* singleton */
45  ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false );
46
47  /* add commands to console */
48  SetConsoleCommand( "startchat", &ChatInputHandler::activate_static );
49  SetConsoleCommand( "startchat_small", &ChatInputHandler::activate_small_static );
50
51  /* constructor */
52  ChatInputHandler::ChatInputHandler()
53  {
54    /* register the object  */
55    RegisterObject(ChatInputHandler);
56
57    /* create necessary objects */
58    this->inpbuf = new InputBuffer();
59    this->disp_offset = 0;
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    /* setup colors */
67    setupColors();
68
69    /* configure the input buffer */
70    configureInputBuffer();
71
72    this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic );
73    this->inputState->setKeyHandler(this->inpbuf);
74  }
75
76  ChatInputHandler::~ChatInputHandler()
77  {
78    /* Clean up */
79    InputManager::getInstance().destroyState("chatinput");
80    delete this->inpbuf;
81  }
82
83  /* configure input buffer, sub for the constructor */
84  void ChatInputHandler::configureInputBuffer()
85  {
86    /* INSTALL CALLBACKS */
87    /* input has changed */
88    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
89
90    /* add a line */
91    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false);
92    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false);
93
94    /* backspace */
95    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true);
96    //this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true);
97
98    /* exit the chatinputhandler thingy (tbd) */
99    this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape
100
101    /* delete character */
102    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete);
103
104    /* cursor movement */
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);
109
110    /* GET WINDOW POINTERS */
111    input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" );
112    inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" );
113
114    /* get pointer to the history window */
115    CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" );
116
117    /* cast it to a listbox */
118    lb_history = dynamic_cast<CEGUI::Listbox*>(history);
119
120    /* assert wee */
121    assert( lb_history );
122  }
123
124  /* setup the colors, sub for the constructor */
125  void ChatInputHandler::setupColors()
126  {
127    /* auto variables */
128    float red = 1.0, green = 0.5, blue = 0.5;
129    int i = 0;
130
131    // three loops: red tones, blue tones and green tones
132    // reds
133    for( i = 0; i < NumberOfColors/3; ++i )
134    { this->text_colors[ i ] = CEGUI::colour( red, green, blue );
135      green += 0.2f, blue += 0.2f;
136    }
137
138    // greens
139    red = 0.5, green = 1, blue = 0.5;
140    for( ; i < NumberOfColors*2/3; ++i )
141    { this->text_colors[ i ] = CEGUI::colour( red, green, blue );
142      red += 0.2f, blue += 0.2f;
143    }
144
145    // blues
146    red = 0.5, green = 0.5, blue = 1;
147    for( ; i < NumberOfColors; ++i )
148    { this->text_colors[ i ] = CEGUI::colour( red, green, blue );
149      red += 0.2f, green += 0.2f;
150    }
151  }
152
153
154  /* activate, deactivate */
155  void ChatInputHandler::activate_static()
156  { ChatInputHandler::getInstance().activate( true ); }
157
158  void ChatInputHandler::activate_small_static()
159  { ChatInputHandler::getInstance().activate( false ); }
160
161  void ChatInputHandler::activate( bool full )
162  {
163    /* start listening */
164    InputManager::getInstance().enterState("chatinput");
165
166    /* MARK add spawning of chat widget stuff here.*/
167    if( full )
168      GUIManager::getInstance().showGUI( "ChatBox" );
169    else
170      GUIManager::getInstance().showGUI( "ChatBox-inputonly" );
171
172    this->fullchat = full;
173  }
174
175  void ChatInputHandler::deactivate()
176  {
177    /* stop listening */
178    InputManager::getInstance().leaveState("chatinput");
179
180    /* un-spawning of chat widget stuff */
181    GUIManager::getInstance().hideGUI( "ChatBox" );
182    GUIManager::getInstance().hideGUI( "ChatBox-inputonly" );
183  }
184
185
186  /* subs for incomingChat */
187  void ChatInputHandler::sub_setcolor( CEGUI::ListboxTextItem *tocolor,
188    std::string name )
189  {
190    /* sanity checks */
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 */
202    tocolor->setTextColours( this->text_colors[ hash ] );
203  }
204
205  /* handle incoming chat */
206  void ChatInputHandler::incomingChat(const std::string& message,
207    unsigned int senderID)
208  {
209    /* look up the actual name of the sender */
210    std::string text, name = "unknown";
211
212    /* setup player name info */
213    if (senderID != CLIENTID_UNKNOWN)
214    {
215       PlayerInfo* player = PlayerManager::getInstance().getClient(senderID);
216       if (player)
217         name = player->getName();
218    }
219
220    /* assemble the text */
221    text = name + ": " + message;
222
223    /* create item */
224    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( text );
225
226    /* setup colors */
227    sub_setcolor( toadd, name );
228
229    /* now add */
230    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
231    this->lb_history->ensureItemIsVisible(
232      dynamic_cast<CEGUI::ListboxItem*>(toadd) );
233
234    /* make sure the history handles it */
235    this->lb_history->handleUpdatedItemData();
236  }
237
238
239  /* sub for inputchanged */
240  void ChatInputHandler::sub_adjust_dispoffset( int maxlen,
241    int cursorpos,
242    int inplen )
243  {
244    /* already start offsetting 5 characters before end */
245    if( cursorpos+5 > maxlen )
246    {
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    }
253
254    /* make sure we don't die at substr */
255    if( inplen <= disp_offset ) disp_offset = 0;
256  }
257
258  /* callbacks for InputBuffer */
259  void ChatInputHandler::inputChanged()
260  {
261    /* update the cursor and the window */
262    std::string raw = this->inpbuf->get();
263    int cursorpos = this->inpbuf->getCursorPosition();
264
265    /* get string before cursor */
266    std::string left = raw.substr( 0, cursorpos );
267
268    /* see if there's a string after the cursor */
269    std::string right = "";
270    if( raw.length() >= left.length()+1 )
271      right = raw.substr( cursorpos );
272
273    /* set the text */
274    std::string assembled = "$ " + left + "|" + right;
275
276    if( this->fullchat )
277    {
278      /* adjust curser position - magic number 5 for font width */
279      sub_adjust_dispoffset( (int)(this->input->getUnclippedInnerRect().getWidth()/6),
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 */
286      sub_adjust_dispoffset( (int)(this->inputonly->getUnclippedInnerRect().getWidth()/6),
287        cursorpos, assembled.length() );
288      this->inputonly->setProperty( "Text", assembled.substr( disp_offset) );
289    }
290
291    /* reset display offset */
292    disp_offset = 0;
293  }
294
295  void ChatInputHandler::addline()
296  {
297    /* actually do send what was input */
298    /* a) get the string out of the inputbuffer */
299    std::string msgtosend = this->inpbuf->get();
300
301    if( msgtosend.length() == 0 )
302    { this->deactivate();
303      return;
304    }
305
306    /* b) clear the input buffer */
307    if (this->inpbuf->getSize() > 0)
308      this->inpbuf->clear();
309
310    /* c) send the chat via some call */
311    Host::Chat( msgtosend );
312
313    /* d) stop listening to input - only if this is not fullchat */
314    if( !this->fullchat )
315      this->deactivate();
316
317  }
318
319  void ChatInputHandler::backspace()
320  { this->inpbuf->removeBehindCursor(); }
321
322  void ChatInputHandler::deleteChar()
323  { this->inpbuf->removeAtCursor(); }
324
325  void ChatInputHandler::cursorRight()
326  { this->inpbuf->increaseCursor(); }
327
328  void ChatInputHandler::cursorLeft()
329  { this->inpbuf->decreaseCursor(); }
330
331  void ChatInputHandler::cursorEnd()
332  { this->inpbuf->setCursorToEnd(); }
333
334  void ChatInputHandler::cursorHome()
335  { this->inpbuf->setCursorToBegin(); }
336
337  void ChatInputHandler::exit()
338  {
339    /* b) clear the input buffer */
340    if (this->inpbuf->getSize() > 0)
341      this->inpbuf->clear();
342
343    /* d) stop listening to input  */
344    this->deactivate();
345  }
346
347}
Note: See TracBrowser for help on using the repository browser.