Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moving up and down through the buffer with [PageUp] and [PageDown] enabled

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