Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5253 was 5253, checked in by bensch, 20 years ago

orxonox/trunk: real safe, and smooth now in the resize-mode of the Shell
→ no more segfaults
→ more dynamic

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