| 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/output/MemoryWriter.h" | 
|---|
| 48 | #include "util/output/OutputManager.h" | 
|---|
| 49 | #include "core/CoreIncludes.h" | 
|---|
| 50 | #include "core/config/ConfigValueIncludes.h" | 
|---|
| 51 | #include "core/command/ConsoleCommandIncludes.h" | 
|---|
| 52 | #include "core/singleton/ScopedSingletonIncludes.h" | 
|---|
| 53 | #include "core/GUIManager.h" | 
|---|
| 54 | #include "core/input/InputManager.h" | 
|---|
| 55 | #include "core/input/InputState.h" | 
|---|
| 56 | #include "core/input/InputBuffer.h" | 
|---|
| 57 | #include "core/LuaState.h" | 
|---|
| 58 |  | 
|---|
| 59 | namespace orxonox | 
|---|
| 60 | { | 
|---|
| 61 |     const int LINES = 30; | 
|---|
| 62 |     const float CHAR_WIDTH = 8.0f; // fix this please - determine the char-width dynamically | 
|---|
| 63 |  | 
|---|
| 64 |     SetConsoleCommand("InGameConsole", "openConsole", &InGameConsole::openConsole); | 
|---|
| 65 |     SetConsoleCommand("InGameConsole", "closeConsole", &InGameConsole::closeConsole); | 
|---|
| 66 |  | 
|---|
| 67 |     ManageScopedSingleton(InGameConsole, ScopeID::GRAPHICS, false); | 
|---|
| 68 |  | 
|---|
| 69 |     RegisterAbstractClass(InGameConsole).inheritsFrom<WindowEventListener>().inheritsFrom<UpdateListener>(); | 
|---|
| 70 |  | 
|---|
| 71 |     /** | 
|---|
| 72 |         @brief Constructor: Creates and initializes the InGameConsole. | 
|---|
| 73 |     */ | 
|---|
| 74 |     InGameConsole::InGameConsole() | 
|---|
| 75 |         : shell_(new Shell("InGameConsole", true)) | 
|---|
| 76 |         , bShowCursor_(false) | 
|---|
| 77 |         , consoleOverlay_(nullptr) | 
|---|
| 78 |         , consoleOverlayContainer_(nullptr) | 
|---|
| 79 |         , consoleOverlayNoise_(nullptr) | 
|---|
| 80 |         , consoleOverlayCursor_(nullptr) | 
|---|
| 81 |         , consoleOverlayBorder_(nullptr) | 
|---|
| 82 |         , consoleOverlayTextAreas_(nullptr) | 
|---|
| 83 |         , inputState_(nullptr) | 
|---|
| 84 |     { | 
|---|
| 85 |         RegisterObject(InGameConsole); | 
|---|
| 86 |  | 
|---|
| 87 |         this->bActive_ = false; | 
|---|
| 88 |         this->cursor_ = 0.0f; | 
|---|
| 89 |         this->cursorSymbol_ = '|'; | 
|---|
| 90 |         this->inputWindowStart_ = 0; | 
|---|
| 91 |         this->numLinesShifted_ = LINES - 1; | 
|---|
| 92 |         // for the beginning, don't scroll | 
|---|
| 93 |         this->scroll_ = 0; | 
|---|
| 94 |  | 
|---|
| 95 |         this->setConfigValues(); | 
|---|
| 96 |         this->initialise(); | 
|---|
| 97 |  | 
|---|
| 98 |         // Output buffering is not anymore needed. Not the best solution to do | 
|---|
| 99 |         // this here, but there isn't much of another way. | 
|---|
| 100 |         OutputManager::getInstance().getMemoryWriter()->disable(); | 
|---|
| 101 |     } | 
|---|
| 102 |  | 
|---|
| 103 |     /** | 
|---|
| 104 |         @brief Destructor: Destroys the TextAreas. | 
|---|
| 105 |     */ | 
|---|
| 106 |     InGameConsole::~InGameConsole() | 
|---|
| 107 |     { | 
|---|
| 108 |         this->deactivate(); | 
|---|
| 109 |  | 
|---|
| 110 |         // destroy the input state previously created (InputBuffer gets destroyed by the Shell) | 
|---|
| 111 |         InputManager::getInstance().destroyState("console"); | 
|---|
| 112 |  | 
|---|
| 113 |         // destroy the underlaying shell | 
|---|
| 114 |         delete this->shell_; | 
|---|
| 115 |  | 
|---|
| 116 |         Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr(); | 
|---|
| 117 |         if (ovMan) | 
|---|
| 118 |         { | 
|---|
| 119 |             if (this->consoleOverlayNoise_) | 
|---|
| 120 |                 Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayNoise_); | 
|---|
| 121 |             if (this->consoleOverlayCursor_) | 
|---|
| 122 |                 Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayCursor_); | 
|---|
| 123 |             Ogre::FontManager::getSingleton().remove("MonofurConsole"); | 
|---|
| 124 |             if (this->consoleOverlayBorder_) | 
|---|
| 125 |                 Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayBorder_); | 
|---|
| 126 |             if (this->consoleOverlayTextAreas_) | 
|---|
| 127 |             { | 
|---|
| 128 |                 for (int i = 0; i < LINES; i++) | 
|---|
| 129 |                 { | 
|---|
| 130 |                     if (this->consoleOverlayTextAreas_[i]) | 
|---|
| 131 |                         Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayTextAreas_[i]); | 
|---|
| 132 |                     this->consoleOverlayTextAreas_[i] = nullptr; | 
|---|
| 133 |                 } | 
|---|
| 134 |  | 
|---|
| 135 |             } | 
|---|
| 136 |             if (this->consoleOverlayContainer_) | 
|---|
| 137 |                 Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayContainer_); | 
|---|
| 138 |         } | 
|---|
| 139 |         if (this->consoleOverlayTextAreas_) | 
|---|
| 140 |         { | 
|---|
| 141 |             delete[] this->consoleOverlayTextAreas_; | 
|---|
| 142 |             this->consoleOverlayTextAreas_ = nullptr; | 
|---|
| 143 |         } | 
|---|
| 144 |  | 
|---|
| 145 |         if (this->consoleOverlay_) | 
|---|
| 146 |             Ogre::OverlayManager::getSingleton().destroy(consoleOverlay_); | 
|---|
| 147 |     } | 
|---|
| 148 |  | 
|---|
| 149 |     /** | 
|---|
| 150 |         @brief Sets the config values, describing the size of the console. | 
|---|
| 151 |     */ | 
|---|
| 152 |     void InGameConsole::setConfigValues() | 
|---|
| 153 |     { | 
|---|
| 154 |         SetConfigValue(relativeWidth, 0.8); | 
|---|
| 155 |         SetConfigValue(relativeHeight, 0.4); | 
|---|
| 156 |         SetConfigValue(blinkTime, 0.5); | 
|---|
| 157 |         SetConfigValue(scrollSpeed_, 3.0f); | 
|---|
| 158 |         SetConfigValue(noiseSize_, 1.0f); | 
|---|
| 159 |         SetConfigValue(cursorSymbol_, '|'); | 
|---|
| 160 |         SetConfigValue(bHidesAllInput_, false).callback(this, &InGameConsole::bHidesAllInputChanged); | 
|---|
| 161 |     } | 
|---|
| 162 |  | 
|---|
| 163 |     /** | 
|---|
| 164 |         @brief Called whenever bHidesAllInput_ changes. | 
|---|
| 165 |     */ | 
|---|
| 166 |     void InGameConsole::bHidesAllInputChanged() | 
|---|
| 167 |     { | 
|---|
| 168 |         if (inputState_) | 
|---|
| 169 |         { | 
|---|
| 170 |             if (bHidesAllInput_) | 
|---|
| 171 |             { | 
|---|
| 172 |                 inputState_->setMouseHandler(&InputHandler::EMPTY); | 
|---|
| 173 |                 inputState_->setJoyStickHandler(&InputHandler::EMPTY); | 
|---|
| 174 |             } | 
|---|
| 175 |             else | 
|---|
| 176 |             { | 
|---|
| 177 |                 inputState_->setMouseHandler(nullptr); | 
|---|
| 178 |                 inputState_->setJoyStickHandler(nullptr); | 
|---|
| 179 |             } | 
|---|
| 180 |         } | 
|---|
| 181 |     } | 
|---|
| 182 |  | 
|---|
| 183 |     /** | 
|---|
| 184 |         @brief Initializes the InGameConsole. | 
|---|
| 185 |     */ | 
|---|
| 186 |     void InGameConsole::initialise() | 
|---|
| 187 |     { | 
|---|
| 188 |         // create the corresponding input state | 
|---|
| 189 |         inputState_ = InputManager::getInstance().createInputState("console", false, false, InputStatePriority::Console); | 
|---|
| 190 |         inputState_->setKeyHandler(this->shell_->getInputBuffer()); | 
|---|
| 191 |         bHidesAllInputChanged(); | 
|---|
| 192 |  | 
|---|
| 193 |         // create overlay and elements | 
|---|
| 194 |         Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr(); | 
|---|
| 195 |  | 
|---|
| 196 |         // create actual overlay | 
|---|
| 197 |         this->consoleOverlay_ = ovMan->create("InGameConsoleConsole"); | 
|---|
| 198 |  | 
|---|
| 199 |         // create a container | 
|---|
| 200 |         this->consoleOverlayContainer_ = static_cast<Ogre::OverlayContainer*>(ovMan->createOverlayElement("Panel", "InGameConsoleContainer")); | 
|---|
| 201 |         this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE); | 
|---|
| 202 |         this->consoleOverlayContainer_->setPosition((1 - this->relativeWidth) / 2, 0); | 
|---|
| 203 |         this->consoleOverlayContainer_->setDimensions(this->relativeWidth, this->relativeHeight); | 
|---|
| 204 |         this->consoleOverlay_->add2D(this->consoleOverlayContainer_); | 
|---|
| 205 |  | 
|---|
| 206 |         // create BorderPanel | 
|---|
| 207 |         this->consoleOverlayBorder_ = static_cast<Ogre::BorderPanelOverlayElement*>(ovMan->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel")); | 
|---|
| 208 |         this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS); | 
|---|
| 209 |         this->consoleOverlayBorder_->setMaterialName("ConsoleCenter"); | 
|---|
| 210 |         this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16); | 
|---|
| 211 |         this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder"); | 
|---|
| 212 |         this->consoleOverlayBorder_->setLeftBorderUV(0.0f, 0.49f, 0.5f, 0.51f); | 
|---|
| 213 |         this->consoleOverlayBorder_->setRightBorderUV(0.5f, 0.49f, 1.0f, 0.5f); | 
|---|
| 214 |         this->consoleOverlayBorder_->setBottomBorderUV(0.49f, 0.5f, 0.51f, 1.0f); | 
|---|
| 215 |         this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0f, 0.5f, 0.5f, 1.0f); | 
|---|
| 216 |         this->consoleOverlayBorder_->setBottomRightBorderUV(0.5f, 0.5f, 1.0f, 1.0f); | 
|---|
| 217 |         this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_); | 
|---|
| 218 |  | 
|---|
| 219 |         // create a new font to match the requested size exactly | 
|---|
| 220 |         Ogre::FontPtr font = static_cast<Ogre::FontPtr> | 
|---|
| 221 |             (Ogre::FontManager::getSingleton().create("MonofurConsole", "General")); | 
|---|
| 222 |         font->setType(Ogre::FT_TRUETYPE); | 
|---|
| 223 |         font->setSource("Monofur.ttf"); | 
|---|
| 224 |         font->setTrueTypeSize(18); | 
|---|
| 225 |         // reto: I don't know why, but setting the resolution twice as high makes the font look a lot clearer | 
|---|
| 226 |         font->setTrueTypeResolution(192); | 
|---|
| 227 |         font->addCodePointRange(Ogre::Font::CodePointRange(33, 126)); | 
|---|
| 228 |         font->addCodePointRange(Ogre::Font::CodePointRange(161, 255)); | 
|---|
| 229 |  | 
|---|
| 230 |         // create noise | 
|---|
| 231 |         this->consoleOverlayNoise_ = static_cast<Ogre::PanelOverlayElement*>(ovMan->createOverlayElement("Panel", "InGameConsoleNoise")); | 
|---|
| 232 |         this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS); | 
|---|
| 233 |         this->consoleOverlayNoise_->setPosition(5,0); | 
|---|
| 234 |         this->consoleOverlayNoise_->setMaterialName("ConsoleNoiseSmall"); | 
|---|
| 235 |         // comment following line to disable noise | 
|---|
| 236 |         this->consoleOverlayBorder_->addChild(this->consoleOverlayNoise_); | 
|---|
| 237 |  | 
|---|
| 238 |         // create the text lines | 
|---|
| 239 |         this->consoleOverlayTextAreas_ = new Ogre::TextAreaOverlayElement*[LINES]; | 
|---|
| 240 |         for (int i = 0; i < LINES; i++) | 
|---|
| 241 |         { | 
|---|
| 242 |             this->consoleOverlayTextAreas_[i] = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleTextArea" + multi_cast<std::string>(i))); | 
|---|
| 243 |             this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS); | 
|---|
| 244 |             this->consoleOverlayTextAreas_[i]->setFontName("MonofurConsole"); | 
|---|
| 245 |             this->consoleOverlayTextAreas_[i]->setCharHeight(18); | 
|---|
| 246 |             this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21"); | 
|---|
| 247 |             this->consoleOverlayTextAreas_[i]->setLeft(8); | 
|---|
| 248 |             this->consoleOverlayTextAreas_[i]->setCaption(""); | 
|---|
| 249 |             this->consoleOverlayNoise_->addChild(this->consoleOverlayTextAreas_[i]); | 
|---|
| 250 |         } | 
|---|
| 251 |  | 
|---|
| 252 |         // create cursor (also a text area overlay element) | 
|---|
| 253 |         this->consoleOverlayCursor_ = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleCursor")); | 
|---|
| 254 |         this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS); | 
|---|
| 255 |         this->consoleOverlayCursor_->setFontName("MonofurConsole"); | 
|---|
| 256 |         this->consoleOverlayCursor_->setCharHeight(18); | 
|---|
| 257 |         this->consoleOverlayCursor_->setParameter("colour_top", "0.21 0.69 0.21"); | 
|---|
| 258 |         this->consoleOverlayCursor_->setLeft(7); | 
|---|
| 259 |         this->consoleOverlayCursor_->setCaption(std::string(this->cursorSymbol_, 1)); | 
|---|
| 260 |         this->consoleOverlayNoise_->addChild(this->consoleOverlayCursor_); | 
|---|
| 261 |  | 
|---|
| 262 |         this->windowResized(this->getWindowWidth(), this->getWindowWidth()); | 
|---|
| 263 |  | 
|---|
| 264 |         // move overlay "above" the top edge of the screen | 
|---|
| 265 |         // we take -1.3 because the border makes the panel bigger | 
|---|
| 266 |         this->consoleOverlayContainer_->setTop(-1.3f * this->relativeHeight); | 
|---|
| 267 |  | 
|---|
| 268 |         orxout(internal_info) << "InGameConsole initialized" << endl; | 
|---|
| 269 |     } | 
|---|
| 270 |  | 
|---|
| 271 |     // ############################### | 
|---|
| 272 |     // ###  ShellListener methods  ### | 
|---|
| 273 |     // ############################### | 
|---|
| 274 |  | 
|---|
| 275 |     /** | 
|---|
| 276 |         @brief Called if all output-lines have to be redrawn. | 
|---|
| 277 |     */ | 
|---|
| 278 |     void InGameConsole::linesChanged() | 
|---|
| 279 |     { | 
|---|
| 280 |         Shell::LineList::const_iterator it = this->shell_->getNewestLineIterator(); | 
|---|
| 281 |         int max = 0; | 
|---|
| 282 |         for (int i = 1; i < LINES; ++i) | 
|---|
| 283 |         { | 
|---|
| 284 |             if (it != this->shell_->getEndIterator()) | 
|---|
| 285 |             { | 
|---|
| 286 |                 ++it; | 
|---|
| 287 |                 max = i; | 
|---|
| 288 |             } | 
|---|
| 289 |             else | 
|---|
| 290 |                 break; | 
|---|
| 291 |         } | 
|---|
| 292 |  | 
|---|
| 293 |         for (int i = LINES - 1; i > max; --i) | 
|---|
| 294 |             this->print("", Shell::LineType::DebugOutput, i, true); | 
|---|
| 295 |  | 
|---|
| 296 |         for (int i = max; i >= 1; --i) | 
|---|
| 297 |         { | 
|---|
| 298 |             --it; | 
|---|
| 299 |             this->print(it->first, it->second, i, true); | 
|---|
| 300 |         } | 
|---|
| 301 |     } | 
|---|
| 302 |  | 
|---|
| 303 |     /** | 
|---|
| 304 |         @brief Called if a new output-line was added. | 
|---|
| 305 |     */ | 
|---|
| 306 |     void InGameConsole::lineAdded() | 
|---|
| 307 |     { | 
|---|
| 308 |         this->numLinesShifted_ = 0; | 
|---|
| 309 |         this->shiftLines(); | 
|---|
| 310 |         if (LINES > 1) | 
|---|
| 311 |             this->print(this->shell_->getNewestLineIterator()->first, this->shell_->getNewestLineIterator()->second, 1); | 
|---|
| 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::LineType::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(), Shell::LineType::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) - 24; | 
|---|
| 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::LineType::Message: | 
|---|
| 565 |             case Shell::LineType::DebugOutput:     colourTop = ColourValue(0.9f, 0.9f, 0.9f); break; | 
|---|
| 566 |  | 
|---|
| 567 |             case Shell::LineType::UserError:       colourTop = ColourValue(0.9f, 0.0f, 0.0f); break; | 
|---|
| 568 |             case Shell::LineType::UserWarning:     colourTop = ColourValue(0.9f, 0.5f, 0.0f); break; | 
|---|
| 569 |             case Shell::LineType::UserStatus:      colourTop = ColourValue(0.0f, 0.9f, 0.0f); break; | 
|---|
| 570 |             case Shell::LineType::UserInfo:        colourTop = ColourValue(0.0f, 0.8f, 0.8f); break; | 
|---|
| 571 |  | 
|---|
| 572 |             case Shell::LineType::InternalError:   colourTop = ColourValue(0.5f, 0.0f, 0.0f); break; | 
|---|
| 573 |             case Shell::LineType::InternalWarning: colourTop = ColourValue(0.5f, 0.2f, 0.0f); break; | 
|---|
| 574 |             case Shell::LineType::InternalStatus:  colourTop = ColourValue(0.0f, 0.5f, 0.0f); break; | 
|---|
| 575 |             case Shell::LineType::InternalInfo:    colourTop = ColourValue(0.0f, 0.4f, 0.4f); break; | 
|---|
| 576 |  | 
|---|
| 577 |             case Shell::LineType::Verbose:         colourTop = ColourValue(0.3f, 0.3f, 0.9f); break; | 
|---|
| 578 |             case Shell::LineType::VerboseMore:     colourTop = ColourValue(0.2f, 0.2f, 0.7f); break; | 
|---|
| 579 |             case Shell::LineType::VerboseUltra:    colourTop = ColourValue(0.1f, 0.1f, 0.5f); break; | 
|---|
| 580 |  | 
|---|
| 581 |             case Shell::LineType::Command:         colourTop = ColourValue(0.8f, 0.2f, 0.8f); break; | 
|---|
| 582 |             case Shell::LineType::Hint:            colourTop = ColourValue(0.4f, 0.0f, 0.4f); break; | 
|---|
| 583 |             case Shell::LineType::Input:           colourTop = ColourValue(0.9f, 0.9f, 0.9f); break; | 
|---|
| 584 |  | 
|---|
| 585 |             default:                               colourTop = ColourValue(0.5f, 0.5f, 0.5f); break; | 
|---|
| 586 |         } | 
|---|
| 587 |  | 
|---|
| 588 |         colourBottom = ColourValue(sqrt(colourTop.r), sqrt(colourTop.g), sqrt(colourTop.b)); | 
|---|
| 589 |  | 
|---|
| 590 |         this->consoleOverlayTextAreas_[index]->setColourTop   (colourTop); | 
|---|
| 591 |         this->consoleOverlayTextAreas_[index]->setColourBottom(colourBottom); | 
|---|
| 592 |     } | 
|---|
| 593 |  | 
|---|
| 594 |     // ################################ | 
|---|
| 595 |     // ###      static methods      ### | 
|---|
| 596 |     // ################################ | 
|---|
| 597 |  | 
|---|
| 598 |     /** | 
|---|
| 599 |         @brief Activates the console. | 
|---|
| 600 |     */ | 
|---|
| 601 |     /*static*/ void InGameConsole::openConsole() | 
|---|
| 602 |     { | 
|---|
| 603 |         InGameConsole::getInstance().activate(); | 
|---|
| 604 |     } | 
|---|
| 605 |  | 
|---|
| 606 |     /** | 
|---|
| 607 |         @brief Deactivates the console. | 
|---|
| 608 |     */ | 
|---|
| 609 |     /*static*/ void InGameConsole::closeConsole() | 
|---|
| 610 |     { | 
|---|
| 611 |         GUIManager::getInstance().getLuaState()->doString("inGameConsoleClosed()");  // Notify the SheetManager in lua, that the console has been closed, but not by ESC. | 
|---|
| 612 |         InGameConsole::getInstance().deactivate(); | 
|---|
| 613 |     } | 
|---|
| 614 |  | 
|---|
| 615 | } | 
|---|