Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7331 was 7330, checked in by bensch, 19 years ago

orxonox/trunk: Sorting in ShellCommand

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