Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model_old/src/lib/shell/shell.cc @ 8462

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

orxonox/trunk: ShellBuffer and ShellInput had the segfault-capability for years, now they have not anymore :)

File size: 13.5 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(15, 0);
46SHELL_COMMAND(textcolor, Shell, setTextColor)
47->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
48->defaultValues(SHELL_DEFAULT_TEXT_COLOR);
49SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
50->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
51->defaultValues(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(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->bufferOffset = 0;
74  this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
75
76  // INPUT LINE
77  this->setLayer(E2D_LAYER_ABOVE_ALL);
78  this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
79
80  this->backgroundMaterial = new Material;
81
82  // Element2D and generals
83  this->setAbsCoor2D(3, -400);
84  this->textSize = 20;
85  this->lineSpacing = 0;
86  this->bActive = true;
87  this->fontFile = SHELL_DEFAULT_FONT;
88
89  this->setBufferDisplaySize(10);
90
91  this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
92  this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
93
94
95  this->deactivate();
96  // register the shell at the ShellBuffer
97  ShellBuffer::getInstance()->registerShell(this);
98}
99
100/**
101 * @brief standard deconstructor
102 */
103Shell::~Shell ()
104{
105  ShellBuffer::getInstance()->unregisterShell(this);
106
107  // delete the displayable Buffers
108  while (!this->bufferText.empty())
109  {
110    delete this->bufferText.front();
111    this->bufferText.pop_front();
112  }
113
114  // delete the inputLine
115  delete this->backgroundMaterial;
116}
117
118/**
119 * @brief 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, 5);
133
134  list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
135  bool top = false;
136  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
137  {
138    (*text)->setVisibility(true);
139    if (!top)
140    {
141      (*text)->setText((*textLine));
142      if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
143        top = true;
144      textLine--;
145    }
146  }
147}
148
149/**
150 * @brief 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, 5);
162
163  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
164    (*text)->setVisibility(false);
165  this->bufferOffset = 0;
166}
167
168/**
169 * @brief 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 std::string& fontFile)
176{
177  //   if (!ResourceManager::isInDataDir(fontFile))
178  //     return false;
179
180  this->fontFile = fontFile;
181
182  this->resetValues();
183}
184
185/**
186 * @brief sets the size of the text and spacing
187 * @param textSize the size of the Text in Pixels
188 * @param lineSpacing the size of the Spacing between two lines in pixels
189 *
190 * this also rebuilds the entire Text, inputLine and displayBuffer,
191 * to be accurate again.
192 */
193void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
194{
195  this->textSize = textSize;
196  this->lineSpacing = lineSpacing;
197
198  this->resetValues();
199}
200
201/**
202 * @brief 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 * @brief 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 * @brief 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 std::string& fileName)
237{
238  this->backgroundMaterial->setDiffuseMap(fileName);
239}
240
241
242/**
243 * @brief 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  this->shellInput.setFont(this->fontFile, this->textSize);
250  this->shellInput.setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
251  this->shellInput.setBlending(this->textColor[3]);
252  this->shellInput.setRelCoor2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize));
253  this->shellInput.setLayer(this->getLayer());
254  if (shellInput.getParent2D() != this)
255    this->shellInput.setParent2D(this);
256
257  unsigned int i = 0;
258  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text, ++i)
259  {
260    (*text)->setFont(this->fontFile, this->textSize);
261    (*text)->setColor(this->textColor[0], this->textColor[1], this->textColor[2]);
262    (*text)->setBlending(this->textColor[3]);
263    (*text)->setRelCoor2D( calculateLinePosition(i) );
264    (*text)->setLayer(this->getLayer());
265    if ((*text)->getParent2D() != this)
266      (*text)->setParent2D(this);
267  }
268  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
269}
270
271
272/**
273 * @brief sets The count of Lines to display in the buffer.
274 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
275 */
276void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
277{
278  unsigned int oldSize = this->bufferText.size();
279  if (oldSize > bufferDisplaySize)
280  {
281    for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
282    {
283      delete this->bufferText.back();
284      this->bufferText.pop_back();
285    }
286  }
287  else if (oldSize < bufferDisplaySize)
288  {
289    for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
290    {
291      this->bufferText.push_back(new Text);
292    }
293  }
294  this->bufferDisplaySize = bufferDisplaySize;
295  this->resetValues();
296}
297
298/**
299 * @brief deletes all the Buffers
300 */
301void Shell::flush()
302{
303  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
304  {
305    (*text)->setText("");  // remove all chars from the BufferTexts.
306  }
307  ShellBuffer::getInstance()->flush();
308  // BUFFER FLUSHING
309}
310
311/**
312 * @brief prints out some text to the input-buffers
313 * @param text the text to output.
314 */
315void Shell::printToDisplayBuffer(const std::string& text)
316{
317  this->bufferText.push_front(this->bufferText.back());
318  this->bufferText.pop_back();
319
320
321  unsigned int i = 0;
322  for (std::list<Text*>::iterator textIt = ++this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt, i++)
323  {
324    (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
325  }
326
327  this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
328  this->bufferText.front()->setRelCoorSoft2D(this->calculateLinePosition(0),10);
329
330  /*  FANCY EFFECTS :)
331    1:
332        lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
333        lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
334    2:
335    lastText->setRelDir2D(-90);
336    lastText->setRelDirSoft2D(0, 20);
337  */
338
339  this->bufferText.front()->setText(text);
340}
341
342/**
343 * moves the Display buffer (up + or down - )
344 * @param lineCount the count by which to shift the InputBuffer.
345 *
346 * @todo make this work
347 */
348void Shell::moveDisplayBuffer(int lineCount)
349{
350  if (this->bufferOffset == 0)
351  {
352    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
353    //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
354    //       this->bufferIterator->prevStep();
355  }
356
357  // boundraries
358  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
359    lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
360  else if (this->bufferOffset + lineCount < 0)
361    lineCount = -bufferOffset;
362  this->bufferOffset += lineCount;
363
364  // moving the iterator to the right position
365  int move = 0;
366  while (move != lineCount)
367  {
368    if (move < lineCount)
369    {
370      ++move;
371      this->bufferIterator--;
372    }
373    else
374    {
375      --move;
376      this->bufferIterator++;
377    }
378  }
379  // redisplay the buffers
380  list<std::string>::const_iterator it = this->bufferIterator;
381  if (it == ShellBuffer::getInstance()->getBuffer().end())
382  {
383    /// FIXME
384    PRINTF(1)("Should not heappen\n");
385    it--;
386  }
387  for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
388  {
389    (*textIt)->setText((*it));
390    if (it == ShellBuffer::getInstance()->getBuffer().begin())
391    {
392      /// FIXME
393      PRINTF(1)("Should not heappen\n");
394      break;
395    }
396    it--;
397  }
398}
399
400/**
401 * clears the Shell (empties all buffers)
402 */
403void Shell::clear()
404{
405  this->flush();
406  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
407}
408
409/**
410 * listens for some event
411 * @param event the Event happened
412 */
413void Shell::process(const Event &event)
414{
415  if (event.bPressed)
416  {
417    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
418    {
419      if (this->bActive == false)
420        this->activate();
421      else
422        this->deactivate();
423    }
424    else if (event.type == SDLK_PAGEUP)
425    {
426      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
427    }
428    else if (event.type == SDLK_PAGEDOWN)
429    {
430      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
431    }
432  }
433}
434
435/**
436 * displays the Shell
437 */
438void Shell::draw() const
439{
440  // transform for alignment.
441  // setting the Blending effects
442
443  this->backgroundMaterial->select();
444
445  glBegin(GL_TRIANGLE_STRIP);
446
447  glTexCoord2f(0, 0);
448  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
449
450  glTexCoord2f(1, 0);
451  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
452
453  glTexCoord2f(0, 1);
454  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
455
456  glTexCoord2f(1, 1);
457  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
458
459  glEnd();
460}
461
462///////////////////////
463// HELPER FUNCTIONS  //
464///////////////////////
465
466/**
467 * @brief calculates the position of a Buffer-Display Line
468 * @param lineNumber the lineNumber from the bottom to calculate the position from
469 * @returns the Position of the Line.
470 */
471Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
472{
473  return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
474}
475
476
477
478/**
479 * @brief displays some nice output from the Shell
480 */
481void Shell::debug() const
482{
483  PRINT(3)("Debugging output to console (not this shell)\n");
484
485  //   if (this->pressedKey != SDLK_FIRST)
486  //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
487
488
489  ShellBuffer::getInstance()->debug();
490}
491
492// void Shell::testI (int i)
493// {
494//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
495// }
496//
497// void Shell::testS (const char* s)
498// {
499//   PRINTF(3)("This is the Test for one String '%s'\n", s);
500// }
501//
502// void Shell::testB (bool b)
503// {
504//   PRINTF(3)("This is the Test for one Bool: ");
505//   if (b)
506//     PRINTF(3)("true\n");
507//   else
508//     PRINTF(3)("false\n");
509// }
510//
511// void Shell::testF (float f)
512// {
513//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
514// }
515//
516// void Shell::testSF (const char* s, float f)
517// {
518//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
519// }
Note: See TracBrowser for help on using the repository browser.