Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5786 was 5786, checked in by bensch, 18 years ago

orxonox/trunk: key-repeat in the Shell is smoother now

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