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