Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Shell no more singleton, and ShellInput is now derive from Text correctly

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