Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minimalized the ShellBuffer

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