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
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[7374]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
[1853]17
[5068]18#include "shell.h"
[5129]19#include "shell_command.h"
[5175]20#include "shell_buffer.h"
[5179]21#include "shell_input.h"
[1853]22
[5175]23
[5344]24#include "text.h"
[5093]25#include "graphics_engine.h"
[5372]26#include "material.h"
[5093]27#include "event_handler.h"
[5129]28#include "debug.h"
[5113]29#include "class_list.h"
30
31#include "key_names.h"
[5075]32#include <stdarg.h>
33#include <stdio.h>
34
[7374]35namespace OrxShell
36{
[1853]37
[5201]38SHELL_COMMAND(clear, Shell, clear)
[7341]39->describe("Clears the shell from unwanted lines (empties all buffers)")
40->setAlias("clear");
[5201]41SHELL_COMMAND(deactivate, Shell, deactivate)
[7341]42->describe("Deactivates the Shell. (moves it into background)")
43->setAlias("hide");
[5208]44SHELL_COMMAND(textsize, Shell, setTextSize)
[7341]45->describe("Sets the size of the Text size, linespacing")
46->defaultValues(15, 0);
[5374]47SHELL_COMMAND(textcolor, Shell, setTextColor)
[7341]48->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
49->defaultValues(SHELL_DEFAULT_TEXT_COLOR);
[5374]50SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
[7341]51->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
52->defaultValues(SHELL_DEFAULT_BACKGROUND_COLOR);
[5374]53SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
[7341]54->describe("sets the background image to load for the Shell");
[5254]55SHELL_COMMAND(font, Shell, setFont)
[7341]56->describe("Sets the font of the Shell")
57->defaultValues(SHELL_DEFAULT_FONT);
[1856]58
[3245]59/**
[4838]60 * standard constructor
[5068]61 */
62Shell::Shell ()
[3365]63{
[5072]64  this->setClassID(CL_SHELL, "Shell");
65  this->setName("Shell");
66
[5245]67  // EVENT-Handler subscription of '`' to all States.
68  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
[5425]69  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_F12);
[5246]70  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEUP);
71  EventHandler::getInstance()->subscribe(this, ES_SHELL, SDLK_PAGEDOWN);
[5206]72
[5372]73  // BUFFER
74  this->bufferOffset = 0;
[7315]75  this->bufferIterator = ShellBuffer::getInstance()->getBuffer().begin();
[5372]76
77  // INPUT LINE
[7341]78  this->setLayer(E2D_LAYER_ABOVE_ALL);
79  this->shellInput.setLayer(E2D_LAYER_ABOVE_ALL);
[5372]80
81  this->backgroundMaterial = new Material;
[7341]82
[5183]83  // Element2D and generals
[5175]84  this->setAbsCoor2D(3, -400);
[5329]85  this->textSize = 20;
[5208]86  this->lineSpacing = 0;
[5420]87  this->bActive = true;
[7221]88  this->fontFile = SHELL_DEFAULT_FONT;
[5072]89
[7341]90  this->setBufferDisplaySize(10);
[5080]91
[5372]92  this->setTextColor(SHELL_DEFAULT_TEXT_COLOR);
93  this->setBackgroundColor(SHELL_DEFAULT_BACKGROUND_COLOR);
[5335]94
[7341]95
[5420]96  this->deactivate();
[5335]97  // register the shell at the ShellBuffer
98  ShellBuffer::getInstance()->registerShell(this);
[5068]99}
[4320]100
[3245]101/**
[7341]102 * @brief standard deconstructor
[5068]103 */
104Shell::~Shell ()
[3543]105{
[5335]106  ShellBuffer::getInstance()->unregisterShell(this);
107
[5099]108  // delete the displayable Buffers
[7341]109  while (!this->bufferText.empty())
110  {
111    delete this->bufferText.front();
112    this->bufferText.pop_front();
113  }
114
[5099]115  // delete the inputLine
[5372]116  delete this->backgroundMaterial;
[3543]117}
[5068]118
[5119]119/**
[7341]120 * @brief activates the shell
[5119]121 *
122 * This also feeds the Last few lines from the main buffers into the displayBuffer
123 */
[5113]124void Shell::activate()
125{
126  if (this->bActive == true)
127    PRINTF(3)("The shell is already active\n");
128  this->bActive = true;
129
[5388]130  EventHandler::getInstance()->pushState(ES_SHELL);
[5786]131  EventHandler::getInstance()->withUNICODE(true);
132
[7316]133  this->setRelCoorSoft2D(0, 0, 5);
[5118]134
[7374]135  std::list<std::string>::const_iterator textLine = --ShellBuffer::getInstance()->getBuffer().end();
[5787]136  bool top = false;
[7341]137  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
[5247]138  {
[7341]139    (*text)->setVisibility(true);
[5787]140    if (!top)
141    {
[7341]142      (*text)->setText((*textLine));
143      if (textLine != ShellBuffer::getInstance()->getBuffer().begin())
144        top = true;
[5787]145      textLine--;
146    }
[5247]147  }
[5113]148}
149
[5119]150/**
[7340]151 * @brief deactiveates the Shell.
[5119]152 */
[5113]153void Shell::deactivate()
154{
155  if (this->bActive == false)
156    PRINTF(3)("The shell is already inactive\n");
157  this->bActive = false;
158
[5786]159  EventHandler::getInstance()->withUNICODE(false);
[5388]160  EventHandler::getInstance()->popState();
161
[7316]162  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 5);
[5118]163
[7341]164  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text)
165    (*text)->setVisibility(false);
[5248]166  this->bufferOffset = 0;
[5113]167}
168
[5119]169/**
[7340]170 * @brief sets the File to load the fonts from
[5251]171 * @param fontFile the file to load the font from
[5254]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)
[5251]175 */
[7221]176void Shell::setFont(const std::string& fontFile)
[5251]177{
[7341]178  //   if (!ResourceManager::isInDataDir(fontFile))
179  //     return false;
[5335]180
[7221]181  this->fontFile = fontFile;
[5251]182
[7341]183  this->resetValues();
[5251]184}
185
186/**
[7340]187 * @brief sets the size of the text and spacing
[5119]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 */
[5113]194void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
195{
196  this->textSize = textSize;
197  this->lineSpacing = lineSpacing;
[7341]198
[5372]199  this->resetValues();
200}
[5113]201
[5372]202/**
[7341]203 * @brief sets the color of the Font.
[5372]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/**
[7341]221 * @brief sets the color of the Backgrond.
[5372]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/**
[7341]234 * @brief sets a nice background image to the Shell's background
[5374]235 * @param fileName the filename of the Image to load
236 */
[7221]237void Shell::setBackgroundImage(const std::string& fileName)
[5374]238{
239  this->backgroundMaterial->setDiffuseMap(fileName);
240}
241
242
243/**
[7341]244 * @brief resets the Values of all visible shell's commandos to the Shell's stored values
[5372]245 *
246 * this functions synchronizes the stored Data with the visible one.
247 */
248void Shell::resetValues()
249{
[7341]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);
[5372]257
[7341]258  unsigned int i = 0;
259  for (std::list<Text*>::iterator text = this->bufferText.begin(); text != this->bufferText.end(); ++text, ++i)
[5369]260  {
[7341]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);
[5369]268  }
269  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5113]270}
271
272
[5074]273/**
[7341]274 * @brief sets The count of Lines to display in the buffer.
[5074]275 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
276 */
[5072]277void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
278{
[7341]279  unsigned int oldSize = this->bufferText.size();
280  if (oldSize > bufferDisplaySize)
[5072]281  {
[7341]282    for (unsigned int i = bufferDisplaySize; i <= oldSize; i++)
283    {
284      delete this->bufferText.back();
285      this->bufferText.pop_back();
286    }
[5072]287  }
[7341]288  else if (oldSize < bufferDisplaySize)
[5072]289  {
[7341]290    for (unsigned int i = oldSize; i <= bufferDisplaySize; i++)
[5787]291    {
[7341]292      this->bufferText.push_back(new Text);
[5787]293    }
[5072]294  }
[5113]295  this->bufferDisplaySize = bufferDisplaySize;
[7341]296  this->resetValues();
[5072]297}
[5068]298
299/**
[7341]300 * @brief deletes all the Buffers
[5068]301 */
[5175]302void Shell::flush()
[5068]303{
[7341]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
[5068]310}
311
312/**
[7341]313 * @brief prints out some text to the input-buffers
[5118]314 * @param text the text to output.
315 */
[7221]316void Shell::printToDisplayBuffer(const std::string& text)
[5118]317{
[7341]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++)
[5118]324  {
[7341]325    (*textIt)->setRelCoorSoft2D(this->calculateLinePosition(i+1), 5);
326  }
[5113]327
[7341]328  this->bufferText.front()->setRelCoor2D(this->calculateLinePosition(0)- Vector2D(-1000,0));
329  this->bufferText.front()->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5375]330
331  /*  FANCY EFFECTS :)
[5376]332    1:
[5375]333        lastText->setRelCoor2D(this->calculateLinePosition(0)- Vector(-1000,0,0));
334        lastText->setRelCoorSoft2D(this->calculateLinePosition(0),10);
[5376]335    2:
[5383]336    lastText->setRelDir2D(-90);
337    lastText->setRelDirSoft2D(0, 20);
[7341]338  */
[5377]339
[7341]340  this->bufferText.front()->setText(text);
[5068]341}
342
343/**
[5783]344 * moves the Display buffer (up + or down - )
[5246]345 * @param lineCount the count by which to shift the InputBuffer.
[5783]346 *
347 * @todo make this work
[5246]348 */
349void Shell::moveDisplayBuffer(int lineCount)
350{
[5783]351  if (this->bufferOffset == 0)
[7341]352  {
353    this->bufferIterator = ShellBuffer::getInstance()->getBuffer().end();
354    //     for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
355    //       this->bufferIterator->prevStep();
356  }
[5783]357
358  // boundraries
[7315]359  if (this->bufferOffset + lineCount > (int)ShellBuffer::getInstance()->getBuffer().size())
360    lineCount = (int)ShellBuffer::getInstance()->getBuffer().size()- this->bufferOffset;
[5783]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
[7374]381  std::list<std::string>::const_iterator it = this->bufferIterator;
[7342]382  if (it == ShellBuffer::getInstance()->getBuffer().end())
383  {
384    /// FIXME
385    PRINTF(1)("Should not heappen\n");
386    it--;
387  }
[7341]388  for (std::list<Text*>::iterator textIt = this->bufferText.begin(); textIt != this->bufferText.end(); ++textIt)
[5783]389  {
[7341]390    (*textIt)->setText((*it));
[7342]391    if (it == ShellBuffer::getInstance()->getBuffer().begin())
392    {
393      /// FIXME
394      PRINTF(1)("Should not heappen\n");
395      break;
396    }
[5783]397    it--;
398  }
[5246]399}
400
401/**
[5166]402 * clears the Shell (empties all buffers)
403 */
[5130]404void Shell::clear()
405{
[5175]406  this->flush();
407  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]408}
409
[5069]410/**
411 * listens for some event
412 * @param event the Event happened
413 */
414void Shell::process(const Event &event)
415{
[5093]416  if (event.bPressed)
417  {
[5425]418    if (event.type == SDLK_BACKQUOTE || event.type == SDLK_F12)
[5093]419    {
[5388]420      if (this->bActive == false)
[5113]421        this->activate();
[5093]422      else
[5113]423        this->deactivate();
[5093]424    }
[5246]425    else if (event.type == SDLK_PAGEUP)
426    {
[5248]427      this->moveDisplayBuffer(+this->bufferDisplaySize-1);
[5246]428    }
429    else if (event.type == SDLK_PAGEDOWN)
430    {
[5248]431      this->moveDisplayBuffer(-this->bufferDisplaySize+1);
[5246]432    }
[5093]433  }
[5069]434}
435
[5068]436/**
437 * displays the Shell
438 */
439void Shell::draw() const
440{
[5099]441  // transform for alignment.
442  // setting the Blending effects
443
[5372]444  this->backgroundMaterial->select();
[5099]445
[5158]446  glBegin(GL_TRIANGLE_STRIP);
[5099]447
[5158]448  glTexCoord2f(0, 0);
[5099]449  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
450
[5158]451  glTexCoord2f(1, 0);
[5113]452  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]453
[5158]454  glTexCoord2f(0, 1);
455  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
456
457  glTexCoord2f(1, 1);
[5113]458  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]459
460  glEnd();
[5068]461}
462
[5120]463///////////////////////
464// HELPER FUNCTIONS  //
465///////////////////////
[5166]466
467/**
[7316]468 * @brief calculates the position of a Buffer-Display Line
[5166]469 * @param lineNumber the lineNumber from the bottom to calculate the position from
470 * @returns the Position of the Line.
471 */
[7316]472Vector2D Shell::calculateLinePosition(unsigned int lineNumber)
[5120]473{
[7316]474  return Vector2D(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber - 2) + this->textSize);
[5120]475}
476
477
478
[5113]479/**
[7316]480 * @brief displays some nice output from the Shell
[5068]481 */
482void Shell::debug() const
483{
[5119]484  PRINT(3)("Debugging output to console (not this shell)\n");
485
[7341]486  //   if (this->pressedKey != SDLK_FIRST)
487  //     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]488
489
[5177]490  ShellBuffer::getInstance()->debug();
[5068]491}
[5166]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// }
[7374]521
522}
Note: See TracBrowser for help on using the repository browser.