Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: shell in c++-style now

File size: 13.3 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  //   lastText->setRelCoor2D(this->calculateLinePosition(0));
340
341  this->bufferText.front()->setText(text);
342}
343
344/**
345 * moves the Display buffer (up + or down - )
346 * @param lineCount the count by which to shift the InputBuffer.
347 *
348 * @todo make this work
349 */
350void Shell::moveDisplayBuffer(int lineCount)
351{
352  if (this->bufferOffset == 0)
353  {
354    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
355    //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
356    //       this->bufferIterator->prevStep();
357  }
358
359  // boundraries
360  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
361    lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
362  else if (this->bufferOffset + lineCount < 0)
363    lineCount = -bufferOffset;
364  this->bufferOffset += lineCount;
365
366  // moving the iterator to the right position
367  int move = 0;
368  while (move != lineCount)
369  {
370    if (move < lineCount)
371    {
372      ++move;
373      this->bufferIterator--;
374    }
375    else
376    {
377      --move;
378      this->bufferIterator++;
379    }
380  }
381  // redisplay the buffers
382  list<std::string>::const_iterator it = this->bufferIterator;
383  for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
384  {
385    (*textIt)->setText((*it));
386    it--;
387  }
388}
389
390/**
391 * clears the Shell (empties all buffers)
392 */
393void Shell::clear()
394{
395  this->flush();
396  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
397}
398
399/**
400 * listens for some event
401 * @param event the Event happened
402 */
403void Shell::process(const Event &event)
404{
405  if (event.bPressed)
406  {
407    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
408    {
409      if (this->bActive == false)
410        this->activate();
411      else
412        this->deactivate();
413    }
414    else if (event.type == SDLK_PAGEUP)
415    {
416      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
417    }
418    else if (event.type == SDLK_PAGEDOWN)
419    {
420      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
421    }
422  }
423}
424
425/**
426 * displays the Shell
427 */
428void Shell::draw() const
429{
430  // transform for alignment.
431  // setting the Blending effects
432
433  this->backgroundMaterial->select();
434
435  glBegin(GL_TRIANGLE_STRIP);
436
437  glTexCoord2f(0, 0);
438  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
439
440  glTexCoord2f(1, 0);
441  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
442
443  glTexCoord2f(0, 1);
444  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
445
446  glTexCoord2f(1, 1);
447  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
448
449  glEnd();
450}
451
452///////////////////////
453// HELPER FUNCTIONS  //
454///////////////////////
455
456/**
457 * @brief calculates the position of a Buffer-Display Line
458 * @param lineNumber the lineNumber from the bottom to calculate the position from
459 * @returns the Position of the Line.
460 */
461Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
462{
463  return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
464}
465
466
467
468/**
469 * @brief displays some nice output from the Shell
470 */
471void Shell::debug() const
472{
473  PRINT(3)("Debugging output to console (not this shell)\n");
474
475  //   if (this->pressedKey != SDLK_FIRST)
476  //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
477
478
479  ShellBuffer::getInstance()->debug();
480}
481
482// void Shell::testI (int i)
483// {
484//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
485// }
486//
487// void Shell::testS (const char* s)
488// {
489//   PRINTF(3)("This is the Test for one String '%s'\n", s);
490// }
491//
492// void Shell::testB (bool b)
493// {
494//   PRINTF(3)("This is the Test for one Bool: ");
495//   if (b)
496//     PRINTF(3)("true\n");
497//   else
498//     PRINTF(3)("false\n");
499// }
500//
501// void Shell::testF (float f)
502// {
503//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
504// }
505//
506// void Shell::testSF (const char* s, float f)
507// {
508//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
509// }
Note: See TracBrowser for help on using the repository browser.