Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some very little todo's…, and Names in all files

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