Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/control/src/lib/shell/shell.cc @ 7134

Last change on this file since 7134 was 6780, checked in by bensch, 19 years ago

trunk: war of the popers

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