Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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