Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

that removes a gcc warning

  • 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->active_ = 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->scrollTimer_ = 0;
204        this->cursor_ = 0;
205
206        // create overlay and elements
207        this->om_ = &Ogre::OverlayManager::getSingleton();
208
209        // create a container
210        this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "InGameConsoleContainer"));
211        this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE);
212        this->consoleOverlayContainer_->setPosition((1 - InGameConsole::REL_WIDTH) / 2, 0);
213        this->consoleOverlayContainer_->setDimensions(InGameConsole::REL_WIDTH, InGameConsole::REL_HEIGHT);
214
215        // create BorderPanel
216        this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel"));
217        this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS);
218        this->consoleOverlayBorder_->setMaterialName("ConsoleCenter");
219        // set parameters for border
220        this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16);
221        this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder");
222        this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51);
223        this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5);
224        this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0);
225        this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);
226        this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);
227
228        // create the text lines
229        this->consoleOverlayTextAreas_ = new TextAreaOverlayElement*[LINES];
230        for (int i = 0; i < LINES; i++)
231        {
232            this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i)));
233            this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS);
234            this->consoleOverlayTextAreas_[i]->setFontName("Console");
235            this->consoleOverlayTextAreas_[i]->setCharHeight(18);
236            this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21");
237            this->consoleOverlayTextAreas_[i]->setLeft(8);
238            this->consoleOverlayTextAreas_[i]->setCaption("");
239        }
240
241        // create noise
242        this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleNoise"));
243        this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS);
244        this->consoleOverlayNoise_->setPosition(5,0);
245        this->consoleOverlayNoise_->setMaterialName("ConsoleNoise");
246
247        // create cursor
248        this->consoleOverlayCursor_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleCursor"));
249        this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS);
250        this->consoleOverlayCursor_->setPosition(5,219);
251        this->consoleOverlayCursor_->setDimensions(1, 14);
252        this->consoleOverlayCursor_->setMaterialName("Orxonox/GreenDot");
253
254        this->consoleOverlay_ = this->om_->create("InGameConsoleConsole");
255        this->consoleOverlay_->add2D(this->consoleOverlayContainer_);
256        this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_);
257        this->consoleOverlayContainer_->addChild(this->consoleOverlayCursor_);
258        //comment following line to disable noise
259        this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_);
260        for (int i = 0; i < LINES; i++)
261            this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]);
262
263        this->resize();
264
265        // move overlay "above" the top edge of the screen
266        // we take -1.2 because the border mkes the panel bigger
267        this->consoleOverlayContainer_->setTop(-1.2 * InGameConsole::REL_HEIGHT);
268        // show overlay
269        this->consoleOverlay_->show();
270
271        COUT(4) << "Info: InGameConsole initialized" << std::endl;
272    }
273
274    /**
275        @brief Resizes the console elements. Call if window size changes.
276    */
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);
285
286        // now adjust the text lines...
287        this->desiredTextWidth_ = (int) (this->windowW_ * InGameConsole::REL_WIDTH) - 12;
288
289        if (LINES > 0)
290            this->maxCharsPerLine_ = max((unsigned int)10, (unsigned int) ((float)this->desiredTextWidth_ / CHAR_WIDTH3));
291        else
292            this->maxCharsPerLine_ = 10;
293
294        for (int i = 0; i < LINES; i++)
295        {
296            this->consoleOverlayTextAreas_[i]->setWidth(this->desiredTextWidth_);
297            this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * InGameConsole::REL_HEIGHT - 24 - 14*i);
298        }
299
300        this->linesChanged();
301        this->cursorChanged();
302    }
303
304    /**
305        @brief Used to control the actual scrolling and the cursor.
306    */
307    void InGameConsole::tick(float dt)
308    {
309        this->scrollTimer_ += dt;
310        if (this->scrollTimer_ >= 0.01)
311        {
312            float top = this->consoleOverlayContainer_->getTop();
313            float timePassed = scrollTimer_;
314            this->scrollTimer_ = 0;
315            if (this->scroll_ != 0)
316            {
317                // scroll
318                top = top + timePassed * this->scroll_;
319                this->consoleOverlayContainer_->setTop(top);
320            }
321            if (top <= -1.2 * InGameConsole::REL_HEIGHT)
322            {
323                // window has completely scrolled up
324                this->scroll_ = 0;
325                this->consoleOverlay_->hide();
326                this->active_ = false;
327                Shell::getInstance().unregisterListener(this);
328            }
329            if (top >= 0)
330            {
331                // window has completely scrolled down
332                this->scroll_ = 0;
333                this->consoleOverlayContainer_->setTop(0);
334                this->active_ = true;
335            }
336        }
337
338        this->cursor_ += dt;
339        if (this->cursor_ >= InGameConsole::BLINK)
340        {
341            this->cursor_ = 0;
342            bShowCursor_ = !bShowCursor_;
343            if (bShowCursor_)
344              this->consoleOverlayCursor_->show();
345            else
346              this->consoleOverlayCursor_->hide();
347        }
348
349        /*if (this->cursor_ >= 2 * InGameConsole::BLINK)
350          this->cursor_ = 0;
351
352        if (this->cursor_ >= InGameConsole::BLINK && this->cursorSymbol_ == '|')
353        {
354            this->cursorSymbol_ = ' ';
355            this->cursorChanged();
356        }
357        else if (this->cursor_ < InGameConsole::BLINK && this->cursorSymbol_ == ' ')
358        {
359            this->cursorSymbol_ = '|';
360            this->cursorChanged();
361        }*/
362
363        // this creates a flickering effect
364        this->consoleOverlayNoise_->setTiling(1, rand() % 5 + 1);
365    }
366
367    /**
368        @brief Shows the InGameConsole.
369    */
370    void InGameConsole::activate()
371    {
372        InputManager::setInputState(InputManager::IS_CONSOLE);
373        Shell::getInstance().registerListener(this);
374        this->linesChanged();
375        this->cursorChanged();
376
377        this->consoleOverlay_->show();
378        // just in case window size has changed...
379        this->resize();
380        // scroll down
381        this->scroll_ = 1;
382        // the rest is done by tick
383    }
384
385    /**
386    @brief Hides the InGameConsole.
387    */
388    void InGameConsole::deactivate()
389    {
390        // scroll up
391        this->scroll_ = -1;
392        // the rest is done by tick
393        InputManager::setInputState(InputManager::IS_NORMAL);
394    }
395
396    /**
397        @brief Activates the console.
398    */
399    void InGameConsole::openConsole()
400    {
401        InGameConsole::getInstance().activate();
402    }
403
404    /**
405        @brief Deactivates the console.
406    */
407    void InGameConsole::closeConsole()
408    {
409        InGameConsole::getInstance().deactivate();
410    }
411
412    /**
413        @brief Shifts all output lines one line up
414    */
415    void InGameConsole::shiftLines()
416    {
417        for (unsigned int i = LINES - 1; i > 1; --i)
418        {
419            this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption());
420            this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop());
421            this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom());
422        }
423    }
424
425    void InGameConsole::colourLine(int colourcode, int index)
426    {
427        if (colourcode == -1)
428        {
429            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.90, 0.90, 0.90, 1.00));
430            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00));
431        }
432        else if (colourcode == 1)
433        {
434            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.25, 0.25, 1.00));
435            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00));
436        }
437        else if (colourcode == 2)
438        {
439            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.50, 0.20, 1.00));
440            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00));
441        }
442        else if (colourcode == 3)
443        {
444            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.50, 0.50, 0.95, 1.00));
445            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00));
446        }
447        else if (colourcode == 4)
448        {
449            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.65, 0.48, 0.44, 1.00));
450            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00));
451        }
452        else if (colourcode == 5)
453        {
454            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.40, 0.20, 0.40, 1.00));
455            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00));
456        }
457        else
458        {
459            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.21, 0.69, 0.21, 1.00));
460            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00));
461        }
462    }
463
464    void InGameConsole::setCursorPosition(unsigned int pos)
465    {
466        static std::string char1 = "bdefgilpqtzCEGIJKNOPQT5[}äü";
467        static std::string char2 = "Z4";
468
469        if (pos > maxCharsPerLine_)
470          pos = maxCharsPerLine_;
471        else if (pos < 0)
472          pos = 0;
473
474        float width = 0;
475        for (unsigned int i = 0; i < pos && i < displayedText_.size(); ++i)
476        {
477            if (char1.find(displayedText_[i]) != std::string::npos)
478                width += CHAR_WIDTH1;
479            else if (char2.find(displayedText_[i]) != std::string::npos)
480                width += CHAR_WIDTH2;
481            else
482                width += CHAR_WIDTH3;
483        }
484        this->consoleOverlayCursor_->setPosition(width + 5, this->windowH_ * InGameConsole::REL_HEIGHT - 20);
485    }
486
487    /**
488        @brief Prints string to bottom line.
489        @param s String to be printed
490    */
491    void InGameConsole::print(const std::string& text, int index, bool alwaysShift)
492    {
493        char level = 0;
494        if (text.size() > 0)
495            level = text[0];
496
497        std::string output = text;
498
499        if (level >= -1 && level <= 5)
500            output.erase(0, 1);
501
502        if (LINES > index)
503        {
504            this->colourLine(level, index);
505
506            if (index > 0)
507            {
508                unsigned int linesUsed = 1;
509                while (output.size() > this->maxCharsPerLine_)
510                {
511                    ++linesUsed;
512                    this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output.substr(0, this->maxCharsPerLine_)));
513                    output.erase(0, this->maxCharsPerLine_);
514                    output.insert(0, 1, ' ');
515                    if (linesUsed > numLinesShifted_ || alwaysShift)
516                        this->shiftLines();
517                    this->colourLine(level, index);
518                }
519                this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output));
520                this->displayedText_ = output;
521                this->numLinesShifted_ = linesUsed;
522            }
523            else
524            {
525                if (output.size() > this->maxCharsPerLine_)
526                {
527                    if (Shell::getInstance().getInputBuffer().getCursorPosition() < this->inputWindowStart_)
528                        this->inputWindowStart_ = Shell::getInstance().getInputBuffer().getCursorPosition();
529                    else if (Shell::getInstance().getInputBuffer().getCursorPosition() >= (this->inputWindowStart_ + this->maxCharsPerLine_ - 1))
530                        this->inputWindowStart_ = Shell::getInstance().getInputBuffer().getCursorPosition() - this->maxCharsPerLine_ + 1;
531
532                    output = output.substr(this->inputWindowStart_, this->maxCharsPerLine_);
533                }
534                else
535                  this->inputWindowStart_ = 0;
536                this->displayedText_ = output;
537                this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output));
538            }
539        }
540    }
541
542    /**
543        @brief Converts a string into an Ogre::UTFString.
544        @param s The string to convert
545        @return The converted string
546    */
547    Ogre::UTFString InGameConsole::convert2UTF(std::string s)
548    {
549        Ogre::UTFString utf;
550        Ogre::UTFString::code_point cp;
551        for (unsigned int i = 0; i < s.size(); ++i)
552        {
553          cp = s[i];
554          cp &= 0xFF;
555          utf.append(1, cp);
556        }
557        return utf;
558    }
559}
Note: See TracBrowser for help on using the repository browser.