[1137] | 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: |
---|
[1317] | 25 | * Fabian 'x3n' Landau |
---|
[1137] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1143] | 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | |
---|
[1137] | 31 | #include "InGameConsole.h" |
---|
[1143] | 32 | |
---|
| 33 | #include <string> |
---|
| 34 | #include <OgreOverlay.h> |
---|
| 35 | #include <OgreOverlayElement.h> |
---|
| 36 | #include <OgreOverlayManager.h> |
---|
| 37 | #include <OgreOverlayContainer.h> |
---|
| 38 | #include <OgreStringConverter.h> |
---|
| 39 | |
---|
| 40 | #include "core/Debug.h" |
---|
[1317] | 41 | #include "core/CoreIncludes.h" |
---|
| 42 | #include "core/ConfigValueIncludes.h" |
---|
[1143] | 43 | #include "core/ConsoleCommand.h" |
---|
| 44 | #include "GraphicsEngine.h" |
---|
| 45 | |
---|
[1137] | 46 | #define LINES 20 |
---|
| 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
| 50 | using namespace Ogre; |
---|
| 51 | |
---|
[1317] | 52 | float InGameConsole::REL_WIDTH = 0.8; |
---|
| 53 | float InGameConsole::REL_HEIGHT = 0.4; |
---|
| 54 | float InGameConsole::BLINK = 0.25; |
---|
[1137] | 55 | |
---|
[1317] | 56 | /** |
---|
| 57 | @brief Constructor: Creates and initializes the InGameConsole. |
---|
| 58 | */ |
---|
| 59 | InGameConsole::InGameConsole() |
---|
| 60 | { |
---|
| 61 | RegisterObject(InGameConsole); |
---|
[1137] | 62 | |
---|
[1317] | 63 | this->active_ = false; |
---|
| 64 | this->cursor_ = 0.0; |
---|
[1137] | 65 | |
---|
[1317] | 66 | this->init(); |
---|
| 67 | this->setConfigValues(); |
---|
[1137] | 68 | } |
---|
| 69 | |
---|
[1317] | 70 | /** |
---|
| 71 | @brief Destructor: Destroys the TextAreas. |
---|
| 72 | */ |
---|
| 73 | InGameConsole::~InGameConsole(void) |
---|
| 74 | { |
---|
| 75 | for (int i = 0; i < LINES; i++) |
---|
| 76 | delete this->consoleOverlayTextAreas_[i]; |
---|
[1137] | 77 | |
---|
[1317] | 78 | delete this->consoleOverlayTextAreas_; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | @brief Returns a reference to the only existing instance of InGameConsole. |
---|
| 83 | */ |
---|
| 84 | InGameConsole& InGameConsole::getInstance() |
---|
| 85 | { |
---|
| 86 | static InGameConsole instance; |
---|
| 87 | return instance; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | @brief Sets the config values, describing the size of the console. |
---|
| 92 | */ |
---|
| 93 | void InGameConsole::setConfigValues() |
---|
| 94 | { |
---|
| 95 | SetConfigValue(REL_WIDTH, 0.8); |
---|
| 96 | SetConfigValue(REL_HEIGHT, 0.4); |
---|
| 97 | SetConfigValue(BLINK, 0.25); |
---|
[1137] | 98 | } |
---|
[1317] | 99 | |
---|
| 100 | /** |
---|
| 101 | @brief Called if all output-lines have to be redrawn. |
---|
| 102 | */ |
---|
| 103 | void InGameConsole::linesChanged() |
---|
| 104 | { |
---|
| 105 | std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator(); |
---|
| 106 | for (int i = 1; i < LINES && it != Shell::getInstance().getEndIterator(); i++) |
---|
| 107 | { |
---|
| 108 | this->consoleOverlayTextAreas_[i]->setCaption(*it); |
---|
| 109 | ++it; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | @brief Called if only the last output-line has changed. |
---|
| 115 | */ |
---|
| 116 | void InGameConsole::onlyLastLineChanged() |
---|
| 117 | { |
---|
| 118 | if (LINES > 1) |
---|
| 119 | this->consoleOverlayTextAreas_[1]->setCaption(*Shell::getInstance().getNewestLineIterator()); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | @brief Called if a new output-line was added. |
---|
| 124 | */ |
---|
| 125 | void InGameConsole::lineAdded() |
---|
| 126 | { |
---|
| 127 | for (int i = LINES - 1; i > 1; i--) |
---|
| 128 | this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption()); |
---|
| 129 | |
---|
| 130 | if (LINES > 1) |
---|
| 131 | this->consoleOverlayTextAreas_[1]->setCaption(*Shell::getInstance().getNewestLineIterator()); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | @brief Called if the text in the input-line has changed. |
---|
| 136 | */ |
---|
| 137 | void InGameConsole::inputChanged() |
---|
| 138 | { |
---|
| 139 | if (LINES > 0) |
---|
| 140 | this->consoleOverlayTextAreas_[0]->setCaption(Shell::getInstance().getInput()); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | @brief Called if the position of the cursor in the input-line has changed. |
---|
| 145 | */ |
---|
| 146 | void InGameConsole::cursorChanged() |
---|
| 147 | { |
---|
| 148 | std::string input = Shell::getInstance().getInput(); |
---|
| 149 | input.insert(Shell::getInstance().getCursorPosition(), 1, '|'); |
---|
| 150 | if (LINES > 0) |
---|
| 151 | this->consoleOverlayTextAreas_[0]->setCaption(input); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | @brief Called if the console gets closed. |
---|
| 156 | */ |
---|
| 157 | void InGameConsole::exit() |
---|
| 158 | { |
---|
| 159 | this->deactivate(); |
---|
| 160 | CommandExecutor::execute("set InputMode 2"); |
---|
| 161 | } |
---|
[1137] | 162 | |
---|
| 163 | /** |
---|
[1317] | 164 | @brief Called once by constructor, initializes the InGameConsole. |
---|
[1137] | 165 | */ |
---|
[1317] | 166 | void InGameConsole::init() |
---|
| 167 | { |
---|
[1137] | 168 | // for the beginning, don't scroll |
---|
[1317] | 169 | this->scroll_ = 0; |
---|
| 170 | this->scrollTimer_ = 0; |
---|
| 171 | this->cursor_ = 0; |
---|
[1169] | 172 | |
---|
[1137] | 173 | // create overlay and elements |
---|
[1317] | 174 | this->om_ = &Ogre::OverlayManager::getSingleton(); |
---|
[1137] | 175 | |
---|
| 176 | // create a container |
---|
[1317] | 177 | this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "container")); |
---|
| 178 | this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE); |
---|
| 179 | this->consoleOverlayContainer_->setPosition((1 - InGameConsole::REL_WIDTH) / 2, 0); |
---|
| 180 | this->consoleOverlayContainer_->setDimensions(InGameConsole::REL_WIDTH, InGameConsole::REL_HEIGHT); |
---|
[1137] | 181 | |
---|
| 182 | // create BorderPanel |
---|
[1317] | 183 | this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "borderPanel")); |
---|
| 184 | this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 185 | this->consoleOverlayBorder_->setMaterialName("ConsoleCenter"); |
---|
[1137] | 186 | // set parameters for border |
---|
[1317] | 187 | this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16); |
---|
| 188 | this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder"); |
---|
| 189 | this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51); |
---|
| 190 | this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5); |
---|
| 191 | this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0); |
---|
| 192 | this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0); |
---|
| 193 | this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0); |
---|
[1137] | 194 | |
---|
| 195 | // create the text lines |
---|
[1317] | 196 | this->consoleOverlayTextAreas_ = new TextAreaOverlayElement*[LINES]; |
---|
| 197 | for (int i = 0; i < LINES; i++) |
---|
| 198 | { |
---|
| 199 | this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "textArea" + Ogre::StringConverter::toString(i))); |
---|
| 200 | this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 201 | this->consoleOverlayTextAreas_[i]->setFontName("Console"); |
---|
| 202 | this->consoleOverlayTextAreas_[i]->setCharHeight(20); |
---|
| 203 | this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21"); |
---|
| 204 | this->consoleOverlayTextAreas_[i]->setLeft(8); |
---|
| 205 | this->consoleOverlayTextAreas_[i]->setCaption(""); |
---|
[1137] | 206 | } |
---|
| 207 | |
---|
| 208 | // create noise |
---|
[1317] | 209 | this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "noise")); |
---|
| 210 | this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 211 | this->consoleOverlayNoise_->setPosition(5,0); |
---|
| 212 | this->consoleOverlayNoise_->setMaterialName("ConsoleNoise"); |
---|
[1137] | 213 | |
---|
[1317] | 214 | this->consoleOverlay_ = this->om_->create("Console"); |
---|
| 215 | this->consoleOverlay_->add2D(this->consoleOverlayContainer_); |
---|
| 216 | this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_); |
---|
| 217 | //comment following line to disable noise |
---|
| 218 | this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_); |
---|
| 219 | for (int i = 0; i < LINES; i++) |
---|
| 220 | this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]); |
---|
[1137] | 221 | |
---|
[1317] | 222 | this->resize(); |
---|
| 223 | |
---|
[1137] | 224 | // move overlay "above" the top edge of the screen |
---|
| 225 | // we take -1.2 because the border mkes the panel bigger |
---|
[1317] | 226 | this->consoleOverlayContainer_->setTop(-1.2 * InGameConsole::REL_HEIGHT); |
---|
[1137] | 227 | // show overlay |
---|
[1317] | 228 | this->consoleOverlay_->show(); |
---|
[1137] | 229 | |
---|
| 230 | COUT(3) << "Info: InGameConsole initialized" << std::endl; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
[1317] | 234 | @brief Used to control the actual scrolling and the cursor. |
---|
[1137] | 235 | */ |
---|
[1317] | 236 | void InGameConsole::tick(float dt) |
---|
| 237 | { |
---|
| 238 | this->scrollTimer_ += dt; |
---|
| 239 | if (this->scrollTimer_ >= 0.01) |
---|
| 240 | { |
---|
| 241 | float top = this->consoleOverlayContainer_->getTop(); |
---|
| 242 | this->scrollTimer_ = 0; |
---|
| 243 | if (this->scroll_ != 0) |
---|
| 244 | { |
---|
[1141] | 245 | // scroll |
---|
[1317] | 246 | top = top + 0.02 * this->scroll_; |
---|
| 247 | this->consoleOverlayContainer_->setTop(top); |
---|
[1141] | 248 | } |
---|
[1317] | 249 | if (top <= -1.2 * InGameConsole::REL_HEIGHT) |
---|
| 250 | { |
---|
[1141] | 251 | // window has completely scrolled up |
---|
[1317] | 252 | this->scroll_ = 0; |
---|
| 253 | this->consoleOverlay_->hide(); |
---|
| 254 | this->active_ = false; |
---|
[1141] | 255 | } |
---|
[1317] | 256 | if (top >= 0) |
---|
| 257 | { |
---|
[1141] | 258 | // window has completely scrolled down |
---|
[1317] | 259 | this->scroll_ = 0; |
---|
| 260 | this->consoleOverlayContainer_->setTop(0); |
---|
| 261 | this->active_ = true; |
---|
[1141] | 262 | } |
---|
[1137] | 263 | } |
---|
| 264 | |
---|
[1317] | 265 | this->cursor_ += dt; |
---|
| 266 | if (this->cursor_ >= 2 * InGameConsole::BLINK) |
---|
| 267 | this->cursor_ = 0; |
---|
| 268 | // print(convert2UTF(this->ib_->get())); |
---|
[1137] | 269 | |
---|
[1317] | 270 | // this creates a flickering effect |
---|
| 271 | this->consoleOverlayNoise_->setTiling(1, rand() % 5 + 1); |
---|
[1137] | 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
[1317] | 275 | @brief Resizes the console elements. Call if window size changes. |
---|
[1137] | 276 | */ |
---|
[1317] | 277 | void InGameConsole::resize() |
---|
| 278 | { |
---|
| 279 | this->windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
| 280 | this->windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
| 281 | this->consoleOverlayBorder_->setWidth((int) this->windowW_* InGameConsole::REL_WIDTH); |
---|
| 282 | this->consoleOverlayBorder_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT); |
---|
| 283 | this->consoleOverlayNoise_->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH - 10); |
---|
| 284 | this->consoleOverlayNoise_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT - 5); |
---|
[1137] | 285 | // now adjust the text lines... |
---|
[1317] | 286 | for (int i = 0; i < LINES; i++) |
---|
| 287 | { |
---|
| 288 | this->consoleOverlayTextAreas_[i]->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH); |
---|
| 289 | this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * InGameConsole::REL_HEIGHT - 24 - 16*i); |
---|
[1137] | 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /** |
---|
[1317] | 294 | @brief Shows the InGameConsole. |
---|
[1137] | 295 | */ |
---|
[1317] | 296 | void InGameConsole::activate() |
---|
| 297 | { |
---|
| 298 | this->consoleOverlay_->show(); |
---|
[1137] | 299 | // just in case window size has changed... |
---|
[1317] | 300 | this->resize(); |
---|
[1137] | 301 | // scroll down |
---|
[1317] | 302 | this->scroll_ = 1; |
---|
[1137] | 303 | // the rest is done by tick |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | /** |
---|
[1317] | 307 | @brief Hides the InGameConsole. |
---|
[1137] | 308 | */ |
---|
[1317] | 309 | void InGameConsole::deactivate() |
---|
| 310 | { |
---|
[1137] | 311 | // scroll up |
---|
[1317] | 312 | this->scroll_ = -1; |
---|
[1137] | 313 | // the rest is done by tick |
---|
[1317] | 314 | } |
---|
| 315 | |
---|
| 316 | /** |
---|
| 317 | @brief Activates the console. |
---|
| 318 | */ |
---|
| 319 | void InGameConsole::openConsole() |
---|
| 320 | { |
---|
| 321 | InGameConsole::getInstance().activate(); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | /** |
---|
| 325 | @brief Deactivates the console. |
---|
| 326 | */ |
---|
| 327 | void InGameConsole::closeConsole() |
---|
| 328 | { |
---|
| 329 | InGameConsole::getInstance().deactivate(); |
---|
[1137] | 330 | } |
---|
| 331 | |
---|
| 332 | /** |
---|
[1317] | 333 | @brief Prints string to bottom line. |
---|
| 334 | @param s String to be printed |
---|
[1137] | 335 | */ |
---|
[1317] | 336 | void InGameConsole::print(Ogre::UTFString s) |
---|
| 337 | { |
---|
| 338 | if (this->cursor_ > InGameConsole::BLINK) |
---|
| 339 | this->consoleOverlayTextAreas_[0]->setCaption(">" + s); |
---|
| 340 | else |
---|
| 341 | this->consoleOverlayTextAreas_[0]->setCaption(">" + s + "_"); |
---|
[1137] | 342 | } |
---|
| 343 | |
---|
| 344 | /** |
---|
[1317] | 345 | @brief Shifts all lines up and clears the bottom line. |
---|
[1137] | 346 | */ |
---|
[1317] | 347 | void InGameConsole::newline() |
---|
| 348 | { |
---|
[1181] | 349 | Ogre::UTFString line; |
---|
[1317] | 350 | for (int i = LINES - 1; i >= 1; i--) |
---|
| 351 | { |
---|
| 352 | line = this->consoleOverlayTextAreas_[i - 1]->getCaption(); |
---|
[1137] | 353 | // don't copy the cursor... |
---|
| 354 | int l = line.length(); |
---|
[1317] | 355 | if (!line.empty() && line.substr(l-1) == "_") |
---|
| 356 | line.erase(l-1); |
---|
| 357 | this->consoleOverlayTextAreas_[i]->setCaption(line); |
---|
[1137] | 358 | } |
---|
[1317] | 359 | this->consoleOverlayTextAreas_[0]->setCaption(">"); |
---|
[1169] | 360 | } |
---|
| 361 | |
---|
[1317] | 362 | /** |
---|
| 363 | @brief Converts a string into an Ogre::UTFString. |
---|
| 364 | @param s The string to convert |
---|
| 365 | @return The converted string |
---|
| 366 | */ |
---|
| 367 | Ogre::UTFString InGameConsole::convert2UTF(std::string s) |
---|
| 368 | { |
---|
[1169] | 369 | Ogre::UTFString utf; |
---|
| 370 | Ogre::UTFString::code_point cp; |
---|
[1317] | 371 | for (unsigned int i = 0; i < s.size(); ++i) |
---|
| 372 | { |
---|
[1169] | 373 | cp = s[i]; |
---|
| 374 | cp &= 0xFF; |
---|
| 375 | utf.append(1, cp); |
---|
| 376 | } |
---|
| 377 | return utf; |
---|
[1137] | 378 | } |
---|
| 379 | } |
---|