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