Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Way to load and unload Text

File size: 11.8 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"
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)
[5253]44    ->describe("Sets the size of the Text (size, linespacing)")
[5254]45    ->defaultValues(1, 15, 0);
46SHELL_COMMAND(font, Shell, setFont)
47    ->describe("Sets the font of the Shell")
48    ->defaultValues(1, SHELL_DEFAULT_FONT);
[1856]49
[3245]50/**
[4838]51 * standard constructor
[5068]52 */
53Shell::Shell ()
[3365]54{
[5072]55  this->setClassID(CL_SHELL, "Shell");
56  this->setName("Shell");
57
[5245]58  // EVENT-Handler subscription of '`' to all States.
59  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
[5246]60  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
61  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
[5206]62
[5183]63  // Element2D and generals
[5175]64  this->setAbsCoor2D(3, -400);
[5329]65  this->textSize = 20;
[5208]66  this->lineSpacing = 0;
[5113]67  this->bActive = false;
[5253]68  this->fontFile = new char[strlen(SHELL_DEFAULT_FONT)+1];
69  strcpy(this->fontFile, SHELL_DEFAULT_FONT);
[5072]70
[5175]71  // BUFFER
72  this->bufferText = NULL;
[5209]73  this->bufferDisplaySize = 10;
[5246]74  this->bufferOffset = 0;
[5248]75  this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
[5080]76
[5175]77  // INPUT LINE
[5179]78  this->shellInput = new ShellInput;
[5175]79  //this->commandList = new tList<ShellCommand>;
[5093]80
[5113]81  this->rebuildText();
[5335]82
83  // register the shell at the ShellBuffer
84  ShellBuffer::getInstance()->registerShell(this);
[5068]85}
[4320]86
[3245]87/**
[4838]88 * standard deconstructor
[5068]89 */
90Shell::~Shell ()
[3543]91{
[5335]92  ShellBuffer::getInstance()->unregisterShell(this);
93
[5099]94  // delete the displayable Buffers
[5227]95  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5080]96    delete this->bufferText[i];
[5113]97  delete[] this->bufferText;
[5248]98  delete this->bufferIterator;
[5251]99  delete[] this->fontFile;
[5099]100  // delete the inputLine
[5180]101  delete this->shellInput;
[3543]102}
[5068]103
[5119]104/**
105 * activates the shell
106 *
107 * This also feeds the Last few lines from the main buffers into the displayBuffer
108 */
[5113]109void Shell::activate()
110{
111  if (this->bActive == true)
112    PRINTF(3)("The shell is already active\n");
113  this->bActive = true;
114
115  EventHandler::getInstance()->setState(ES_SHELL);
116  this->setRelCoorSoft2D(0, 0, 1, 5);
[5118]117
[5246]118  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
119  bufferIT->lastElement();
120  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]121  {
122    this->bufferText[i]->setText(bufferIT->getCurrent(), true);
123    bufferIT->prevStep();
124  }
[5246]125  delete bufferIT;
[5113]126}
127
[5119]128/**
129 * deactiveates the Shell.
130 */
[5113]131void Shell::deactivate()
132{
133  if (this->bActive == false)
134    PRINTF(3)("The shell is already inactive\n");
135  this->bActive = false;
136
137  EventHandler::getInstance()->setState(ES_GAME);
[5253]138  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
[5118]139
[5246]140  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
141  bufferIT->lastElement();
[5157]142  for (int i = 0; i < this->bufferDisplaySize; i++)
[5247]143  {
[5256]144    this->bufferText[i]->setText(bufferIT->getCurrent(), false);
[5247]145    bufferIT->prevStep();
146  }
[5246]147  delete bufferIT;
[5248]148
149  this->bufferOffset = 0;
[5113]150}
151
[5119]152/**
[5251]153 * sets the File to load the fonts from
154 * @param fontFile the file to load the font from
[5254]155 *
156 * it is quite important, that the font pointed too really exists!
157 * (be aware that within orxonox fontFile is relative to the Data-Dir)
[5251]158 */
159void Shell::setFont(const char* fontFile)
160{
[5335]161//   if (!ResourceManager::isInDataDir(fontFile))
162//     return false;
163
[5251]164  if (this->fontFile != NULL)
165    delete[] this->fontFile;
166
167  this->fontFile = new char[strlen(fontFile)+1];
168  strcpy(this->fontFile, fontFile);
169
[5253]170  this->rebuildText();
[5251]171}
172
173/**
[5119]174 * sets the size of the text and spacing
175 * @param textSize the size of the Text in Pixels
176 * @param lineSpacing the size of the Spacing between two lines in pixels
177 *
178 * this also rebuilds the entire Text, inputLine and displayBuffer,
179 * to be accurate again.
180 */
[5113]181void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
182{
183  this->textSize = textSize;
184  this->lineSpacing = lineSpacing;
[5251]185  this->shellInput->setFont(this->fontFile, this->textSize);
[5113]186
[5253]187  this->rebuildText();
[5113]188}
189
[5127]190/**
191 * rebuilds the Text's
192 *
193 * use this function, if you changed the Font/Size or something else.
194 */
[5113]195void Shell::rebuildText()
196{
[5251]197  this->shellInput->setFont(this->fontFile, this->textSize);
[5179]198  this->shellInput->setColor(1, 0, 0);
199  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
[5253]200  if (shellInput->getParent() != this)
201    this->shellInput->setParent2D(this);
[5179]202  this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize);
[5113]203
204  this->setBufferDisplaySize(this->bufferDisplaySize);
205}
206
[5074]207/**
208 * sets The count of Lines to display in the buffer.
209 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
210 */
[5072]211void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
212{
[5251]213  Text** bufferText = this->bufferText;
214  this->bufferText = NULL;
215  if (bufferText != NULL)
[5072]216  {
[5080]217    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
[5251]218      delete bufferText[i];
219    delete[] bufferText;
[5072]220  }
[5080]221
[5253]222  tIterator<char>* it = ShellBuffer::getInstance()->getBuffer()->getIterator();
223  char* text = it->lastElement();
[5251]224  bufferText = new Text*[bufferDisplaySize];
[5080]225  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]226  {
[5344]227    bufferText[i] = new Text(this->fontFile, this->textSize, TEXT_RENDER_DYNAMIC);
[5251]228    bufferText[i]->setColor(1, 0, 0);
229    bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
230    bufferText[i]->setRelCoor2D(calculateLinePosition(i));
[5253]231    bufferText[i]->setText(text);
[5251]232    bufferText[i]->setParent2D(this);
[5253]233    text = it->prevElement();
[5072]234  }
[5253]235  delete it;
[5113]236  this->bufferDisplaySize = bufferDisplaySize;
[5111]237
[5251]238  this->bufferText = bufferText;
[5113]239  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5072]240}
[5068]241
242/**
243 * deletes all the Buffers
244 */
[5175]245void Shell::flush()
[5068]246{
[5072]247  // remove all chars from the BufferTexts.
[5251]248  if (this->bufferText != NULL)
[5125]249    for (int i = 0; i < this->bufferDisplaySize; i++)
[5080]250    {
[5125]251      this->bufferText[i]->setText(NULL, true);
[5080]252    }
[5246]253
254    ShellBuffer::getInstance()->flush();
255    // BUFFER FLUSHING
[5068]256}
257
258/**
[5118]259 * prints out some text to the input-buffers
260 * @param text the text to output.
261 */
262void Shell::printToDisplayBuffer(const char* text)
263{
264  if(likely(bufferText != NULL))
265  {
266    Text* lastText = this->bufferText[this->bufferDisplaySize-1];
[5113]267
[5118]268    Text* swapText;
269    Text* moveText = this->bufferText[0];
[5120]270    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
[5118]271    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
272    {
273      if ( i < this->bufferDisplaySize-1)
[5120]274        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
[5118]275      swapText = this->bufferText[i];
276      this  ->bufferText[i] = moveText;
277      moveText = swapText;
278    }
[5120]279    lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]280    this->bufferText[0] = lastText;
281
[5122]282    this->bufferText[0]->setText(text, true);
[5118]283  }
[5068]284}
285
286/**
[5246]287 * moves the Display buffer (up or down)
288 * @param lineCount the count by which to shift the InputBuffer.
289 */
290void Shell::moveDisplayBuffer(int lineCount)
291{
[5248]292  if (!this->bufferIterator->compareListPointer(ShellBuffer::getInstance()->getBuffer()))
293  {
294    delete this->bufferIterator;
295    this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
296  }
[5246]297
[5248]298  if (this->bufferOffset == 0)
299   {
300     this->bufferIterator->lastElement();
301//     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
302//       this->bufferIterator->prevStep();
303   }
[5246]304
[5248]305  // boundraries
306  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer()->getSize())
307    lineCount = (int)ShellBuffer::getInstance()->getBuffer()->getSize()- this->bufferOffset;
308  else if (this->bufferOffset + lineCount < 0)
309    lineCount = -bufferOffset;
310  this->bufferOffset += lineCount;
311
312  // moving the iterator to the right position
313  int move = 0;
314  while (move != lineCount)
315  {
316    if (move < lineCount)
317    {
318      ++move;
319      this->bufferIterator->prevStep();
320    }
321    else
322    {
323      --move;
324      this->bufferIterator->nextStep();
325    }
326  }
327  // redisplay the buffers
328  tIterator<char> it = *this->bufferIterator;
329  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
330  {
331    this->bufferText[i]->setText(it.getCurrent(), false);
332    it.prevStep();
333  }
[5246]334}
335
336/**
[5166]337 * clears the Shell (empties all buffers)
338 */
[5130]339void Shell::clear()
340{
[5175]341  this->flush();
342  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]343}
344
[5069]345/**
346 * listens for some event
347 * @param event the Event happened
348 */
349void Shell::process(const Event &event)
350{
[5093]351  if (event.bPressed)
352  {
353    if (event.type == SDLK_BACKQUOTE)
354    {
355      if (EventHandler::getInstance()->getState() == ES_GAME)
[5113]356        this->activate();
[5093]357      else
[5113]358        this->deactivate();
[5093]359    }
[5246]360    else if (event.type == SDLK_PAGEUP)
361    {
[5248]362      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
[5246]363    }
364    else if (event.type == SDLK_PAGEDOWN)
365    {
[5248]366      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
[5246]367    }
[5093]368  }
[5069]369}
370
[5068]371/**
372 * displays the Shell
373 */
374void Shell::draw() const
375{
[5099]376  glPushMatrix();
377  // transform for alignment.
378  // setting the Blending effects
379
380  glColor4f(0.0f, 0.0f, 0.8f, .4);
381  glEnable(GL_BLEND);
382  glDisable(GL_TEXTURE_2D);
383  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
384
[5158]385  glBindTexture(GL_TEXTURE_2D, 0);
386  glBegin(GL_TRIANGLE_STRIP);
[5099]387
[5158]388  glTexCoord2f(0, 0);
[5099]389  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
390
[5158]391  glTexCoord2f(1, 0);
[5113]392  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]393
[5158]394  glTexCoord2f(0, 1);
395  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
396
397  glTexCoord2f(1, 1);
[5113]398  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]399
400  glEnd();
[5068]401}
402
[5120]403///////////////////////
404// HELPER FUNCTIONS  //
405///////////////////////
[5166]406
407/**
408 * calculates the position of a Buffer-Display Line
409 * @param lineNumber the lineNumber from the bottom to calculate the position from
410 * @returns the Position of the Line.
411 */
[5120]412Vector Shell::calculateLinePosition(unsigned int lineNumber)
413{
[5124]414  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
[5120]415}
416
417
418
[5113]419/**
[5068]420 * displays some nice output from the Shell
421 */
422void Shell::debug() const
423{
[5119]424  PRINT(3)("Debugging output to console (not this shell)\n");
425
[5180]426//   if (this->pressedKey != SDLK_FIRST)
427//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]428
429
[5177]430  ShellBuffer::getInstance()->debug();
[5068]431}
[5166]432
433// void Shell::testI (int i)
434// {
435//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
436// }
437//
438// void Shell::testS (const char* s)
439// {
440//   PRINTF(3)("This is the Test for one String '%s'\n", s);
441// }
442//
443// void Shell::testB (bool b)
444// {
445//   PRINTF(3)("This is the Test for one Bool: ");
446//   if (b)
447//     PRINTF(3)("true\n");
448//   else
449//     PRINTF(3)("false\n");
450// }
451//
452// void Shell::testF (float f)
453// {
454//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
455// }
456//
457// void Shell::testSF (const char* s, float f)
458// {
459//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
460// }
Note: See TracBrowser for help on using the repository browser.