Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/lib/shell/shell.cc @ 5380

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

orxonox/trunk: some 2d-adaptions

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