Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: removed support for text-render-static

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