Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/2d-recalc: some recalculations.. do not know it i will continue with this, as has certain disadvantages over the old approach… maybe later

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/**
61 * standard constructor
62 */
63Shell::Shell ()
64{
65  this->setClassID(CL_SHELL, "Shell");
66  this->setName("Shell");
67
68  // EVENT-Handler subscription of '`' to all States.
69  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
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->textSize = 16;
85  this->lineSpacing = 1;
86  this->bActive = false;
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  this->setRelCoor2D(0,-this->shellHeight, 0);
96  // register the shell at the ShellBuffer
97  ShellBuffer::getInstance()->registerShell(this);
98}
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->bufferIterator;
113  delete[] this->fontFile;
114  // delete the inputLine
115  delete this->shellInput;
116  delete this->backgroundMaterial;
117}
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()->setState(ES_SHELL);
132  this->setRelCoorSoft2D(0, 0, 0, 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    bufferIT->prevStep();
140  }
141  delete bufferIT;
142}
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()->setState(ES_GAME);
155  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
156
157  tIterator<char>* bufferIT = ShellBuffer::getInstance()->getBuffer()->getIterator();
158  bufferIT->lastElement();
159  for (int i = 0; i < this->bufferDisplaySize; i++)
160  {
161    this->bufferText[i]->setText(bufferIT->getCurrent(), false);
162    bufferIT->prevStep();
163  }
164  delete bufferIT;
165
166  this->bufferOffset = 0;
167}
168
169
170/**
171 * sets the File to load the fonts from
172 * @param fontFile the file to load the font from
173 *
174 * it is quite important, that the font pointed too really exists!
175 * (be aware that within orxonox fontFile is relative to the Data-Dir)
176 */
177void Shell::setFont(const char* fontFile)
178{
179//   if (!ResourceManager::isInDataDir(fontFile))
180//     return false;
181
182  if (this->fontFile != NULL)
183    delete[] this->fontFile;
184
185  this->fontFile = new char[strlen(fontFile)+1];
186  strcpy(this->fontFile, fontFile);
187
188  this->rebuildText();
189}
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/**
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/**
241 * sets a nice background image to the Shell's background
242 * @param fileName the filename of the Image to load
243 */
244void Shell::setBackgroundImage(const char* fileName)
245{
246  this->backgroundMaterial->setDiffuseMap(fileName);
247}
248
249
250/**
251 * resets the Values of all visible shell's commandos to the Shell's stored values
252 *
253 * this functions synchronizes the stored Data with the visible one.
254 */
255void Shell::resetValues()
256{
257  if (this->shellInput != NULL)
258  {
259    this->shellInput->setSizeY2D(this->textSize);
260    this->shellInput->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
261    this->shellInput->setBlending(this->textColor[3]);
262    this->shellInput->setRelCoor2D(.005, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize);
263  }
264
265  if (this->bufferText != NULL)
266  {
267    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
268    {
269      if (this->bufferText[i] != NULL)
270      {
271        this->bufferText[i]->setSizeY2D(this->textSize);
272        this->bufferText[i]->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
273        this->bufferText[i]->setBlending(this->textColor[3]);
274        this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
275      }
276    }
277  }
278  this->shellHeight = (float)(this->textSize + this->lineSpacing) * (bufferDisplaySize+1.0);
279}
280
281
282/**
283 * rebuilds the Text's
284 *
285 * use this function, if you changed the Font/Size or something else.
286 */
287void Shell::rebuildText()
288{
289  this->shellInput->setFont(this->fontFile, this->textSize);
290  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
291  if (shellInput->getParent() != this)
292    this->shellInput->setParent2D(this);
293
294  this->setBufferDisplaySize(this->bufferDisplaySize);
295}
296
297
298/**
299 * sets The count of Lines to display in the buffer.
300 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
301 */
302void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
303{
304  Text** bufferText = this->bufferText;
305  this->bufferText = NULL;
306  if (bufferText != NULL)
307  {
308    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
309      delete bufferText[i];
310    delete[] bufferText;
311  }
312
313  tIterator<char>* it = ShellBuffer::getInstance()->getBuffer()->getIterator();
314  char* text = it->lastElement();
315  bufferText = new Text*[bufferDisplaySize];
316  for (unsigned int i = 0; i < bufferDisplaySize; i++)
317  {
318    bufferText[i] = new Text(this->fontFile, this->textSize, TEXT_RENDER_DYNAMIC);
319    bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
320    bufferText[i]->setText(text);
321    bufferText[i]->setParent2D(this);
322    text = it->prevElement();
323  }
324  delete it;
325  this->bufferDisplaySize = bufferDisplaySize;
326
327  this->bufferText = bufferText;
328  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
329}
330
331
332/**
333 * deletes all the Buffers
334 */
335void Shell::flush()
336{
337  // remove all chars from the BufferTexts.
338  if (this->bufferText != NULL)
339    for (int i = 0; i < this->bufferDisplaySize; i++)
340    {
341      this->bufferText[i]->setText(NULL, true);
342    }
343
344    ShellBuffer::getInstance()->flush();
345    // BUFFER FLUSHING
346}
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, 1);
378    lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1,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/**
390 * moves the Display buffer (up or down)
391 * @param lineCount the count by which to shift the InputBuffer.
392 */
393void Shell::moveDisplayBuffer(int lineCount)
394{
395  if (!this->bufferIterator->compareListPointer(ShellBuffer::getInstance()->getBuffer()))
396  {
397    delete this->bufferIterator;
398    this->bufferIterator = ShellBuffer::getInstance()->getBuffer()->getIterator();
399  }
400
401  if (this->bufferOffset == 0)
402   {
403     this->bufferIterator->lastElement();
404//     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
405//       this->bufferIterator->prevStep();
406   }
407
408  // boundraries
409  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer()->getSize())
410    lineCount = (int)ShellBuffer::getInstance()->getBuffer()->getSize()- this->bufferOffset;
411  else if (this->bufferOffset + lineCount < 0)
412    lineCount = -bufferOffset;
413  this->bufferOffset += lineCount;
414
415  // moving the iterator to the right position
416  int move = 0;
417  while (move != lineCount)
418  {
419    if (move < lineCount)
420    {
421      ++move;
422      this->bufferIterator->prevStep();
423    }
424    else
425    {
426      --move;
427      this->bufferIterator->nextStep();
428    }
429  }
430  // redisplay the buffers
431  tIterator<char> it = *this->bufferIterator;
432  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
433  {
434    this->bufferText[i]->setText(it.getCurrent(), false);
435    it.prevStep();
436  }
437}
438
439
440/**
441 * clears the Shell (empties all buffers)
442 */
443void Shell::clear()
444{
445  this->flush();
446  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
447}
448
449
450/**
451 * listens for some event
452 * @param event the Event happened
453 */
454void Shell::process(const Event &event)
455{
456  if (event.bPressed)
457  {
458    if (event.type == SDLK_BACKQUOTE)
459    {
460      if (EventHandler::getInstance()->getState() == ES_GAME)
461        this->activate();
462      else
463        this->deactivate();
464    }
465    else if (event.type == SDLK_PAGEUP)
466    {
467      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
468    }
469    else if (event.type == SDLK_PAGEDOWN)
470    {
471      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
472    }
473  }
474}
475
476
477/**
478 * displays the Shell
479 */
480void Shell::draw() const
481{
482  glPushMatrix();
483  // transform for alignment.
484  // setting the Blending effects
485
486  this->backgroundMaterial->select();
487
488  glBegin(GL_TRIANGLE_STRIP);
489
490  glTexCoord2f(0, 0);
491  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
492
493  glTexCoord2f(1, 0);
494  glVertex2f(1 - this->getAbsCoor2D().x, this->getAbsCoor2D().);
495
496  glTexCoord2f(0, 1);
497  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
498
499  glTexCoord2f(1, 1);
500  glVertex2f(1 - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
501
502  glEnd();
503}
504
505
506///////////////////////
507// HELPER FUNCTIONS  //
508///////////////////////
509/**
510 * calculates the position of a Buffer-Display Line
511 * @param lineNumber the lineNumber from the bottom to calculate the position from
512 * @returns the Position of the Line.
513 */
514Vector Shell::calculateLinePosition(unsigned int lineNumber)
515{
516  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
517}
518
519
520/**
521 * displays some nice output from the Shell
522 */
523void Shell::debug() const
524{
525  PRINT(3)("Debugging output to console (not this shell)\n");
526
527//   if (this->pressedKey != SDLK_FIRST)
528//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
529
530
531  ShellBuffer::getInstance()->debug();
532}
533
534// void Shell::testI (int i)
535// {
536//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
537// }
538//
539// void Shell::testS (const char* s)
540// {
541//   PRINTF(3)("This is the Test for one String '%s'\n", s);
542// }
543//
544// void Shell::testB (bool b)
545// {
546//   PRINTF(3)("This is the Test for one Bool: ");
547//   if (b)
548//     PRINTF(3)("true\n");
549//   else
550//     PRINTF(3)("false\n");
551// }
552//
553// void Shell::testF (float f)
554// {
555//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
556// }
557//
558// void Shell::testSF (const char* s, float f)
559// {
560//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
561// }
Note: See TracBrowser for help on using the repository browser.