Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added new Files shell_completion_plugin for the new Plugin Structure.
Also created the first namespace: OrxShell

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