Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/console/InGameConsole.cc @ 1784

Last change on this file since 1784 was 1784, checked in by rgrieder, 16 years ago
  • removed obsolete Convert.h includes (possibly from old XML loading)
  • replaced tabs in audio library, plus minor code cleanup because removing the tabs screwed layout
  • replaced all "#define name number" with "const Type name = number" if possible
  • Property svn:eol-style set to native
File size: 22.1 KB
Line 
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#include "OrxonoxStableHeaders.h"
30
31#include "InGameConsole.h"
32
33#include <string>
34#include <OgreOverlay.h>
35#include <OgreOverlayElement.h>
36#include <OgreOverlayManager.h>
37#include <OgreOverlayContainer.h>
38#include <OgreFontManager.h>
39#include <OgreFont.h>
40
41#include "util/Math.h"
42#include "util/Convert.h"
43#include "util/Debug.h"
44#include "core/CoreIncludes.h"
45#include "core/ConfigValueIncludes.h"
46#include "core/ConsoleCommand.h"
47#include "core/input/InputManager.h"
48#include "core/input/SimpleInputState.h"
49#include "core/input/InputBuffer.h"
50#include "GraphicsEngine.h"
51
52namespace orxonox
53{
54    const int LINES = 30;
55    const float CHAR_WIDTH = 7.45f; // fix this please - determine the char-width dynamically
56
57    SetConsoleCommand(InGameConsole, openConsole, true);
58    SetConsoleCommand(InGameConsole, closeConsole, true);
59
60    InGameConsole* InGameConsole::singletonRef_s = 0;
61
62    /**
63        @brief Constructor: Creates and initializes the InGameConsole.
64    */
65    InGameConsole::InGameConsole()
66        : consoleOverlay_(0)
67        , consoleOverlayContainer_(0)
68        , consoleOverlayNoise_(0)
69        , consoleOverlayCursor_(0)
70        , consoleOverlayBorder_(0)
71        , consoleOverlayTextAreas_(0)
72    {
73        RegisterObject(InGameConsole);
74
75        assert(singletonRef_s == 0);
76        singletonRef_s = this;
77
78        this->bActive_ = false;
79        this->cursor_ = 0.0f;
80        this->cursorSymbol_ = '|';
81        this->inputWindowStart_ = 0;
82        this->numLinesShifted_ = LINES - 1;
83        // for the beginning, don't scroll
84        this->scroll_ = 0;
85
86        this->setConfigValues();
87    }
88
89    /**
90        @brief Destructor: Destroys the TextAreas.
91    */
92    InGameConsole::~InGameConsole(void)
93    {
94        this->deactivate();
95
96        // destroy the input state previously created (InputBuffer gets destroyed by the Shell)
97        InputManager::getInstance().requestDestroyState("console");
98
99        Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr();
100        if (ovMan)
101        {
102            if (this->consoleOverlayNoise_)
103                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayNoise_);
104            if (this->consoleOverlayCursor_)
105                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayCursor_);
106            if (this->consoleOverlayBorder_)
107                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayBorder_);
108            if (this->consoleOverlayTextAreas_)
109            {
110                for (int i = 0; i < LINES; i++)
111                {
112                    if (this->consoleOverlayTextAreas_[i])
113                      Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayTextAreas_[i]);
114                    this->consoleOverlayTextAreas_[i] = 0;
115                }
116
117            }
118            if (this->consoleOverlayContainer_)
119                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayContainer_);
120        }
121        if (this->consoleOverlayTextAreas_)
122        {
123            delete[] this->consoleOverlayTextAreas_;
124            this->consoleOverlayTextAreas_ = 0;
125        }
126
127        singletonRef_s = 0;
128    }
129
130    /**
131        @brief Sets the config values, describing the size of the console.
132    */
133    void InGameConsole::setConfigValues()
134    {
135        SetConfigValue(relativeWidth, 0.8);
136        SetConfigValue(relativeHeight, 0.4);
137        SetConfigValue(blinkTime, 0.5);
138        SetConfigValue(scrollSpeed_, 3.0f);
139        SetConfigValue(noiseSize_, 1.0f);
140        SetConfigValue(cursorSymbol_, '|');
141    }
142
143    /**
144        @brief Initializes the InGameConsole.
145    */
146    void InGameConsole::initialise()
147    {
148        // create the corresponding input state
149        InputManager::getInstance().createInputState<SimpleInputState>("console", 40)
150            ->setKeyHandler(Shell::getInstance().getInputBuffer());
151
152        // create overlay and elements
153        Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr();
154
155        // create actual overlay
156        this->consoleOverlay_ = ovMan->create("InGameConsoleConsole");
157
158        // create a container
159        this->consoleOverlayContainer_ = static_cast<Ogre::OverlayContainer*>(ovMan->createOverlayElement("Panel", "InGameConsoleContainer"));
160        this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE);
161        this->consoleOverlayContainer_->setPosition((1 - this->relativeWidth) / 2, 0);
162        this->consoleOverlayContainer_->setDimensions(this->relativeWidth, this->relativeHeight);
163        this->consoleOverlay_->add2D(this->consoleOverlayContainer_);
164
165        // create BorderPanel
166        this->consoleOverlayBorder_ = static_cast<Ogre::BorderPanelOverlayElement*>(ovMan->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel"));
167        this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS);
168        this->consoleOverlayBorder_->setMaterialName("ConsoleCenter");
169        this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16);
170        this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder");
171        this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51);
172        this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5);
173        this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0);
174        this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);
175        this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);
176        this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_);
177
178        // create a new font to match the requested size exactly
179        Ogre::FontPtr font = static_cast<Ogre::FontPtr>
180            (Ogre::FontManager::getSingleton().create("MonofurConsole", "General"));
181        font->setType(Ogre::FT_TRUETYPE);
182        font->setSource("Monofur.ttf");
183        font->setTrueTypeSize(18);
184        // reto: I don't know why, but setting the resolution twice as high makes the font look a lot clearer
185        font->setTrueTypeResolution(192);
186        font->addCodePointRange(Ogre::Font::CodePointRange(33, 126));
187        font->addCodePointRange(Ogre::Font::CodePointRange(161, 255));
188
189        // create the text lines
190        this->consoleOverlayTextAreas_ = new Ogre::TextAreaOverlayElement*[LINES];
191        for (int i = 0; i < LINES; i++)
192        {
193            this->consoleOverlayTextAreas_[i] = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleTextArea" + convertToString(i)));
194            this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS);
195            this->consoleOverlayTextAreas_[i]->setFontName("MonofurConsole");
196            this->consoleOverlayTextAreas_[i]->setCharHeight(18);
197            this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21");
198            this->consoleOverlayTextAreas_[i]->setLeft(8);
199            this->consoleOverlayTextAreas_[i]->setCaption("");
200            this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]);
201        }
202
203        // create cursor (also a text area overlay element)
204        this->consoleOverlayCursor_ = static_cast<Ogre::TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleCursor"));
205        this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS);
206        this->consoleOverlayCursor_->setFontName("MonofurConsole");
207        this->consoleOverlayCursor_->setCharHeight(18);
208        this->consoleOverlayCursor_->setParameter("colour_top", "0.21 0.69 0.21");
209        this->consoleOverlayCursor_->setLeft(7);
210        this->consoleOverlayCursor_->setCaption(std::string(this->cursorSymbol_, 1));
211        this->consoleOverlayContainer_->addChild(this->consoleOverlayCursor_);
212
213        // create noise
214        this->consoleOverlayNoise_ = static_cast<Ogre::PanelOverlayElement*>(ovMan->createOverlayElement("Panel", "InGameConsoleNoise"));
215        this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS);
216        this->consoleOverlayNoise_->setPosition(5,0);
217        this->consoleOverlayNoise_->setMaterialName("ConsoleNoiseSmall");
218        // comment following line to disable noise
219        this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_);
220
221        this->windowResized(GraphicsEngine::getInstance().getWindowWidth(), GraphicsEngine::getInstance().getWindowHeight());
222
223        // move overlay "above" the top edge of the screen
224        // we take -1.2 because the border makes the panel bigger
225        this->consoleOverlayContainer_->setTop(-1.2 * this->relativeHeight);
226
227        Shell::getInstance().addOutputLevel(true);
228
229        COUT(4) << "Info: InGameConsole initialized" << std::endl;
230    }
231
232    // ###############################
233    // ###  ShellListener methods  ###
234    // ###############################
235
236    /**
237        @brief Called if all output-lines have to be redrawn.
238    */
239    void InGameConsole::linesChanged()
240    {
241        std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator();
242        int max = 0;
243        for (int i = 1; i < LINES; ++i)
244        {
245            if (it != Shell::getInstance().getEndIterator())
246            {
247                ++it;
248                max = i;
249            }
250            else
251                break;
252        }
253
254        for (int i = LINES - 1; i > max; --i)
255            this->print("", i, true);
256
257        for (int i = max; i >= 1; --i)
258        {
259            --it;
260            this->print(*it, i, true);
261        }
262    }
263
264    /**
265        @brief Called if only the last output-line has changed.
266    */
267    void InGameConsole::onlyLastLineChanged()
268    {
269        if (LINES > 1)
270            this->print(*Shell::getInstance().getNewestLineIterator(), 1);
271    }
272
273    /**
274        @brief Called if a new output-line was added.
275    */
276    void InGameConsole::lineAdded()
277    {
278        this->numLinesShifted_ = 0;
279        this->shiftLines();
280        this->onlyLastLineChanged();
281    }
282
283    /**
284        @brief Called if the text in the input-line has changed.
285    */
286    void InGameConsole::inputChanged()
287    {
288        if (LINES > 0)
289            this->print(Shell::getInstance().getInput(), 0);
290
291        if (Shell::getInstance().getInput() == "" || Shell::getInstance().getInput().size() == 0)
292            this->inputWindowStart_ = 0;
293    }
294
295    /**
296        @brief Called if the position of the cursor in the input-line has changed.
297    */
298    void InGameConsole::cursorChanged()
299    {
300        unsigned int pos = Shell::getInstance().getCursorPosition() - inputWindowStart_;
301        if (pos > maxCharsPerLine_)
302            pos = maxCharsPerLine_;
303        else if (pos < 0)
304            pos = 0;
305
306        this->consoleOverlayCursor_->setCaption(std::string(pos,' ') + cursorSymbol_);
307        this->consoleOverlayCursor_->setTop((int) this->windowH_ * this->relativeHeight - 24);
308    }
309
310    /**
311        @brief Called if the console gets closed.
312    */
313    void InGameConsole::exit()
314    {
315        this->deactivate();
316    }
317
318    // ###############################
319    // ###  other external calls   ###
320    // ###############################
321
322    /**
323        @brief Used to control the actual scrolling and the cursor.
324    */
325    void InGameConsole::tick(float dt)
326    {
327        if (this->scroll_ != 0)
328        {
329            float oldTop = this->consoleOverlayContainer_->getTop();
330
331            if (this->scroll_ > 0)
332            {
333                // scrolling down
334                // enlarge oldTop a little bit so that this exponential function
335                // reaches 0 before infinite time has passed...
336                float deltaScroll = (oldTop - 0.01) * dt * this->scrollSpeed_;
337                if (oldTop - deltaScroll >= 0)
338                {
339                    // window has completely scrolled down
340                    this->consoleOverlayContainer_->setTop(0);
341                    this->scroll_ = 0;
342                }
343                else
344                    this->consoleOverlayContainer_->setTop(oldTop - deltaScroll);
345            }
346
347            else
348            {
349                // scrolling up
350                // note: +0.01 for the same reason as when scrolling down
351                float deltaScroll = (1.2 * this->relativeHeight + 0.01 + oldTop) * dt * this->scrollSpeed_;
352                if (oldTop - deltaScroll <= -1.2 * this->relativeHeight)
353                {
354                    // window has completely scrolled up
355                    this->consoleOverlayContainer_->setTop(-1.2 * this->relativeHeight);
356                    this->scroll_ = 0;
357                    this->consoleOverlay_->hide();
358                }
359                else
360                    this->consoleOverlayContainer_->setTop(oldTop - deltaScroll);
361            }
362        }
363
364        if (this->bActive_)
365        {
366            this->cursor_ += dt;
367            if (this->cursor_ >= this->blinkTime)
368            {
369                this->cursor_ = 0;
370                bShowCursor_ = !bShowCursor_;
371                if (bShowCursor_)
372                    this->consoleOverlayCursor_->show();
373                else
374                    this->consoleOverlayCursor_->hide();
375            }
376
377            // this creates a flickering effect (extracts exactly 80% of the texture at a random location)
378            float uRand = (rand() & 1023) / 1023.0f * 0.2f;
379            float vRand = (rand() & 1023) / 1023.0f * 0.2f;
380            this->consoleOverlayNoise_->setUV(uRand, vRand, 0.8f + uRand, 0.8f + vRand);
381        }
382    }
383
384    /**
385        @brief Resizes the console elements. Call if window size changes.
386    */
387    void InGameConsole::windowResized(int newWidth, int newHeight)
388    {
389        this->windowW_ = newWidth;
390        this->windowH_ = newHeight;
391        this->consoleOverlayBorder_->setWidth((int) this->windowW_* this->relativeWidth);
392        this->consoleOverlayBorder_->setHeight((int) this->windowH_ * this->relativeHeight);
393        this->consoleOverlayNoise_->setWidth((int) this->windowW_ * this->relativeWidth - 10);
394        this->consoleOverlayNoise_->setHeight((int) this->windowH_ * this->relativeHeight - 5);
395        this->consoleOverlayNoise_->setTiling(consoleOverlayNoise_->getWidth() / (50.0f * this->noiseSize_), consoleOverlayNoise_->getHeight() / (50.0f * this->noiseSize_));
396
397        // now adjust the text lines...
398        this->desiredTextWidth_ = (int) (this->windowW_ * this->relativeWidth) - 12;
399
400        if (LINES > 0)
401            this->maxCharsPerLine_ = max((unsigned int)10, (unsigned int) ((float)this->desiredTextWidth_ / CHAR_WIDTH));
402        else
403            this->maxCharsPerLine_ = 10;
404
405        for (int i = 0; i < LINES; i++)
406        {
407            this->consoleOverlayTextAreas_[i]->setWidth(this->desiredTextWidth_);
408            this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * this->relativeHeight - 24 - 14*i);
409        }
410
411        this->linesChanged();
412        this->cursorChanged();
413    }
414
415    // ###############################
416    // ###    internal methods     ###
417    // ###############################
418
419    /**
420        @brief Prints string to bottom line.
421        @param s String to be printed
422    */
423    void InGameConsole::print(const std::string& text, int index, bool alwaysShift)
424    {
425        char level = 0;
426        if (text.size() > 0)
427            level = text[0];
428
429        std::string output = text;
430
431        if (level >= -1 && level <= 5)
432            output.erase(0, 1);
433
434        if (LINES > index)
435        {
436            this->colourLine(level, index);
437
438            if (index > 0)
439            {
440                unsigned int linesUsed = 1;
441                while (output.size() > this->maxCharsPerLine_)
442                {
443                    ++linesUsed;
444                    this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output.substr(0, this->maxCharsPerLine_)));
445                    output.erase(0, this->maxCharsPerLine_);
446                    output.insert(0, 1, ' ');
447                    if (linesUsed > numLinesShifted_ || alwaysShift)
448                        this->shiftLines();
449                    this->colourLine(level, index);
450                }
451                this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output));
452                this->displayedText_ = output;
453                this->numLinesShifted_ = linesUsed;
454            }
455            else
456            {
457                if (output.size() > this->maxCharsPerLine_)
458                {
459                    if (Shell::getInstance().getInputBuffer()->getCursorPosition() < this->inputWindowStart_)
460                        this->inputWindowStart_ = Shell::getInstance().getInputBuffer()->getCursorPosition();
461                    else if (Shell::getInstance().getInputBuffer()->getCursorPosition() >= (this->inputWindowStart_ + this->maxCharsPerLine_ - 1))
462                        this->inputWindowStart_ = Shell::getInstance().getInputBuffer()->getCursorPosition() - this->maxCharsPerLine_ + 1;
463
464                    output = output.substr(this->inputWindowStart_, this->maxCharsPerLine_);
465                }
466                else
467                  this->inputWindowStart_ = 0;
468                this->displayedText_ = output;
469                this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output));
470            }
471        }
472    }
473
474    /**
475        @brief Shows the InGameConsole.
476    */
477    void InGameConsole::activate()
478    {
479        if (!this->bActive_)
480        {
481            this->bActive_ = true;
482            InputManager::getInstance().requestEnterState("console");
483            Shell::getInstance().registerListener(this);
484
485            this->windowResized(this->windowW_, this->windowH_);
486            this->linesChanged();
487            this->cursorChanged();
488            this->consoleOverlay_->show();
489
490            // scroll down
491            this->scroll_ = 1;
492            // the rest is done by tick
493        }
494    }
495
496    /**
497    @brief Hides the InGameConsole.
498    */
499    void InGameConsole::deactivate()
500    {
501        if (this->bActive_)
502        {
503            this->bActive_ = false;
504            InputManager::getInstance().requestLeaveState("console");
505            Shell::getInstance().unregisterListener(this);
506
507            // scroll up
508            this->scroll_ = -1;
509            // the rest is done by tick
510        }
511    }
512
513    /**
514        @brief Shifts all output lines one line up
515    */
516    void InGameConsole::shiftLines()
517    {
518        for (unsigned int i = LINES - 1; i > 1; --i)
519        {
520            this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption());
521            this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop());
522            this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom());
523        }
524    }
525
526    void InGameConsole::colourLine(int colourcode, int index)
527    {
528        if (colourcode == -1)
529        {
530            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.90, 0.90, 0.90, 1.00));
531            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00));
532        }
533        else if (colourcode == 1)
534        {
535            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.25, 0.25, 1.00));
536            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00));
537        }
538        else if (colourcode == 2)
539        {
540            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.50, 0.20, 1.00));
541            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00));
542        }
543        else if (colourcode == 3)
544        {
545            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.50, 0.50, 0.95, 1.00));
546            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00));
547        }
548        else if (colourcode == 4)
549        {
550            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.65, 0.48, 0.44, 1.00));
551            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00));
552        }
553        else if (colourcode == 5)
554        {
555            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.40, 0.20, 0.40, 1.00));
556            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00));
557        }
558        else
559        {
560            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.21, 0.69, 0.21, 1.00));
561            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00));
562        }
563    }
564
565    // ###############################
566    // ###      satic methods      ###
567    // ###############################
568
569    /**
570        @brief Activates the console.
571    */
572    /*static*/ void InGameConsole::openConsole()
573    {
574        InGameConsole::getInstance().activate();
575    }
576
577    /**
578        @brief Deactivates the console.
579    */
580    /*static*/ void InGameConsole::closeConsole()
581    {
582        InGameConsole::getInstance().deactivate();
583    }
584
585    /**
586        @brief Converts a string into an Ogre::UTFString.
587        @param s The string to convert
588        @return The converted string
589    */
590    /*static*/ Ogre::UTFString InGameConsole::convert2UTF(std::string s)
591    {
592        Ogre::UTFString utf;
593        Ogre::UTFString::code_point cp;
594        for (unsigned int i = 0; i < s.size(); ++i)
595        {
596          cp = s[i];
597          cp &= 0xFF;
598          utf.append(1, cp);
599        }
600        return utf;
601    }
602}
Note: See TracBrowser for help on using the repository browser.