Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Way to load and unload Text

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