Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6879 was 6879, checked in by smerkli, 14 years ago

bugfixes, performance improvements.

  • Property svn:eol-style set to native
File size: 8.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#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  void ChatInputHandler::activate( bool full )
120  {
121    /* start listening */
122    //COUT(0) << "chatinput activated." << std::endl;
123    InputManager::getInstance().enterState("chatinput");
124
125    /* MARK add spawning of chat widget stuff here.*/
126    if( full )
127      GUIManager::getInstance().showGUI( "ChatBox" );
128    else
129      GUIManager::getInstance().showGUI( "ChatBox-inputonly" );
130
131    this->fullchat = full;
132  }
133
134  void ChatInputHandler::deactivate() 
135  {
136    /* stop listening */
137    InputManager::getInstance().leaveState("chatinput");
138
139    /* un-spawning of chat widget stuff */
140    GUIManager::getInstance().hideGUI( "ChatBox" );
141    GUIManager::getInstance().hideGUI( "ChatBox-inputonly" );
142  }
143
144  void ChatInputHandler::sub_adjust_dispoffset( int maxlen, int cursorpos, int inplen )
145  {
146    /* already start offsetting 5 characters before end */
147    if( cursorpos+5 > maxlen )
148    { 
149      /* always stay 5 characters ahead of end, looks better */
150      ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0;
151
152      /* enforce visibility of cursor */
153      (disp_offset > cursorpos ) ? disp_offset = 0 : 1;
154    }
155     
156    /* make sure we don't die at substr */
157    if( inplen <= disp_offset ) disp_offset = 0;
158  }
159
160  /* callbacks for InputBuffer */
161  void ChatInputHandler::inputChanged()
162  {
163    /* update the cursor and the window */
164    std::string raw = this->inpbuf->get();
165    int cursorpos = this->inpbuf->getCursorPosition();
166   
167    /* get string before cursor */
168    std::string left = raw.substr( 0, cursorpos );
169
170    /* see if there's a string after the cursor */
171    std::string right = "";
172    if( raw.length() >= left.length()+1 )
173      right = raw.substr( cursorpos );
174     
175    /* set the text */
176    std::string assembled = "$ " + left + "|" + right;
177
178    if( this->fullchat )
179    { 
180      /* adjust curser position - magic number 5 for font width */
181      sub_adjust_dispoffset( (this->input->getUnclippedInnerRect().getWidth()/6), 
182        cursorpos, assembled.length() );
183      this->input->setProperty( "Text", assembled.substr( disp_offset ) );
184    }
185    else
186    {
187      /* adjust curser position - magic number 5 for font width */
188      sub_adjust_dispoffset( (this->inputonly->getUnclippedInnerRect().getWidth()/6), 
189        cursorpos, assembled.length() );
190      this->inputonly->setProperty( "Text", assembled.substr( disp_offset) );
191    }
192
193    /* reset display offset */
194    disp_offset = 0;
195  }
196
197  void ChatInputHandler::addline()
198  {
199    /* actually do send what was input */
200    /* a) get the string out of the inputbuffer */
201    std::string msgtosend = this->inpbuf->get();
202
203    if( msgtosend.length() == 0 )
204    { this->deactivate();
205      return;
206    }
207
208    /* b) clear the input buffer */
209    if (this->inpbuf->getSize() > 0)
210      this->inpbuf->clear();
211
212    /* c) send the chat via some call */
213    Host::Chat( msgtosend );
214
215    /* d) stop listening to input  */
216    if( !this->fullchat )
217      this->deactivate();
218
219    /* e) create item and add to history */
220    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( msgtosend );
221    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
222    this->lb_history->ensureItemIsVisible( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
223
224    /* f) make sure the history handles it */
225    this->lb_history->handleUpdatedItemData();
226  }
227
228  void ChatInputHandler::backspace()
229  { this->inpbuf->removeBehindCursor(); }
230
231  void ChatInputHandler::deleteChar()
232  { this->inpbuf->removeAtCursor(); }
233
234  void ChatInputHandler::cursorRight()
235  { this->inpbuf->increaseCursor(); }
236 
237  void ChatInputHandler::cursorLeft()
238  { this->inpbuf->decreaseCursor(); }
239 
240  void ChatInputHandler::cursorEnd()
241  { this->inpbuf->setCursorToEnd(); }
242
243  void ChatInputHandler::cursorHome()
244  { this->inpbuf->setCursorToBegin(); }
245
246  void ChatInputHandler::exit()
247  {
248    /* b) clear the input buffer */
249    if (this->inpbuf->getSize() > 0)
250      this->inpbuf->clear();
251
252    /* d) stop listening to input  */
253    this->deactivate();
254  }
255
256}
Note: See TracBrowser for help on using the repository browser.