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