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