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