Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moving up and down through the buffer with [PageUp] and [PageDown] enabled

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