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