Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6969 was 6916, checked in by smerkli, 15 years ago

added some comments, made reasonable-length lines - ready to merge.

  • Property svn:eol-style set to native
File size: 9.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( 
72      "chatinput", false, false, InputStatePriority::Dynamic );
73    this->inputState->setKeyHandler(this->inpbuf);
74  }
75
76  void ChatInputHandler::configureInputBuffer()
77  {
78    /* INSTALL CALLBACKS */
79    /* input has changed */
80    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
81
82    /* add a line */
83    this->inpbuf->registerListener(this, &ChatInputHandler::addline,
84      '\r',   false);
85    this->inpbuf->registerListener(this, &ChatInputHandler::addline, 
86      '\n',   false);
87
88    /* backspace */
89    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       
90      '\b',   true);
91
92    /* NOTE this doesn't work on my debian linux box, it makes backspace get
93     * registered also when delete is pressed
94     */
95    //this->inpbuf->registerListener(this, &ChatInputHandler::backspace,
96      //'\177', true);
97
98    /* exit the chatinputhandler thingy (tbd) */
99    this->inpbuf->registerListener(this, &ChatInputHandler::exit, 
100      '\033', true); // escape
101
102    /* delete character */
103    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar, 
104      KeyCode::Delete);
105
106    /* cursor movement */
107    this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight,
108      KeyCode::Right);
109    this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft, 
110      KeyCode::Left);
111    this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd,     
112      KeyCode::End);
113    this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome,     
114      KeyCode::Home);
115
116    /* GET WINDOW POINTERS */
117    input = CEGUI::WindowManager::getSingleton().getWindow( 
118      "orxonox/ChatBox/input" );
119    inputonly = CEGUI::WindowManager::getSingleton().getWindow( 
120      "orxonox/ChatBox-inputonly/input" );
121
122    /* get pointer to the history window */
123    CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( 
124      "orxonox/ChatBox/history" );
125
126    /* cast it to a listbox */
127    lb_history = dynamic_cast<CEGUI::Listbox*>(history); 
128
129    /* assert wee */
130    assert( lb_history );
131  }
132
133
134  /* activate, deactivate */
135  void ChatInputHandler::activate_static()
136  { ChatInputHandler::getInstance().activate( true ); }
137
138  void ChatInputHandler::activate_small_static()
139  { ChatInputHandler::getInstance().activate( false ); }
140
141
142
143
144  void ChatInputHandler::activate( bool full )
145  {
146    /* start listening */
147    //COUT(0) << "chatinput activated." << std::endl;
148    InputManager::getInstance().enterState("chatinput");
149
150    /* MARK add spawning of chat widget stuff here.*/
151    if( full )
152      GUIManager::getInstance().showGUI( "ChatBox" );
153    else
154      GUIManager::getInstance().showGUI( "ChatBox-inputonly" );
155
156    this->fullchat = full;
157  }
158
159  void ChatInputHandler::deactivate() 
160  {
161    /* stop listening */
162    InputManager::getInstance().leaveState("chatinput");
163
164    /* un-spawning of chat widget stuff */
165    GUIManager::getInstance().hideGUI( "ChatBox" );
166    GUIManager::getInstance().hideGUI( "ChatBox-inputonly" );
167  }
168
169
170  void ChatInputHandler::incomingChat(const std::string& message, 
171    unsigned int senderID)
172  {
173    /* --> a) look up the actual name of the sender */
174    std::string text;
175
176    if (senderID != CLIENTID_UNKNOWN)
177    {
178       std::string name = "unknown";
179       PlayerInfo* player = PlayerManager::getInstance().getClient(senderID);
180       if (player)
181         name = player->getName();
182
183         text = name + ": " + message;
184    }
185    else
186      text = message;
187
188    /* e) create item and add to history */
189    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( text );
190    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
191    this->lb_history->ensureItemIsVisible( 
192      dynamic_cast<CEGUI::ListboxItem*>(toadd) );
193
194    /* f) make sure the history handles it */
195    this->lb_history->handleUpdatedItemData();
196  } 
197
198
199  void ChatInputHandler::sub_adjust_dispoffset( int maxlen, 
200    int cursorpos, 
201    int inplen )
202  {
203    /* already start offsetting 5 characters before end */
204    if( cursorpos+5 > maxlen )
205    { 
206      /* always stay 5 characters ahead of end, looks better */
207      ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0;
208
209      /* enforce visibility of cursor */
210      (disp_offset > cursorpos ) ? disp_offset = 0 : 1;
211    }
212     
213    /* make sure we don't die at substr */
214    if( inplen <= disp_offset ) disp_offset = 0;
215  }
216
217  /* callbacks for InputBuffer */
218  void ChatInputHandler::inputChanged()
219  {
220    /* update the cursor and the window */
221    std::string raw = this->inpbuf->get();
222    int cursorpos = this->inpbuf->getCursorPosition();
223   
224    /* get string before cursor */
225    std::string left = raw.substr( 0, cursorpos );
226
227    /* see if there's a string after the cursor */
228    std::string right = "";
229    if( raw.length() >= left.length()+1 )
230      right = raw.substr( cursorpos );
231     
232    /* set the text */
233    std::string assembled = "$ " + left + "|" + right;
234
235    /* decide what to do based on the active view */
236    if( this->fullchat )
237    { 
238      /* adjust curser position - magic number 5 for font width */
239      sub_adjust_dispoffset( 
240        (this->input->getUnclippedInnerRect().getWidth()/6), cursorpos, 
241        assembled.length() );
242      this->input->setProperty( "Text", assembled.substr( disp_offset ) );
243    }
244    else
245    {
246      /* adjust curser position - magic number 5 for font width */
247      sub_adjust_dispoffset( 
248        (this->inputonly->getUnclippedInnerRect().getWidth()/6), cursorpos, 
249        assembled.length() );
250      this->inputonly->setProperty( "Text", assembled.substr( disp_offset) );
251    }
252
253    /* reset display offset */
254    disp_offset = 0;
255  }
256
257  void ChatInputHandler::addline()
258  {
259    /* actually do send what was input */
260    /* a) get the string out of the inputbuffer */
261    std::string msgtosend = this->inpbuf->get();
262
263    /* if someone pressed return and nothing was put in,
264     * just close the window
265     */
266    if( msgtosend.length() == 0 )
267    { this->deactivate();
268      return;
269    }
270
271    /* b) clear the input buffer */
272    if (this->inpbuf->getSize() > 0)
273      this->inpbuf->clear();
274
275    /* c) send the chat via some call */
276    Host::Chat( msgtosend );
277
278    /* d) stop listening to input - only if this is not fullchat */
279    if( !this->fullchat )
280      this->deactivate();
281
282  }
283
284  void ChatInputHandler::backspace()
285  { this->inpbuf->removeBehindCursor(); }
286
287  void ChatInputHandler::deleteChar()
288  { this->inpbuf->removeAtCursor(); }
289
290  void ChatInputHandler::cursorRight()
291  { this->inpbuf->increaseCursor(); }
292 
293  void ChatInputHandler::cursorLeft()
294  { this->inpbuf->decreaseCursor(); }
295 
296  void ChatInputHandler::cursorEnd()
297  { this->inpbuf->setCursorToEnd(); }
298
299  void ChatInputHandler::cursorHome()
300  { this->inpbuf->setCursorToBegin(); }
301
302  void ChatInputHandler::exit()
303  {
304    /* b) clear the input buffer */
305    if (this->inpbuf->getSize() > 0)
306      this->inpbuf->clear();
307
308    /* d) stop listening to input  */
309    this->deactivate();
310  }
311
312}
Note: See TracBrowser for help on using the repository browser.