Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more stl in Shell

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