Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5411 was 5398, checked in by bensch, 20 years ago

orxonox/trunk: fixed a bug in the allocation of Element2D

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