Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: autoCompletion now gives a very nice output

File size: 8.8 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
59  // Element2D and generals
60  this->setAbsCoor2D(3, -400);
61  this->textSize = 15;
62  this->lineSpacing = 0;
63  this->bActive = false;
64
65  // BUFFER
66  this->bufferText = NULL;
67  this->bufferDisplaySize = 10;
68
69  // INPUT LINE
70  this->shellInput = new ShellInput;
71  //this->commandList = new tList<ShellCommand>;
72
73  this->rebuildText();
74
75}
76
77/**
78 * standard deconstructor
79 */
80Shell::~Shell ()
81{
82  // delete the displayable Buffers
83  for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
84    delete this->bufferText[i];
85  delete[] this->bufferText;
86
87  // delete the inputLine
88  delete this->shellInput;
89  ShellBuffer::getInstance()->unregisterShell(this);
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 (unsigned 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  this->shellInput->setFont("fonts/dpquake_.ttf", this->textSize);
142
143//  this->rebuildText();
144}
145
146/**
147 * rebuilds the Text's
148 *
149 * use this function, if you changed the Font/Size or something else.
150 */
151void Shell::rebuildText()
152{
153  this->shellInput->setFont("fonts/dpquake_.ttf", this->textSize);
154  this->shellInput->setColor(1, 0, 0);
155  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
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/dpquake_.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  // BUFFER FLUSHING
202}
203
204/**
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];
213
214    Text* swapText;
215    Text* moveText = this->bufferText[0];
216    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
217    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
218    {
219      if ( i < this->bufferDisplaySize-1)
220        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
221      swapText = this->bufferText[i];
222      this  ->bufferText[i] = moveText;
223      moveText = swapText;
224    }
225    lastText->setRelCoor2D(this->calculateLinePosition(0));
226    this->bufferText[0] = lastText;
227
228    this->bufferText[0]->setText(text, true);
229  }
230}
231
232/**
233 * clears the Shell (empties all buffers)
234 */
235void Shell::clear()
236{
237  this->flush();
238  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
239}
240
241/**
242 * listens for some event
243 * @param event the Event happened
244 */
245void Shell::process(const Event &event)
246{
247  if (event.bPressed)
248  {
249    if (event.type == SDLK_BACKQUOTE)
250    {
251      if (EventHandler::getInstance()->getState() == ES_GAME)
252        this->activate();
253      else
254        this->deactivate();
255    }
256  }
257}
258
259/**
260 * displays the Shell
261 */
262void Shell::draw() const
263{
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
273  glBindTexture(GL_TEXTURE_2D, 0);
274  glBegin(GL_TRIANGLE_STRIP);
275
276  glTexCoord2f(0, 0);
277  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
278
279  glTexCoord2f(1, 0);
280  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
281
282  glTexCoord2f(0, 1);
283  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
284
285  glTexCoord2f(1, 1);
286  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
287
288  glEnd();
289}
290
291///////////////////////
292// HELPER FUNCTIONS  //
293///////////////////////
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 */
300Vector Shell::calculateLinePosition(unsigned int lineNumber)
301{
302  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
303}
304
305
306
307/**
308 * displays some nice output from the Shell
309 */
310void Shell::debug() const
311{
312  PRINT(3)("Debugging output to console (not this shell)\n");
313
314//   if (this->pressedKey != SDLK_FIRST)
315//     printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
316
317
318  ShellBuffer::getInstance()->debug();
319}
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.