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 | |
---|
30 | #include "InGameConsole.h" |
---|
31 | |
---|
32 | #include <algorithm> |
---|
33 | #include <string> |
---|
34 | #include <OgreOverlay.h> |
---|
35 | #include <OgreOverlayElement.h> |
---|
36 | #include <OgreOverlayManager.h> |
---|
37 | #include <OgreOverlayContainer.h> |
---|
38 | #include <OgreBorderPanelOverlayElement.h> |
---|
39 | #include <OgreTextAreaOverlayElement.h> |
---|
40 | #include <OgreFontManager.h> |
---|
41 | #include <OgreFont.h> |
---|
42 | |
---|
43 | #include "util/Clock.h" |
---|
44 | #include "util/Convert.h" |
---|
45 | #include "util/Math.h" |
---|
46 | #include "util/DisplayStringConversions.h" |
---|
47 | #include "core/CoreIncludes.h" |
---|
48 | #include "core/ConfigValueIncludes.h" |
---|
49 | #include "core/ConsoleCommand.h" |
---|
50 | #include "core/ScopedSingletonManager.h" |
---|
51 | #include "core/input/InputManager.h" |
---|
52 | #include "core/input/InputState.h" |
---|
53 | #include "core/input/InputBuffer.h" |
---|
54 | |
---|
55 | namespace orxonox |
---|
56 | { |
---|
57 | const int LINES = 30; |
---|
58 | const float CHAR_WIDTH = 7.45f; // fix this please - determine the char-width dynamically |
---|
59 | |
---|
60 | SetConsoleCommand(InGameConsole, openConsole, true); |
---|
61 | SetConsoleCommand(InGameConsole, closeConsole, true); |
---|
62 | |
---|
63 | ManageScopedSingleton(InGameConsole, ScopeID::Graphics, false); |
---|
64 | |
---|
65 | /** |
---|
66 | @brief Constructor: Creates and initializes the InGameConsole. |
---|
67 | */ |
---|
68 | InGameConsole::InGameConsole() |
---|
69 | : shell_(new Shell("InGameConsole", true)) |
---|
70 | , consoleOverlay_(0) |
---|
71 | , consoleOverlayContainer_(0) |
---|
72 | , consoleOverlayNoise_(0) |
---|
73 | , consoleOverlayCursor_(0) |
---|
74 | , consoleOverlayBorder_(0) |
---|
75 | , consoleOverlayTextAreas_(0) |
---|
76 | , inputState_(0) |
---|
77 | { |
---|
78 | RegisterObject(InGameConsole); |
---|
79 | |
---|
80 | this->bActive_ = false; |
---|
81 | this->cursor_ = 0.0f; |
---|
82 | this->cursorSymbol_ = '|'; |
---|
83 | this->inputWindowStart_ = 0; |
---|
84 | this->numLinesShifted_ = LINES - 1; |
---|
85 | // for the beginning, don't scroll |
---|
86 | this->scroll_ = 0; |
---|
87 | |
---|
88 | this->setConfigValues(); |
---|
89 | this->initialise(); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | @brief Destructor: Destroys the TextAreas. |
---|
94 | */ |
---|
95 | InGameConsole::~InGameConsole() |
---|
96 | { |
---|
97 | this->deactivate(); |
---|
98 | |
---|
99 | // destroy the input state previously created (InputBuffer gets destroyed by the Shell) |
---|
100 | InputManager::getInstance().destroyState("console"); |
---|
101 | |
---|
102 | // destroy the underlaying shell |
---|
103 | this->shell_->destroy(); |
---|
104 | |
---|
105 | Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr(); |
---|
106 | if (ovMan) |
---|
107 | { |
---|
108 | if (this->consoleOverlayNoise_) |
---|
109 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayNoise_); |
---|
110 | if (this->consoleOverlayCursor_) |
---|
111 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayCursor_); |
---|
112 | Ogre::FontManager::getSingleton().remove("MonofurConsole"); |
---|
113 | if (this->consoleOverlayBorder_) |
---|
114 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayBorder_); |
---|
115 | if (this->consoleOverlayTextAreas_) |
---|
116 | { |
---|
117 | for (int i = 0; i < LINES; i++) |
---|
118 | { |
---|
119 | if (this->consoleOverlayTextAreas_[i]) |
---|
120 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayTextAreas_[i]); |
---|
121 | this->consoleOverlayTextAreas_[i] = 0; |
---|
122 | } |
---|
123 | |
---|
124 | } |
---|
125 | if (this->consoleOverlayContainer_) |
---|
126 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayContainer_); |
---|
127 | } |
---|
128 | if (this->consoleOverlayTextAreas_) |
---|
129 | { |
---|
130 | delete[] this->consoleOverlayTextAreas_; |
---|
131 | this->consoleOverlayTextAreas_ = 0; |
---|
132 | } |
---|
133 | |
---|
134 | if (this->consoleOverlay_) |
---|
135 | Ogre::OverlayManager::getSingleton().destroy(consoleOverlay_); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | @brief Sets the config values, describing the size of the console. |
---|
140 | */ |
---|
141 | void InGameConsole::setConfigValues() |
---|
142 | { |
---|
143 | SetConfigValue(relativeWidth, 0.8); |
---|
144 | SetConfigValue(relativeHeight, 0.4); |
---|
145 | SetConfigValue(blinkTime, 0.5); |
---|
146 | SetConfigValue(scrollSpeed_, 3.0f); |
---|
147 | SetConfigValue(noiseSize_, 1.0f); |
---|
148 | SetConfigValue(cursorSymbol_, '|'); |
---|
149 | SetConfigValue(bHidesAllInput_, false).callback(this, &InGameConsole::bHidesAllInputChanged); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | @brief Called whenever bHidesAllInput_ changes. |
---|
154 | */ |
---|
155 | void InGameConsole::bHidesAllInputChanged() |
---|
156 | { |
---|
157 | if (inputState_) |
---|
158 | { |
---|
159 | if (bHidesAllInput_) |
---|
160 | { |
---|
161 | inputState_->setMouseHandler(&InputHandler::EMPTY); |
---|
162 | inputState_->setJoyStickHandler(&InputHandler::EMPTY); |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | inputState_->setMouseHandler(0); |
---|
167 | inputState_->setJoyStickHandler(0); |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | @brief Initializes the InGameConsole. |
---|
174 | */ |
---|
175 | void InGameConsole::initialise() |
---|
176 | { |
---|
177 | // create the corresponding input state |
---|
178 | inputState_ = InputManager::getInstance().createInputState("console", false, false, InputStatePriority::Console); |
---|
179 | inputState_->setKeyHandler(this->shell_->getInputBuffer()); |
---|
180 | bHidesAllInputChanged(); |
---|
181 | |
---|
182 | // create overlay and elements |
---|
183 | Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr(); |
---|
184 | |
---|
185 | // create actual overlay |
---|
186 | this->consoleOverlay_ = ovMan->create("InGameConsoleConsole"); |
---|
187 | |
---|
188 | // create a container |
---|
189 | this->consoleOverlayContainer_ = static_cast<Ogre::OverlayContainer*>(ovMan->createOverlayElement("Panel", "InGameConsoleContainer")); |
---|
190 | this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
191 | this->consoleOverlayContainer_->setPosition((1 - this->relativeWidth) / 2, 0); |
---|
192 | this->consoleOverlayContainer_->setDimensions(this->relativeWidth, this->relativeHeight); |
---|
193 | this->consoleOverlay_->add2D(this->consoleOverlayContainer_); |
---|
194 | |
---|
195 | // create BorderPanel |
---|
196 | this->consoleOverlayBorder_ = static_cast<Ogre::BorderPanelOverlayElement*>(ovMan->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel")); |
---|
197 | this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
198 | this->consoleOverlayBorder_->setMaterialName("ConsoleCenter"); |
---|
199 | this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16); |
---|
200 | this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder"); |
---|
201 | this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51); |
---|
202 | this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5); |
---|
203 | this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0); |
---|
204 | this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0); |
---|
205 | this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0); |
---|
206 | this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_); |
---|
207 | |
---|
208 | // create a new font to match the requested size exactly |
---|
209 | Ogre::FontPtr font = static_cast<Ogre::FontPtr> |
---|
210 | (Ogre::FontManager::getSingleton().create("MonofurConsole", "General")); |
---|
211 | font->setType(Ogre::FT_TRUETYPE); |
---|
212 | font->setSource("Monofur.ttf"); |
---|
213 | font->setTrueTypeSize(18); |
---|
214 | // reto: I don't know why, but setting the resolution twice as high makes the font look a lot clearer |
---|
215 | font->setTrueTypeResolution(192); |
---|
216 | font->addCodePointRange(Ogre::Font::CodePointRange(33, 126)); |
---|
217 | font->addCodePointRange(Ogre::Font::CodePointRange(161, 255)); |
---|
218 | |
---|
219 | // create noise |
---|
220 | this->consoleOverlayNoise_ = static_cast<Ogre::PanelOverlayElement*>(ovMan->createOverlayElement("Panel", "InGameConsoleNoise")); |
---|
221 | this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
222 | this->consoleOverlayNoise_->setPosition(5,0); |
---|
223 | this->consoleOverlayNoise_->setMaterialName("ConsoleNoiseSmall"); |
---|
224 | // comment following line to disable noise |
---|
225 | this->consoleOverlayBorder_->addChild(this->consoleOverlayNoise_); |
---|
226 | |
---|
227 | // create the text lines |
---|
228 | this->consoleOverlayTextAreas_ = new Ogre::TextAreaOverlayElement*[LINES]; |
---|
229 | for (int i = 0; i < LINES; i++) |
---|
230 | { |
---|
231 | this->consoleOverlayTextAreas_[i] = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleTextArea" + multi_cast<std::string>(i))); |
---|
232 | this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS); |
---|
233 | this->consoleOverlayTextAreas_[i]->setFontName("MonofurConsole"); |
---|
234 | this->consoleOverlayTextAreas_[i]->setCharHeight(18); |
---|
235 | this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
236 | this->consoleOverlayTextAreas_[i]->setLeft(8); |
---|
237 | this->consoleOverlayTextAreas_[i]->setCaption(""); |
---|
238 | this->consoleOverlayNoise_->addChild(this->consoleOverlayTextAreas_[i]); |
---|
239 | } |
---|
240 | |
---|
241 | // create cursor (also a text area overlay element) |
---|
242 | this->consoleOverlayCursor_ = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleCursor")); |
---|
243 | this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
244 | this->consoleOverlayCursor_->setFontName("MonofurConsole"); |
---|
245 | this->consoleOverlayCursor_->setCharHeight(18); |
---|
246 | this->consoleOverlayCursor_->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
247 | this->consoleOverlayCursor_->setLeft(7); |
---|
248 | this->consoleOverlayCursor_->setCaption(std::string(this->cursorSymbol_, 1)); |
---|
249 | this->consoleOverlayNoise_->addChild(this->consoleOverlayCursor_); |
---|
250 | |
---|
251 | this->windowResized(this->getWindowWidth(), this->getWindowWidth()); |
---|
252 | |
---|
253 | // move overlay "above" the top edge of the screen |
---|
254 | // we take -1.3 because the border makes the panel bigger |
---|
255 | this->consoleOverlayContainer_->setTop(-1.3 * this->relativeHeight); |
---|
256 | |
---|
257 | COUT(4) << "Info: InGameConsole initialized" << std::endl; |
---|
258 | } |
---|
259 | |
---|
260 | // ############################### |
---|
261 | // ### ShellListener methods ### |
---|
262 | // ############################### |
---|
263 | |
---|
264 | /** |
---|
265 | @brief Called if all output-lines have to be redrawn. |
---|
266 | */ |
---|
267 | void InGameConsole::linesChanged() |
---|
268 | { |
---|
269 | Shell::LineList::const_iterator it = this->shell_->getNewestLineIterator(); |
---|
270 | int max = 0; |
---|
271 | for (int i = 1; i < LINES; ++i) |
---|
272 | { |
---|
273 | if (it != this->shell_->getEndIterator()) |
---|
274 | { |
---|
275 | ++it; |
---|
276 | max = i; |
---|
277 | } |
---|
278 | else |
---|
279 | break; |
---|
280 | } |
---|
281 | |
---|
282 | for (int i = LINES - 1; i > max; --i) |
---|
283 | this->print("", Shell::None, i, true); |
---|
284 | |
---|
285 | for (int i = max; i >= 1; --i) |
---|
286 | { |
---|
287 | --it; |
---|
288 | this->print(it->first, it->second, i, true); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | @brief Called if only the last output-line has changed. |
---|
294 | */ |
---|
295 | void InGameConsole::onlyLastLineChanged() |
---|
296 | { |
---|
297 | if (LINES > 1) |
---|
298 | this->print(this->shell_->getNewestLineIterator()->first, this->shell_->getNewestLineIterator()->second, 1); |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | @brief Called if a new output-line was added. |
---|
303 | */ |
---|
304 | void InGameConsole::lineAdded() |
---|
305 | { |
---|
306 | this->numLinesShifted_ = 0; |
---|
307 | this->shiftLines(); |
---|
308 | this->onlyLastLineChanged(); |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | @brief Called if the text in the input-line has changed. |
---|
313 | */ |
---|
314 | void InGameConsole::inputChanged() |
---|
315 | { |
---|
316 | if (LINES > 0) |
---|
317 | this->print(this->shell_->getInput(), Shell::Input, 0); |
---|
318 | |
---|
319 | if (this->shell_->getInput() == "" || this->shell_->getInput().size() == 0) |
---|
320 | this->inputWindowStart_ = 0; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | @brief Called if the position of the cursor in the input-line has changed. |
---|
325 | */ |
---|
326 | void InGameConsole::cursorChanged() |
---|
327 | { |
---|
328 | unsigned int pos = this->shell_->getCursorPosition() - inputWindowStart_; |
---|
329 | if (pos > maxCharsPerLine_) |
---|
330 | pos = maxCharsPerLine_; |
---|
331 | |
---|
332 | this->consoleOverlayCursor_->setCaption(std::string(pos,' ') + cursorSymbol_); |
---|
333 | this->consoleOverlayCursor_->setTop(static_cast<int>(this->windowH_ * this->relativeHeight) - 24); |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | @brief Called if a command is about to be executed |
---|
338 | */ |
---|
339 | void InGameConsole::executed() |
---|
340 | { |
---|
341 | this->shell_->addOutputLine(this->shell_->getInput(), Shell::Command); |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | @brief Called if the console gets closed. |
---|
346 | */ |
---|
347 | void InGameConsole::exit() |
---|
348 | { |
---|
349 | this->deactivate(); |
---|
350 | } |
---|
351 | |
---|
352 | // ############################### |
---|
353 | // ### other external calls ### |
---|
354 | // ############################### |
---|
355 | |
---|
356 | /** |
---|
357 | @brief Used to control the actual scrolling and the cursor. |
---|
358 | */ |
---|
359 | void InGameConsole::preUpdate(const Clock& time) |
---|
360 | { |
---|
361 | if (this->scroll_ != 0) |
---|
362 | { |
---|
363 | float oldTop = this->consoleOverlayContainer_->getTop(); |
---|
364 | |
---|
365 | if (this->scroll_ > 0) |
---|
366 | { |
---|
367 | // scrolling down |
---|
368 | // enlarge oldTop a little bit so that this exponential function |
---|
369 | // reaches 0 before infinite time has passed... |
---|
370 | float deltaScroll = (oldTop - 0.01) * time.getDeltaTime() * this->scrollSpeed_; |
---|
371 | if (oldTop - deltaScroll >= 0) |
---|
372 | { |
---|
373 | // window has completely scrolled down |
---|
374 | this->consoleOverlayContainer_->setTop(0); |
---|
375 | this->scroll_ = 0; |
---|
376 | } |
---|
377 | else |
---|
378 | this->consoleOverlayContainer_->setTop(oldTop - deltaScroll); |
---|
379 | } |
---|
380 | |
---|
381 | else |
---|
382 | { |
---|
383 | // scrolling up |
---|
384 | // note: +0.01 for the same reason as when scrolling down |
---|
385 | float deltaScroll = (1.3 * this->relativeHeight + 0.01 + oldTop) * time.getDeltaTime() * this->scrollSpeed_; |
---|
386 | if (oldTop - deltaScroll <= -1.3 * this->relativeHeight) |
---|
387 | { |
---|
388 | // window has completely scrolled up |
---|
389 | this->consoleOverlayContainer_->setTop(-1.3 * this->relativeHeight); |
---|
390 | this->scroll_ = 0; |
---|
391 | this->consoleOverlay_->hide(); |
---|
392 | } |
---|
393 | else |
---|
394 | this->consoleOverlayContainer_->setTop(oldTop - deltaScroll); |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | if (this->bActive_) |
---|
399 | { |
---|
400 | this->cursor_ += time.getDeltaTime(); |
---|
401 | if (this->cursor_ >= this->blinkTime) |
---|
402 | { |
---|
403 | this->cursor_ = 0; |
---|
404 | bShowCursor_ = !bShowCursor_; |
---|
405 | if (bShowCursor_) |
---|
406 | this->consoleOverlayCursor_->show(); |
---|
407 | else |
---|
408 | this->consoleOverlayCursor_->hide(); |
---|
409 | } |
---|
410 | |
---|
411 | // this creates a flickering effect (extracts exactly 80% of the texture at a random location) |
---|
412 | float uRand = (rand() & 1023) / 1023.0f * 0.2f; |
---|
413 | float vRand = (rand() & 1023) / 1023.0f * 0.2f; |
---|
414 | this->consoleOverlayNoise_->setUV(uRand, vRand, 0.8f + uRand, 0.8f + vRand); |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | /** |
---|
419 | @brief Resizes the console elements. Call if window size changes. |
---|
420 | */ |
---|
421 | void InGameConsole::windowResized(unsigned int newWidth, unsigned int newHeight) |
---|
422 | { |
---|
423 | this->windowW_ = newWidth; |
---|
424 | this->windowH_ = newHeight; |
---|
425 | this->consoleOverlayBorder_->setWidth(static_cast<int>(this->windowW_* this->relativeWidth)); |
---|
426 | this->consoleOverlayBorder_->setHeight(static_cast<int>(this->windowH_ * this->relativeHeight)); |
---|
427 | this->consoleOverlayNoise_->setWidth(static_cast<int>(this->windowW_ * this->relativeWidth) - 10); |
---|
428 | this->consoleOverlayNoise_->setHeight(static_cast<int>(this->windowH_ * this->relativeHeight) - 5); |
---|
429 | this->consoleOverlayNoise_->setTiling(consoleOverlayNoise_->getWidth() / (50.0f * this->noiseSize_), consoleOverlayNoise_->getHeight() / (50.0f * this->noiseSize_)); |
---|
430 | |
---|
431 | // now adjust the text lines... |
---|
432 | this->desiredTextWidth_ = static_cast<int>(this->windowW_ * this->relativeWidth) - 12; |
---|
433 | |
---|
434 | if (LINES > 0) |
---|
435 | this->maxCharsPerLine_ = std::max(10U, static_cast<unsigned int>(static_cast<float>(this->desiredTextWidth_) / CHAR_WIDTH)); |
---|
436 | else |
---|
437 | this->maxCharsPerLine_ = 10; |
---|
438 | |
---|
439 | for (int i = 0; i < LINES; i++) |
---|
440 | { |
---|
441 | this->consoleOverlayTextAreas_[i]->setWidth(this->desiredTextWidth_); |
---|
442 | this->consoleOverlayTextAreas_[i]->setTop(static_cast<int>(this->windowH_ * this->relativeHeight) - 24 - 14*i); |
---|
443 | } |
---|
444 | |
---|
445 | this->linesChanged(); |
---|
446 | this->cursorChanged(); |
---|
447 | } |
---|
448 | |
---|
449 | // ############################### |
---|
450 | // ### internal methods ### |
---|
451 | // ############################### |
---|
452 | |
---|
453 | /** |
---|
454 | @brief Prints string to bottom line. |
---|
455 | @param s String to be printed |
---|
456 | */ |
---|
457 | void InGameConsole::print(const std::string& text, Shell::LineType type, int index, bool alwaysShift) |
---|
458 | { |
---|
459 | std::string output = text; |
---|
460 | if (LINES > index) |
---|
461 | { |
---|
462 | this->colourLine(type, index); |
---|
463 | |
---|
464 | if (index > 0) |
---|
465 | { |
---|
466 | unsigned int linesUsed = 1; |
---|
467 | while (output.size() > this->maxCharsPerLine_) |
---|
468 | { |
---|
469 | ++linesUsed; |
---|
470 | this->consoleOverlayTextAreas_[index]->setCaption(multi_cast<Ogre::DisplayString>(output.substr(0, this->maxCharsPerLine_))); |
---|
471 | output.erase(0, this->maxCharsPerLine_); |
---|
472 | output.insert(0, 1, ' '); |
---|
473 | if (linesUsed > numLinesShifted_ || alwaysShift) |
---|
474 | this->shiftLines(); |
---|
475 | this->colourLine(type, index); |
---|
476 | } |
---|
477 | this->consoleOverlayTextAreas_[index]->setCaption(multi_cast<Ogre::DisplayString>(output)); |
---|
478 | this->displayedText_ = output; |
---|
479 | this->numLinesShifted_ = linesUsed; |
---|
480 | } |
---|
481 | else |
---|
482 | { |
---|
483 | if (output.size() > this->maxCharsPerLine_) |
---|
484 | { |
---|
485 | if (this->shell_->getInputBuffer()->getCursorPosition() < this->inputWindowStart_) |
---|
486 | this->inputWindowStart_ = this->shell_->getInputBuffer()->getCursorPosition(); |
---|
487 | else if (this->shell_->getInputBuffer()->getCursorPosition() >= (this->inputWindowStart_ + this->maxCharsPerLine_ - 1)) |
---|
488 | this->inputWindowStart_ = this->shell_->getInputBuffer()->getCursorPosition() - this->maxCharsPerLine_ + 1; |
---|
489 | |
---|
490 | output = output.substr(this->inputWindowStart_, this->maxCharsPerLine_); |
---|
491 | } |
---|
492 | else |
---|
493 | this->inputWindowStart_ = 0; |
---|
494 | this->displayedText_ = output; |
---|
495 | this->consoleOverlayTextAreas_[index]->setCaption(multi_cast<Ogre::DisplayString>(output)); |
---|
496 | } |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | /** |
---|
501 | @brief Shows the InGameConsole. |
---|
502 | */ |
---|
503 | void InGameConsole::activate() |
---|
504 | { |
---|
505 | if (!this->bActive_) |
---|
506 | { |
---|
507 | this->bActive_ = true; |
---|
508 | InputManager::getInstance().enterState("console"); |
---|
509 | this->shell_->registerListener(this); |
---|
510 | |
---|
511 | this->windowResized(this->windowW_, this->windowH_); |
---|
512 | this->linesChanged(); |
---|
513 | this->cursorChanged(); |
---|
514 | this->consoleOverlay_->show(); |
---|
515 | |
---|
516 | // scroll down |
---|
517 | this->scroll_ = 1; |
---|
518 | // the rest is done by tick |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | /** |
---|
523 | @brief Hides the InGameConsole. |
---|
524 | */ |
---|
525 | void InGameConsole::deactivate() |
---|
526 | { |
---|
527 | if (this->bActive_) |
---|
528 | { |
---|
529 | this->bActive_ = false; |
---|
530 | InputManager::getInstance().leaveState("console"); |
---|
531 | this->shell_->unregisterListener(this); |
---|
532 | |
---|
533 | // scroll up |
---|
534 | this->scroll_ = -1; |
---|
535 | // the rest is done by tick |
---|
536 | } |
---|
537 | } |
---|
538 | |
---|
539 | /** |
---|
540 | @brief Shifts all output lines one line up |
---|
541 | */ |
---|
542 | void InGameConsole::shiftLines() |
---|
543 | { |
---|
544 | for (unsigned int i = LINES - 1; i > 1; --i) |
---|
545 | { |
---|
546 | this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption()); |
---|
547 | this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop()); |
---|
548 | this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom()); |
---|
549 | } |
---|
550 | } |
---|
551 | |
---|
552 | void InGameConsole::colourLine(Shell::LineType type, int index) |
---|
553 | { |
---|
554 | ColourValue colourTop, colourBottom; |
---|
555 | switch (type) |
---|
556 | { |
---|
557 | case Shell::Error: colourTop = ColourValue(0.95, 0.25, 0.25, 1.00); |
---|
558 | colourBottom = ColourValue(1.00, 0.50, 0.50, 1.00); break; |
---|
559 | |
---|
560 | case Shell::Warning: colourTop = ColourValue(0.95, 0.50, 0.20, 1.00); |
---|
561 | colourBottom = ColourValue(1.00, 0.70, 0.50, 1.00); break; |
---|
562 | |
---|
563 | case Shell::Info: colourTop = ColourValue(0.50, 0.50, 0.95, 1.00); |
---|
564 | colourBottom = ColourValue(0.80, 0.80, 1.00, 1.00); break; |
---|
565 | |
---|
566 | case Shell::Debug: colourTop = ColourValue(0.65, 0.48, 0.44, 1.00); |
---|
567 | colourBottom = ColourValue(1.00, 0.90, 0.90, 1.00); break; |
---|
568 | |
---|
569 | case Shell::Verbose: colourTop = ColourValue(0.40, 0.20, 0.40, 1.00); |
---|
570 | colourBottom = ColourValue(0.80, 0.60, 0.80, 1.00); break; |
---|
571 | |
---|
572 | case Shell::Ultra: colourTop = ColourValue(0.21, 0.69, 0.21, 1.00); |
---|
573 | colourBottom = ColourValue(0.80, 1.00, 0.80, 1.00); break; |
---|
574 | |
---|
575 | case Shell::Command: colourTop = ColourValue(0.80, 0.80, 0.80, 1.00); |
---|
576 | colourBottom = ColourValue(0.90, 0.90, 0.90, 0.90); break; |
---|
577 | |
---|
578 | case Shell::Hint: colourTop = ColourValue(0.80, 0.80, 0.80, 1.00); |
---|
579 | colourBottom = ColourValue(0.90, 0.90, 0.90, 1.00); break; |
---|
580 | |
---|
581 | default: colourTop = ColourValue(0.90, 0.90, 0.90, 1.00); |
---|
582 | colourBottom = ColourValue(1.00, 1.00, 1.00, 1.00); break; |
---|
583 | } |
---|
584 | |
---|
585 | this->consoleOverlayTextAreas_[index]->setColourTop (colourTop); |
---|
586 | this->consoleOverlayTextAreas_[index]->setColourBottom(colourBottom); |
---|
587 | } |
---|
588 | |
---|
589 | // ################################ |
---|
590 | // ### static methods ### |
---|
591 | // ################################ |
---|
592 | |
---|
593 | /** |
---|
594 | @brief Activates the console. |
---|
595 | */ |
---|
596 | /*static*/ void InGameConsole::openConsole() |
---|
597 | { |
---|
598 | InGameConsole::getInstance().activate(); |
---|
599 | } |
---|
600 | |
---|
601 | /** |
---|
602 | @brief Deactivates the console. |
---|
603 | */ |
---|
604 | /*static*/ void InGameConsole::closeConsole() |
---|
605 | { |
---|
606 | InGameConsole::getInstance().deactivate(); |
---|
607 | } |
---|
608 | } |
---|