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