Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some 2d-adaptions

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