[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" |
---|
[8078] | 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 | |
---|
[8032] | 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(); |
---|
[1505] | 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | @brief Destructor: Destroys the TextAreas. |
---|
| 97 | */ |
---|
[3301] | 98 | InGameConsole::~InGameConsole() |
---|
[1505] | 99 | { |
---|
[1755] | 100 | this->deactivate(); |
---|
[1505] | 101 | |
---|
[1755] | 102 | // destroy the input state previously created (InputBuffer gets destroyed by the Shell) |
---|
[3327] | 103 | InputManager::getInstance().destroyState("console"); |
---|
[1755] | 104 | |
---|
[6105] | 105 | // destroy the underlaying shell |
---|
| 106 | this->shell_->destroy(); |
---|
| 107 | |
---|
[1755] | 108 | Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr(); |
---|
| 109 | if (ovMan) |
---|
| 110 | { |
---|
| 111 | if (this->consoleOverlayNoise_) |
---|
| 112 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayNoise_); |
---|
| 113 | if (this->consoleOverlayCursor_) |
---|
| 114 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayCursor_); |
---|
[1819] | 115 | Ogre::FontManager::getSingleton().remove("MonofurConsole"); |
---|
[1755] | 116 | if (this->consoleOverlayBorder_) |
---|
| 117 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayBorder_); |
---|
| 118 | if (this->consoleOverlayTextAreas_) |
---|
| 119 | { |
---|
| 120 | for (int i = 0; i < LINES; i++) |
---|
| 121 | { |
---|
| 122 | if (this->consoleOverlayTextAreas_[i]) |
---|
[7689] | 123 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayTextAreas_[i]); |
---|
[1755] | 124 | this->consoleOverlayTextAreas_[i] = 0; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | } |
---|
| 128 | if (this->consoleOverlayContainer_) |
---|
| 129 | Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayContainer_); |
---|
| 130 | } |
---|
| 131 | if (this->consoleOverlayTextAreas_) |
---|
| 132 | { |
---|
| 133 | delete[] this->consoleOverlayTextAreas_; |
---|
| 134 | this->consoleOverlayTextAreas_ = 0; |
---|
| 135 | } |
---|
| 136 | |
---|
[1819] | 137 | if (this->consoleOverlay_) |
---|
| 138 | Ogre::OverlayManager::getSingleton().destroy(consoleOverlay_); |
---|
[1505] | 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief Sets the config values, describing the size of the console. |
---|
| 143 | */ |
---|
| 144 | void InGameConsole::setConfigValues() |
---|
| 145 | { |
---|
[1577] | 146 | SetConfigValue(relativeWidth, 0.8); |
---|
| 147 | SetConfigValue(relativeHeight, 0.4); |
---|
| 148 | SetConfigValue(blinkTime, 0.5); |
---|
| 149 | SetConfigValue(scrollSpeed_, 3.0f); |
---|
| 150 | SetConfigValue(noiseSize_, 1.0f); |
---|
[1578] | 151 | SetConfigValue(cursorSymbol_, '|'); |
---|
[1878] | 152 | SetConfigValue(bHidesAllInput_, false).callback(this, &InGameConsole::bHidesAllInputChanged); |
---|
[1505] | 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
[1878] | 156 | @brief Called whenever bHidesAllInput_ changes. |
---|
| 157 | */ |
---|
| 158 | void InGameConsole::bHidesAllInputChanged() |
---|
| 159 | { |
---|
| 160 | if (inputState_) |
---|
| 161 | { |
---|
| 162 | if (bHidesAllInput_) |
---|
| 163 | { |
---|
[3327] | 164 | inputState_->setMouseHandler(&InputHandler::EMPTY); |
---|
| 165 | inputState_->setJoyStickHandler(&InputHandler::EMPTY); |
---|
[1878] | 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | inputState_->setMouseHandler(0); |
---|
| 170 | inputState_->setJoyStickHandler(0); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
[1577] | 176 | @brief Initializes the InGameConsole. |
---|
| 177 | */ |
---|
[3327] | 178 | void InGameConsole::initialise() |
---|
[1577] | 179 | { |
---|
[1755] | 180 | // create the corresponding input state |
---|
[3327] | 181 | inputState_ = InputManager::getInstance().createInputState("console", false, false, InputStatePriority::Console); |
---|
[6105] | 182 | inputState_->setKeyHandler(this->shell_->getInputBuffer()); |
---|
[1878] | 183 | bHidesAllInputChanged(); |
---|
[1755] | 184 | |
---|
[1577] | 185 | // create overlay and elements |
---|
| 186 | Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr(); |
---|
| 187 | |
---|
| 188 | // create actual overlay |
---|
| 189 | this->consoleOverlay_ = ovMan->create("InGameConsoleConsole"); |
---|
| 190 | |
---|
| 191 | // create a container |
---|
[1615] | 192 | this->consoleOverlayContainer_ = static_cast<Ogre::OverlayContainer*>(ovMan->createOverlayElement("Panel", "InGameConsoleContainer")); |
---|
[1577] | 193 | this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
| 194 | this->consoleOverlayContainer_->setPosition((1 - this->relativeWidth) / 2, 0); |
---|
| 195 | this->consoleOverlayContainer_->setDimensions(this->relativeWidth, this->relativeHeight); |
---|
| 196 | this->consoleOverlay_->add2D(this->consoleOverlayContainer_); |
---|
| 197 | |
---|
| 198 | // create BorderPanel |
---|
[1615] | 199 | this->consoleOverlayBorder_ = static_cast<Ogre::BorderPanelOverlayElement*>(ovMan->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel")); |
---|
[1577] | 200 | this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 201 | this->consoleOverlayBorder_->setMaterialName("ConsoleCenter"); |
---|
| 202 | this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16); |
---|
| 203 | this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder"); |
---|
[6502] | 204 | this->consoleOverlayBorder_->setLeftBorderUV(0.0f, 0.49f, 0.5f, 0.51f); |
---|
| 205 | this->consoleOverlayBorder_->setRightBorderUV(0.5f, 0.49f, 1.0f, 0.5f); |
---|
| 206 | this->consoleOverlayBorder_->setBottomBorderUV(0.49f, 0.5f, 0.51f, 1.0f); |
---|
| 207 | this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0f, 0.5f, 0.5f, 1.0f); |
---|
| 208 | this->consoleOverlayBorder_->setBottomRightBorderUV(0.5f, 0.5f, 1.0f, 1.0f); |
---|
[1577] | 209 | this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_); |
---|
| 210 | |
---|
[1633] | 211 | // create a new font to match the requested size exactly |
---|
| 212 | Ogre::FontPtr font = static_cast<Ogre::FontPtr> |
---|
| 213 | (Ogre::FontManager::getSingleton().create("MonofurConsole", "General")); |
---|
| 214 | font->setType(Ogre::FT_TRUETYPE); |
---|
| 215 | font->setSource("Monofur.ttf"); |
---|
| 216 | font->setTrueTypeSize(18); |
---|
| 217 | // reto: I don't know why, but setting the resolution twice as high makes the font look a lot clearer |
---|
| 218 | font->setTrueTypeResolution(192); |
---|
| 219 | font->addCodePointRange(Ogre::Font::CodePointRange(33, 126)); |
---|
| 220 | font->addCodePointRange(Ogre::Font::CodePointRange(161, 255)); |
---|
| 221 | |
---|
[6417] | 222 | // create noise |
---|
| 223 | this->consoleOverlayNoise_ = static_cast<Ogre::PanelOverlayElement*>(ovMan->createOverlayElement("Panel", "InGameConsoleNoise")); |
---|
| 224 | this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 225 | this->consoleOverlayNoise_->setPosition(5,0); |
---|
| 226 | this->consoleOverlayNoise_->setMaterialName("ConsoleNoiseSmall"); |
---|
| 227 | // comment following line to disable noise |
---|
| 228 | this->consoleOverlayBorder_->addChild(this->consoleOverlayNoise_); |
---|
| 229 | |
---|
[1577] | 230 | // create the text lines |
---|
[1615] | 231 | this->consoleOverlayTextAreas_ = new Ogre::TextAreaOverlayElement*[LINES]; |
---|
[1577] | 232 | for (int i = 0; i < LINES; i++) |
---|
| 233 | { |
---|
[3280] | 234 | this->consoleOverlayTextAreas_[i] = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleTextArea" + multi_cast<std::string>(i))); |
---|
[1577] | 235 | this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS); |
---|
[1633] | 236 | this->consoleOverlayTextAreas_[i]->setFontName("MonofurConsole"); |
---|
[1577] | 237 | this->consoleOverlayTextAreas_[i]->setCharHeight(18); |
---|
| 238 | this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
| 239 | this->consoleOverlayTextAreas_[i]->setLeft(8); |
---|
| 240 | this->consoleOverlayTextAreas_[i]->setCaption(""); |
---|
[6417] | 241 | this->consoleOverlayNoise_->addChild(this->consoleOverlayTextAreas_[i]); |
---|
[1577] | 242 | } |
---|
| 243 | |
---|
| 244 | // create cursor (also a text area overlay element) |
---|
[1615] | 245 | this->consoleOverlayCursor_ = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleCursor")); |
---|
[1577] | 246 | this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
[1633] | 247 | this->consoleOverlayCursor_->setFontName("MonofurConsole"); |
---|
[1577] | 248 | this->consoleOverlayCursor_->setCharHeight(18); |
---|
| 249 | this->consoleOverlayCursor_->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
| 250 | this->consoleOverlayCursor_->setLeft(7); |
---|
[1578] | 251 | this->consoleOverlayCursor_->setCaption(std::string(this->cursorSymbol_, 1)); |
---|
[6417] | 252 | this->consoleOverlayNoise_->addChild(this->consoleOverlayCursor_); |
---|
[1577] | 253 | |
---|
[3327] | 254 | this->windowResized(this->getWindowWidth(), this->getWindowWidth()); |
---|
[1577] | 255 | |
---|
| 256 | // move overlay "above" the top edge of the screen |
---|
[5960] | 257 | // we take -1.3 because the border makes the panel bigger |
---|
[6502] | 258 | this->consoleOverlayContainer_->setTop(-1.3f * this->relativeHeight); |
---|
[1577] | 259 | |
---|
| 260 | COUT(4) << "Info: InGameConsole initialized" << std::endl; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | // ############################### |
---|
| 264 | // ### ShellListener methods ### |
---|
| 265 | // ############################### |
---|
| 266 | |
---|
| 267 | /** |
---|
[1505] | 268 | @brief Called if all output-lines have to be redrawn. |
---|
| 269 | */ |
---|
| 270 | void InGameConsole::linesChanged() |
---|
| 271 | { |
---|
[6417] | 272 | Shell::LineList::const_iterator it = this->shell_->getNewestLineIterator(); |
---|
[1505] | 273 | int max = 0; |
---|
| 274 | for (int i = 1; i < LINES; ++i) |
---|
| 275 | { |
---|
[6105] | 276 | if (it != this->shell_->getEndIterator()) |
---|
[1505] | 277 | { |
---|
| 278 | ++it; |
---|
| 279 | max = i; |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | break; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | for (int i = LINES - 1; i > max; --i) |
---|
[6417] | 286 | this->print("", Shell::None, i, true); |
---|
[1505] | 287 | |
---|
| 288 | for (int i = max; i >= 1; --i) |
---|
| 289 | { |
---|
| 290 | --it; |
---|
[6417] | 291 | this->print(it->first, it->second, i, true); |
---|
[1505] | 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | /** |
---|
| 296 | @brief Called if only the last output-line has changed. |
---|
| 297 | */ |
---|
| 298 | void InGameConsole::onlyLastLineChanged() |
---|
| 299 | { |
---|
| 300 | if (LINES > 1) |
---|
[6417] | 301 | this->print(this->shell_->getNewestLineIterator()->first, this->shell_->getNewestLineIterator()->second, 1); |
---|
[1505] | 302 | } |
---|
| 303 | |
---|
| 304 | /** |
---|
| 305 | @brief Called if a new output-line was added. |
---|
| 306 | */ |
---|
| 307 | void InGameConsole::lineAdded() |
---|
| 308 | { |
---|
| 309 | this->numLinesShifted_ = 0; |
---|
| 310 | this->shiftLines(); |
---|
| 311 | this->onlyLastLineChanged(); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | /** |
---|
| 315 | @brief Called if the text in the input-line has changed. |
---|
| 316 | */ |
---|
| 317 | void InGameConsole::inputChanged() |
---|
| 318 | { |
---|
| 319 | if (LINES > 0) |
---|
[6417] | 320 | this->print(this->shell_->getInput(), Shell::Input, 0); |
---|
[1505] | 321 | |
---|
[6417] | 322 | if (this->shell_->getInput().empty()) |
---|
[1505] | 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 | { |
---|
[6105] | 331 | unsigned int pos = this->shell_->getCursorPosition() - inputWindowStart_; |
---|
[1577] | 332 | if (pos > maxCharsPerLine_) |
---|
| 333 | pos = maxCharsPerLine_; |
---|
| 334 | |
---|
| 335 | this->consoleOverlayCursor_->setCaption(std::string(pos,' ') + cursorSymbol_); |
---|
[6502] | 336 | this->consoleOverlayCursor_->setTop(static_cast<int>(this->windowH_ * this->relativeHeight) - 24.0f); |
---|
[1505] | 337 | } |
---|
| 338 | |
---|
| 339 | /** |
---|
[6105] | 340 | @brief Called if a command is about to be executed |
---|
| 341 | */ |
---|
| 342 | void InGameConsole::executed() |
---|
| 343 | { |
---|
[6417] | 344 | this->shell_->addOutput(this->shell_->getInput() + '\n', Shell::Command); |
---|
[6105] | 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
[1505] | 348 | @brief Called if the console gets closed. |
---|
| 349 | */ |
---|
| 350 | void InGameConsole::exit() |
---|
| 351 | { |
---|
| 352 | this->deactivate(); |
---|
| 353 | } |
---|
| 354 | |
---|
[1577] | 355 | // ############################### |
---|
| 356 | // ### other external calls ### |
---|
| 357 | // ############################### |
---|
| 358 | |
---|
[1505] | 359 | /** |
---|
[1577] | 360 | @brief Used to control the actual scrolling and the cursor. |
---|
[1505] | 361 | */ |
---|
[6417] | 362 | void InGameConsole::preUpdate(const Clock& time) |
---|
[1505] | 363 | { |
---|
[1577] | 364 | if (this->scroll_ != 0) |
---|
| 365 | { |
---|
| 366 | float oldTop = this->consoleOverlayContainer_->getTop(); |
---|
[1505] | 367 | |
---|
[1577] | 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... |
---|
[6502] | 373 | float deltaScroll = (oldTop - 0.01f) * time.getDeltaTime() * this->scrollSpeed_; |
---|
[1577] | 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 | } |
---|
[1505] | 383 | |
---|
[1577] | 384 | else |
---|
| 385 | { |
---|
| 386 | // scrolling up |
---|
| 387 | // note: +0.01 for the same reason as when scrolling down |
---|
[6502] | 388 | float deltaScroll = (1.3f * this->relativeHeight + 0.01f + oldTop) * time.getDeltaTime() * this->scrollSpeed_; |
---|
[5960] | 389 | if (oldTop - deltaScroll <= -1.3 * this->relativeHeight) |
---|
[1577] | 390 | { |
---|
| 391 | // window has completely scrolled up |
---|
[6502] | 392 | this->consoleOverlayContainer_->setTop(-1.3f * this->relativeHeight); |
---|
[1577] | 393 | this->scroll_ = 0; |
---|
| 394 | this->consoleOverlay_->hide(); |
---|
| 395 | } |
---|
| 396 | else |
---|
| 397 | this->consoleOverlayContainer_->setTop(oldTop - deltaScroll); |
---|
| 398 | } |
---|
| 399 | } |
---|
[1505] | 400 | |
---|
[1577] | 401 | if (this->bActive_) |
---|
| 402 | { |
---|
[2896] | 403 | this->cursor_ += time.getDeltaTime(); |
---|
[1577] | 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 | } |
---|
[1505] | 413 | |
---|
[1577] | 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); |
---|
[1505] | 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | /** |
---|
| 422 | @brief Resizes the console elements. Call if window size changes. |
---|
| 423 | */ |
---|
[2896] | 424 | void InGameConsole::windowResized(unsigned int newWidth, unsigned int newHeight) |
---|
[1505] | 425 | { |
---|
[1590] | 426 | this->windowW_ = newWidth; |
---|
| 427 | this->windowH_ = newHeight; |
---|
[6502] | 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); |
---|
[1601] | 432 | this->consoleOverlayNoise_->setTiling(consoleOverlayNoise_->getWidth() / (50.0f * this->noiseSize_), consoleOverlayNoise_->getHeight() / (50.0f * this->noiseSize_)); |
---|
[1505] | 433 | |
---|
| 434 | // now adjust the text lines... |
---|
[3300] | 435 | this->desiredTextWidth_ = static_cast<int>(this->windowW_ * this->relativeWidth) - 12; |
---|
[1505] | 436 | |
---|
| 437 | if (LINES > 0) |
---|
[3300] | 438 | this->maxCharsPerLine_ = std::max(10U, static_cast<unsigned int>(static_cast<float>(this->desiredTextWidth_) / CHAR_WIDTH)); |
---|
[1505] | 439 | else |
---|
| 440 | this->maxCharsPerLine_ = 10; |
---|
| 441 | |
---|
| 442 | for (int i = 0; i < LINES; i++) |
---|
| 443 | { |
---|
[6502] | 444 | this->consoleOverlayTextAreas_[i]->setWidth((float)this->desiredTextWidth_); |
---|
| 445 | this->consoleOverlayTextAreas_[i]->setTop((float)(int)(this->windowH_ * this->relativeHeight) - 24 - 14*i); |
---|
[1505] | 446 | } |
---|
| 447 | |
---|
| 448 | this->linesChanged(); |
---|
[1538] | 449 | this->cursorChanged(); |
---|
[1505] | 450 | } |
---|
| 451 | |
---|
[1577] | 452 | // ############################### |
---|
| 453 | // ### internal methods ### |
---|
| 454 | // ############################### |
---|
| 455 | |
---|
[1505] | 456 | /** |
---|
[1577] | 457 | @brief Prints string to bottom line. |
---|
[7401] | 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 |
---|
[1505] | 462 | */ |
---|
[6417] | 463 | void InGameConsole::print(const std::string& text, Shell::LineType type, int index, bool alwaysShift) |
---|
[1505] | 464 | { |
---|
[1577] | 465 | std::string output = text; |
---|
| 466 | if (LINES > index) |
---|
[1505] | 467 | { |
---|
[6417] | 468 | this->colourLine(type, index); |
---|
[1505] | 469 | |
---|
[1577] | 470 | if (index > 0) |
---|
[1540] | 471 | { |
---|
[1577] | 472 | unsigned int linesUsed = 1; |
---|
| 473 | while (output.size() > this->maxCharsPerLine_) |
---|
| 474 | { |
---|
| 475 | ++linesUsed; |
---|
[6417] | 476 | this->consoleOverlayTextAreas_[index]->setCaption(multi_cast<Ogre::DisplayString>(output.substr(0, this->maxCharsPerLine_))); |
---|
[1577] | 477 | output.erase(0, this->maxCharsPerLine_); |
---|
| 478 | output.insert(0, 1, ' '); |
---|
| 479 | if (linesUsed > numLinesShifted_ || alwaysShift) |
---|
| 480 | this->shiftLines(); |
---|
[6417] | 481 | this->colourLine(type, index); |
---|
[1577] | 482 | } |
---|
[6417] | 483 | this->consoleOverlayTextAreas_[index]->setCaption(multi_cast<Ogre::DisplayString>(output)); |
---|
[1577] | 484 | this->displayedText_ = output; |
---|
| 485 | this->numLinesShifted_ = linesUsed; |
---|
[1540] | 486 | } |
---|
[1577] | 487 | else |
---|
[1540] | 488 | { |
---|
[1577] | 489 | if (output.size() > this->maxCharsPerLine_) |
---|
| 490 | { |
---|
[6105] | 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; |
---|
[1540] | 495 | |
---|
[1577] | 496 | output = output.substr(this->inputWindowStart_, this->maxCharsPerLine_); |
---|
| 497 | } |
---|
| 498 | else |
---|
[7689] | 499 | this->inputWindowStart_ = 0; |
---|
[1577] | 500 | this->displayedText_ = output; |
---|
[6417] | 501 | this->consoleOverlayTextAreas_[index]->setCaption(multi_cast<Ogre::DisplayString>(output)); |
---|
[1577] | 502 | } |
---|
[1505] | 503 | } |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | /** |
---|
| 507 | @brief Shows the InGameConsole. |
---|
| 508 | */ |
---|
| 509 | void InGameConsole::activate() |
---|
| 510 | { |
---|
[1540] | 511 | if (!this->bActive_) |
---|
| 512 | { |
---|
| 513 | this->bActive_ = true; |
---|
[3327] | 514 | InputManager::getInstance().enterState("console"); |
---|
[6105] | 515 | this->shell_->registerListener(this); |
---|
[1505] | 516 | |
---|
[1590] | 517 | this->windowResized(this->windowW_, this->windowH_); |
---|
[1540] | 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 | } |
---|
[1505] | 526 | } |
---|
| 527 | |
---|
| 528 | /** |
---|
| 529 | @brief Hides the InGameConsole. |
---|
| 530 | */ |
---|
| 531 | void InGameConsole::deactivate() |
---|
| 532 | { |
---|
[1540] | 533 | if (this->bActive_) |
---|
| 534 | { |
---|
| 535 | this->bActive_ = false; |
---|
[8078] | 536 | GUIManager::getInstance().getLuaState()->doString("inGameConsoleClosed()"); // Notify the SheetManager in lua, that the console has been closed. |
---|
[3327] | 537 | InputManager::getInstance().leaveState("console"); |
---|
[6105] | 538 | this->shell_->unregisterListener(this); |
---|
[1540] | 539 | |
---|
| 540 | // scroll up |
---|
| 541 | this->scroll_ = -1; |
---|
| 542 | // the rest is done by tick |
---|
| 543 | } |
---|
[1505] | 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 | |
---|
[6417] | 559 | void InGameConsole::colourLine(Shell::LineType type, int index) |
---|
[1505] | 560 | { |
---|
[6417] | 561 | ColourValue colourTop, colourBottom; |
---|
| 562 | switch (type) |
---|
[1505] | 563 | { |
---|
[6502] | 564 | case Shell::Error: colourTop = ColourValue(0.95f, 0.25f, 0.25f, 1.00f); |
---|
| 565 | colourBottom = ColourValue(1.00f, 0.50f, 0.50f, 1.00f); break; |
---|
[6417] | 566 | |
---|
[6502] | 567 | case Shell::Warning: colourTop = ColourValue(0.95f, 0.50f, 0.20f, 1.00f); |
---|
| 568 | colourBottom = ColourValue(1.00f, 0.70f, 0.50f, 1.00f); break; |
---|
[6417] | 569 | |
---|
[6502] | 570 | case Shell::Info: colourTop = ColourValue(0.50f, 0.50f, 0.95f, 1.00f); |
---|
| 571 | colourBottom = ColourValue(0.80f, 0.80f, 1.00f, 1.00f); break; |
---|
[6417] | 572 | |
---|
[6502] | 573 | case Shell::Debug: colourTop = ColourValue(0.65f, 0.48f, 0.44f, 1.00f); |
---|
| 574 | colourBottom = ColourValue(1.00f, 0.90f, 0.90f, 1.00f); break; |
---|
[6417] | 575 | |
---|
[6502] | 576 | case Shell::Verbose: colourTop = ColourValue(0.40f, 0.20f, 0.40f, 1.00f); |
---|
| 577 | colourBottom = ColourValue(0.80f, 0.60f, 0.80f, 1.00f); break; |
---|
[6417] | 578 | |
---|
[6502] | 579 | case Shell::Ultra: colourTop = ColourValue(0.21f, 0.69f, 0.21f, 1.00f); |
---|
| 580 | colourBottom = ColourValue(0.80f, 1.00f, 0.80f, 1.00f); break; |
---|
[6417] | 581 | |
---|
[6502] | 582 | case Shell::Command: colourTop = ColourValue(0.80f, 0.80f, 0.80f, 1.00f); |
---|
| 583 | colourBottom = ColourValue(0.90f, 0.90f, 0.90f, 0.90f); break; |
---|
[6417] | 584 | |
---|
[6502] | 585 | case Shell::Hint: colourTop = ColourValue(0.80f, 0.80f, 0.80f, 1.00f); |
---|
| 586 | colourBottom = ColourValue(0.90f, 0.90f, 0.90f, 1.00f); break; |
---|
[6417] | 587 | |
---|
[6502] | 588 | default: colourTop = ColourValue(0.90f, 0.90f, 0.90f, 1.00f); |
---|
| 589 | colourBottom = ColourValue(1.00f, 1.00f, 1.00f, 1.00f); break; |
---|
[1505] | 590 | } |
---|
[6417] | 591 | |
---|
| 592 | this->consoleOverlayTextAreas_[index]->setColourTop (colourTop); |
---|
| 593 | this->consoleOverlayTextAreas_[index]->setColourBottom(colourBottom); |
---|
[1505] | 594 | } |
---|
| 595 | |
---|
[6105] | 596 | // ################################ |
---|
| 597 | // ### static methods ### |
---|
| 598 | // ################################ |
---|
[1577] | 599 | |
---|
| 600 | /** |
---|
| 601 | @brief Activates the console. |
---|
| 602 | */ |
---|
| 603 | /*static*/ void InGameConsole::openConsole() |
---|
[1505] | 604 | { |
---|
[1577] | 605 | InGameConsole::getInstance().activate(); |
---|
[1505] | 606 | } |
---|
| 607 | |
---|
| 608 | /** |
---|
[1577] | 609 | @brief Deactivates the console. |
---|
[1505] | 610 | */ |
---|
[1577] | 611 | /*static*/ void InGameConsole::closeConsole() |
---|
[1505] | 612 | { |
---|
[8078] | 613 | GUIManager::getInstance().getLuaState()->doString("inGameConsoleClosed()"); // Notify the SheetManager in lua, that the console has been closed, but not by ESC. |
---|
[1577] | 614 | InGameConsole::getInstance().deactivate(); |
---|
[1505] | 615 | } |
---|
[7689] | 616 | |
---|
[1505] | 617 | } |
---|