Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/lib/shell/shell.cc @ 5558

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

orxonox/branches/2d-recalc: some recalculations.. do not know it i will continue with this, as has certain disadvantages over the old approach… maybe later

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