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