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 | * Felix Schulthess |
---|
24 | * Co-authors: |
---|
25 | * Fabian 'x3n' Landau |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | |
---|
31 | #include "InGameConsole.h" |
---|
32 | |
---|
33 | #include <string> |
---|
34 | #include <OgreOverlay.h> |
---|
35 | #include <OgreOverlayElement.h> |
---|
36 | #include <OgreOverlayManager.h> |
---|
37 | #include <OgreOverlayContainer.h> |
---|
38 | #include <OgreStringConverter.h> |
---|
39 | |
---|
40 | #include "core/Debug.h" |
---|
41 | #include "core/CoreIncludes.h" |
---|
42 | #include "core/ConfigValueIncludes.h" |
---|
43 | #include "core/ConsoleCommand.h" |
---|
44 | #include "core/InputManager.h" |
---|
45 | #include "GraphicsEngine.h" |
---|
46 | |
---|
47 | #define LINES 30 |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | ConsoleCommand(InGameConsole, openConsole, AccessLevel::None, true); |
---|
52 | ConsoleCommand(InGameConsole, closeConsole, AccessLevel::None, true); |
---|
53 | |
---|
54 | using namespace Ogre; |
---|
55 | |
---|
56 | float InGameConsole::REL_WIDTH = 0.8; |
---|
57 | float InGameConsole::REL_HEIGHT = 0.4; |
---|
58 | float InGameConsole::BLINK = 0.5; |
---|
59 | |
---|
60 | /** |
---|
61 | @brief Constructor: Creates and initializes the InGameConsole. |
---|
62 | */ |
---|
63 | InGameConsole::InGameConsole() |
---|
64 | { |
---|
65 | RegisterObject(InGameConsole); |
---|
66 | |
---|
67 | this->active_ = false; |
---|
68 | this->cursor_ = 0.0; |
---|
69 | this->cursorSymbol_ = '|'; |
---|
70 | |
---|
71 | this->init(); |
---|
72 | this->setConfigValues(); |
---|
73 | |
---|
74 | Shell::getInstance().addOutputLevel(true); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | @brief Destructor: Destroys the TextAreas. |
---|
79 | */ |
---|
80 | InGameConsole::~InGameConsole(void) |
---|
81 | { |
---|
82 | for (int i = 0; i < LINES; i++) |
---|
83 | delete this->consoleOverlayTextAreas_[i]; |
---|
84 | |
---|
85 | delete this->consoleOverlayTextAreas_; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | @brief Returns a reference to the only existing instance of InGameConsole. |
---|
90 | */ |
---|
91 | InGameConsole& InGameConsole::getInstance() |
---|
92 | { |
---|
93 | static InGameConsole instance; |
---|
94 | return instance; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | @brief Sets the config values, describing the size of the console. |
---|
99 | */ |
---|
100 | void InGameConsole::setConfigValues() |
---|
101 | { |
---|
102 | SetConfigValue(REL_WIDTH, 0.8); |
---|
103 | SetConfigValue(REL_HEIGHT, 0.4); |
---|
104 | SetConfigValue(BLINK, 0.5); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | @brief Called if all output-lines have to be redrawn. |
---|
109 | */ |
---|
110 | void InGameConsole::linesChanged() |
---|
111 | { |
---|
112 | std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator(); |
---|
113 | for (int i = 1; i < LINES; i++) |
---|
114 | { |
---|
115 | if (it != Shell::getInstance().getEndIterator()) |
---|
116 | { |
---|
117 | this->print(*it, i); |
---|
118 | ++it; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | this->print("", i); |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | @brief Called if only the last output-line has changed. |
---|
129 | */ |
---|
130 | void InGameConsole::onlyLastLineChanged() |
---|
131 | { |
---|
132 | if (LINES > 1) |
---|
133 | this->print(*Shell::getInstance().getNewestLineIterator(), 1); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | @brief Called if a new output-line was added. |
---|
138 | */ |
---|
139 | void InGameConsole::lineAdded() |
---|
140 | { |
---|
141 | for (unsigned int i = LINES - 1; i > 1; --i) |
---|
142 | { |
---|
143 | this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption()); |
---|
144 | this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop()); |
---|
145 | this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom()); |
---|
146 | } |
---|
147 | |
---|
148 | this->onlyLastLineChanged(); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | @brief Called if the text in the input-line has changed. |
---|
153 | */ |
---|
154 | void InGameConsole::inputChanged() |
---|
155 | { |
---|
156 | if (LINES > 0) |
---|
157 | this->print(Shell::getInstance().getInput(), 0); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | @brief Called if the position of the cursor in the input-line has changed. |
---|
162 | */ |
---|
163 | void InGameConsole::cursorChanged() |
---|
164 | { |
---|
165 | std::string input = Shell::getInstance().getInput(); |
---|
166 | input.insert(Shell::getInstance().getCursorPosition(), 1, this->cursorSymbol_); |
---|
167 | if (LINES > 0) |
---|
168 | this->print(input, 0); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | @brief Called if the console gets closed. |
---|
173 | */ |
---|
174 | void InGameConsole::exit() |
---|
175 | { |
---|
176 | this->deactivate(); |
---|
177 | InputManager::getSingleton().setInputMode(IM_INGAME); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | @brief Called once by constructor, initializes the InGameConsole. |
---|
182 | */ |
---|
183 | void InGameConsole::init() |
---|
184 | { |
---|
185 | // for the beginning, don't scroll |
---|
186 | this->scroll_ = 0; |
---|
187 | this->scrollTimer_ = 0; |
---|
188 | this->cursor_ = 0; |
---|
189 | |
---|
190 | // create overlay and elements |
---|
191 | this->om_ = &Ogre::OverlayManager::getSingleton(); |
---|
192 | |
---|
193 | // create a container |
---|
194 | this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "InGameConsoleContainer")); |
---|
195 | this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
196 | this->consoleOverlayContainer_->setPosition((1 - InGameConsole::REL_WIDTH) / 2, 0); |
---|
197 | this->consoleOverlayContainer_->setDimensions(InGameConsole::REL_WIDTH, InGameConsole::REL_HEIGHT); |
---|
198 | |
---|
199 | // create BorderPanel |
---|
200 | this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel")); |
---|
201 | this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
202 | this->consoleOverlayBorder_->setMaterialName("ConsoleCenter"); |
---|
203 | // set parameters for border |
---|
204 | this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16); |
---|
205 | this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder"); |
---|
206 | this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51); |
---|
207 | this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5); |
---|
208 | this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0); |
---|
209 | this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0); |
---|
210 | this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0); |
---|
211 | |
---|
212 | // create the text lines |
---|
213 | this->consoleOverlayTextAreas_ = new TextAreaOverlayElement*[LINES]; |
---|
214 | for (int i = 0; i < LINES; i++) |
---|
215 | { |
---|
216 | this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i))); |
---|
217 | this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS); |
---|
218 | this->consoleOverlayTextAreas_[i]->setFontName("Console"); |
---|
219 | this->consoleOverlayTextAreas_[i]->setCharHeight(18); |
---|
220 | this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
221 | this->consoleOverlayTextAreas_[i]->setLeft(8); |
---|
222 | this->consoleOverlayTextAreas_[i]->setCaption(""); |
---|
223 | } |
---|
224 | |
---|
225 | // create noise |
---|
226 | this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleNoise")); |
---|
227 | this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
228 | this->consoleOverlayNoise_->setPosition(5,0); |
---|
229 | this->consoleOverlayNoise_->setMaterialName("ConsoleNoise"); |
---|
230 | |
---|
231 | this->consoleOverlay_ = this->om_->create("InGameConsoleConsole"); |
---|
232 | this->consoleOverlay_->add2D(this->consoleOverlayContainer_); |
---|
233 | this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_); |
---|
234 | //comment following line to disable noise |
---|
235 | this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_); |
---|
236 | for (int i = 0; i < LINES; i++) |
---|
237 | this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]); |
---|
238 | |
---|
239 | this->resize(); |
---|
240 | |
---|
241 | // move overlay "above" the top edge of the screen |
---|
242 | // we take -1.2 because the border mkes the panel bigger |
---|
243 | this->consoleOverlayContainer_->setTop(-1.2 * InGameConsole::REL_HEIGHT); |
---|
244 | // show overlay |
---|
245 | this->consoleOverlay_->show(); |
---|
246 | |
---|
247 | COUT(4) << "Info: InGameConsole initialized" << std::endl; |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | @brief Used to control the actual scrolling and the cursor. |
---|
252 | */ |
---|
253 | void InGameConsole::tick(float dt) |
---|
254 | { |
---|
255 | this->scrollTimer_ += dt; |
---|
256 | if (this->scrollTimer_ >= 0.01) |
---|
257 | { |
---|
258 | float top = this->consoleOverlayContainer_->getTop(); |
---|
259 | this->scrollTimer_ = 0; |
---|
260 | if (this->scroll_ != 0) |
---|
261 | { |
---|
262 | // scroll |
---|
263 | top = top + 0.02 * this->scroll_; |
---|
264 | this->consoleOverlayContainer_->setTop(top); |
---|
265 | } |
---|
266 | if (top <= -1.2 * InGameConsole::REL_HEIGHT) |
---|
267 | { |
---|
268 | // window has completely scrolled up |
---|
269 | this->scroll_ = 0; |
---|
270 | this->consoleOverlay_->hide(); |
---|
271 | this->active_ = false; |
---|
272 | Shell::getInstance().unregisterListener(this); |
---|
273 | } |
---|
274 | if (top >= 0) |
---|
275 | { |
---|
276 | // window has completely scrolled down |
---|
277 | this->scroll_ = 0; |
---|
278 | this->consoleOverlayContainer_->setTop(0); |
---|
279 | this->active_ = true; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | this->cursor_ += dt; |
---|
284 | if (this->cursor_ >= 2 * InGameConsole::BLINK) |
---|
285 | this->cursor_ = 0; |
---|
286 | |
---|
287 | if (this->cursor_ >= InGameConsole::BLINK && this->cursorSymbol_ == '|') |
---|
288 | { |
---|
289 | this->cursorSymbol_ = ' '; |
---|
290 | this->cursorChanged(); |
---|
291 | } |
---|
292 | else if (this->cursor_ < InGameConsole::BLINK && this->cursorSymbol_ == ' ') |
---|
293 | { |
---|
294 | this->cursorSymbol_ = '|'; |
---|
295 | this->cursorChanged(); |
---|
296 | } |
---|
297 | |
---|
298 | // this creates a flickering effect |
---|
299 | this->consoleOverlayNoise_->setTiling(1, rand() % 5 + 1); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | @brief Resizes the console elements. Call if window size changes. |
---|
304 | */ |
---|
305 | void InGameConsole::resize() |
---|
306 | { |
---|
307 | this->windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
308 | this->windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
309 | this->consoleOverlayBorder_->setWidth((int) this->windowW_* InGameConsole::REL_WIDTH); |
---|
310 | this->consoleOverlayBorder_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT); |
---|
311 | this->consoleOverlayNoise_->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH - 10); |
---|
312 | this->consoleOverlayNoise_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT - 5); |
---|
313 | // now adjust the text lines... |
---|
314 | for (int i = 0; i < LINES; i++) |
---|
315 | { |
---|
316 | this->consoleOverlayTextAreas_[i]->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH); |
---|
317 | this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * InGameConsole::REL_HEIGHT - 24 - 14*i); |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | @brief Shows the InGameConsole. |
---|
323 | */ |
---|
324 | void InGameConsole::activate() |
---|
325 | { |
---|
326 | Shell::getInstance().registerListener(this); |
---|
327 | this->linesChanged(); |
---|
328 | |
---|
329 | this->consoleOverlay_->show(); |
---|
330 | // just in case window size has changed... |
---|
331 | this->resize(); |
---|
332 | // scroll down |
---|
333 | this->scroll_ = 1; |
---|
334 | // the rest is done by tick |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | @brief Hides the InGameConsole. |
---|
339 | */ |
---|
340 | void InGameConsole::deactivate() |
---|
341 | { |
---|
342 | // scroll up |
---|
343 | this->scroll_ = -1; |
---|
344 | // the rest is done by tick |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | @brief Activates the console. |
---|
349 | */ |
---|
350 | void InGameConsole::openConsole() |
---|
351 | { |
---|
352 | InGameConsole::getInstance().activate(); |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | @brief Deactivates the console. |
---|
357 | */ |
---|
358 | void InGameConsole::closeConsole() |
---|
359 | { |
---|
360 | InGameConsole::getInstance().deactivate(); |
---|
361 | } |
---|
362 | |
---|
363 | /** |
---|
364 | @brief Prints string to bottom line. |
---|
365 | @param s String to be printed |
---|
366 | */ |
---|
367 | void InGameConsole::print(const std::string& text, int index) |
---|
368 | { |
---|
369 | char level = 0; |
---|
370 | if (text.size() > 0) |
---|
371 | level = text[0]; |
---|
372 | |
---|
373 | std::string output = text; |
---|
374 | |
---|
375 | if (level >= -1 && level <= 5) |
---|
376 | output.erase(0, 1); |
---|
377 | |
---|
378 | if (LINES > index) |
---|
379 | { |
---|
380 | if (level == -1) |
---|
381 | { |
---|
382 | this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.90, 0.90, 0.90, 1.00)); |
---|
383 | this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00)); |
---|
384 | } |
---|
385 | else if (level == 1) |
---|
386 | { |
---|
387 | this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.95, 0.25, 0.25, 1.00)); |
---|
388 | this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00)); |
---|
389 | } |
---|
390 | else if (level == 2) |
---|
391 | { |
---|
392 | this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.95, 0.50, 0.20, 1.00)); |
---|
393 | this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00)); |
---|
394 | } |
---|
395 | else if (level == 3) |
---|
396 | { |
---|
397 | this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.50, 0.50, 0.95, 1.00)); |
---|
398 | this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00)); |
---|
399 | } |
---|
400 | else if (level == 4) |
---|
401 | { |
---|
402 | this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.65, 0.48, 0.44, 1.00)); |
---|
403 | this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00)); |
---|
404 | } |
---|
405 | else if (level == 5) |
---|
406 | { |
---|
407 | this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.40, 0.20, 0.40, 1.00)); |
---|
408 | this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00)); |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | this->consoleOverlayTextAreas_[index]->setColourTop (ColourValue(0.21, 0.69, 0.21, 1.00)); |
---|
413 | this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00)); |
---|
414 | } |
---|
415 | |
---|
416 | this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output)); |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | @brief Converts a string into an Ogre::UTFString. |
---|
422 | @param s The string to convert |
---|
423 | @return The converted string |
---|
424 | */ |
---|
425 | Ogre::UTFString InGameConsole::convert2UTF(std::string s) |
---|
426 | { |
---|
427 | Ogre::UTFString utf; |
---|
428 | Ogre::UTFString::code_point cp; |
---|
429 | for (unsigned int i = 0; i < s.size(); ++i) |
---|
430 | { |
---|
431 | cp = s[i]; |
---|
432 | cp &= 0xFF; |
---|
433 | utf.append(1, cp); |
---|
434 | } |
---|
435 | return utf; |
---|
436 | } |
---|
437 | } |
---|