Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now the Shell's background-render pathc is in the Material

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