Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Scrolling chat now works, discovered delete-button bug, fixing it now.

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