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