Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

corrected syntax

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