Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: safer shell, better key-handling

File size: 14.4 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
[5344]24#include "text.h"
[5072]25#include "list.h"
[5093]26#include "graphics_engine.h"
[5372]27#include "material.h"
[5093]28#include "event_handler.h"
[5129]29#include "debug.h"
[5113]30#include "class_list.h"
31
32#include "key_names.h"
[5075]33#include <stdarg.h>
34#include <stdio.h>
35
[1856]36using namespace std;
[1853]37
[5201]38SHELL_COMMAND(clear, Shell, clear)
39    ->describe("Clears the shell from unwanted lines (empties all buffers)")
40    ->setAlias("clear");
41SHELL_COMMAND(deactivate, Shell, deactivate)
42    ->describe("Deactivates the Shell. (moves it into background)")
[5204]43    ->setAlias("hide");
[5208]44SHELL_COMMAND(textsize, Shell, setTextSize)
[5374]45    ->describe("Sets the size of the Text size, linespacing")
[5254]46    ->defaultValues(1, 15, 0);
[5374]47SHELL_COMMAND(textcolor, Shell, setTextColor)
48    ->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
49    ->defaultValues(4, SHELL_DEFAULT_TEXT_COLOR);
50SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
51    ->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
52    ->defaultValues(4, SHELL_DEFAULT_BACKGROUND_COLOR);
53SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
54    ->describe("sets the background image to load for the Shell");
[5254]55SHELL_COMMAND(font, Shell, setFont)
56    ->describe("Sets the font of the Shell")
57    ->defaultValues(1, SHELL_DEFAULT_FONT);
[1856]58
[3245]59/**
[4838]60 * standard constructor
[5068]61 */
62Shell::Shell ()
[3365]63{
[5072]64  this->setClassID(CL_SHELL, "Shell");
65  this->setName("Shell");
66
[5245]67  // EVENT-Handler subscription of '`' to all States.
68  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
[5425]69  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
[5246]70  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
71  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
[5206]72
[5372]73  // BUFFER
74  this->bufferText = NULL;
75  this->bufferDisplaySize = 10;
76  this->bufferOffset = 0;
[5781]77  this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->begin();
[5372]78
79  // INPUT LINE
80  this->shellInput = new ShellInput;
81
82  this->backgroundMaterial = new Material;
[5183]83  // Element2D and generals
[5175]84  this->setAbsCoor2D(3, -400);
[5329]85  this->textSize = 20;
[5208]86  this->lineSpacing = 0;
[5420]87  this->bActive = true;
[5253]88  this->fontFile = new char[strlen(SHELL_DEFAULT_FONT)+1];
89  strcpy(this->fontFile, SHELL_DEFAULT_FONT);
[5072]90
[5080]91
[5113]92  this->rebuildText();
[5374]93
[5372]94  this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
95  this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
[5335]96
[5420]97  this->deactivate();
[5335]98  // register the shell at the ShellBuffer
99  ShellBuffer::getInstance()->registerShell(this);
[5068]100}
[4320]101
[3245]102/**
[4838]103 * standard deconstructor
[5068]104 */
105Shell::~Shell ()
[3543]106{
[5335]107  ShellBuffer::getInstance()->unregisterShell(this);
108
[5099]109  // delete the displayable Buffers
[5227]110  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5080]111    delete this->bufferText[i];
[5113]112  delete[] this->bufferText;
[5251]113  delete[] this->fontFile;
[5099]114  // delete the inputLine
[5180]115  delete this->shellInput;
[5372]116  delete this->backgroundMaterial;
[3543]117}
[5068]118
[5119]119/**
120 * activates the shell
121 *
122 * This also feeds the Last few lines from the main buffers into the displayBuffer
123 */
[5113]124void Shell::activate()
125{
126  if (this->bActive == true)
127    PRINTF(3)("The shell is already active\n");
128  this->bActive = true;
129
[5388]130  EventHandler::getInstance()->pushState(ES_SHELL);
[5786]131  EventHandler::getInstance()->withUNICODE(true);
132
[5113]133  this->setRelCoorSoft2D(0, 0, 1, 5);
[5118]134
[5787]135  list<char*>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer()->end();
136  bool top = false;
[5246]137  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]138  {
[5420]139    this->bufferText[i]->setVisibility(true);
[5787]140    if (!top)
141    {
142      this->bufferText[i]->setText((*textLine), true);
143    if (textLine != ShellBuffer::getInstance()->getBuffer()->begin())
144      top = true;
145      textLine--;
146    }
[5247]147  }
[5113]148}
149
[5119]150/**
151 * deactiveates the Shell.
152 */
[5113]153void Shell::deactivate()
154{
155  if (this->bActive == false)
156    PRINTF(3)("The shell is already inactive\n");
157  this->bActive = false;
158
[5786]159  EventHandler::getInstance()->withUNICODE(false);
[5388]160  EventHandler::getInstance()->popState();
161
[5253]162  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
[5118]163
[5787]164  list<char*>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer()->end();
[5157]165  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]166  {
[5420]167    this->bufferText[i]->setVisibility(false);
[5787]168    if (textLine != ShellBuffer::getInstance()->getBuffer()->begin())
169    {
170      this->bufferText[i]->setText((*textLine), false);
171      textLine--;
172    }
[5247]173  }
[5248]174  this->bufferOffset = 0;
[5113]175}
176
[5119]177/**
[5251]178 * sets the File to load the fonts from
179 * @param fontFile the file to load the font from
[5254]180 *
181 * it is quite important, that the font pointed too really exists!
182 * (be aware that within orxonox fontFile is relative to the Data-Dir)
[5251]183 */
184void Shell::setFont(const char* fontFile)
185{
[5335]186//   if (!ResourceManager::isInDataDir(fontFile))
187//     return false;
188
[5251]189  if (this->fontFile != NULL)
190    delete[] this->fontFile;
191
192  this->fontFile = new char[strlen(fontFile)+1];
193  strcpy(this->fontFile, fontFile);
194
[5253]195  this->rebuildText();
[5251]196}
197
198/**
[5119]199 * sets the size of the text and spacing
200 * @param textSize the size of the Text in Pixels
201 * @param lineSpacing the size of the Spacing between two lines in pixels
202 *
203 * this also rebuilds the entire Text, inputLine and displayBuffer,
204 * to be accurate again.
205 */
[5113]206void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
207{
208  this->textSize = textSize;
209  this->lineSpacing = lineSpacing;
[5372]210  this->resetValues();
211}
[5113]212
[5372]213/**
214 * sets the color of the Font.
215 * @param r: red
216 * @param g: green
217 * @param b: blue
218 * @param a: alpha-value.
219 */
220void Shell::setTextColor(float r, float g, float b, float a)
221{
222  this->textColor[0] = r;
223  this->textColor[1] = g;
224  this->textColor[2] = b;
225  this->textColor[3] = a;
226
227  this->resetValues();
228}
229
230
231/**
232 * sets the color of the Backgrond.
233 * @param r: red
234 * @param g: green
235 * @param b: blue
236 * @param a: alpha-value.
237 */
238void Shell::setBackgroundColor(float r, float g, float b, float a)
239{
240  this->backgroundMaterial->setDiffuse(r, g, b);
241  this->backgroundMaterial->setTransparency(a);
242}
243
244/**
[5374]245 * sets a nice background image to the Shell's background
246 * @param fileName the filename of the Image to load
247 */
248void Shell::setBackgroundImage(const char* fileName)
249{
250  this->backgroundMaterial->setDiffuseMap(fileName);
251}
252
253
254/**
[5372]255 * resets the Values of all visible shell's commandos to the Shell's stored values
256 *
257 * this functions synchronizes the stored Data with the visible one.
258 */
259void Shell::resetValues()
260{
261  if (this->shellInput != NULL)
262  {
263    this->shellInput->setSize(this->textSize);
264    this->shellInput->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
265    this->shellInput->setBlending(this->textColor[3]);
[5420]266    this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
[5372]267  }
268
[5369]269  if (this->bufferText != NULL)
270  {
271    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
272    {
273      if (this->bufferText[i] != NULL)
274      {
275        this->bufferText[i]->setSize(this->textSize);
[5372]276        this->bufferText[i]->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
277        this->bufferText[i]->setBlending(this->textColor[3]);
278        this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
[5369]279      }
280    }
281  }
282  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5113]283}
284
[5127]285/**
286 * rebuilds the Text's
287 *
288 * use this function, if you changed the Font/Size or something else.
289 */
[5113]290void Shell::rebuildText()
291{
[5251]292  this->shellInput->setFont(this->fontFile, this->textSize);
[5179]293  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
[5397]294  if (shellInput->getParent2D() != this)
[5253]295    this->shellInput->setParent2D(this);
[5113]296
297  this->setBufferDisplaySize(this->bufferDisplaySize);
298}
299
[5074]300/**
301 * sets The count of Lines to display in the buffer.
302 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
303 */
[5072]304void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
305{
[5251]306  Text** bufferText = this->bufferText;
307  this->bufferText = NULL;
308  if (bufferText != NULL)
[5072]309  {
[5080]310    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5251]311      delete bufferText[i];
312    delete[] bufferText;
[5072]313  }
[5080]314
[5787]315  list<char*>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer()->end();
[5251]316  bufferText = new Text*[bufferDisplaySize];
[5080]317  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]318  {
[5767]319    bufferText[i] = new Text(this->fontFile, this->textSize);
[5251]320    bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
321    bufferText[i]->setParent2D(this);
[5787]322    if(textLine != ShellBuffer::getInstance()->getBuffer()->begin())
323    {
324      bufferText[i]->setText(*textLine);
325      textLine--;
326    }
[5072]327  }
[5113]328  this->bufferDisplaySize = bufferDisplaySize;
[5111]329
[5251]330  this->bufferText = bufferText;
[5113]331  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5072]332}
[5068]333
334/**
335 * deletes all the Buffers
336 */
[5175]337void Shell::flush()
[5068]338{
[5072]339  // remove all chars from the BufferTexts.
[5251]340  if (this->bufferText != NULL)
[5125]341    for (int i = 0; i < this->bufferDisplaySize; i++)
[5080]342    {
[5125]343      this->bufferText[i]->setText(NULL, true);
[5080]344    }
[5246]345
346    ShellBuffer::getInstance()->flush();
347    // BUFFER FLUSHING
[5068]348}
349
350/**
[5118]351 * prints out some text to the input-buffers
352 * @param text the text to output.
353 */
354void Shell::printToDisplayBuffer(const char* text)
355{
356  if(likely(bufferText != NULL))
357  {
358    Text* lastText = this->bufferText[this->bufferDisplaySize-1];
[5113]359
[5118]360    Text* swapText;
361    Text* moveText = this->bufferText[0];
[5375]362    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5118]363    {
364      if ( i < this->bufferDisplaySize-1)
[5375]365        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
[5118]366      swapText = this->bufferText[i];
[5375]367      this->bufferText[i] = moveText;
[5118]368      moveText = swapText;
369    }
[5375]370
371  /*  FANCY EFFECTS :)
[5376]372    1:
[5375]373        lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
374        lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5376]375    2:
[5377]376  */
[5383]377    lastText->setRelDir2D(-90);
378    lastText->setRelDirSoft2D(0, 20);
[5376]379    lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
380    lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5377]381
382 //   lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]383    this->bufferText[0] = lastText;
384
[5122]385    this->bufferText[0]->setText(text, true);
[5118]386  }
[5068]387}
388
389/**
[5783]390 * moves the Display buffer (up + or down - )
[5246]391 * @param lineCount the count by which to shift the InputBuffer.
[5783]392 *
393 * @todo make this work
[5246]394 */
395void Shell::moveDisplayBuffer(int lineCount)
396{
[5783]397  if (this->bufferOffset == 0)
398   {
399     this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->end();
400//     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5248]401//       this->bufferIterator->prevStep();
[5783]402   }
403
404  // boundraries
405  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer()->size())
406    lineCount = (int)ShellBuffer::getInstance()->getBuffer()->size()- this->bufferOffset;
407  else if (this->bufferOffset + lineCount < 0)
408    lineCount = -bufferOffset;
409  this->bufferOffset += lineCount;
410
411  // moving the iterator to the right position
412  int move = 0;
413  while (move != lineCount)
414  {
415    if (move < lineCount)
416    {
417      ++move;
418      this->bufferIterator--;
419    }
420    else
421    {
422      --move;
423      this->bufferIterator++;
424    }
425  }
426  // redisplay the buffers
427  list<char*>::const_iterator it = this->bufferIterator;
428  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
429  {
430    this->bufferText[i]->setText((*it), false);
431    it--;
432  }
[5246]433}
434
435/**
[5166]436 * clears the Shell (empties all buffers)
437 */
[5130]438void Shell::clear()
439{
[5175]440  this->flush();
441  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]442}
443
[5069]444/**
445 * listens for some event
446 * @param event the Event happened
447 */
448void Shell::process(const Event &event)
449{
[5093]450  if (event.bPressed)
451  {
[5425]452    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
[5093]453    {
[5388]454      if (this->bActive == false)
[5113]455        this->activate();
[5093]456      else
[5113]457        this->deactivate();
[5093]458    }
[5246]459    else if (event.type == SDLK_PAGEUP)
460    {
[5248]461      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
[5246]462    }
463    else if (event.type == SDLK_PAGEDOWN)
464    {
[5248]465      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
[5246]466    }
[5093]467  }
[5069]468}
469
[5068]470/**
471 * displays the Shell
472 */
473void Shell::draw() const
474{
[5099]475  glPushMatrix();
476  // transform for alignment.
477  // setting the Blending effects
478
[5372]479  this->backgroundMaterial->select();
[5099]480
[5158]481  glBegin(GL_TRIANGLE_STRIP);
[5099]482
[5158]483  glTexCoord2f(0, 0);
[5099]484  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
485
[5158]486  glTexCoord2f(1, 0);
[5113]487  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]488
[5158]489  glTexCoord2f(0, 1);
490  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
491
492  glTexCoord2f(1, 1);
[5113]493  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]494
495  glEnd();
[5068]496}
497
[5120]498///////////////////////
499// HELPER FUNCTIONS  //
500///////////////////////
[5166]501
502/**
503 * calculates the position of a Buffer-Display Line
504 * @param lineNumber the lineNumber from the bottom to calculate the position from
505 * @returns the Position of the Line.
506 */
[5120]507Vector Shell::calculateLinePosition(unsigned int lineNumber)
508{
[5420]509  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize, 0);
[5120]510}
511
512
513
[5113]514/**
[5068]515 * displays some nice output from the Shell
516 */
517void Shell::debug() const
518{
[5119]519  PRINT(3)("Debugging output to console (not this shell)\n");
520
[5180]521//   if (this->pressedKey != SDLK_FIRST)
522//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]523
524
[5177]525  ShellBuffer::getInstance()->debug();
[5068]526}
[5166]527
528// void Shell::testI (int i)
529// {
530//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
531// }
532//
533// void Shell::testS (const char* s)
534// {
535//   PRINTF(3)("This is the Test for one String '%s'\n", s);
536// }
537//
538// void Shell::testB (bool b)
539// {
540//   PRINTF(3)("This is the Test for one Bool: ");
541//   if (b)
542//     PRINTF(3)("true\n");
543//   else
544//     PRINTF(3)("false\n");
545// }
546//
547// void Shell::testF (float f)
548// {
549//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
550// }
551//
552// void Shell::testSF (const char* s, float f)
553// {
554//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
555// }
Note: See TracBrowser for help on using the repository browser.