Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5205 was 5204, checked in by bensch, 20 years ago

orxonox/trunk: Shell now has a real nice help function

File size: 8.6 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
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[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
[5072]24#include "text_engine.h"
25#include "list.h"
[5093]26#include "graphics_engine.h"
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
[1856]35using namespace std;
[1853]36
[5201]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)")
[5204]42    ->setAlias("hide");
[1856]43
[3245]44/**
[4838]45 * standard constructor
[5068]46 */
47Shell::Shell ()
[3365]48{
[5072]49  this->setClassID(CL_SHELL, "Shell");
50  this->setName("Shell");
51
[5183]52  // Element2D and generals
[5175]53  this->setAbsCoor2D(3, -400);
54  this->textSize = 15;
55  this->lineSpacing = 5;
[5113]56  this->bActive = false;
[5072]57
[5175]58  // BUFFER
59  this->bufferText = NULL;
60  this->setBufferDisplaySize(10);
[5080]61
[5175]62  // INPUT LINE
[5179]63  this->shellInput = new ShellInput;
[5175]64  //this->commandList = new tList<ShellCommand>;
[5093]65
[5113]66  this->rebuildText();
[5093]67
[5180]68  // EVENT-Handler subscription of '`' to all States.
69  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
70
[5068]71}
[4320]72
[5068]73Shell* Shell::singletonRef = NULL;
[1853]74
[3245]75/**
[4838]76 * standard deconstructor
[5068]77 */
78Shell::~Shell ()
[3543]79{
[5099]80  // delete the displayable Buffers
[5080]81  for (int i = 0; i < this->bufferDisplaySize; i++)
82    delete this->bufferText[i];
[5113]83  delete[] this->bufferText;
[5093]84
[5099]85  // delete the inputLine
[5180]86  delete this->shellInput;
[5079]87
[5068]88  Shell::singletonRef = NULL;
[3543]89}
[5068]90
[5119]91/**
92 * activates the shell
93 *
94 * This also feeds the Last few lines from the main buffers into the displayBuffer
95 */
[5113]96void Shell::activate()
97{
98  if (this->bActive == true)
99    PRINTF(3)("The shell is already active\n");
100  this->bActive = true;
101
102  EventHandler::getInstance()->setState(ES_SHELL);
103  this->setRelCoorSoft2D(0, 0, 1, 5);
[5118]104
[5175]105  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
[5118]106  for (int i = 0; i < this->bufferDisplaySize; i++)
[5175]107    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true);
[5113]108}
109
[5119]110/**
111 * deactiveates the Shell.
112 */
[5113]113void Shell::deactivate()
114{
115  if (this->bActive == false)
116    PRINTF(3)("The shell is already inactive\n");
117  this->bActive = false;
118
119  EventHandler::getInstance()->setState(ES_GAME);
120  this->setRelCoorSoft2D(0, -400, 1, 5);
[5118]121
[5175]122  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
[5157]123  for (int i = 0; i < this->bufferDisplaySize; i++)
[5175]124    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false);
[5113]125}
126
[5158]127
[5119]128/**
129 * sets the size of the text and spacing
130 * @param textSize the size of the Text in Pixels
131 * @param lineSpacing the size of the Spacing between two lines in pixels
132 *
133 * this also rebuilds the entire Text, inputLine and displayBuffer,
134 * to be accurate again.
135 */
[5113]136void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
137{
138  this->textSize = textSize;
139  this->lineSpacing = lineSpacing;
140
141  this->rebuildText();
142}
143
[5127]144/**
145 * rebuilds the Text's
146 *
147 * use this function, if you changed the Font/Size or something else.
148 */
[5113]149void Shell::rebuildText()
150{
[5179]151  this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize);
152  this->shellInput->setColor(1, 0, 0);
153  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
154  this->shellInput->setText(NULL);
155  this->shellInput->setParent2D(this);
156  this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize);
[5113]157
158  this->setBufferDisplaySize(this->bufferDisplaySize);
159}
160
[5074]161/**
162 * sets The count of Lines to display in the buffer.
163 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
164 */
[5072]165void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
166{
[5080]167  if (this->bufferText != NULL)
[5072]168  {
[5080]169    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
170      delete this->bufferText[i];
[5113]171    delete[] this->bufferText;
[5072]172  }
[5080]173
174  this->bufferText = new Text*[bufferDisplaySize];
175  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]176  {
[5122]177    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC);
[5121]178    this->bufferText[i]->setColor(1, 0, 0);
[5080]179    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
[5120]180    this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
[5080]181    this->bufferText[i]->setText(NULL);
[5089]182    this->bufferText[i]->setParent2D(this);
[5072]183  }
[5113]184  this->bufferDisplaySize = bufferDisplaySize;
[5111]185
[5113]186  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5072]187}
[5068]188
189/**
190 * deletes all the Buffers
191 */
[5175]192void Shell::flush()
[5068]193{
[5072]194  // remove all chars from the BufferTexts.
[5080]195  if (this->bufferText)
[5125]196    for (int i = 0; i < this->bufferDisplaySize; i++)
[5080]197    {
[5125]198      this->bufferText[i]->setText(NULL, true);
[5080]199    }
[5068]200
[5072]201
[5175]202    // BUFFER FLUSHING
[5068]203}
204
205/**
[5118]206 * prints out some text to the input-buffers
207 * @param text the text to output.
208 */
209void Shell::printToDisplayBuffer(const char* text)
210{
211  if(likely(bufferText != NULL))
212  {
213    Text* lastText = this->bufferText[this->bufferDisplaySize-1];
[5113]214
[5118]215    Text* swapText;
216    Text* moveText = this->bufferText[0];
[5120]217    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
[5118]218    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
219    {
220      if ( i < this->bufferDisplaySize-1)
[5120]221        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
[5118]222      swapText = this->bufferText[i];
223      this  ->bufferText[i] = moveText;
224      moveText = swapText;
225    }
[5120]226    lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]227    this->bufferText[0] = lastText;
228
[5122]229    this->bufferText[0]->setText(text, true);
[5118]230  }
[5068]231}
232
233/**
[5166]234 * clears the Shell (empties all buffers)
235 */
[5130]236void Shell::clear()
237{
[5175]238  this->flush();
239  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]240}
241
[5097]242
243
[5069]244/**
245 * listens for some event
246 * @param event the Event happened
247 */
248void Shell::process(const Event &event)
249{
[5093]250  if (event.bPressed)
251  {
252    if (event.type == SDLK_BACKQUOTE)
253    {
254      if (EventHandler::getInstance()->getState() == ES_GAME)
[5113]255        this->activate();
[5093]256      else
[5113]257        this->deactivate();
[5093]258    }
259  }
[5069]260}
261
[5068]262/**
263 * displays the Shell
264 */
265void Shell::draw() const
266{
[5099]267  glPushMatrix();
268  // transform for alignment.
269  // setting the Blending effects
270
271  glColor4f(0.0f, 0.0f, 0.8f, .4);
272  glEnable(GL_BLEND);
273  glDisable(GL_TEXTURE_2D);
274  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
275
[5158]276  glBindTexture(GL_TEXTURE_2D, 0);
277  glBegin(GL_TRIANGLE_STRIP);
[5099]278
[5158]279  glTexCoord2f(0, 0);
[5099]280  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
281
[5158]282  glTexCoord2f(1, 0);
[5113]283  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]284
[5158]285  glTexCoord2f(0, 1);
286  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
287
288  glTexCoord2f(1, 1);
[5113]289  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]290
291  glEnd();
[5068]292}
293
[5120]294///////////////////////
295// HELPER FUNCTIONS  //
296///////////////////////
[5166]297
298/**
299 * calculates the position of a Buffer-Display Line
300 * @param lineNumber the lineNumber from the bottom to calculate the position from
301 * @returns the Position of the Line.
302 */
[5120]303Vector Shell::calculateLinePosition(unsigned int lineNumber)
304{
[5124]305  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
[5120]306}
307
308
309
[5113]310/**
[5068]311 * displays some nice output from the Shell
312 */
313void Shell::debug() const
314{
[5119]315  PRINT(3)("Debugging output to console (not this shell)\n");
316
[5180]317//   if (this->pressedKey != SDLK_FIRST)
318//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]319
320
[5177]321  ShellBuffer::getInstance()->debug();
[5068]322}
[5166]323
324
325
326// void Shell::testI (int i)
327// {
328//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
329// }
330//
331// void Shell::testS (const char* s)
332// {
333//   PRINTF(3)("This is the Test for one String '%s'\n", s);
334// }
335//
336// void Shell::testB (bool b)
337// {
338//   PRINTF(3)("This is the Test for one Bool: ");
339//   if (b)
340//     PRINTF(3)("true\n");
341//   else
342//     PRINTF(3)("false\n");
343// }
344//
345// void Shell::testF (float f)
346// {
347//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
348// }
349//
350// void Shell::testSF (const char* s, float f)
351// {
352//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
353// }
Note: See TracBrowser for help on using the repository browser.