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