Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1540 was 1540, checked in by landauf, 16 years ago

some console tweaks

  • Property svn:eol-style set to native
File size: 19.6 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 <OgreStringConverter.h>
39
40#include "core/Debug.h"
41#include "core/CoreIncludes.h"
42#include "core/ConfigValueIncludes.h"
43#include "core/ConsoleCommand.h"
44#include "core/input/InputManager.h"
45#include "util/Math.h"
46#include "GraphicsEngine.h"
47
48#define LINES 30
49#define CHAR_WIDTH1 7.78 //34 // fix this please - determine the char-width dynamically
50#define CHAR_WIDTH2 8.28 // fix this please - determine the char-width dynamically
51#define CHAR_WIDTH3 7.80 //78 // fix this please - determine the char-width dynamically
52
53namespace orxonox
54{
55    SetConsoleCommand(InGameConsole, openConsole, true);
56    SetConsoleCommand(InGameConsole, closeConsole, true);
57
58    using namespace Ogre;
59
60    float InGameConsole::REL_WIDTH = 0.8;
61    float InGameConsole::REL_HEIGHT = 0.4;
62    float InGameConsole::BLINK = 0.5;
63
64    /**
65        @brief Constructor: Creates and initializes the InGameConsole.
66    */
67    InGameConsole::InGameConsole() :
68        om_(0), consoleOverlay_(0), consoleOverlayContainer_(0),
69        consoleOverlayNoise_(0), consoleOverlayBorder_(0), consoleOverlayTextAreas_(0)
70    {
71        RegisterObject(InGameConsole);
72
73        this->bActive_ = false;
74        this->cursor_ = 0.0;
75        this->cursorSymbol_ = '|';
76        this->inputWindowStart_ = 0;
77        this->numLinesShifted_ = LINES - 1;
78
79        this->init();
80        this->setConfigValues();
81
82        Shell::getInstance().addOutputLevel(true);
83    }
84
85    /**
86        @brief Destructor: Destroys the TextAreas.
87    */
88    InGameConsole::~InGameConsole(void)
89    {
90        /*for (int i = 0; i < LINES; i++)
91            if (this->consoleOverlayTextAreas_[i])
92                om_->destroyOverlayElement(this->consoleOverlayTextAreas_[i]);
93
94        if (this->consoleOverlayTextAreas_)
95            delete[] this->consoleOverlayTextAreas_;*/
96    }
97
98    /**
99        @brief Returns a reference to the only existing instance of InGameConsole.
100    */
101    InGameConsole& InGameConsole::getInstance()
102    {
103        static InGameConsole instance;
104        return instance;
105    }
106
107    /**
108        @brief Sets the config values, describing the size of the console.
109    */
110    void InGameConsole::setConfigValues()
111    {
112        SetConfigValue(REL_WIDTH, 0.8);
113        SetConfigValue(REL_HEIGHT, 0.4);
114        SetConfigValue(BLINK, 0.5);
115    }
116
117    /**
118        @brief Called if all output-lines have to be redrawn.
119    */
120    void InGameConsole::linesChanged()
121    {
122        std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator();
123        int max = 0;
124        for (int i = 1; i < LINES; ++i)
125        {
126            if (it != Shell::getInstance().getEndIterator())
127            {
128                ++it;
129                max = i;
130            }
131            else
132                break;
133        }
134
135        for (int i = LINES - 1; i > max; --i)
136            this->print("", i, true);
137
138        for (int i = max; i >= 1; --i)
139        {
140            --it;
141            this->print(*it, i, true);
142        }
143    }
144
145    /**
146        @brief Called if only the last output-line has changed.
147    */
148    void InGameConsole::onlyLastLineChanged()
149    {
150        if (LINES > 1)
151            this->print(*Shell::getInstance().getNewestLineIterator(), 1);
152    }
153
154    /**
155        @brief Called if a new output-line was added.
156    */
157    void InGameConsole::lineAdded()
158    {
159        this->numLinesShifted_ = 0;
160        this->shiftLines();
161        this->onlyLastLineChanged();
162    }
163
164    /**
165        @brief Called if the text in the input-line has changed.
166    */
167    void InGameConsole::inputChanged()
168    {
169        if (LINES > 0)
170            this->print(Shell::getInstance().getInput(), 0);
171
172        if (Shell::getInstance().getInput() == "" || Shell::getInstance().getInput().size() == 0)
173            this->inputWindowStart_ = 0;
174    }
175
176    /**
177        @brief Called if the position of the cursor in the input-line has changed.
178    */
179    void InGameConsole::cursorChanged()
180    {
181        /*std::string input = Shell::getInstance().getInput();
182        input.insert(Shell::getInstance().getCursorPosition(), 1, this->cursorSymbol_);
183        if (LINES > 0)
184            this->print(input, 0);*/
185        this->setCursorPosition(Shell::getInstance().getCursorPosition() - inputWindowStart_);
186    }
187
188    /**
189        @brief Called if the console gets closed.
190    */
191    void InGameConsole::exit()
192    {
193        this->deactivate();
194    }
195
196    /**
197        @brief Called once by constructor, initializes the InGameConsole.
198    */
199    void InGameConsole::init()
200    {
201        // for the beginning, don't scroll
202        this->scroll_ = 0;
203        this->cursor_ = 0;
204
205        // create overlay and elements
206        this->om_ = &Ogre::OverlayManager::getSingleton();
207
208        // create a container
209        this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "InGameConsoleContainer"));
210        this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE);
211        this->consoleOverlayContainer_->setPosition((1 - InGameConsole::REL_WIDTH) / 2, 0);
212        this->consoleOverlayContainer_->setDimensions(InGameConsole::REL_WIDTH, InGameConsole::REL_HEIGHT);
213
214        // create BorderPanel
215        this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel"));
216        this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS);
217        this->consoleOverlayBorder_->setMaterialName("ConsoleCenter");
218        // set parameters for border
219        this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16);
220        this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder");
221        this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51);
222        this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5);
223        this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0);
224        this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);
225        this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);
226
227        // create the text lines
228        this->consoleOverlayTextAreas_ = new TextAreaOverlayElement*[LINES];
229        for (int i = 0; i < LINES; i++)
230        {
231            this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i)));
232            this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS);
233            this->consoleOverlayTextAreas_[i]->setFontName("Console");
234            this->consoleOverlayTextAreas_[i]->setCharHeight(18);
235            this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21");
236            this->consoleOverlayTextAreas_[i]->setLeft(8);
237            this->consoleOverlayTextAreas_[i]->setCaption("");
238        }
239
240        // create noise
241        this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleNoise"));
242        this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS);
243        this->consoleOverlayNoise_->setPosition(5,0);
244        this->consoleOverlayNoise_->setMaterialName("ConsoleNoise");
245
246        // create cursor
247        this->consoleOverlayCursor_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleCursor"));
248        this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS);
249        this->consoleOverlayCursor_->setPosition(5,219);
250        this->consoleOverlayCursor_->setDimensions(1, 14);
251        this->consoleOverlayCursor_->setMaterialName("Orxonox/GreenDot");
252
253        this->consoleOverlay_ = this->om_->create("InGameConsoleConsole");
254        this->consoleOverlay_->add2D(this->consoleOverlayContainer_);
255        this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_);
256        this->consoleOverlayContainer_->addChild(this->consoleOverlayCursor_);
257        //comment following line to disable noise
258        this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_);
259        for (int i = 0; i < LINES; i++)
260            this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]);
261
262        this->resize();
263
264        // move overlay "above" the top edge of the screen
265        // we take -1.2 because the border mkes the panel bigger
266        this->consoleOverlayContainer_->setTop(-1.2 * InGameConsole::REL_HEIGHT);
267        // show overlay
268        this->consoleOverlay_->show();
269
270        COUT(4) << "Info: InGameConsole initialized" << std::endl;
271    }
272
273    /**
274        @brief Resizes the console elements. Call if window size changes.
275    */
276    void InGameConsole::resize()
277    {
278        this->windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
279        this->windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
280        this->consoleOverlayBorder_->setWidth((int) this->windowW_* InGameConsole::REL_WIDTH);
281        this->consoleOverlayBorder_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT);
282        this->consoleOverlayNoise_->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH - 10);
283        this->consoleOverlayNoise_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT - 5);
284
285        // now adjust the text lines...
286        this->desiredTextWidth_ = (int) (this->windowW_ * InGameConsole::REL_WIDTH) - 12;
287
288        if (LINES > 0)
289            this->maxCharsPerLine_ = max((unsigned int)10, (unsigned int) ((float)this->desiredTextWidth_ / CHAR_WIDTH3));
290        else
291            this->maxCharsPerLine_ = 10;
292
293        for (int i = 0; i < LINES; i++)
294        {
295            this->consoleOverlayTextAreas_[i]->setWidth(this->desiredTextWidth_);
296            this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * InGameConsole::REL_HEIGHT - 24 - 14*i);
297        }
298
299        this->linesChanged();
300        this->cursorChanged();
301    }
302
303    /**
304        @brief Used to control the actual scrolling and the cursor.
305    */
306    void InGameConsole::tick(float dt)
307    {
308        if (this->scroll_ != 0)
309        {
310            float top = this->consoleOverlayContainer_->getTop() + dt * this->scroll_;
311            this->consoleOverlayContainer_->setTop(top);
312
313            if (this->scroll_ < 0 && top <= -1.2 * InGameConsole::REL_HEIGHT)
314            {
315                // window has completely scrolled up
316                this->scroll_ = 0;
317                this->consoleOverlay_->hide();
318            }
319
320            if (this->scroll_ > 0 && top >= 0)
321            {
322                // window has completely scrolled down
323                this->scroll_ = 0;
324                this->consoleOverlayContainer_->setTop(0);
325            }
326        }
327
328        if (this->bActive_)
329        {
330            this->cursor_ += dt;
331            if (this->cursor_ >= InGameConsole::BLINK)
332            {
333                this->cursor_ = 0;
334                bShowCursor_ = !bShowCursor_;
335                if (bShowCursor_)
336                  this->consoleOverlayCursor_->show();
337                else
338                  this->consoleOverlayCursor_->hide();
339            }
340
341            /*if (this->cursor_ >= 2 * InGameConsole::BLINK)
342              this->cursor_ = 0;
343
344            if (this->cursor_ >= InGameConsole::BLINK && this->cursorSymbol_ == '|')
345            {
346                this->cursorSymbol_ = ' ';
347                this->cursorChanged();
348            }
349            else if (this->cursor_ < InGameConsole::BLINK && this->cursorSymbol_ == ' ')
350            {
351                this->cursorSymbol_ = '|';
352                this->cursorChanged();
353            }*/
354
355            // this creates a flickering effect
356            this->consoleOverlayNoise_->setTiling(1, rand() % 5 + 1);
357        }
358    }
359
360    /**
361        @brief Shows the InGameConsole.
362    */
363    void InGameConsole::activate()
364    {
365        if (!this->bActive_)
366        {
367            this->bActive_ = true;
368            InputManager::setInputState(InputManager::IS_CONSOLE);
369            Shell::getInstance().registerListener(this);
370
371            this->resize();
372            this->linesChanged();
373            this->cursorChanged();
374            this->consoleOverlay_->show();
375
376            // scroll down
377            this->scroll_ = 1;
378            // the rest is done by tick
379        }
380    }
381
382    /**
383    @brief Hides the InGameConsole.
384    */
385    void InGameConsole::deactivate()
386    {
387        if (this->bActive_)
388        {
389            this->bActive_ = false;
390            InputManager::setInputState(InputManager::IS_NORMAL);
391            Shell::getInstance().unregisterListener(this);
392
393            // scroll up
394            this->scroll_ = -1;
395            // the rest is done by tick
396        }
397    }
398
399    /**
400        @brief Activates the console.
401    */
402    void InGameConsole::openConsole()
403    {
404        InGameConsole::getInstance().activate();
405    }
406
407    /**
408        @brief Deactivates the console.
409    */
410    void InGameConsole::closeConsole()
411    {
412        InGameConsole::getInstance().deactivate();
413    }
414
415    /**
416        @brief Shifts all output lines one line up
417    */
418    void InGameConsole::shiftLines()
419    {
420        for (unsigned int i = LINES - 1; i > 1; --i)
421        {
422            this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption());
423            this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop());
424            this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom());
425        }
426    }
427
428    void InGameConsole::colourLine(int colourcode, int index)
429    {
430        if (colourcode == -1)
431        {
432            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.90, 0.90, 0.90, 1.00));
433            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00));
434        }
435        else if (colourcode == 1)
436        {
437            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.25, 0.25, 1.00));
438            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00));
439        }
440        else if (colourcode == 2)
441        {
442            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.50, 0.20, 1.00));
443            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00));
444        }
445        else if (colourcode == 3)
446        {
447            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.50, 0.50, 0.95, 1.00));
448            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00));
449        }
450        else if (colourcode == 4)
451        {
452            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.65, 0.48, 0.44, 1.00));
453            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00));
454        }
455        else if (colourcode == 5)
456        {
457            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.40, 0.20, 0.40, 1.00));
458            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00));
459        }
460        else
461        {
462            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.21, 0.69, 0.21, 1.00));
463            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00));
464        }
465    }
466
467    void InGameConsole::setCursorPosition(unsigned int pos)
468    {
469        static std::string char1 = "bdefgilpqtzCEGIJKNOPQT5[}äü";
470        static std::string char2 = "Z4";
471
472        if (pos > maxCharsPerLine_)
473          pos = maxCharsPerLine_;
474        else if (pos < 0)
475          pos = 0;
476
477        float width = 0;
478        for (unsigned int i = 0; i < pos && i < displayedText_.size(); ++i)
479        {
480            if (char1.find(displayedText_[i]) != std::string::npos)
481                width += CHAR_WIDTH1;
482            else if (char2.find(displayedText_[i]) != std::string::npos)
483                width += CHAR_WIDTH2;
484            else
485                width += CHAR_WIDTH3;
486        }
487        this->consoleOverlayCursor_->setPosition(width + 5, this->windowH_ * InGameConsole::REL_HEIGHT - 20);
488    }
489
490    /**
491        @brief Prints string to bottom line.
492        @param s String to be printed
493    */
494    void InGameConsole::print(const std::string& text, int index, bool alwaysShift)
495    {
496        char level = 0;
497        if (text.size() > 0)
498            level = text[0];
499
500        std::string output = text;
501
502        if (level >= -1 && level <= 5)
503            output.erase(0, 1);
504
505        if (LINES > index)
506        {
507            this->colourLine(level, index);
508
509            if (index > 0)
510            {
511                unsigned int linesUsed = 1;
512                while (output.size() > this->maxCharsPerLine_)
513                {
514                    ++linesUsed;
515                    this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output.substr(0, this->maxCharsPerLine_)));
516                    output.erase(0, this->maxCharsPerLine_);
517                    output.insert(0, 1, ' ');
518                    if (linesUsed > numLinesShifted_ || alwaysShift)
519                        this->shiftLines();
520                    this->colourLine(level, index);
521                }
522                this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output));
523                this->displayedText_ = output;
524                this->numLinesShifted_ = linesUsed;
525            }
526            else
527            {
528                if (output.size() > this->maxCharsPerLine_)
529                {
530                    if (Shell::getInstance().getInputBuffer().getCursorPosition() < this->inputWindowStart_)
531                        this->inputWindowStart_ = Shell::getInstance().getInputBuffer().getCursorPosition();
532                    else if (Shell::getInstance().getInputBuffer().getCursorPosition() >= (this->inputWindowStart_ + this->maxCharsPerLine_ - 1))
533                        this->inputWindowStart_ = Shell::getInstance().getInputBuffer().getCursorPosition() - this->maxCharsPerLine_ + 1;
534
535                    output = output.substr(this->inputWindowStart_, this->maxCharsPerLine_);
536                }
537                else
538                  this->inputWindowStart_ = 0;
539                this->displayedText_ = output;
540                this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output));
541            }
542        }
543    }
544
545    /**
546        @brief Converts a string into an Ogre::UTFString.
547        @param s The string to convert
548        @return The converted string
549    */
550    Ogre::UTFString InGameConsole::convert2UTF(std::string s)
551    {
552        Ogre::UTFString utf;
553        Ogre::UTFString::code_point cp;
554        for (unsigned int i = 0; i < s.size(); ++i)
555        {
556          cp = s[i];
557          cp &= 0xFF;
558          utf.append(1, cp);
559        }
560        return utf;
561    }
562}
Note: See TracBrowser for help on using the repository browser.