Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/chat2/src/orxonox/ChatInputHandler.cc @ 6890

Last change on this file since 6890 was 6890, checked in by smerkli, 14 years ago
  • Property svn:eol-style set to native
File size: 8.7 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 <core/ScopedSingletonManager.h>
31#include "core/ConsoleCommand.h"
32#include "core/CoreIncludes.h"
33#include "core/GUIManager.h"
34#include "core/CorePrereqs.h"
35#include <CEGUIWindow.h>
36#include <CEGUI/elements/CEGUIListbox.h>
37#include <CEGUI/elements/CEGUIListboxItem.h>
38#include <CEGUI/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  SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat",
49    true );
50  SetConsoleCommandAlias( ChatInputHandler, activate_small_static, 
51    "startchat_small", true );
52
53  /* constructor */
54  ChatInputHandler::ChatInputHandler()
55  {
56    /* register the object  */
57    RegisterObject(ChatInputHandler);
58
59    /* create necessary objects */
60    this->inpbuf = new InputBuffer();
61    this->disp_offset = 0;
62    assert( this->inpbuf != NULL );
63
64    /* generate chatbox ui and chatbox-inputonly ui */
65    GUIManager::getInstance().loadGUI( "ChatBox" );
66    GUIManager::getInstance().loadGUI( "ChatBox-inputonly" );
67
68    /* configure the input buffer */
69    configureInputBuffer();
70
71    this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic );
72    this->inputState->setKeyHandler(this->inpbuf);
73  }
74
75  void ChatInputHandler::configureInputBuffer()
76  {
77    /* input has changed */
78    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
79
80    /* add a line */
81    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false);
82    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false);
83
84    /* backspace */
85    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true);
86    //this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true);
87
88    /* exit the chatinputhandler thingy (tbd) */
89    this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape
90
91    /* delete character */
92    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete);
93
94    /* cursor movement */
95    this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight,     KeyCode::Right);
96    this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft,      KeyCode::Left);
97    this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd,       KeyCode::End);
98    this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome,      KeyCode::Home);
99
100    /* get window pointers */
101    input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" );
102    inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" );
103
104    CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" );
105    lb_history = dynamic_cast<CEGUI::Listbox*>(history); 
106
107    /* assert wee */
108    assert( lb_history );
109  }
110
111
112  /* activate, deactivate */
113  void ChatInputHandler::activate_static()
114  { ChatInputHandler::getInstance().activate( true ); }
115
116  void ChatInputHandler::activate_small_static()
117  { ChatInputHandler::getInstance().activate( false ); }
118
119
120
121
122  void ChatInputHandler::activate( bool full )
123  {
124    /* start listening */
125    //COUT(0) << "chatinput activated." << std::endl;
126    InputManager::getInstance().enterState("chatinput");
127
128    /* MARK add spawning of chat widget stuff here.*/
129    if( full )
130      GUIManager::getInstance().showGUI( "ChatBox" );
131    else
132      GUIManager::getInstance().showGUI( "ChatBox-inputonly" );
133
134    this->fullchat = full;
135  }
136
137  void ChatInputHandler::deactivate() 
138  {
139    /* stop listening */
140    InputManager::getInstance().leaveState("chatinput");
141
142    /* un-spawning of chat widget stuff */
143    GUIManager::getInstance().hideGUI( "ChatBox" );
144    GUIManager::getInstance().hideGUI( "ChatBox-inputonly" );
145  }
146
147
148  void ChatInputHandler::incomingChat(const std::string& message, 
149    unsigned int senderID)
150  {
151    /* --> a) look up the actual name of the sender */
152    std::string text;
153
154    if (senderID != CLIENTID_UNKNOWN)
155    {
156       std::string name = "unknown";
157       PlayerInfo* player = PlayerManager::getInstance().getClient(senderID);
158       if (player)
159         name = player->getName();
160
161         text = name + ": " + message;
162    }
163    else
164      text = message;
165
166    /* e) create item and add to history */
167    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( text );
168    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
169    this->lb_history->ensureItemIsVisible( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
170
171    /* f) make sure the history handles it */
172    this->lb_history->handleUpdatedItemData();
173  } 
174
175
176  void ChatInputHandler::sub_adjust_dispoffset( int maxlen, 
177    int cursorpos, 
178    int inplen )
179  {
180    /* already start offsetting 5 characters before end */
181    if( cursorpos+5 > maxlen )
182    { 
183      /* always stay 5 characters ahead of end, looks better */
184      ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0;
185
186      /* enforce visibility of cursor */
187      (disp_offset > cursorpos ) ? disp_offset = 0 : 1;
188    }
189     
190    /* make sure we don't die at substr */
191    if( inplen <= disp_offset ) disp_offset = 0;
192  }
193
194  /* callbacks for InputBuffer */
195  void ChatInputHandler::inputChanged()
196  {
197    /* update the cursor and the window */
198    std::string raw = this->inpbuf->get();
199    int cursorpos = this->inpbuf->getCursorPosition();
200   
201    /* get string before cursor */
202    std::string left = raw.substr( 0, cursorpos );
203
204    /* see if there's a string after the cursor */
205    std::string right = "";
206    if( raw.length() >= left.length()+1 )
207      right = raw.substr( cursorpos );
208     
209    /* set the text */
210    std::string assembled = "$ " + left + "|" + right;
211
212    if( this->fullchat )
213    { 
214      /* adjust curser position - magic number 5 for font width */
215      sub_adjust_dispoffset( (this->input->getUnclippedInnerRect().getWidth()/6), 
216        cursorpos, assembled.length() );
217      this->input->setProperty( "Text", assembled.substr( disp_offset ) );
218    }
219    else
220    {
221      /* adjust curser position - magic number 5 for font width */
222      sub_adjust_dispoffset( (this->inputonly->getUnclippedInnerRect().getWidth()/6), 
223        cursorpos, assembled.length() );
224      this->inputonly->setProperty( "Text", assembled.substr( disp_offset) );
225    }
226
227    /* reset display offset */
228    disp_offset = 0;
229  }
230
231  void ChatInputHandler::addline()
232  {
233    /* actually do send what was input */
234    /* a) get the string out of the inputbuffer */
235    std::string msgtosend = this->inpbuf->get();
236
237    if( msgtosend.length() == 0 )
238    { this->deactivate();
239      return;
240    }
241
242    /* b) clear the input buffer */
243    if (this->inpbuf->getSize() > 0)
244      this->inpbuf->clear();
245
246    /* c) send the chat via some call */
247    Host::Chat( msgtosend );
248
249    /* d) stop listening to input - only if this is not fullchat */
250    if( !this->fullchat )
251      this->deactivate();
252
253  }
254
255  void ChatInputHandler::backspace()
256  { this->inpbuf->removeBehindCursor(); }
257
258  void ChatInputHandler::deleteChar()
259  { this->inpbuf->removeAtCursor(); }
260
261  void ChatInputHandler::cursorRight()
262  { this->inpbuf->increaseCursor(); }
263 
264  void ChatInputHandler::cursorLeft()
265  { this->inpbuf->decreaseCursor(); }
266 
267  void ChatInputHandler::cursorEnd()
268  { this->inpbuf->setCursorToEnd(); }
269
270  void ChatInputHandler::cursorHome()
271  { this->inpbuf->setCursorToBegin(); }
272
273  void ChatInputHandler::exit()
274  {
275    /* b) clear the input buffer */
276    if (this->inpbuf->getSize() > 0)
277      this->inpbuf->clear();
278
279    /* d) stop listening to input  */
280    this->deactivate();
281  }
282
283}
Note: See TracBrowser for help on using the repository browser.