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 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | /* singleton */ |
---|
38 | ManageScopedSingleton( ChatInputHandler, ScopeID::Graphics, false ); |
---|
39 | |
---|
40 | /* constructor */ |
---|
41 | ChatInputHandler::ChatInputHandler() |
---|
42 | { |
---|
43 | /* register the object */ |
---|
44 | RegisterObject(ChatInputHandler); |
---|
45 | |
---|
46 | //COUT(0) << "Singleton registered for ChatInputHandler." << std::endl; |
---|
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() |
---|
91 | { |
---|
92 | /* start listening */ |
---|
93 | COUT(0) << "chatinput activated." << std::endl; |
---|
94 | InputManager::getInstance().enterState("chatinput"); |
---|
95 | } |
---|
96 | |
---|
97 | void ChatInputHandler::deactivate() |
---|
98 | { |
---|
99 | /* stop listening */ |
---|
100 | InputManager::getInstance().leaveState("chatinput"); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /* callbacks for InputBuffer */ |
---|
106 | void ChatInputHandler::inputChanged() |
---|
107 | { |
---|
108 | //this->updateListeners<&ShellListener::inputChanged>(); |
---|
109 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
110 | } |
---|
111 | |
---|
112 | void ChatInputHandler::addline() |
---|
113 | { |
---|
114 | /* MARK MARK */ |
---|
115 | |
---|
116 | /* actually do send what was input */ |
---|
117 | /* a) get the string out of the inputbuffer */ |
---|
118 | std::string msgtosend = this->inpbuf->get(); |
---|
119 | |
---|
120 | /* b) clear the input buffer */ |
---|
121 | if (this->inpbuf->getSize() > 0) |
---|
122 | this->inpbuf->clear(); |
---|
123 | |
---|
124 | /* c) send the chat via some call */ |
---|
125 | Host::Chat( msgtosend ); |
---|
126 | |
---|
127 | /* d) stop listening to input */ |
---|
128 | this->deactivate(); |
---|
129 | } |
---|
130 | |
---|
131 | void ChatInputHandler::backspace() |
---|
132 | { |
---|
133 | this->inpbuf->removeBehindCursor(); |
---|
134 | //this->updateListeners<&ShellListener::inputChanged>(); |
---|
135 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
136 | } |
---|
137 | |
---|
138 | void ChatInputHandler::deleteChar() |
---|
139 | { |
---|
140 | this->inpbuf->removeAtCursor(); |
---|
141 | //this->updateListeners<&ShellListener::inputChanged>(); |
---|
142 | } |
---|
143 | |
---|
144 | void ChatInputHandler::cursorRight() |
---|
145 | { |
---|
146 | this->inpbuf->increaseCursor(); |
---|
147 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
148 | } |
---|
149 | |
---|
150 | void ChatInputHandler::cursorLeft() |
---|
151 | { |
---|
152 | this->inpbuf->decreaseCursor(); |
---|
153 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
154 | } |
---|
155 | |
---|
156 | void ChatInputHandler::cursorEnd() |
---|
157 | { |
---|
158 | this->inpbuf->setCursorToEnd(); |
---|
159 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
160 | } |
---|
161 | |
---|
162 | void ChatInputHandler::cursorHome() |
---|
163 | { |
---|
164 | this->inpbuf->setCursorToBegin(); |
---|
165 | //this->updateListeners<&ShellListener::cursorChanged>(); |
---|
166 | } |
---|
167 | |
---|
168 | void ChatInputHandler::exit() |
---|
169 | { |
---|
170 | //if (this->inpbuf->getSize() > 0) |
---|
171 | //{ |
---|
172 | //this->clearInput(); |
---|
173 | //return; |
---|
174 | //} |
---|
175 | |
---|
176 | //this->clearInput(); |
---|
177 | //this->scrollPosition_ = 0; |
---|
178 | //this->scrollIterator_ = this->outputLines_.begin(); |
---|
179 | |
---|
180 | //this->updateListeners<&ShellListener::exit>(); |
---|
181 | } |
---|
182 | |
---|
183 | } |
---|