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, 19 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
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    ->defaultValues(15, 0);
46
47/**
48 * standard constructor
49 */
50Shell::Shell ()
51{
52  this->setClassID(CL_SHELL, "Shell");
53  this->setName("Shell");
54
55  // register the shell at the ShellBuffer
56  ShellBuffer::getInstance()->registerShell(this);
57  // EVENT-Handler subscription of '`' to all States.
58  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
59  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
60  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
61
62  // Element2D and generals
63  this->setAbsCoor2D(3, -400);
64  this->textSize = 15;
65  this->lineSpacing = 0;
66  this->bActive = false;
67  this->fontFile = new char[strlen(SHELL_DEFAULT_FONT)+1];
68  strcpy(this->fontFile, SHELL_DEFAULT_FONT);
69
70  // BUFFER
71  this->bufferText = NULL;
72  this->bufferDisplaySize = 10;
73  this->bufferOffset = 0;
74  this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
75
76  // INPUT LINE
77  this->shellInput = new ShellInput;
78  //this->commandList = new tList<ShellCommand>;
79
80  this->rebuildText();
81}
82
83/**
84 * standard deconstructor
85 */
86Shell::~Shell ()
87{
88  // delete the displayable Buffers
89  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
90    delete this->bufferText[i];
91  delete[] this->bufferText;
92  delete this->bufferIterator;
93  delete[] this->fontFile;
94  // delete the inputLine
95  delete this->shellInput;
96  ShellBuffer::getInstance()->unregisterShell(this);
97}
98
99/**
100 * activates the shell
101 *
102 * This also feeds the Last few lines from the main buffers into the displayBuffer
103 */
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);
112
113  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
114  bufferIT->lastElement();
115  for (int i = 0; i < this->bufferDisplaySize; i++)
116  {
117    this->bufferText[i]->setText(bufferIT->getCurrent(), true);
118    bufferIT->prevStep();
119  }
120  delete bufferIT;
121}
122
123/**
124 * deactiveates the Shell.
125 */
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);
133  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
134
135  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
136  bufferIT->lastElement();
137  for (int i = 0; i < this->bufferDisplaySize; i++)
138  {
139    this->bufferText[i]->setText(bufferIT->getCurrent(), true);
140    bufferIT->prevStep();
141  }
142  delete bufferIT;
143
144  this->bufferOffset = 0;
145}
146
147/**
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
159  this->rebuildText();
160}
161
162/**
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 */
170void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
171{
172  this->textSize = textSize;
173  this->lineSpacing = lineSpacing;
174  this->shellInput->setFont(this->fontFile, this->textSize);
175
176  this->rebuildText();
177}
178
179/**
180 * rebuilds the Text's
181 *
182 * use this function, if you changed the Font/Size or something else.
183 */
184void Shell::rebuildText()
185{
186  this->shellInput->setFont(this->fontFile, this->textSize);
187  this->shellInput->setColor(1, 0, 0);
188  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
189  if (shellInput->getParent() != this)
190    this->shellInput->setParent2D(this);
191  this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize);
192
193  this->setBufferDisplaySize(this->bufferDisplaySize);
194}
195
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 */
200void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
201{
202  Text** bufferText = this->bufferText;
203  this->bufferText = NULL;
204  if (bufferText != NULL)
205  {
206    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
207      delete bufferText[i];
208    delete[] bufferText;
209  }
210
211  tIterator<char>* it = ShellBuffer::getInstance()->getBuffer()->getIterator();
212  char* text = it->lastElement();
213  bufferText = new Text*[bufferDisplaySize];
214  for (unsigned int i = 0; i < bufferDisplaySize; i++)
215  {
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));
220    bufferText[i]->setText(text);
221    bufferText[i]->setParent2D(this);
222    text = it->prevElement();
223  }
224  delete it;
225  this->bufferDisplaySize = bufferDisplaySize;
226
227  this->bufferText = bufferText;
228  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
229}
230
231/**
232 * deletes all the Buffers
233 */
234void Shell::flush()
235{
236  // remove all chars from the BufferTexts.
237  if (this->bufferText != NULL)
238    for (int i = 0; i < this->bufferDisplaySize; i++)
239    {
240      this->bufferText[i]->setText(NULL, true);
241    }
242
243    ShellBuffer::getInstance()->flush();
244    // BUFFER FLUSHING
245}
246
247/**
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];
256
257    Text* swapText;
258    Text* moveText = this->bufferText[0];
259    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
260    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
261    {
262      if ( i < this->bufferDisplaySize-1)
263        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
264      swapText = this->bufferText[i];
265      this  ->bufferText[i] = moveText;
266      moveText = swapText;
267    }
268    lastText->setRelCoor2D(this->calculateLinePosition(0));
269    this->bufferText[0] = lastText;
270
271    this->bufferText[0]->setText(text, true);
272  }
273}
274
275/**
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{
281  if (!this->bufferIterator->compareListPointer(ShellBuffer::getInstance()->getBuffer()))
282  {
283    delete this->bufferIterator;
284    this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
285  }
286
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   }
293
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  }
323}
324
325/**
326 * clears the Shell (empties all buffers)
327 */
328void Shell::clear()
329{
330  this->flush();
331  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
332}
333
334/**
335 * listens for some event
336 * @param event the Event happened
337 */
338void Shell::process(const Event &event)
339{
340  if (event.bPressed)
341  {
342    if (event.type == SDLK_BACKQUOTE)
343    {
344      if (EventHandler::getInstance()->getState() == ES_GAME)
345        this->activate();
346      else
347        this->deactivate();
348    }
349    else if (event.type == SDLK_PAGEUP)
350    {
351      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
352    }
353    else if (event.type == SDLK_PAGEDOWN)
354    {
355      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
356    }
357  }
358}
359
360/**
361 * displays the Shell
362 */
363void Shell::draw() const
364{
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
374  glBindTexture(GL_TEXTURE_2D, 0);
375  glBegin(GL_TRIANGLE_STRIP);
376
377  glTexCoord2f(0, 0);
378  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
379
380  glTexCoord2f(1, 0);
381  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
382
383  glTexCoord2f(0, 1);
384  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
385
386  glTexCoord2f(1, 1);
387  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
388
389  glEnd();
390}
391
392///////////////////////
393// HELPER FUNCTIONS  //
394///////////////////////
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 */
401Vector Shell::calculateLinePosition(unsigned int lineNumber)
402{
403  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
404}
405
406
407
408/**
409 * displays some nice output from the Shell
410 */
411void Shell::debug() const
412{
413  PRINT(3)("Debugging output to console (not this shell)\n");
414
415//   if (this->pressedKey != SDLK_FIRST)
416//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
417
418
419  ShellBuffer::getInstance()->debug();
420}
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.