Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1241 was 1223, checked in by rgrieder, 17 years ago
  • restored compatibility with new InputManager
  • update VS files for tcl support
File size: 9.0 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 *      ...
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/ConsoleCommand.h"
43#include "core/InputManager.h"
44#include "GraphicsEngine.h"
45
46#define LINES 20
47
48namespace orxonox
49{
50    using namespace Ogre;
51
52    const float REL_WIDTH = 0.8;
53    const float REL_HEIGHT = 0.4;
54    const float BLINK = 0.25;
55
56    InGameConsole::InGameConsole(InputBuffer* ib){
57        //RegisterObject(InGameConsole);
58        ib_ = ib;
59        active = false;
60        cursor = 0.0;
61        init();
62    }
63
64    InGameConsole::~InGameConsole(void){
65        for(int i=0; i<LINES; i++) delete consoleOverlayTextAreas[i];
66        delete consoleOverlayTextAreas;
67    }
68
69    void InGameConsole::listen(){
70        if(!active) activate();
71        print(convert2UTF(this->ib_->get()));
72    }
73
74    void InGameConsole::execute(){
75        newline();
76        if (!CommandExecutor::execute(this->ib_->get())){
77            print("Error");
78            newline();
79        }
80        this->ib_->clear();
81    }
82
83    void InGameConsole::hintandcomplete(){
84        print(CommandExecutor::hint(this->ib_->get()));
85        newline();
86        this->ib_->set(CommandExecutor::complete(this->ib_->get()));
87        print(convert2UTF(this->ib_->get()));
88    }
89
90    void InGameConsole::clear(){
91        this->ib_->clear();
92    }
93
94    void InGameConsole::removeLast(){
95        this->ib_->removeLast();
96    }
97
98    void InGameConsole::exit(){
99        clear();
100        deactivate();
101        InputManager::setInputState(InputManager::IS_NORMAL);
102    }
103
104    /**
105    @brief called once by constructor
106    */
107    void InGameConsole::init(){
108        // for the beginning, don't scroll
109        scroll = 0;
110        scrollTimer = 0;
111        cursor = 0;
112
113        // create overlay and elements
114        om = &Ogre::OverlayManager::getSingleton();
115
116        // create a container
117        consoleOverlayContainer = static_cast<OverlayContainer*>(om->createOverlayElement("Panel", "container"));
118        consoleOverlayContainer->setMetricsMode(Ogre::GMM_RELATIVE);
119        consoleOverlayContainer->setPosition((1-REL_WIDTH)/2, 0);
120        consoleOverlayContainer->setDimensions(REL_WIDTH, REL_HEIGHT);
121
122        // create BorderPanel
123        consoleOverlayBorder = static_cast<BorderPanelOverlayElement*>(om->createOverlayElement("BorderPanel", "borderPanel"));
124        consoleOverlayBorder->setMetricsMode(Ogre::GMM_PIXELS);
125        consoleOverlayBorder->setMaterialName("ConsoleCenter");
126        // set parameters for border
127        consoleOverlayBorder->setBorderSize(16, 16, 0, 16);
128        consoleOverlayBorder->setBorderMaterialName("ConsoleBorder");
129        consoleOverlayBorder->setLeftBorderUV(0.0, 0.49, 0.5, 0.51);
130        consoleOverlayBorder->setRightBorderUV(0.5, 0.49, 1.0, 0.5);
131        consoleOverlayBorder->setBottomBorderUV(0.49, 0.5, 0.51, 1.0);
132        consoleOverlayBorder->setBottomLeftBorderUV(0.0, 0.5, 0.5, 1.0);
133        consoleOverlayBorder->setBottomRightBorderUV(0.5, 0.5, 1.0, 1.0);
134
135        // create the text lines
136        consoleOverlayTextAreas = new TextAreaOverlayElement*[LINES];
137        for(int i = 0; i<LINES; i++){
138            consoleOverlayTextAreas[i] = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "textArea"+Ogre::StringConverter::toString(i)));
139            consoleOverlayTextAreas[i]->setMetricsMode(Ogre::GMM_PIXELS);
140            consoleOverlayTextAreas[i]->setFontName("Console");
141            consoleOverlayTextAreas[i]->setCharHeight(20);
142            consoleOverlayTextAreas[i]->setParameter("colour_top", "0.21 0.69 0.21");
143            consoleOverlayTextAreas[i]->setLeft(8);
144            consoleOverlayTextAreas[i]->setCaption("");
145        }
146
147        // create noise
148        consoleOverlayNoise = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "noise"));
149        consoleOverlayNoise->setMetricsMode(Ogre::GMM_PIXELS);
150        consoleOverlayNoise->setPosition(5,0);
151        consoleOverlayNoise->setMaterialName("ConsoleNoise");
152
153        consoleOverlay = om->create("Console");
154        consoleOverlay->add2D(consoleOverlayContainer);
155        consoleOverlayContainer->addChild(consoleOverlayBorder);
156//comment following line to disable noise
157        consoleOverlayContainer->addChild(consoleOverlayNoise);
158        for(int i = 0; i<LINES; i++) consoleOverlayContainer->addChild(consoleOverlayTextAreas[i]);
159        resize();
160
161        // move overlay "above" the top edge of the screen
162        // we take -1.2 because the border mkes the panel bigger
163        consoleOverlayContainer->setTop(-1.2*REL_HEIGHT);
164        // show overlay
165        consoleOverlay->show();
166
167        COUT(3) << "Info: InGameConsole initialized" << std::endl;
168    }
169
170    /**
171    @brief used to control the actual scrolling and cursor
172    */
173    void InGameConsole::tick(float dt){
174        scrollTimer += dt;
175        if(scrollTimer >= 0.01){
176            float top = consoleOverlayContainer->getTop();
177            scrollTimer = 0;
178            if(scroll!=0){
179                // scroll
180                top = top + 0.02*scroll;
181                consoleOverlayContainer->setTop(top);
182            }
183            if(top <= -1.2*REL_HEIGHT){
184                // window has completely scrolled up
185                scroll = 0;
186                consoleOverlay->hide();
187                active = false;
188            }
189            if(top >= 0){
190                // window has completely scrolled down
191                scroll = 0;
192                consoleOverlayContainer->setTop(0);
193                active = true;
194            }
195        }
196
197        cursor += dt;
198        if(cursor >= 2*BLINK) cursor = 0;
199        print(convert2UTF(this->ib_->get()));
200
201// this creates a flickering effect
202        consoleOverlayNoise->setTiling(1, rand()%5+1);
203    }
204
205    /**
206    @brief resizes the console elements. call if window size changes
207    */
208    void InGameConsole::resize(){
209        windowW = GraphicsEngine::getSingleton().getWindowWidth();
210        windowH = GraphicsEngine::getSingleton().getWindowHeight();
211        consoleOverlayBorder->setWidth((int) windowW*REL_WIDTH);
212        consoleOverlayBorder->setHeight((int) windowH*REL_HEIGHT);
213        consoleOverlayNoise->setWidth((int) windowW*REL_WIDTH - 10);
214        consoleOverlayNoise->setHeight((int) windowH*REL_HEIGHT - 5);
215        // now adjust the text lines...
216        for(int i = 0; i<LINES; i++){
217            consoleOverlayTextAreas[i]->setWidth(windowW*REL_WIDTH);
218            consoleOverlayTextAreas[i]->setTop((int)windowH*REL_HEIGHT - 24 - 16*i);
219        }
220    }
221
222    /**
223    @brief shows console
224    */
225    void InGameConsole::activate(){
226        consoleOverlay->show();
227        // just in case window size has changed...
228        resize();
229        // scroll down
230        scroll = 1;
231        // the rest is done by tick
232    }
233
234    /**
235    @brief hides console
236    */
237    void InGameConsole::deactivate(){
238        // scroll up
239        scroll = -1;
240        // the rest is done by tick
241    }
242
243    /**
244    @brief prints string to bottom line
245    @param s string to be printed
246    */
247    void InGameConsole::print(Ogre::UTFString s){
248        if(cursor>BLINK) consoleOverlayTextAreas[0]->setCaption(">" + s);
249        else consoleOverlayTextAreas[0]->setCaption(">" + s + "_");
250    }
251
252    /**
253    @brief shifts all lines up and clears the bottom line
254    */
255    void InGameConsole::newline(){
256        Ogre::UTFString line;
257        for(int i = LINES-1; i>=1; i--){
258            line = consoleOverlayTextAreas[i-1]->getCaption();
259            // don't copy the cursor...
260            int l = line.length();
261            if(!line.empty() && line.substr(l-1) == "_") line.erase(l-1);
262            consoleOverlayTextAreas[i]->setCaption(line);
263        }
264        consoleOverlayTextAreas[0]->setCaption(">");
265    }
266
267    Ogre::UTFString InGameConsole::convert2UTF(std::string s){
268        Ogre::UTFString utf;
269        int i;
270        Ogre::UTFString::code_point cp;
271        for (i=0; i<(int)s.size(); ++i){
272          cp = s[i];
273          cp &= 0xFF;
274          utf.append(1, cp);
275        }
276        return utf;
277    }
278}
Note: See TracBrowser for help on using the repository browser.