Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5786 was 5786, checked in by bensch, 18 years ago

orxonox/trunk: key-repeat in the Shell is smoother now

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