Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell.cc @ 5180

Last change on this file since 5180 was 5180, checked in by bensch, 19 years ago

orxonox/trunk: Shell Input is totally out of shell.cc/h

File size: 8.6 KB
RevLine 
[4744]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5068]18#include "shell.h"
[5129]19#include "shell_command.h"
[5175]20#include "shell_buffer.h"
[5179]21#include "shell_input.h"
[1853]22
[5175]23
[5072]24#include "text_engine.h"
25#include "list.h"
[5093]26#include "graphics_engine.h"
27#include "event_handler.h"
[5129]28#include "debug.h"
[5113]29#include "class_list.h"
30
31#include "key_names.h"
[5075]32#include <stdarg.h>
33#include <stdio.h>
34
[1856]35using namespace std;
[1853]36
[5164]37SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)");
[5172]38SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)");
[1856]39
[3245]40/**
[4838]41 * standard constructor
[5068]42 */
43Shell::Shell ()
[3365]44{
[5072]45  this->setClassID(CL_SHELL, "Shell");
46  this->setName("Shell");
47
[5175]48  this->setAbsCoor2D(3, -400);
49  this->textSize = 15;
50  this->lineSpacing = 5;
[5113]51  this->bActive = false;
[5072]52
[5129]53
[5175]54  // BUFFER
55  this->bufferText = NULL;
56  this->setBufferDisplaySize(10);
[5080]57
[5175]58  // INPUT LINE
[5179]59  this->shellInput = new ShellInput;
[5175]60  //this->commandList = new tList<ShellCommand>;
[5093]61
[5113]62  this->rebuildText();
63  this->completionList = NULL;
[5093]64
[5180]65  // EVENT-Handler subscription of '`' to all States.
66  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
67
[5068]68}
[4320]69
[5068]70Shell* Shell::singletonRef = NULL;
[1853]71
[3245]72/**
[4838]73 * standard deconstructor
[5068]74 */
75Shell::~Shell ()
[3543]76{
[5099]77  // delete the displayable Buffers
[5080]78  for (int i = 0; i < this->bufferDisplaySize; i++)
79    delete this->bufferText[i];
[5113]80  delete[] this->bufferText;
[5093]81
[5099]82  // delete the inputLine
[5180]83  delete this->shellInput;
[5079]84
[5068]85  Shell::singletonRef = NULL;
[3543]86}
[5068]87
[5119]88/**
89 * activates the shell
90 *
91 * This also feeds the Last few lines from the main buffers into the displayBuffer
92 */
[5113]93void Shell::activate()
94{
95  if (this->bActive == true)
96    PRINTF(3)("The shell is already active\n");
97  this->bActive = true;
98
99  EventHandler::getInstance()->setState(ES_SHELL);
100  this->setRelCoorSoft2D(0, 0, 1, 5);
[5118]101
[5175]102  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
[5118]103  for (int i = 0; i < this->bufferDisplaySize; i++)
[5175]104    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true);
[5113]105}
106
[5119]107/**
108 * deactiveates the Shell.
109 */
[5113]110void Shell::deactivate()
111{
112  if (this->bActive == false)
113    PRINTF(3)("The shell is already inactive\n");
114  this->bActive = false;
115
116  EventHandler::getInstance()->setState(ES_GAME);
117  this->setRelCoorSoft2D(0, -400, 1, 5);
[5118]118
[5175]119  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
[5157]120  for (int i = 0; i < this->bufferDisplaySize; i++)
[5175]121    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false);
[5113]122}
123
[5158]124
[5119]125/**
126 * sets the size of the text and spacing
127 * @param textSize the size of the Text in Pixels
128 * @param lineSpacing the size of the Spacing between two lines in pixels
129 *
130 * this also rebuilds the entire Text, inputLine and displayBuffer,
131 * to be accurate again.
132 */
[5113]133void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
134{
135  this->textSize = textSize;
136  this->lineSpacing = lineSpacing;
137
138  this->rebuildText();
139}
140
[5127]141/**
142 * rebuilds the Text's
143 *
144 * use this function, if you changed the Font/Size or something else.
145 */
[5113]146void Shell::rebuildText()
147{
[5179]148  this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize);
149  this->shellInput->setColor(1, 0, 0);
150  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
151  this->shellInput->setText(NULL);
152  this->shellInput->setParent2D(this);
153  this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize);
[5113]154
155  this->setBufferDisplaySize(this->bufferDisplaySize);
156}
157
[5074]158/**
159 * sets The count of Lines to display in the buffer.
160 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
161 */
[5072]162void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
163{
[5080]164  if (this->bufferText != NULL)
[5072]165  {
[5080]166    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
167      delete this->bufferText[i];
[5113]168    delete[] this->bufferText;
[5072]169  }
[5080]170
171  this->bufferText = new Text*[bufferDisplaySize];
172  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]173  {
[5122]174    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC);
[5121]175    this->bufferText[i]->setColor(1, 0, 0);
[5080]176    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
[5120]177    this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
[5080]178    this->bufferText[i]->setText(NULL);
[5089]179    this->bufferText[i]->setParent2D(this);
[5072]180  }
[5113]181  this->bufferDisplaySize = bufferDisplaySize;
[5111]182
[5113]183  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5072]184}
[5068]185
186/**
187 * deletes all the Buffers
188 */
[5175]189void Shell::flush()
[5068]190{
[5072]191  // remove all chars from the BufferTexts.
[5080]192  if (this->bufferText)
[5125]193    for (int i = 0; i < this->bufferDisplaySize; i++)
[5080]194    {
[5125]195      this->bufferText[i]->setText(NULL, true);
[5080]196    }
[5068]197
[5072]198
[5175]199    // BUFFER FLUSHING
[5068]200}
201
202/**
[5118]203 * prints out some text to the input-buffers
204 * @param text the text to output.
205 */
206void Shell::printToDisplayBuffer(const char* text)
207{
208  if(likely(bufferText != NULL))
209  {
210    Text* lastText = this->bufferText[this->bufferDisplaySize-1];
[5113]211
[5118]212    Text* swapText;
213    Text* moveText = this->bufferText[0];
[5120]214    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
[5118]215    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
216    {
217      if ( i < this->bufferDisplaySize-1)
[5120]218        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
[5118]219      swapText = this->bufferText[i];
220      this  ->bufferText[i] = moveText;
221      moveText = swapText;
222    }
[5120]223    lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]224    this->bufferText[0] = lastText;
225
[5122]226    this->bufferText[0]->setText(text, true);
[5118]227  }
[5068]228}
229
230/**
[5166]231 * clears the Shell (empties all buffers)
232 */
[5130]233void Shell::clear()
234{
[5175]235  this->flush();
236  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]237}
238
[5097]239
240
[5069]241/**
242 * listens for some event
243 * @param event the Event happened
244 */
245void Shell::process(const Event &event)
246{
[5093]247  if (event.bPressed)
248  {
249    if (event.type == SDLK_BACKQUOTE)
250    {
251      if (EventHandler::getInstance()->getState() == ES_GAME)
[5113]252        this->activate();
[5093]253      else
[5113]254        this->deactivate();
[5093]255    }
256  }
[5069]257}
258
[5068]259/**
260 * displays the Shell
261 */
262void Shell::draw() const
263{
[5099]264  glPushMatrix();
265  // transform for alignment.
266  // setting the Blending effects
267
268  glColor4f(0.0f, 0.0f, 0.8f, .4);
269  glEnable(GL_BLEND);
270  glDisable(GL_TEXTURE_2D);
271  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
272
[5158]273  glBindTexture(GL_TEXTURE_2D, 0);
274  glBegin(GL_TRIANGLE_STRIP);
[5099]275
[5158]276  glTexCoord2f(0, 0);
[5099]277  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
278
[5158]279  glTexCoord2f(1, 0);
[5113]280  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]281
[5158]282  glTexCoord2f(0, 1);
283  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
284
285  glTexCoord2f(1, 1);
[5113]286  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]287
288  glEnd();
[5068]289}
290
[5120]291///////////////////////
292// HELPER FUNCTIONS  //
293///////////////////////
[5166]294
295/**
296 * calculates the position of a Buffer-Display Line
297 * @param lineNumber the lineNumber from the bottom to calculate the position from
298 * @returns the Position of the Line.
299 */
[5120]300Vector Shell::calculateLinePosition(unsigned int lineNumber)
301{
[5124]302  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
[5120]303}
304
305
306
[5113]307/**
[5068]308 * displays some nice output from the Shell
309 */
310void Shell::debug() const
311{
[5119]312  PRINT(3)("Debugging output to console (not this shell)\n");
313
[5180]314//   if (this->pressedKey != SDLK_FIRST)
315//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]316
317
[5177]318  ShellBuffer::getInstance()->debug();
[5068]319}
[5166]320
321
322
323// void Shell::testI (int i)
324// {
325//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
326// }
327//
328// void Shell::testS (const char* s)
329// {
330//   PRINTF(3)("This is the Test for one String '%s'\n", s);
331// }
332//
333// void Shell::testB (bool b)
334// {
335//   PRINTF(3)("This is the Test for one Bool: ");
336//   if (b)
337//     PRINTF(3)("true\n");
338//   else
339//     PRINTF(3)("false\n");
340// }
341//
342// void Shell::testF (float f)
343// {
344//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
345// }
346//
347// void Shell::testSF (const char* s, float f)
348// {
349//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
350// }
Note: See TracBrowser for help on using the repository browser.