Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/chat/ChatInputHandler.cc @ 9672

Last change on this file since 9672 was 9672, checked in by smerkli, 11 years ago

Added a few includes that were necessary with the switch to GCC > 4.7
as well as a small define that lets boost work with threads on this
new GCC version. Things compile now, but have not been tested to be running
yet.

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