Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Chatbox done, with cursor goodness and everything. Thanks for the help

  • Property svn:eol-style set to native
File size: 6.4 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  SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat",
47    true );
48
49
50  /* constructor */
51  ChatInputHandler::ChatInputHandler()
52  {
53    /* register the object  */
54    RegisterObject(ChatInputHandler);
55
56    /* create necessary objects */
57    this->inpbuf = new InputBuffer();
58    assert( this->inpbuf != NULL );
59
60    /* MARK add generation of ChatBox thingy here */
61    GUIManager::getInstance().loadGUI( "ChatBox" );
62
63    /* configure the input buffer */
64    configureInputBuffer();
65
66    this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic );
67    this->inputState->setKeyHandler(this->inpbuf);
68
69    //[> set console shortcut <]
70    //this->getIdentifier()->addConsoleCommand(createConsoleCommand(createFunctor(
71      //&ChatInputHandler::activate, this), "startchat"), false);
72  }
73
74  void ChatInputHandler::configureInputBuffer()
75  {
76    /* input has changed */
77    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
78
79    /* add a line */
80    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false);
81    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false);
82
83    /* backspace */
84    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true);
85    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true);
86
87    /* exit the chatinputhandler thingy (tbd) */
88    this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape
89
90    /* delete character */
91    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete);
92
93    /* cursor movement */
94    this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight,     KeyCode::Right);
95    this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft,      KeyCode::Left);
96    this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd,       KeyCode::End);
97    this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome,      KeyCode::Home);
98
99    /* get window pointers */
100    input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" );
101    CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" );
102    lb_history = dynamic_cast<CEGUI::Listbox*>(history); 
103
104    /* assert wee */
105    assert( lb_history );
106  }
107
108
109  /* activate, deactivate */
110  void ChatInputHandler::activate_static()
111  { ChatInputHandler::getInstance().activate(); }
112 
113  void ChatInputHandler::activate()
114  {
115    /* start listening */
116    //COUT(0) << "chatinput activated." << std::endl;
117    InputManager::getInstance().enterState("chatinput");
118
119    /* MARK add spawning of chat widget stuff here.*/
120    GUIManager::getInstance().showGUI( "ChatBox" );
121  }
122
123  void ChatInputHandler::deactivate() 
124  {
125    /* stop listening */
126    InputManager::getInstance().leaveState("chatinput");
127
128    /* MARK add un-spawning of chat widget stuff here. */
129    GUIManager::getInstance().hideGUI( "ChatBox" );
130  }
131
132  /* callbacks for InputBuffer */
133  void ChatInputHandler::inputChanged()
134  {
135    /* update the cursor and the window */
136    std::string raw = this->inpbuf->get();
137   
138    /* get string before cursor */
139    std::string left = raw.substr( 0, this->inpbuf->getCursorPosition() );
140
141    /* see if there's a string after the cursor */
142    std::string right = "";
143    if( raw.length() >= left.length()+1 )
144      right = raw.substr( this->inpbuf->getCursorPosition() );
145     
146    /* set the text */
147    this->input->setProperty( "Text", left + "|" + right );
148  }
149
150  void ChatInputHandler::addline()
151  {
152    /* actually do send what was input */
153    /* a) get the string out of the inputbuffer */
154    std::string msgtosend = this->inpbuf->get();
155
156    if( msgtosend.length() == 0 )
157    { this->deactivate();
158      return;
159    }
160
161    /* b) clear the input buffer */
162    if (this->inpbuf->getSize() > 0)
163      this->inpbuf->clear();
164
165    /* c) send the chat via some call */
166    Host::Chat( msgtosend );
167
168    /* d) stop listening to input  */
169    this->deactivate();
170
171    /* e) create item and add to history */
172    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( msgtosend );
173    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
174    this->lb_history->ensureItemIsVisible( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
175
176    /* f) make sure the history handles it */
177    this->lb_history->handleUpdatedItemData();
178  }
179
180  void ChatInputHandler::backspace()
181  { this->inpbuf->removeBehindCursor(); }
182
183  void ChatInputHandler::deleteChar()
184  { this->inpbuf->removeAtCursor(); }
185
186  void ChatInputHandler::cursorRight()
187  { this->inpbuf->increaseCursor(); }
188 
189  void ChatInputHandler::cursorLeft()
190  { this->inpbuf->decreaseCursor(); }
191 
192  void ChatInputHandler::cursorEnd()
193  { this->inpbuf->setCursorToEnd(); }
194
195  void ChatInputHandler::cursorHome()
196  { this->inpbuf->setCursorToBegin(); }
197
198  void ChatInputHandler::exit()
199  { }
200
201}
Note: See TracBrowser for help on using the repository browser.