Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: test/flush/and 41

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)")
42    ->setAlias("deactivate");
[1856]43
[5201]44
[3245]45/**
[4838]46 * standard constructor
[5068]47 */
48Shell::Shell ()
[3365]49{
[5072]50  this->setClassID(CL_SHELL, "Shell");
51  this->setName("Shell");
52
[5183]53  // Element2D and generals
[5175]54  this->setAbsCoor2D(3, -400);
55  this->textSize = 15;
56  this->lineSpacing = 5;
[5113]57  this->bActive = false;
[5072]58
[5175]59  // BUFFER
60  this->bufferText = NULL;
61  this->setBufferDisplaySize(10);
[5080]62
[5175]63  // INPUT LINE
[5179]64  this->shellInput = new ShellInput;
[5175]65  //this->commandList = new tList<ShellCommand>;
[5093]66
[5113]67  this->rebuildText();
[5093]68
[5180]69  // EVENT-Handler subscription of '`' to all States.
70  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
71
[5068]72}
[4320]73
[5068]74Shell* Shell::singletonRef = NULL;
[1853]75
[3245]76/**
[4838]77 * standard deconstructor
[5068]78 */
79Shell::~Shell ()
[3543]80{
[5099]81  // delete the displayable Buffers
[5080]82  for (int i = 0; i < this->bufferDisplaySize; i++)
83    delete this->bufferText[i];
[5113]84  delete[] this->bufferText;
[5093]85
[5099]86  // delete the inputLine
[5180]87  delete this->shellInput;
[5079]88
[5068]89  Shell::singletonRef = NULL;
[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    }
[5068]201
[5072]202
[5175]203    // BUFFER FLUSHING
[5068]204}
205
206/**
[5118]207 * prints out some text to the input-buffers
208 * @param text the text to output.
209 */
210void Shell::printToDisplayBuffer(const char* text)
211{
212  if(likely(bufferText != NULL))
213  {
214    Text* lastText = this->bufferText[this->bufferDisplaySize-1];
[5113]215
[5118]216    Text* swapText;
217    Text* moveText = this->bufferText[0];
[5120]218    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
[5118]219    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
220    {
221      if ( i < this->bufferDisplaySize-1)
[5120]222        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
[5118]223      swapText = this->bufferText[i];
224      this  ->bufferText[i] = moveText;
225      moveText = swapText;
226    }
[5120]227    lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]228    this->bufferText[0] = lastText;
229
[5122]230    this->bufferText[0]->setText(text, true);
[5118]231  }
[5068]232}
233
234/**
[5166]235 * clears the Shell (empties all buffers)
236 */
[5130]237void Shell::clear()
238{
[5175]239  this->flush();
240  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]241}
242
[5097]243
244
[5069]245/**
246 * listens for some event
247 * @param event the Event happened
248 */
249void Shell::process(const Event &event)
250{
[5093]251  if (event.bPressed)
252  {
253    if (event.type == SDLK_BACKQUOTE)
254    {
255      if (EventHandler::getInstance()->getState() == ES_GAME)
[5113]256        this->activate();
[5093]257      else
[5113]258        this->deactivate();
[5093]259    }
260  }
[5069]261}
262
[5068]263/**
264 * displays the Shell
265 */
266void Shell::draw() const
267{
[5099]268  glPushMatrix();
269  // transform for alignment.
270  // setting the Blending effects
271
272  glColor4f(0.0f, 0.0f, 0.8f, .4);
273  glEnable(GL_BLEND);
274  glDisable(GL_TEXTURE_2D);
275  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
276
[5158]277  glBindTexture(GL_TEXTURE_2D, 0);
278  glBegin(GL_TRIANGLE_STRIP);
[5099]279
[5158]280  glTexCoord2f(0, 0);
[5099]281  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
282
[5158]283  glTexCoord2f(1, 0);
[5113]284  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]285
[5158]286  glTexCoord2f(0, 1);
287  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
288
289  glTexCoord2f(1, 1);
[5113]290  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]291
292  glEnd();
[5068]293}
294
[5120]295///////////////////////
296// HELPER FUNCTIONS  //
297///////////////////////
[5166]298
299/**
300 * calculates the position of a Buffer-Display Line
301 * @param lineNumber the lineNumber from the bottom to calculate the position from
302 * @returns the Position of the Line.
303 */
[5120]304Vector Shell::calculateLinePosition(unsigned int lineNumber)
305{
[5124]306  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
[5120]307}
308
309
310
[5113]311/**
[5068]312 * displays some nice output from the Shell
313 */
314void Shell::debug() const
315{
[5119]316  PRINT(3)("Debugging output to console (not this shell)\n");
317
[5180]318//   if (this->pressedKey != SDLK_FIRST)
319//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]320
321
[5177]322  ShellBuffer::getInstance()->debug();
[5068]323}
[5166]324
325
326
327// void Shell::testI (int i)
328// {
329//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
330// }
331//
332// void Shell::testS (const char* s)
333// {
334//   PRINTF(3)("This is the Test for one String '%s'\n", s);
335// }
336//
337// void Shell::testB (bool b)
338// {
339//   PRINTF(3)("This is the Test for one Bool: ");
340//   if (b)
341//     PRINTF(3)("true\n");
342//   else
343//     PRINTF(3)("false\n");
344// }
345//
346// void Shell::testF (float f)
347// {
348//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
349// }
350//
351// void Shell::testSF (const char* s, float f)
352// {
353//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
354// }
Note: See TracBrowser for help on using the repository browser.