Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1139 was 1139, checked in by FelixSchulthess, 16 years ago

adjustments

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