Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: stuff, that has been done (removed todo's)

File size: 10.8 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell.h"
19#include "shell_command.h"
20#include "shell_buffer.h"
21#include "shell_input.h"
22
23
24#include "text_engine.h"
25#include "list.h"
26#include "graphics_engine.h"
27#include "event_handler.h"
28#include "debug.h"
29#include "class_list.h"
30
31#include "key_names.h"
32#include <stdarg.h>
33#include <stdio.h>
34
35using namespace std;
36
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)")
42    ->setAlias("hide");
43SHELL_COMMAND(textsize, Shell, setTextSize)
44    ->describe("Sets the size of the Text (size, linespacing)");
45
46/**
47 * standard constructor
48 */
49Shell::Shell ()
50{
51  this->setClassID(CL_SHELL, "Shell");
52  this->setName("Shell");
53
54  // register the shell at the ShellBuffer
55  ShellBuffer::getInstance()->registerShell(this);
56  // EVENT-Handler subscription of '`' to all States.
57  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
58  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
59  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
60
61  // Element2D and generals
62  this->setAbsCoor2D(3, -400);
63  this->textSize = 15;
64  this->lineSpacing = 0;
65  this->bActive = false;
66
67  // BUFFER
68  this->bufferText = NULL;
69  this->bufferDisplaySize = 10;
70  this->bufferOffset = 0;
71  this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
72
73  // INPUT LINE
74  this->shellInput = new ShellInput;
75  //this->commandList = new tList<ShellCommand>;
76
77  this->rebuildText();
78}
79
80/**
81 * standard deconstructor
82 */
83Shell::~Shell ()
84{
85  // delete the displayable Buffers
86  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
87    delete this->bufferText[i];
88  delete[] this->bufferText;
89  delete this->bufferIterator;
90
91  // delete the inputLine
92  delete this->shellInput;
93  ShellBuffer::getInstance()->unregisterShell(this);
94}
95
96/**
97 * activates the shell
98 *
99 * This also feeds the Last few lines from the main buffers into the displayBuffer
100 */
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);
109
110  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
111  bufferIT->lastElement();
112  for (int i = 0; i < this->bufferDisplaySize; i++)
113  {
114    this->bufferText[i]->setText(bufferIT->getCurrent(), true);
115    bufferIT->prevStep();
116  }
117  delete bufferIT;
118}
119
120/**
121 * deactiveates the Shell.
122 */
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);
131
132  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
133  bufferIT->lastElement();
134  for (int i = 0; i < this->bufferDisplaySize; i++)
135  {
136    this->bufferText[i]->setText(bufferIT->getCurrent(), true);
137    bufferIT->prevStep();
138  }
139  delete bufferIT;
140
141  this->bufferOffset = 0;
142}
143
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 */
152void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
153{
154  this->textSize = textSize;
155  this->lineSpacing = lineSpacing;
156  this->shellInput->setFont("fonts/dpquake_.ttf", this->textSize);
157
158//  this->rebuildText();
159}
160
161/**
162 * rebuilds the Text's
163 *
164 * use this function, if you changed the Font/Size or something else.
165 */
166void Shell::rebuildText()
167{
168  this->shellInput->setFont("fonts/dpquake_.ttf", this->textSize);
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);
173
174  this->setBufferDisplaySize(this->bufferDisplaySize);
175}
176
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 */
181void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
182{
183  if (this->bufferText != NULL)
184  {
185    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
186      delete this->bufferText[i];
187    delete[] this->bufferText;
188  }
189
190  this->bufferText = new Text*[bufferDisplaySize];
191  for (unsigned int i = 0; i < bufferDisplaySize; i++)
192  {
193    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/dpquake_.ttf", this->textSize, TEXT_RENDER_DYNAMIC);
194    this->bufferText[i]->setColor(1, 0, 0);
195    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
196    this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
197    this->bufferText[i]->setText(NULL);
198    this->bufferText[i]->setParent2D(this);
199  }
200  this->bufferDisplaySize = bufferDisplaySize;
201
202  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
203}
204
205/**
206 * deletes all the Buffers
207 */
208void Shell::flush()
209{
210  // remove all chars from the BufferTexts.
211  if (this->bufferText)
212    for (int i = 0; i < this->bufferDisplaySize; i++)
213    {
214      this->bufferText[i]->setText(NULL, true);
215    }
216
217    ShellBuffer::getInstance()->flush();
218    // BUFFER FLUSHING
219}
220
221/**
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];
230
231    Text* swapText;
232    Text* moveText = this->bufferText[0];
233    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
234    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
235    {
236      if ( i < this->bufferDisplaySize-1)
237        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
238      swapText = this->bufferText[i];
239      this  ->bufferText[i] = moveText;
240      moveText = swapText;
241    }
242    lastText->setRelCoor2D(this->calculateLinePosition(0));
243    this->bufferText[0] = lastText;
244
245    this->bufferText[0]->setText(text, true);
246  }
247}
248
249/**
250 * moves the Display buffer (up or down)
251 * @param lineCount the count by which to shift the InputBuffer.
252 */
253void Shell::moveDisplayBuffer(int lineCount)
254{
255  if (!this->bufferIterator->compareListPointer(ShellBuffer::getInstance()->getBuffer()))
256  {
257    delete this->bufferIterator;
258    this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
259  }
260
261  if (this->bufferOffset == 0)
262   {
263     this->bufferIterator->lastElement();
264//     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
265//       this->bufferIterator->prevStep();
266   }
267
268  // boundraries
269  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer()->getSize())
270    lineCount = (int)ShellBuffer::getInstance()->getBuffer()->getSize()- this->bufferOffset;
271  else if (this->bufferOffset + lineCount < 0)
272    lineCount = -bufferOffset;
273  this->bufferOffset += lineCount;
274
275  // moving the iterator to the right position
276  int move = 0;
277  while (move != lineCount)
278  {
279    if (move < lineCount)
280    {
281      ++move;
282      this->bufferIterator->prevStep();
283    }
284    else
285    {
286      --move;
287      this->bufferIterator->nextStep();
288    }
289  }
290  // redisplay the buffers
291  tIterator<char> it = *this->bufferIterator;
292  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
293  {
294    this->bufferText[i]->setText(it.getCurrent(), false);
295    it.prevStep();
296  }
297}
298
299/**
300 * clears the Shell (empties all buffers)
301 */
302void Shell::clear()
303{
304  this->flush();
305  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
306}
307
308/**
309 * listens for some event
310 * @param event the Event happened
311 */
312void Shell::process(const Event &event)
313{
314  if (event.bPressed)
315  {
316    if (event.type == SDLK_BACKQUOTE)
317    {
318      if (EventHandler::getInstance()->getState() == ES_GAME)
319        this->activate();
320      else
321        this->deactivate();
322    }
323    else if (event.type == SDLK_PAGEUP)
324    {
325      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
326    }
327    else if (event.type == SDLK_PAGEDOWN)
328    {
329      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
330    }
331  }
332}
333
334/**
335 * displays the Shell
336 */
337void Shell::draw() const
338{
339  glPushMatrix();
340  // transform for alignment.
341  // setting the Blending effects
342
343  glColor4f(0.0f, 0.0f, 0.8f, .4);
344  glEnable(GL_BLEND);
345  glDisable(GL_TEXTURE_2D);
346  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
347
348  glBindTexture(GL_TEXTURE_2D, 0);
349  glBegin(GL_TRIANGLE_STRIP);
350
351  glTexCoord2f(0, 0);
352  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
353
354  glTexCoord2f(1, 0);
355  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
356
357  glTexCoord2f(0, 1);
358  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
359
360  glTexCoord2f(1, 1);
361  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
362
363  glEnd();
364}
365
366///////////////////////
367// HELPER FUNCTIONS  //
368///////////////////////
369
370/**
371 * calculates the position of a Buffer-Display Line
372 * @param lineNumber the lineNumber from the bottom to calculate the position from
373 * @returns the Position of the Line.
374 */
375Vector Shell::calculateLinePosition(unsigned int lineNumber)
376{
377  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
378}
379
380
381
382/**
383 * displays some nice output from the Shell
384 */
385void Shell::debug() const
386{
387  PRINT(3)("Debugging output to console (not this shell)\n");
388
389//   if (this->pressedKey != SDLK_FIRST)
390//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
391
392
393  ShellBuffer::getInstance()->debug();
394}
395
396// void Shell::testI (int i)
397// {
398//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
399// }
400//
401// void Shell::testS (const char* s)
402// {
403//   PRINTF(3)("This is the Test for one String '%s'\n", s);
404// }
405//
406// void Shell::testB (bool b)
407// {
408//   PRINTF(3)("This is the Test for one Bool: ");
409//   if (b)
410//     PRINTF(3)("true\n");
411//   else
412//     PRINTF(3)("false\n");
413// }
414//
415// void Shell::testF (float f)
416// {
417//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
418// }
419//
420// void Shell::testSF (const char* s, float f)
421// {
422//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
423// }
Note: See TracBrowser for help on using the repository browser.