Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Shell Input is totally out of shell.cc/h

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)->describe("Clears the shell from unwanted lines (empties all buffers)");
38SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)");
39
40/**
41 * standard constructor
42 */
43Shell::Shell ()
44{
45  this->setClassID(CL_SHELL, "Shell");
46  this->setName("Shell");
47
48  this->setAbsCoor2D(3, -400);
49  this->textSize = 15;
50  this->lineSpacing = 5;
51  this->bActive = false;
52
53
54  // BUFFER
55  this->bufferText = NULL;
56  this->setBufferDisplaySize(10);
57
58  // INPUT LINE
59  this->shellInput = new ShellInput;
60  //this->commandList = new tList<ShellCommand>;
61
62  this->rebuildText();
63  this->completionList = NULL;
64
65  // EVENT-Handler subscription of '`' to all States.
66  EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
67
68}
69
70Shell* Shell::singletonRef = NULL;
71
72/**
73 * standard deconstructor
74 */
75Shell::~Shell ()
76{
77  // delete the displayable Buffers
78  for (int i = 0; i < this->bufferDisplaySize; i++)
79    delete this->bufferText[i];
80  delete[] this->bufferText;
81
82  // delete the inputLine
83  delete this->shellInput;
84
85  Shell::singletonRef = NULL;
86}
87
88/**
89 * activates the shell
90 *
91 * This also feeds the Last few lines from the main buffers into the displayBuffer
92 */
93void Shell::activate()
94{
95  if (this->bActive == true)
96    PRINTF(3)("The shell is already active\n");
97  this->bActive = true;
98
99  EventHandler::getInstance()->setState(ES_SHELL);
100  this->setRelCoorSoft2D(0, 0, 1, 5);
101
102  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
103  for (int i = 0; i < this->bufferDisplaySize; i++)
104    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true);
105}
106
107/**
108 * deactiveates the Shell.
109 */
110void Shell::deactivate()
111{
112  if (this->bActive == false)
113    PRINTF(3)("The shell is already inactive\n");
114  this->bActive = false;
115
116  EventHandler::getInstance()->setState(ES_GAME);
117  this->setRelCoorSoft2D(0, -400, 1, 5);
118
119  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
120  for (int i = 0; i < this->bufferDisplaySize; i++)
121    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false);
122}
123
124
125/**
126 * sets the size of the text and spacing
127 * @param textSize the size of the Text in Pixels
128 * @param lineSpacing the size of the Spacing between two lines in pixels
129 *
130 * this also rebuilds the entire Text, inputLine and displayBuffer,
131 * to be accurate again.
132 */
133void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
134{
135  this->textSize = textSize;
136  this->lineSpacing = lineSpacing;
137
138  this->rebuildText();
139}
140
141/**
142 * rebuilds the Text's
143 *
144 * use this function, if you changed the Font/Size or something else.
145 */
146void Shell::rebuildText()
147{
148  this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize);
149  this->shellInput->setColor(1, 0, 0);
150  this->shellInput->setAlignment(TEXT_ALIGN_LEFT);
151  this->shellInput->setText(NULL);
152  this->shellInput->setParent2D(this);
153  this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize);
154
155  this->setBufferDisplaySize(this->bufferDisplaySize);
156}
157
158/**
159 * sets The count of Lines to display in the buffer.
160 * @param bufferDisplaySize the count of lines to display in the Shell-Buffer.
161 */
162void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
163{
164  if (this->bufferText != NULL)
165  {
166    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
167      delete this->bufferText[i];
168    delete[] this->bufferText;
169  }
170
171  this->bufferText = new Text*[bufferDisplaySize];
172  for (unsigned int i = 0; i < bufferDisplaySize; i++)
173  {
174    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC);
175    this->bufferText[i]->setColor(1, 0, 0);
176    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
177    this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
178    this->bufferText[i]->setText(NULL);
179    this->bufferText[i]->setParent2D(this);
180  }
181  this->bufferDisplaySize = bufferDisplaySize;
182
183  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
184}
185
186/**
187 * deletes all the Buffers
188 */
189void Shell::flush()
190{
191  // remove all chars from the BufferTexts.
192  if (this->bufferText)
193    for (int i = 0; i < this->bufferDisplaySize; i++)
194    {
195      this->bufferText[i]->setText(NULL, true);
196    }
197
198
199    // BUFFER FLUSHING
200}
201
202/**
203 * prints out some text to the input-buffers
204 * @param text the text to output.
205 */
206void Shell::printToDisplayBuffer(const char* text)
207{
208  if(likely(bufferText != NULL))
209  {
210    Text* lastText = this->bufferText[this->bufferDisplaySize-1];
211
212    Text* swapText;
213    Text* moveText = this->bufferText[0];
214    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
215    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
216    {
217      if ( i < this->bufferDisplaySize-1)
218        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
219      swapText = this->bufferText[i];
220      this  ->bufferText[i] = moveText;
221      moveText = swapText;
222    }
223    lastText->setRelCoor2D(this->calculateLinePosition(0));
224    this->bufferText[0] = lastText;
225
226    this->bufferText[0]->setText(text, true);
227  }
228}
229
230/**
231 * clears the Shell (empties all buffers)
232 */
233void Shell::clear()
234{
235  this->flush();
236  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
237}
238
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
322
323// void Shell::testI (int i)
324// {
325//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
326// }
327//
328// void Shell::testS (const char* s)
329// {
330//   PRINTF(3)("This is the Test for one String '%s'\n", s);
331// }
332//
333// void Shell::testB (bool b)
334// {
335//   PRINTF(3)("This is the Test for one Bool: ");
336//   if (b)
337//     PRINTF(3)("true\n");
338//   else
339//     PRINTF(3)("false\n");
340// }
341//
342// void Shell::testF (float f)
343// {
344//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
345// }
346//
347// void Shell::testSF (const char* s, float f)
348// {
349//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
350// }
Note: See TracBrowser for help on using the repository browser.