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
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_
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_engine.h"
25#include "list.h"
26#include "graphics_engine.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
35using namespace std;
36
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");
43
44
45/**
46 * standard constructor
47 */
48Shell::Shell ()
49{
50  this->setClassID(CL_SHELL, "Shell");
51  this->setName("Shell");
52
53  // Element2D and generals
54  this->setAbsCoor2D(3, -400);
55  this->textSize = 15;
56  this->lineSpacing = 5;
57  this->bActive = false;
58
59  // BUFFER
60  this->bufferText = NULL;
61  this->setBufferDisplaySize(10);
62
63  // INPUT LINE
64  this->shellInput = new ShellInput;
65  //this->commandList = new tList<ShellCommand>;
66
67  this->rebuildText();
68
69  // EVENT-Handler subscription of '`' to all States.
70  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
71
72}
73
74Shell* Shell::singletonRef = NULL;
75
76/**
77 * standard deconstructor
78 */
79Shell::~Shell ()
80{
81  // delete the displayable Buffers
82  for (int i = 0; i < this->bufferDisplaySize; i++)
83    delete this->bufferText[i];
84  delete[] this->bufferText;
85
86  // delete the inputLine
87  delete this->shellInput;
88
89  Shell::singletonRef = NULL;
90}
91
92/**
93 * activates the shell
94 *
95 * This also feeds the Last few lines from the main buffers into the displayBuffer
96 */
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);
105
106  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
107  for (int i = 0; i < this->bufferDisplaySize; i++)
108    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true);
109}
110
111/**
112 * deactiveates the Shell.
113 */
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);
122
123  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
124  for (int i = 0; i < this->bufferDisplaySize; i++)
125    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false);
126}
127
128
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 */
137void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
138{
139  this->textSize = textSize;
140  this->lineSpacing = lineSpacing;
141
142  this->rebuildText();
143}
144
145/**
146 * rebuilds the Text's
147 *
148 * use this function, if you changed the Font/Size or something else.
149 */
150void Shell::rebuildText()
151{
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);
158
159  this->setBufferDisplaySize(this->bufferDisplaySize);
160}
161
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 */
166void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
167{
168  if (this->bufferText != NULL)
169  {
170    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
171      delete this->bufferText[i];
172    delete[] this->bufferText;
173  }
174
175  this->bufferText = new Text*[bufferDisplaySize];
176  for (unsigned int i = 0; i < bufferDisplaySize; i++)
177  {
178    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC);
179    this->bufferText[i]->setColor(1, 0, 0);
180    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
181    this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
182    this->bufferText[i]->setText(NULL);
183    this->bufferText[i]->setParent2D(this);
184  }
185  this->bufferDisplaySize = bufferDisplaySize;
186
187  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
188}
189
190/**
191 * deletes all the Buffers
192 */
193void Shell::flush()
194{
195  // remove all chars from the BufferTexts.
196  if (this->bufferText)
197    for (int i = 0; i < this->bufferDisplaySize; i++)
198    {
199      this->bufferText[i]->setText(NULL, true);
200    }
201
202
203    // BUFFER FLUSHING
204}
205
206/**
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];
215
216    Text* swapText;
217    Text* moveText = this->bufferText[0];
218    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
219    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
220    {
221      if ( i < this->bufferDisplaySize-1)
222        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
223      swapText = this->bufferText[i];
224      this  ->bufferText[i] = moveText;
225      moveText = swapText;
226    }
227    lastText->setRelCoor2D(this->calculateLinePosition(0));
228    this->bufferText[0] = lastText;
229
230    this->bufferText[0]->setText(text, true);
231  }
232}
233
234/**
235 * clears the Shell (empties all buffers)
236 */
237void Shell::clear()
238{
239  this->flush();
240  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
241}
242
243
244
245/**
246 * listens for some event
247 * @param event the Event happened
248 */
249void Shell::process(const Event &event)
250{
251  if (event.bPressed)
252  {
253    if (event.type == SDLK_BACKQUOTE)
254    {
255      if (EventHandler::getInstance()->getState() == ES_GAME)
256        this->activate();
257      else
258        this->deactivate();
259    }
260  }
261}
262
263/**
264 * displays the Shell
265 */
266void Shell::draw() const
267{
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
277  glBindTexture(GL_TEXTURE_2D, 0);
278  glBegin(GL_TRIANGLE_STRIP);
279
280  glTexCoord2f(0, 0);
281  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
282
283  glTexCoord2f(1, 0);
284  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
285
286  glTexCoord2f(0, 1);
287  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
288
289  glTexCoord2f(1, 1);
290  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
291
292  glEnd();
293}
294
295///////////////////////
296// HELPER FUNCTIONS  //
297///////////////////////
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 */
304Vector Shell::calculateLinePosition(unsigned int lineNumber)
305{
306  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
307}
308
309
310
311/**
312 * displays some nice output from the Shell
313 */
314void Shell::debug() const
315{
316  PRINT(3)("Debugging output to console (not this shell)\n");
317
318//   if (this->pressedKey != SDLK_FIRST)
319//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
320
321
322  ShellBuffer::getInstance()->debug();
323}
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.