Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Shell now has a real nice help function

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