Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1819 was 1819, checked in by rgrieder, 16 years ago

more fixes, sorry.

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