Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/orxonox/console/InGameConsole.cc @ 1338

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

renamed the overlay-elements in InGameConsole to more specific names

File size: 15.0 KB
RevLine 
[1137]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Felix Schulthess
24 *   Co-authors:
[1317]25 *      Fabian 'x3n' Landau
[1137]26 *
27 */
28
[1143]29#include "OrxonoxStableHeaders.h"
30
[1137]31#include "InGameConsole.h"
[1143]32
33#include <string>
34#include <OgreOverlay.h>
35#include <OgreOverlayElement.h>
36#include <OgreOverlayManager.h>
37#include <OgreOverlayContainer.h>
38#include <OgreStringConverter.h>
39
40#include "core/Debug.h"
[1317]41#include "core/CoreIncludes.h"
42#include "core/ConfigValueIncludes.h"
[1143]43#include "core/ConsoleCommand.h"
[1322]44#include "core/InputManager.h"
[1143]45#include "GraphicsEngine.h"
46
[1334]47#define LINES 30
[1137]48
49namespace orxonox
[1322]50{
51    ConsoleCommand(InGameConsole, openConsole, AccessLevel::None, true);
52    ConsoleCommand(InGameConsole, closeConsole, AccessLevel::None, true);
53
[1137]54    using namespace Ogre;
55
[1317]56    float InGameConsole::REL_WIDTH = 0.8;
57    float InGameConsole::REL_HEIGHT = 0.4;
[1322]58    float InGameConsole::BLINK = 0.5;
59
[1317]60    /**
61        @brief Constructor: Creates and initializes the InGameConsole.
62    */
63    InGameConsole::InGameConsole()
64    {
65        RegisterObject(InGameConsole);
[1137]66
[1317]67        this->active_ = false;
68        this->cursor_ = 0.0;
[1322]69        this->cursorSymbol_ = '|';
[1137]70
[1317]71        this->init();
[1322]72        this->setConfigValues();
73
[1334]74        Shell::getInstance().addOutputLevel(true);
[1137]75    }
76
[1317]77    /**
78        @brief Destructor: Destroys the TextAreas.
79    */
80    InGameConsole::~InGameConsole(void)
81    {
82        for (int i = 0; i < LINES; i++)
83            delete this->consoleOverlayTextAreas_[i];
[1137]84
[1317]85        delete this->consoleOverlayTextAreas_;
86    }
87
88    /**
89        @brief Returns a reference to the only existing instance of InGameConsole.
90    */
91    InGameConsole& InGameConsole::getInstance()
92    {
93        static InGameConsole instance;
94        return instance;
95    }
96
97    /**
98        @brief Sets the config values, describing the size of the console.
99    */
100    void InGameConsole::setConfigValues()
101    {
102        SetConfigValue(REL_WIDTH, 0.8);
103        SetConfigValue(REL_HEIGHT, 0.4);
[1322]104        SetConfigValue(BLINK, 0.5);
[1137]105    }
[1317]106
107    /**
108        @brief Called if all output-lines have to be redrawn.
109    */
110    void InGameConsole::linesChanged()
111    {
112        std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator();
[1322]113        for (int i = 1; i < LINES; i++)
[1317]114        {
[1322]115            if (it != Shell::getInstance().getEndIterator())
116            {
117                this->print(*it, i);
118                ++it;
119            }
120            else
121            {
122                this->print("", i);
123            }
[1317]124        }
125    }
126
127    /**
128        @brief Called if only the last output-line has changed.
129    */
130    void InGameConsole::onlyLastLineChanged()
131    {
132        if (LINES > 1)
[1322]133            this->print(*Shell::getInstance().getNewestLineIterator(), 1);
[1317]134    }
135
136    /**
137        @brief Called if a new output-line was added.
138    */
139    void InGameConsole::lineAdded()
140    {
[1327]141        for (unsigned int i = LINES - 1; i > 1; --i)
[1334]142        {
[1327]143            this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption());
[1334]144            this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop());
145            this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom());
146        }
[1327]147
148        this->onlyLastLineChanged();
[1317]149    }
150
151    /**
152        @brief Called if the text in the input-line has changed.
153    */
154    void InGameConsole::inputChanged()
155    {
156        if (LINES > 0)
[1322]157            this->print(Shell::getInstance().getInput(), 0);
[1317]158    }
159
160    /**
161        @brief Called if the position of the cursor in the input-line has changed.
162    */
163    void InGameConsole::cursorChanged()
164    {
165        std::string input = Shell::getInstance().getInput();
[1322]166        input.insert(Shell::getInstance().getCursorPosition(), 1, this->cursorSymbol_);
[1317]167        if (LINES > 0)
[1322]168            this->print(input, 0);
[1317]169    }
170
171    /**
172        @brief Called if the console gets closed.
173    */
174    void InGameConsole::exit()
175    {
176        this->deactivate();
[1322]177        InputManager::getSingleton().setInputMode(IM_INGAME);
[1317]178    }
[1137]179
180    /**
[1317]181        @brief Called once by constructor, initializes the InGameConsole.
[1137]182    */
[1317]183    void InGameConsole::init()
184    {
[1137]185        // for the beginning, don't scroll
[1317]186        this->scroll_ = 0;
187        this->scrollTimer_ = 0;
188        this->cursor_ = 0;
[1169]189
[1137]190        // create overlay and elements
[1317]191        this->om_ = &Ogre::OverlayManager::getSingleton();
[1137]192
193        // create a container
[1338]194        this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "InGameConsoleContainer"));
[1317]195        this->consoleOverlayContainer_->setMetricsMode(Ogre::GMM_RELATIVE);
196        this->consoleOverlayContainer_->setPosition((1 - InGameConsole::REL_WIDTH) / 2, 0);
[1334]197        this->consoleOverlayContainer_->setDimensions(InGameConsole::REL_WIDTH, InGameConsole::REL_HEIGHT);
[1137]198
199        // create BorderPanel
[1338]200        this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel"));
[1317]201        this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS);
[1334]202        this->consoleOverlayBorder_->setMaterialName("ConsoleCenter");
[1137]203        // set parameters for border
[1317]204        this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16);
205        this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder");
206        this->consoleOverlayBorder_->setLeftBorderUV(0.0, 0.49, 0.5, 0.51);
207        this->consoleOverlayBorder_->setRightBorderUV(0.5, 0.49, 1.0, 0.5);
208        this->consoleOverlayBorder_->setBottomBorderUV(0.49, 0.5, 0.51, 1.0);
209        this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);
210        this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);
[1137]211
212        // create the text lines
[1317]213        this->consoleOverlayTextAreas_ = new TextAreaOverlayElement*[LINES];
214        for (int i = 0; i < LINES; i++)
215        {
[1338]216            this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i)));
[1317]217            this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS);
218            this->consoleOverlayTextAreas_[i]->setFontName("Console");
[1334]219            this->consoleOverlayTextAreas_[i]->setCharHeight(18);
[1317]220            this->consoleOverlayTextAreas_[i]->setParameter("colour_top", "0.21 0.69 0.21");
221            this->consoleOverlayTextAreas_[i]->setLeft(8);
222            this->consoleOverlayTextAreas_[i]->setCaption("");
[1137]223        }
224
225        // create noise
[1338]226        this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleNoise"));
[1317]227        this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS);
[1334]228        this->consoleOverlayNoise_->setPosition(5,0);
[1317]229        this->consoleOverlayNoise_->setMaterialName("ConsoleNoise");
[1137]230
[1338]231        this->consoleOverlay_ = this->om_->create("InGameConsoleConsole");
[1317]232        this->consoleOverlay_->add2D(this->consoleOverlayContainer_);
233        this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_);
234        //comment following line to disable noise
235        this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_);
236        for (int i = 0; i < LINES; i++)
237            this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]);
[1137]238
[1317]239        this->resize();
240
[1137]241        // move overlay "above" the top edge of the screen
242        // we take -1.2 because the border mkes the panel bigger
[1317]243        this->consoleOverlayContainer_->setTop(-1.2 * InGameConsole::REL_HEIGHT);
[1137]244        // show overlay
[1317]245        this->consoleOverlay_->show();
[1137]246
[1338]247        COUT(4) << "Info: InGameConsole initialized" << std::endl;
[1137]248    }
249
250    /**
[1317]251        @brief Used to control the actual scrolling and the cursor.
[1137]252    */
[1317]253    void InGameConsole::tick(float dt)
254    {
255        this->scrollTimer_ += dt;
256        if (this->scrollTimer_ >= 0.01)
257        {
258            float top = this->consoleOverlayContainer_->getTop();
259            this->scrollTimer_ = 0;
260            if (this->scroll_ != 0)
261            {
[1141]262                // scroll
[1317]263                top = top + 0.02 * this->scroll_;
264                this->consoleOverlayContainer_->setTop(top);
[1141]265            }
[1317]266            if (top <= -1.2 * InGameConsole::REL_HEIGHT)
267            {
[1141]268                // window has completely scrolled up
[1317]269                this->scroll_ = 0;
270                this->consoleOverlay_->hide();
271                this->active_ = false;
[1334]272                Shell::getInstance().unregisterListener(this);
[1141]273            }
[1317]274            if (top >= 0)
275            {
[1141]276                // window has completely scrolled down
[1317]277                this->scroll_ = 0;
278                this->consoleOverlayContainer_->setTop(0);
279                this->active_ = true;
[1141]280            }
[1137]281        }
282
[1317]283        this->cursor_ += dt;
284        if (this->cursor_ >= 2 * InGameConsole::BLINK)
[1322]285            this->cursor_ = 0;
286
287        if (this->cursor_ >= InGameConsole::BLINK && this->cursorSymbol_ == '|')
288        {
289            this->cursorSymbol_ = ' ';
290            this->cursorChanged();
291        }
292        else if (this->cursor_ < InGameConsole::BLINK && this->cursorSymbol_ == ' ')
293        {
294            this->cursorSymbol_ = '|';
295            this->cursorChanged();
296        }
[1137]297
[1317]298        // this creates a flickering effect
299        this->consoleOverlayNoise_->setTiling(1, rand() % 5 + 1);
[1137]300    }
301
302    /**
[1317]303        @brief Resizes the console elements. Call if window size changes.
[1137]304    */
[1317]305    void InGameConsole::resize()
306    {
307        this->windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
308        this->windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
309        this->consoleOverlayBorder_->setWidth((int) this->windowW_* InGameConsole::REL_WIDTH);
310        this->consoleOverlayBorder_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT);
311        this->consoleOverlayNoise_->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH - 10);
312        this->consoleOverlayNoise_->setHeight((int) this->windowH_ * InGameConsole::REL_HEIGHT - 5);
[1137]313        // now adjust the text lines...
[1317]314        for (int i = 0; i < LINES; i++)
315        {
316            this->consoleOverlayTextAreas_[i]->setWidth((int) this->windowW_ * InGameConsole::REL_WIDTH);
[1334]317            this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * InGameConsole::REL_HEIGHT - 24 - 14*i);
[1137]318        }
319    }
320
321    /**
[1317]322        @brief Shows the InGameConsole.
[1137]323    */
[1317]324    void InGameConsole::activate()
[1322]325    {
[1334]326        Shell::getInstance().registerListener(this);
[1322]327        this->linesChanged();
328
[1317]329        this->consoleOverlay_->show();
[1137]330        // just in case window size has changed...
[1317]331        this->resize();
[1137]332        // scroll down
[1317]333        this->scroll_ = 1;
[1137]334        // the rest is done by tick
335    }
336
337    /**
[1317]338    @brief Hides the InGameConsole.
[1137]339    */
[1317]340    void InGameConsole::deactivate()
341    {
[1137]342        // scroll up
[1317]343        this->scroll_ = -1;
[1137]344        // the rest is done by tick
[1317]345    }
346
347    /**
348        @brief Activates the console.
349    */
350    void InGameConsole::openConsole()
351    {
352        InGameConsole::getInstance().activate();
353    }
354
355    /**
356        @brief Deactivates the console.
357    */
358    void InGameConsole::closeConsole()
359    {
360        InGameConsole::getInstance().deactivate();
[1137]361    }
362
363    /**
[1317]364        @brief Prints string to bottom line.
365        @param s String to be printed
[1137]366    */
[1322]367    void InGameConsole::print(const std::string& text, int index)
[1334]368    {
369        char level = 0;
370        if (text.size() > 0)
371            level = text[0];
372
373        std::string output = text;
374
375        if (level >= -1 && level <= 5)
376            output.erase(0, 1);
377
[1322]378        if (LINES > index)
[1334]379        {
380            if (level == -1)
381            {
382                this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.90, 0.90, 0.90, 1.00));
383                this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00));
384            }
385            else if (level == 1)
386            {
387                this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.25, 0.25, 1.00));
388                this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00));
389            }
390            else if (level == 2)
391            {
392                this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.50, 0.20, 1.00));
393                this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00));
394            }
395            else if (level == 3)
396            {
[1338]397                this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.50, 0.50, 0.95, 1.00));
[1334]398                this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00));
399            }
400            else if (level == 4)
401            {
402                this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.65, 0.48, 0.44, 1.00));
403                this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00));
404            }
405            else if (level == 5)
406            {
407                this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.40, 0.20, 0.40, 1.00));
408                this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00));
409            }
410            else
411            {
412                this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.21, 0.69, 0.21, 1.00));
413                this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00));
414            }
415
416            this->consoleOverlayTextAreas_[index]->setCaption(convert2UTF(output));
417        }
[1137]418    }
419
[1317]420    /**
421        @brief Converts a string into an Ogre::UTFString.
422        @param s The string to convert
423        @return The converted string
424    */
425    Ogre::UTFString InGameConsole::convert2UTF(std::string s)
426    {
[1169]427        Ogre::UTFString utf;
428        Ogre::UTFString::code_point cp;
[1317]429        for (unsigned int i = 0; i < s.size(); ++i)
430        {
[1169]431          cp = s[i];
432          cp &= 0xFF;
433          utf.append(1, cp);
434        }
435        return utf;
[1137]436    }
437}
Note: See TracBrowser for help on using the repository browser.