Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 9, 2008, 6:21:04 PM (16 years ago)
Author:
rgrieder
Message:
  • cleaned up InGameConsole a little bit
  • adjusted noise (has a config value noiseSize_)
  • replaced panel cursor with text area cursor
  • initialise()/destroy() concept like in other Singletons
  • therefore console is initialised and destroyed via Orxonox class (earlier message colouring)
  • replace linear console scroll with exponential one (no, it doesn't take infinite time to reach 0 ;))

UPDATE YOUR MEDIA REPOSITORY!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/console/InGameConsole.cc

    r1571 r1577  
    4747
    4848#define LINES 30
    49 #define CHAR_WIDTH1 7.45 //78 //34 // fix this please - determine the char-width dynamically
    50 #define CHAR_WIDTH2 CHAR_WIDTH1 //28 // fix this please - determine the char-width dynamically
    51 #define CHAR_WIDTH3 CHAR_WIDTH1 //80 //78 // fix this please - determine the char-width dynamically
     49#define CHAR_WIDTH 7.45 // fix this please - determine the char-width dynamically
    5250
    5351namespace orxonox
     
    5856    using namespace Ogre;
    5957
    60     float InGameConsole::REL_WIDTH = 0.8;
    61     float InGameConsole::REL_HEIGHT = 0.4;
    62     float InGameConsole::BLINK = 0.5;
    63 
    6458    /**
    6559        @brief Constructor: Creates and initializes the InGameConsole.
    6660    */
    6761    InGameConsole::InGameConsole() :
    68         om_(0), consoleOverlay_(0), consoleOverlayContainer_(0),
    69         consoleOverlayNoise_(0), consoleOverlayBorder_(0), consoleOverlayTextAreas_(0)
     62        consoleOverlay_(0), consoleOverlayContainer_(0),
     63        consoleOverlayNoise_(0), consoleOverlayCursor_(0), consoleOverlayBorder_(0),
     64        consoleOverlayTextAreas_(0)
    7065    {
    7166        RegisterObject(InGameConsole);
    7267
    7368        this->bActive_ = false;
    74         this->cursor_ = 0.0;
     69        this->cursor_ = 0.0f;
    7570        this->cursorSymbol_ = '|';
    7671        this->inputWindowStart_ = 0;
    7772        this->numLinesShifted_ = LINES - 1;
    78 
    79         this->init();
     73        // for the beginning, don't scroll
     74        this->scroll_ = 0;
     75
    8076        this->setConfigValues();
    81 
    82         Shell::getInstance().addOutputLevel(true);
    8377    }
    8478
     
    8882    InGameConsole::~InGameConsole(void)
    8983    {
    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_;*/
     84        this->destroy();
    9685    }
    9786
     
    11099    void InGameConsole::setConfigValues()
    111100    {
    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 
     101        SetConfigValue(relativeWidth, 0.8);
     102        SetConfigValue(relativeHeight, 0.4);
     103        SetConfigValue(blinkTime, 0.5);
     104        SetConfigValue(scrollSpeed_, 3.0f);
     105        SetConfigValue(noiseSize_, 1.0f);
     106    }
     107
     108    /**
     109        @brief Initializes the InGameConsole.
     110    */
     111    void InGameConsole::initialise()
     112    {
    205113        // create overlay and elements
    206         this->om_ = &Ogre::OverlayManager::getSingleton();
     114        Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr();
     115
     116        // create actual overlay
     117        this->consoleOverlay_ = ovMan->create("InGameConsoleConsole");
    207118
    208119        // create a container
    209         this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(this->om_->createOverlayElement("Panel", "InGameConsoleContainer"));
     120        this->consoleOverlayContainer_ = static_cast<OverlayContainer*>(ovMan->createOverlayElement("Panel", "InGameConsoleContainer"));
    210121        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);
     122        this->consoleOverlayContainer_->setPosition((1 - this->relativeWidth) / 2, 0);
     123        this->consoleOverlayContainer_->setDimensions(this->relativeWidth, this->relativeHeight);
     124        this->consoleOverlay_->add2D(this->consoleOverlayContainer_);
    213125
    214126        // create BorderPanel
    215         this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(this->om_->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel"));
     127        this->consoleOverlayBorder_ = static_cast<BorderPanelOverlayElement*>(ovMan->createOverlayElement("BorderPanel", "InGameConsoleBorderPanel"));
    216128        this->consoleOverlayBorder_->setMetricsMode(Ogre::GMM_PIXELS);
    217129        this->consoleOverlayBorder_->setMaterialName("ConsoleCenter");
    218         // set parameters for border
    219130        this->consoleOverlayBorder_->setBorderSize(16, 16, 0, 16);
    220131        this->consoleOverlayBorder_->setBorderMaterialName("ConsoleBorder");
     
    224135        this->consoleOverlayBorder_->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);
    225136        this->consoleOverlayBorder_->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);
     137        this->consoleOverlayContainer_->addChild(this->consoleOverlayBorder_);
    226138
    227139        // create the text lines
     
    229141        for (int i = 0; i < LINES; i++)
    230142        {
    231             this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(this->om_->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i)));
     143            this->consoleOverlayTextAreas_[i] = static_cast<TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i)));
    232144            this->consoleOverlayTextAreas_[i]->setMetricsMode(Ogre::GMM_PIXELS);
    233145            this->consoleOverlayTextAreas_[i]->setFontName("Monofur");
     
    236148            this->consoleOverlayTextAreas_[i]->setLeft(8);
    237149            this->consoleOverlayTextAreas_[i]->setCaption("");
    238         }
     150            this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]);
     151        }
     152
     153        // create cursor (also a text area overlay element)
     154        this->consoleOverlayCursor_ = static_cast<TextAreaOverlayElement*>(ovMan->createOverlayElement("TextArea", "InGameConsoleTextArea" + Ogre::StringConverter::toString(i)));
     155        this->consoleOverlayCursor_->setMetricsMode(Ogre::GMM_PIXELS);
     156        this->consoleOverlayCursor_->setFontName("Monofur");
     157        this->consoleOverlayCursor_->setCharHeight(18);
     158        this->consoleOverlayCursor_->setParameter("colour_top", "0.21 0.69 0.21");
     159        this->consoleOverlayCursor_->setLeft(7);
     160        this->consoleOverlayCursor_->setCaption("|");
     161        this->consoleOverlayContainer_->addChild(this->consoleOverlayCursor_);
    239162
    240163        // create noise
    241         this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(this->om_->createOverlayElement("Panel", "InGameConsoleNoise"));
     164        this->consoleOverlayNoise_ = static_cast<PanelOverlayElement*>(ovMan->createOverlayElement("Panel", "InGameConsoleNoise"));
    242165        this->consoleOverlayNoise_->setMetricsMode(Ogre::GMM_PIXELS);
    243166        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
     167        this->consoleOverlayNoise_->setMaterialName("ConsoleNoiseSmall");
     168        // comment following line to disable noise
    258169        this->consoleOverlayContainer_->addChild(this->consoleOverlayNoise_);
    259         for (int i = 0; i < LINES; i++)
    260             this->consoleOverlayContainer_->addChild(this->consoleOverlayTextAreas_[i]);
    261170
    262171        this->resize();
    263172
    264173        // 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();
     174        // we take -1.2 because the border makes the panel bigger
     175        this->consoleOverlayContainer_->setTop(-1.2 * this->relativeHeight);
     176
     177        Shell::getInstance().addOutputLevel(true);
    269178
    270179        COUT(4) << "Info: InGameConsole initialized" << std::endl;
     
    272181
    273182    /**
    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 
     183        @brief Destroys all the elements if necessary.
     184    */
     185    void InGameConsole::destroy()
     186    {
     187        Ogre::OverlayManager* ovMan = Ogre::OverlayManager::getSingletonPtr();
     188        if (ovMan)
     189        {
     190            if (this->consoleOverlayNoise_)
     191                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayNoise_);
     192            if (this->consoleOverlayCursor_)
     193                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayCursor_);
     194            if (this->consoleOverlayBorder_)
     195                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayBorder_);
     196            if (this->consoleOverlayTextAreas_)
     197            {
     198                for (int i = 0; i < LINES; i++)
     199                {
     200                    if (this->consoleOverlayTextAreas_[i])
     201                      Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayTextAreas_[i]);
     202                    this->consoleOverlayTextAreas_[i] = 0;
     203                }
     204
     205            }
     206            if (this->consoleOverlayContainer_)
     207                Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->consoleOverlayContainer_);
     208        }
     209        if (this->consoleOverlayTextAreas_)
     210        {
     211            delete[] this->consoleOverlayTextAreas_;
     212            this->consoleOverlayTextAreas_ = 0;
     213        }
     214    }
     215
     216    // ###############################
     217    // ###  ShellListener methods  ###
     218    // ###############################
     219
     220    /**
     221        @brief Called if all output-lines have to be redrawn.
     222    */
     223    void InGameConsole::linesChanged()
     224    {
     225        std::list<std::string>::const_iterator it = Shell::getInstance().getNewestLineIterator();
     226        int max = 0;
     227        for (int i = 1; i < LINES; ++i)
     228        {
     229            if (it != Shell::getInstance().getEndIterator())
     230            {
     231                ++it;
     232                max = i;
     233            }
     234            else
     235                break;
     236        }
     237
     238        for (int i = LINES - 1; i > max; --i)
     239            this->print("", i, true);
     240
     241        for (int i = max; i >= 1; --i)
     242        {
     243            --it;
     244            this->print(*it, i, true);
     245        }
     246    }
     247
     248    /**
     249        @brief Called if only the last output-line has changed.
     250    */
     251    void InGameConsole::onlyLastLineChanged()
     252    {
     253        if (LINES > 1)
     254            this->print(*Shell::getInstance().getNewestLineIterator(), 1);
     255    }
     256
     257    /**
     258        @brief Called if a new output-line was added.
     259    */
     260    void InGameConsole::lineAdded()
     261    {
     262        this->numLinesShifted_ = 0;
     263        this->shiftLines();
     264        this->onlyLastLineChanged();
     265    }
     266
     267    /**
     268        @brief Called if the text in the input-line has changed.
     269    */
     270    void InGameConsole::inputChanged()
     271    {
    288272        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     }
     273            this->print(Shell::getInstance().getInput(), 0);
     274
     275        if (Shell::getInstance().getInput() == "" || Shell::getInstance().getInput().size() == 0)
     276            this->inputWindowStart_ = 0;
     277    }
     278
     279    /**
     280        @brief Called if the position of the cursor in the input-line has changed.
     281    */
     282    void InGameConsole::cursorChanged()
     283    {
     284        unsigned int pos = Shell::getInstance().getCursorPosition() - inputWindowStart_;
     285        if (pos > maxCharsPerLine_)
     286            pos = maxCharsPerLine_;
     287        else if (pos < 0)
     288            pos = 0;
     289
     290        this->consoleOverlayCursor_->setCaption(std::string(pos,' ') + cursorSymbol_);
     291        this->consoleOverlayCursor_->setTop((int) this->windowH_ * this->relativeHeight - 24);
     292    }
     293
     294    /**
     295        @brief Called if the console gets closed.
     296    */
     297    void InGameConsole::exit()
     298    {
     299        this->deactivate();
     300    }
     301
     302    // ###############################
     303    // ###  other external calls   ###
     304    // ###############################
    302305
    303306    /**
     
    308311        if (this->scroll_ != 0)
    309312        {
    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);
     313            float oldTop = this->consoleOverlayContainer_->getTop();
     314
     315            if (this->scroll_ > 0)
     316            {
     317                // scrolling down
     318                // enlarge oldTop a little bit so that this exponential function
     319                // reaches 0 before infinite time has passed...
     320                float deltaScroll = (oldTop - 0.01) * dt * this->scrollSpeed_;
     321                if (oldTop - deltaScroll >= 0)
     322                {
     323                    // window has completely scrolled down
     324                    this->consoleOverlayContainer_->setTop(0);
     325                    this->scroll_ = 0;
     326                }
     327                else
     328                    this->consoleOverlayContainer_->setTop(oldTop - deltaScroll);
     329            }
     330
     331            else
     332            {
     333                // scrolling up
     334                // note: +0.01 for the same reason as when scrolling down
     335                float deltaScroll = (1.2 * this->relativeHeight + 0.01 + oldTop) * dt * this->scrollSpeed_;
     336                if (oldTop - deltaScroll <= -1.2 * this->relativeHeight)
     337                {
     338                    // window has completely scrolled up
     339                    this->consoleOverlayContainer_->setTop(-1.2 * this->relativeHeight);
     340                    this->scroll_ = 0;
     341                    this->consoleOverlay_->hide();
     342                }
     343                else
     344                    this->consoleOverlayContainer_->setTop(oldTop - deltaScroll);
    325345            }
    326346        }
     
    329349        {
    330350            this->cursor_ += dt;
    331             if (this->cursor_ >= InGameConsole::BLINK)
     351            if (this->cursor_ >= this->blinkTime)
    332352            {
    333353                this->cursor_ = 0;
    334354                bShowCursor_ = !bShowCursor_;
    335355                if (bShowCursor_)
    336                   this->consoleOverlayCursor_->show();
     356                    this->consoleOverlayCursor_->show();
    337357                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         }
     358                    this->consoleOverlayCursor_->hide();
     359            }
     360
     361            // this creates a flickering effect (extracts exactly 80% of the texture at a random location)
     362            float uRand = (rand() & 1023) / 1023.0f * 0.2f;
     363            float vRand = (rand() & 1023) / 1023.0f * 0.2f;
     364            this->consoleOverlayNoise_->setUV(uRand, vRand, 0.8f + uRand, 0.8f + vRand);
     365        }
     366    }
     367
     368    /**
     369        @brief Resizes the console elements. Call if window size changes.
     370    */
     371    void InGameConsole::resize()
     372    {
     373        this->windowW_ = GraphicsEngine::getSingleton().getWindowWidth();
     374        this->windowH_ = GraphicsEngine::getSingleton().getWindowHeight();
     375        this->consoleOverlayBorder_->setWidth((int) this->windowW_* this->relativeWidth);
     376        this->consoleOverlayBorder_->setHeight((int) this->windowH_ * this->relativeHeight);
     377        this->consoleOverlayNoise_->setWidth((int) this->windowW_ * this->relativeWidth - 10);
     378        this->consoleOverlayNoise_->setHeight((int) this->windowH_ * this->relativeHeight - 5);
     379        this->consoleOverlayNoise_->setTiling(consoleOverlayNoise_->getWidth() / 80.0f * this->noiseSize_, consoleOverlayNoise_->getHeight() / 80.0f * this->noiseSize_);
     380
     381        // now adjust the text lines...
     382        this->desiredTextWidth_ = (int) (this->windowW_ * this->relativeWidth) - 12;
     383
     384        if (LINES > 0)
     385            this->maxCharsPerLine_ = max((unsigned int)10, (unsigned int) ((float)this->desiredTextWidth_ / CHAR_WIDTH));
    460386        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 + 8, this->windowH_ * InGameConsole::REL_HEIGHT - 20);
    488     }
     387            this->maxCharsPerLine_ = 10;
     388
     389        for (int i = 0; i < LINES; i++)
     390        {
     391            this->consoleOverlayTextAreas_[i]->setWidth(this->desiredTextWidth_);
     392            this->consoleOverlayTextAreas_[i]->setTop((int) this->windowH_ * this->relativeHeight - 24 - 14*i);
     393        }
     394
     395        this->linesChanged();
     396        this->cursorChanged();
     397    }
     398
     399    // ###############################
     400    // ###    internal methods     ###
     401    // ###############################
    489402
    490403    /**
     
    544457
    545458    /**
     459        @brief Shows the InGameConsole.
     460    */
     461    void InGameConsole::activate()
     462    {
     463        if (!this->bActive_)
     464        {
     465            this->bActive_ = true;
     466            InputManager::setInputState(InputManager::IS_CONSOLE);
     467            Shell::getInstance().registerListener(this);
     468
     469            this->resize();
     470            this->linesChanged();
     471            this->cursorChanged();
     472            this->consoleOverlay_->show();
     473
     474            // scroll down
     475            this->scroll_ = 1;
     476            // the rest is done by tick
     477        }
     478    }
     479
     480    /**
     481    @brief Hides the InGameConsole.
     482    */
     483    void InGameConsole::deactivate()
     484    {
     485        if (this->bActive_)
     486        {
     487            this->bActive_ = false;
     488            InputManager::setInputState(InputManager::IS_NORMAL);
     489            Shell::getInstance().unregisterListener(this);
     490
     491            // scroll up
     492            this->scroll_ = -1;
     493            // the rest is done by tick
     494        }
     495    }
     496
     497    /**
     498        @brief Shifts all output lines one line up
     499    */
     500    void InGameConsole::shiftLines()
     501    {
     502        for (unsigned int i = LINES - 1; i > 1; --i)
     503        {
     504            this->consoleOverlayTextAreas_[i]->setCaption(this->consoleOverlayTextAreas_[i - 1]->getCaption());
     505            this->consoleOverlayTextAreas_[i]->setColourTop(this->consoleOverlayTextAreas_[i - 1]->getColourTop());
     506            this->consoleOverlayTextAreas_[i]->setColourBottom(this->consoleOverlayTextAreas_[i - 1]->getColourBottom());
     507        }
     508    }
     509
     510    void InGameConsole::colourLine(int colourcode, int index)
     511    {
     512        if (colourcode == -1)
     513        {
     514            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.90, 0.90, 0.90, 1.00));
     515            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00));
     516        }
     517        else if (colourcode == 1)
     518        {
     519            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.25, 0.25, 1.00));
     520            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00));
     521        }
     522        else if (colourcode == 2)
     523        {
     524            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.50, 0.20, 1.00));
     525            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00));
     526        }
     527        else if (colourcode == 3)
     528        {
     529            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.50, 0.50, 0.95, 1.00));
     530            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00));
     531        }
     532        else if (colourcode == 4)
     533        {
     534            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.65, 0.48, 0.44, 1.00));
     535            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00));
     536        }
     537        else if (colourcode == 5)
     538        {
     539            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.40, 0.20, 0.40, 1.00));
     540            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00));
     541        }
     542        else
     543        {
     544            this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.21, 0.69, 0.21, 1.00));
     545            this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00));
     546        }
     547    }
     548
     549    // ###############################
     550    // ###      satic methods      ###
     551    // ###############################
     552
     553    /**
     554        @brief Activates the console.
     555    */
     556    /*static*/ void InGameConsole::openConsole()
     557    {
     558        InGameConsole::getInstance().activate();
     559    }
     560
     561    /**
     562        @brief Deactivates the console.
     563    */
     564    /*static*/ void InGameConsole::closeConsole()
     565    {
     566        InGameConsole::getInstance().deactivate();
     567    }
     568
     569    /**
    546570        @brief Converts a string into an Ogre::UTFString.
    547571        @param s The string to convert
    548572        @return The converted string
    549573    */
    550     Ogre::UTFString InGameConsole::convert2UTF(std::string s)
     574    /*static*/ Ogre::UTFString InGameConsole::convert2UTF(std::string s)
    551575    {
    552576        Ogre::UTFString utf;
Note: See TracChangeset for help on using the changeset viewer.