Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed an error in ShellDisplay: did not show the last line on first open

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