Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/chat/src/orxonox/ChatInputHandler.cc @ 6797

Last change on this file since 6797 was 6797, checked in by rgrieder, 15 years ago

svn:eol-style "native" for all text-based files.

  • Property svn:eol-style set to native
File size: 5.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 <string>
34
35namespace orxonox
36{
37  /* singleton */
38  ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false );
39  SetConsoleCommandAlias( ChatInputHandler, activate_static, "startchat",
40    true );
41
42  /* constructor */
43  ChatInputHandler::ChatInputHandler()
44  {
45    /* register the object  */
46    RegisterObject(ChatInputHandler);
47
48    /* create necessary objects */
49    this->inpbuf = new InputBuffer();
50
51    /* configure the input buffer */
52    configureInputBuffer();
53
54    this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic );
55    this->inputState->setKeyHandler(this->inpbuf);
56
57    //[> set console shortcut <]
58    //this->getIdentifier()->addConsoleCommand(createConsoleCommand(createFunctor(
59      //&ChatInputHandler::activate, this), "startchat"), false);
60  }
61
62  void ChatInputHandler::configureInputBuffer()
63  {
64          /* input has changed */
65    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
66               
67                /* add a line */
68    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false);
69    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false);
70
71                /* backspace */
72    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true);
73    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true);
74
75                /* exit the chatinputhandler thingy (tbd) */
76    this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape
77
78                /* delete character */
79    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete);
80
81                /* cursor movement */
82    this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight,     KeyCode::Right);
83    this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft,      KeyCode::Left);
84    this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd,       KeyCode::End);
85    this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome,      KeyCode::Home);
86  }
87
88
89  /* activate, deactivate */
90  void ChatInputHandler::activate_static()
91  { ChatInputHandler::getInstance().activate(); }
92 
93  void ChatInputHandler::activate()
94  {
95    /* start listening */
96    COUT(0) << "chatinput activated." << std::endl;
97    InputManager::getInstance().enterState("chatinput");
98  }
99
100  void ChatInputHandler::deactivate() 
101  {
102    /* stop listening */
103    InputManager::getInstance().leaveState("chatinput");
104  }
105
106
107
108  /* callbacks for InputBuffer */
109  void ChatInputHandler::inputChanged()
110  {
111    //this->updateListeners<&ShellListener::inputChanged>();
112    //this->updateListeners<&ShellListener::cursorChanged>();
113  }
114
115  void ChatInputHandler::addline()
116  {
117    /* MARK MARK */
118
119    /* actually do send what was input */
120    /* a) get the string out of the inputbuffer */
121    std::string msgtosend = this->inpbuf->get();
122
123    /* b) clear the input buffer */
124    if (this->inpbuf->getSize() > 0)
125      this->inpbuf->clear();
126
127    /* c) send the chat via some call */
128    Host::Chat( msgtosend );
129
130    /* d) stop listening to input  */
131    this->deactivate();
132  }
133
134  void ChatInputHandler::backspace()
135  {
136    this->inpbuf->removeBehindCursor();
137    //this->updateListeners<&ShellListener::inputChanged>();
138    //this->updateListeners<&ShellListener::cursorChanged>();
139  }
140
141  void ChatInputHandler::deleteChar()
142  {
143    this->inpbuf->removeAtCursor();
144    //this->updateListeners<&ShellListener::inputChanged>();
145  }
146
147  void ChatInputHandler::cursorRight()
148  {
149    this->inpbuf->increaseCursor();
150    //this->updateListeners<&ShellListener::cursorChanged>();
151  }
152 
153  void ChatInputHandler::cursorLeft()
154  {
155    this->inpbuf->decreaseCursor();
156    //this->updateListeners<&ShellListener::cursorChanged>();
157  }
158 
159  void ChatInputHandler::cursorEnd()
160  {
161    this->inpbuf->setCursorToEnd();
162    //this->updateListeners<&ShellListener::cursorChanged>();
163  }
164
165  void ChatInputHandler::cursorHome()
166  {
167    this->inpbuf->setCursorToBegin();
168    //this->updateListeners<&ShellListener::cursorChanged>();
169  }
170
171  void ChatInputHandler::exit()
172  {
173    //if (this->inpbuf->getSize() > 0)
174    //{
175      //this->clearInput();
176      //return;
177    //}
178
179    //this->clearInput();
180    //this->scrollPosition_ = 0;
181    //this->scrollIterator_ = this->outputLines_.begin();
182
183    //this->updateListeners<&ShellListener::exit>();
184  }
185
186}
Note: See TracBrowser for help on using the repository browser.