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, 19 years ago

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

File size: 13.2 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 "material.h"
28#include "event_handler.h"
29#include "debug.h"
30#include "class_list.h"
31
32#include "key_names.h"
33#include <stdarg.h>
34#include <stdio.h>
35
36using namespace std;
37
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)")
43    ->setAlias("hide");
44SHELL_COMMAND(textsize, Shell, setTextSize)
45    ->describe("Sets the size of the Text (size, linespacing)")
46    ->defaultValues(1, 15, 0);
47SHELL_COMMAND(font, Shell, setFont)
48    ->describe("Sets the font of the Shell")
49    ->defaultValues(1, SHELL_DEFAULT_FONT);
50
51/**
52 * standard constructor
53 */
54Shell::Shell ()
55{
56  this->setClassID(CL_SHELL, "Shell");
57  this->setName("Shell");
58
59  // EVENT-Handler subscription of '`' to all States.
60  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
61  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
62  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
63
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;
74  // Element2D and generals
75  this->setAbsCoor2D(3, -400);
76  this->textSize = 20;
77  this->lineSpacing = 0;
78  this->bActive = false;
79  this->fontFile = new char[strlen(SHELL_DEFAULT_FONT)+1];
80  strcpy(this->fontFile, SHELL_DEFAULT_FONT);
81
82
83  this->rebuildText();
84  this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
85  this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
86
87  // register the shell at the ShellBuffer
88  ShellBuffer::getInstance()->registerShell(this);
89}
90
91/**
92 * standard deconstructor
93 */
94Shell::~Shell ()
95{
96  ShellBuffer::getInstance()->unregisterShell(this);
97
98  // delete the displayable Buffers
99  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
100    delete this->bufferText[i];
101  delete[] this->bufferText;
102  delete this->bufferIterator;
103  delete[] this->fontFile;
104  // delete the inputLine
105  delete this->shellInput;
106  delete this->backgroundMaterial;
107}
108
109/**
110 * activates the shell
111 *
112 * This also feeds the Last few lines from the main buffers into the displayBuffer
113 */
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);
122
123  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
124  bufferIT->lastElement();
125  for (int i = 0; i < this->bufferDisplaySize; i++)
126  {
127    this->bufferText[i]->setText(bufferIT->getCurrent(), true);
128    bufferIT->prevStep();
129  }
130  delete bufferIT;
131}
132
133/**
134 * deactiveates the Shell.
135 */
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);
143  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
144
145  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
146  bufferIT->lastElement();
147  for (int i = 0; i < this->bufferDisplaySize; i++)
148  {
149    this->bufferText[i]->setText(bufferIT->getCurrent(), false);
150    bufferIT->prevStep();
151  }
152  delete bufferIT;
153
154  this->bufferOffset = 0;
155}
156
157/**
158 * sets the File to load the fonts from
159 * @param fontFile the file to load the font from
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)
163 */
164void Shell::setFont(const char* fontFile)
165{
166//   if (!ResourceManager::isInDataDir(fontFile))
167//     return false;
168
169  if (this->fontFile != NULL)
170    delete[] this->fontFile;
171
172  this->fontFile = new char[strlen(fontFile)+1];
173  strcpy(this->fontFile, fontFile);
174
175  this->rebuildText();
176}
177
178/**
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 */
186void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
187{
188  this->textSize = textSize;
189  this->lineSpacing = lineSpacing;
190  this->resetValues();
191}
192
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
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);
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));
249      }
250    }
251  }
252  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
253
254}
255
256/**
257 * rebuilds the Text's
258 *
259 * use this function, if you changed the Font/Size or something else.
260 */
261void Shell::rebuildText()
262{
263  this->shellInput->setFont(this->fontFile, this->textSize);
264  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
265  if (shellInput->getParent() != this)
266    this->shellInput->setParent2D(this);
267
268  this->setBufferDisplaySize(this->bufferDisplaySize);
269}
270
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 */
275void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
276{
277  Text** bufferText = this->bufferText;
278  this->bufferText = NULL;
279  if (bufferText != NULL)
280  {
281    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
282      delete bufferText[i];
283    delete[] bufferText;
284  }
285
286  tIterator<char>* it = ShellBuffer::getInstance()->getBuffer()->getIterator();
287  char* text = it->lastElement();
288  bufferText = new Text*[bufferDisplaySize];
289  for (unsigned int i = 0; i < bufferDisplaySize; i++)
290  {
291    bufferText[i] = new Text(this->fontFile, this->textSize, TEXT_RENDER_DYNAMIC);
292    bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
293    bufferText[i]->setText(text);
294    bufferText[i]->setParent2D(this);
295    text = it->prevElement();
296  }
297  delete it;
298  this->bufferDisplaySize = bufferDisplaySize;
299
300  this->bufferText = bufferText;
301  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
302}
303
304/**
305 * deletes all the Buffers
306 */
307void Shell::flush()
308{
309  // remove all chars from the BufferTexts.
310  if (this->bufferText != NULL)
311    for (int i = 0; i < this->bufferDisplaySize; i++)
312    {
313      this->bufferText[i]->setText(NULL, true);
314    }
315
316    ShellBuffer::getInstance()->flush();
317    // BUFFER FLUSHING
318}
319
320/**
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];
329
330    Text* swapText;
331    Text* moveText = this->bufferText[0];
332    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
333    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
334    {
335      if ( i < this->bufferDisplaySize-1)
336        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
337      swapText = this->bufferText[i];
338      this  ->bufferText[i] = moveText;
339      moveText = swapText;
340    }
341    lastText->setRelCoor2D(this->calculateLinePosition(0));
342    this->bufferText[0] = lastText;
343
344    this->bufferText[0]->setText(text, true);
345  }
346}
347
348/**
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{
354  if (!this->bufferIterator->compareListPointer(ShellBuffer::getInstance()->getBuffer()))
355  {
356    delete this->bufferIterator;
357    this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
358  }
359
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   }
366
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  }
396}
397
398/**
399 * clears the Shell (empties all buffers)
400 */
401void Shell::clear()
402{
403  this->flush();
404  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
405}
406
407/**
408 * listens for some event
409 * @param event the Event happened
410 */
411void Shell::process(const Event &event)
412{
413  if (event.bPressed)
414  {
415    if (event.type == SDLK_BACKQUOTE)
416    {
417      if (EventHandler::getInstance()->getState() == ES_GAME)
418        this->activate();
419      else
420        this->deactivate();
421    }
422    else if (event.type == SDLK_PAGEUP)
423    {
424      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
425    }
426    else if (event.type == SDLK_PAGEDOWN)
427    {
428      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
429    }
430  }
431}
432
433/**
434 * displays the Shell
435 */
436void Shell::draw() const
437{
438  glPushMatrix();
439  // transform for alignment.
440  // setting the Blending effects
441
442  this->backgroundMaterial->select();
443
444  glBegin(GL_TRIANGLE_STRIP);
445
446  glTexCoord2f(0, 0);
447  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
448
449  glTexCoord2f(1, 0);
450  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
451
452  glTexCoord2f(0, 1);
453  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
454
455  glTexCoord2f(1, 1);
456  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
457
458  glEnd();
459}
460
461///////////////////////
462// HELPER FUNCTIONS  //
463///////////////////////
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 */
470Vector Shell::calculateLinePosition(unsigned int lineNumber)
471{
472  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
473}
474
475
476
477/**
478 * displays some nice output from the Shell
479 */
480void Shell::debug() const
481{
482  PRINT(3)("Debugging output to console (not this shell)\n");
483
484//   if (this->pressedKey != SDLK_FIRST)
485//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
486
487
488  ShellBuffer::getInstance()->debug();
489}
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.