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