Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented a t-Stack, for dynamic stacks, and integrated it into the Shell.

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