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