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