Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5728 was 5425, checked in by bensch, 19 years ago

orxonox/trunk: a key for those poor orxonox player wanting to use the shell with deutsch keybords

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