Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/resources/src/lib/shell/shell.cc @ 7913

Last change on this file since 7913 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

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