Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: safer shell, better key-handling

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