Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/libs/src/orxonox/chat/ChatInputHandler.cc @ 10045

Last change on this file since 10045 was 9671, checked in by landauf, 11 years ago

adjusted code to compile with cegui 0.8 and 0.7. see www.cegui.org.uk/wiki/index.php/Changes_and_Porting_Tips_for_0.8.0

doesn't run yet (lua scripts fail)

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