Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: war of the popers

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"
[5093]25#include "graphics_engine.h"
[5372]26#include "material.h"
[5093]27#include "event_handler.h"
[5129]28#include "debug.h"
[5113]29#include "class_list.h"
30
31#include "key_names.h"
[5075]32#include <stdarg.h>
33#include <stdio.h>
34
[1856]35using namespace std;
[1853]36
[5201]37SHELL_COMMAND(clear, Shell, clear)
38    ->describe("Clears the shell from unwanted lines (empties all buffers)")
39    ->setAlias("clear");
40SHELL_COMMAND(deactivate, Shell, deactivate)
41    ->describe("Deactivates the Shell. (moves it into background)")
[5204]42    ->setAlias("hide");
[5208]43SHELL_COMMAND(textsize, Shell, setTextSize)
[5374]44    ->describe("Sets the size of the Text size, linespacing")
[5254]45    ->defaultValues(1, 15, 0);
[5374]46SHELL_COMMAND(textcolor, Shell, setTextColor)
47    ->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
48    ->defaultValues(4, SHELL_DEFAULT_TEXT_COLOR);
49SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
50    ->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
51    ->defaultValues(4, SHELL_DEFAULT_BACKGROUND_COLOR);
52SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
53    ->describe("sets the background image to load for the Shell");
[5254]54SHELL_COMMAND(font, Shell, setFont)
55    ->describe("Sets the font of the Shell")
56    ->defaultValues(1, SHELL_DEFAULT_FONT);
[1856]57
[3245]58/**
[4838]59 * standard constructor
[5068]60 */
61Shell::Shell ()
[3365]62{
[5072]63  this->setClassID(CL_SHELL, "Shell");
64  this->setName("Shell");
65
[5245]66  // EVENT-Handler subscription of '`' to all States.
67  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
[5425]68  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
[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;
[5781]76  this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->begin();
[5372]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;
[5420]86  this->bActive = true;
[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
[5420]96  this->deactivate();
[5335]97  // register the shell at the ShellBuffer
98  ShellBuffer::getInstance()->registerShell(this);
[5068]99}
[4320]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;
[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);
[5786]130  EventHandler::getInstance()->withUNICODE(true);
131
[5113]132  this->setRelCoorSoft2D(0, 0, 1, 5);
[5118]133
[5787]134  list<char*>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer()->end();
135  bool top = false;
[5246]136  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]137  {
[5420]138    this->bufferText[i]->setVisibility(true);
[5787]139    if (!top)
140    {
141      this->bufferText[i]->setText((*textLine), true);
142    if (textLine != ShellBuffer::getInstance()->getBuffer()->begin())
143      top = true;
144      textLine--;
145    }
[5247]146  }
[5113]147}
148
[5119]149/**
150 * deactiveates the Shell.
151 */
[5113]152void Shell::deactivate()
153{
154  if (this->bActive == false)
155    PRINTF(3)("The shell is already inactive\n");
156  this->bActive = false;
157
[5786]158  EventHandler::getInstance()->withUNICODE(false);
[5388]159  EventHandler::getInstance()->popState();
160
[5253]161  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
[5118]162
[5787]163  list<char*>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer()->end();
[5157]164  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]165  {
[5420]166    this->bufferText[i]->setVisibility(false);
[5787]167    if (textLine != ShellBuffer::getInstance()->getBuffer()->begin())
168    {
169      this->bufferText[i]->setText((*textLine), false);
170      textLine--;
171    }
[5247]172  }
[5248]173  this->bufferOffset = 0;
[5113]174}
175
[5119]176/**
[5251]177 * sets the File to load the fonts from
178 * @param fontFile the file to load the font from
[5254]179 *
180 * it is quite important, that the font pointed too really exists!
181 * (be aware that within orxonox fontFile is relative to the Data-Dir)
[5251]182 */
183void Shell::setFont(const char* fontFile)
184{
[5335]185//   if (!ResourceManager::isInDataDir(fontFile))
186//     return false;
187
[5251]188  if (this->fontFile != NULL)
189    delete[] this->fontFile;
190
191  this->fontFile = new char[strlen(fontFile)+1];
192  strcpy(this->fontFile, fontFile);
193
[5253]194  this->rebuildText();
[5251]195}
196
197/**
[5119]198 * sets the size of the text and spacing
199 * @param textSize the size of the Text in Pixels
200 * @param lineSpacing the size of the Spacing between two lines in pixels
201 *
202 * this also rebuilds the entire Text, inputLine and displayBuffer,
203 * to be accurate again.
204 */
[5113]205void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
206{
207  this->textSize = textSize;
208  this->lineSpacing = lineSpacing;
[5372]209  this->resetValues();
210}
[5113]211
[5372]212/**
213 * sets the color of the Font.
214 * @param r: red
215 * @param g: green
216 * @param b: blue
217 * @param a: alpha-value.
218 */
219void Shell::setTextColor(float r, float g, float b, float a)
220{
221  this->textColor[0] = r;
222  this->textColor[1] = g;
223  this->textColor[2] = b;
224  this->textColor[3] = a;
225
226  this->resetValues();
227}
228
229
230/**
231 * sets the color of the Backgrond.
232 * @param r: red
233 * @param g: green
234 * @param b: blue
235 * @param a: alpha-value.
236 */
237void Shell::setBackgroundColor(float r, float g, float b, float a)
238{
239  this->backgroundMaterial->setDiffuse(r, g, b);
240  this->backgroundMaterial->setTransparency(a);
241}
242
243/**
[5374]244 * sets a nice background image to the Shell's background
245 * @param fileName the filename of the Image to load
246 */
247void Shell::setBackgroundImage(const char* fileName)
248{
249  this->backgroundMaterial->setDiffuseMap(fileName);
250}
251
252
253/**
[5372]254 * resets the Values of all visible shell's commandos to the Shell's stored values
255 *
256 * this functions synchronizes the stored Data with the visible one.
257 */
258void Shell::resetValues()
259{
260  if (this->shellInput != NULL)
261  {
262    this->shellInput->setSize(this->textSize);
263    this->shellInput->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
264    this->shellInput->setBlending(this->textColor[3]);
[5420]265    this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
[5372]266  }
267
[5369]268  if (this->bufferText != NULL)
269  {
270    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
271    {
272      if (this->bufferText[i] != NULL)
273      {
274        this->bufferText[i]->setSize(this->textSize);
[5372]275        this->bufferText[i]->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
276        this->bufferText[i]->setBlending(this->textColor[3]);
277        this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
[5369]278      }
279    }
280  }
281  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5113]282}
283
[5127]284/**
285 * rebuilds the Text's
286 *
287 * use this function, if you changed the Font/Size or something else.
288 */
[5113]289void Shell::rebuildText()
290{
[5251]291  this->shellInput->setFont(this->fontFile, this->textSize);
[5179]292  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
[5397]293  if (shellInput->getParent2D() != this)
[5253]294    this->shellInput->setParent2D(this);
[5113]295
296  this->setBufferDisplaySize(this->bufferDisplaySize);
297}
298
[5074]299/**
300 * sets The count of Lines to display in the buffer.
301 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
302 */
[5072]303void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
304{
[5251]305  Text** bufferText = this->bufferText;
306  this->bufferText = NULL;
307  if (bufferText != NULL)
[5072]308  {
[5080]309    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5251]310      delete bufferText[i];
311    delete[] bufferText;
[5072]312  }
[5080]313
[5787]314  list<char*>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer()->end();
[5251]315  bufferText = new Text*[bufferDisplaySize];
[5080]316  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]317  {
[5767]318    bufferText[i] = new Text(this->fontFile, this->textSize);
[5251]319    bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
320    bufferText[i]->setParent2D(this);
[5787]321    if(textLine != ShellBuffer::getInstance()->getBuffer()->begin())
322    {
323      bufferText[i]->setText(*textLine);
324      textLine--;
325    }
[5072]326  }
[5113]327  this->bufferDisplaySize = bufferDisplaySize;
[5111]328
[5251]329  this->bufferText = bufferText;
[5113]330  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5072]331}
[5068]332
333/**
334 * deletes all the Buffers
335 */
[5175]336void Shell::flush()
[5068]337{
[5072]338  // remove all chars from the BufferTexts.
[5251]339  if (this->bufferText != NULL)
[5125]340    for (int i = 0; i < this->bufferDisplaySize; i++)
[5080]341    {
[5125]342      this->bufferText[i]->setText(NULL, true);
[5080]343    }
[5246]344
345    ShellBuffer::getInstance()->flush();
346    // BUFFER FLUSHING
[5068]347}
348
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  */
[5383]376    lastText->setRelDir2D(-90);
377    lastText->setRelDirSoft2D(0, 20);
[5376]378    lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
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
388/**
[5783]389 * moves the Display buffer (up + or down - )
[5246]390 * @param lineCount the count by which to shift the InputBuffer.
[5783]391 *
392 * @todo make this work
[5246]393 */
394void Shell::moveDisplayBuffer(int lineCount)
395{
[5783]396  if (this->bufferOffset == 0)
397   {
398     this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->end();
399//     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5248]400//       this->bufferIterator->prevStep();
[5783]401   }
402
403  // boundraries
404  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer()->size())
405    lineCount = (int)ShellBuffer::getInstance()->getBuffer()->size()- this->bufferOffset;
406  else if (this->bufferOffset + lineCount < 0)
407    lineCount = -bufferOffset;
408  this->bufferOffset += lineCount;
409
410  // moving the iterator to the right position
411  int move = 0;
412  while (move != lineCount)
413  {
414    if (move < lineCount)
415    {
416      ++move;
417      this->bufferIterator--;
418    }
419    else
420    {
421      --move;
422      this->bufferIterator++;
423    }
424  }
425  // redisplay the buffers
426  list<char*>::const_iterator it = this->bufferIterator;
427  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
428  {
429    this->bufferText[i]->setText((*it), false);
430    it--;
431  }
[5246]432}
433
434/**
[5166]435 * clears the Shell (empties all buffers)
436 */
[5130]437void Shell::clear()
438{
[5175]439  this->flush();
440  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]441}
442
[5069]443/**
444 * listens for some event
445 * @param event the Event happened
446 */
447void Shell::process(const Event &event)
448{
[5093]449  if (event.bPressed)
450  {
[5425]451    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
[5093]452    {
[5388]453      if (this->bActive == false)
[5113]454        this->activate();
[5093]455      else
[5113]456        this->deactivate();
[5093]457    }
[5246]458    else if (event.type == SDLK_PAGEUP)
459    {
[5248]460      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
[5246]461    }
462    else if (event.type == SDLK_PAGEDOWN)
463    {
[5248]464      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
[5246]465    }
[5093]466  }
[5069]467}
468
[5068]469/**
470 * displays the Shell
471 */
472void Shell::draw() const
473{
[5099]474  // transform for alignment.
475  // setting the Blending effects
476
[5372]477  this->backgroundMaterial->select();
[5099]478
[5158]479  glBegin(GL_TRIANGLE_STRIP);
[5099]480
[5158]481  glTexCoord2f(0, 0);
[5099]482  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
483
[5158]484  glTexCoord2f(1, 0);
[5113]485  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]486
[5158]487  glTexCoord2f(0, 1);
488  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
489
490  glTexCoord2f(1, 1);
[5113]491  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]492
493  glEnd();
[5068]494}
495
[5120]496///////////////////////
497// HELPER FUNCTIONS  //
498///////////////////////
[5166]499
500/**
501 * calculates the position of a Buffer-Display Line
502 * @param lineNumber the lineNumber from the bottom to calculate the position from
503 * @returns the Position of the Line.
504 */
[5120]505Vector Shell::calculateLinePosition(unsigned int lineNumber)
506{
[5420]507  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize, 0);
[5120]508}
509
510
511
[5113]512/**
[5068]513 * displays some nice output from the Shell
514 */
515void Shell::debug() const
516{
[5119]517  PRINT(3)("Debugging output to console (not this shell)\n");
518
[5180]519//   if (this->pressedKey != SDLK_FIRST)
520//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]521
522
[5177]523  ShellBuffer::getInstance()->debug();
[5068]524}
[5166]525
526// void Shell::testI (int i)
527// {
528//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
529// }
530//
531// void Shell::testS (const char* s)
532// {
533//   PRINTF(3)("This is the Test for one String '%s'\n", s);
534// }
535//
536// void Shell::testB (bool b)
537// {
538//   PRINTF(3)("This is the Test for one Bool: ");
539//   if (b)
540//     PRINTF(3)("true\n");
541//   else
542//     PRINTF(3)("false\n");
543// }
544//
545// void Shell::testF (float f)
546// {
547//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
548// }
549//
550// void Shell::testSF (const char* s, float f)
551// {
552//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
553// }
Note: See TracBrowser for help on using the repository browser.