Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellInput is now almost perfectly extern.
ShellCompletion taken out.
Working again :)

File size: 11.1 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5068]18#include "shell.h"
[5129]19#include "shell_command.h"
[5175]20#include "shell_buffer.h"
[5179]21#include "shell_input.h"
[1853]22
[5175]23
[5072]24#include "text_engine.h"
25#include "list.h"
[5093]26#include "graphics_engine.h"
27#include "event_handler.h"
[5129]28#include "debug.h"
[5113]29#include "class_list.h"
30
31#include "key_names.h"
[5075]32#include <stdarg.h>
33#include <stdio.h>
34
[1856]35using namespace std;
[1853]36
[5164]37SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)");
[5172]38SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)");
[1856]39
[3245]40/**
[4838]41 * standard constructor
[5068]42 */
43Shell::Shell ()
[3365]44{
[5072]45  this->setClassID(CL_SHELL, "Shell");
46  this->setName("Shell");
47
[5175]48  this->setAbsCoor2D(3, -400);
49  this->textSize = 15;
50  this->lineSpacing = 5;
[5113]51  this->bActive = false;
[5072]52
[5129]53
[5175]54  // BUFFER
55  this->bufferText = NULL;
56  this->setBufferDisplaySize(10);
[5080]57
[5175]58  // INPUT LINE
[5179]59  this->shellInput = new ShellInput;
[5095]60  this->delayed = 0;
[5179]61  this->shellInput->setRepeatDelay(.3, .05);
[5175]62  //this->commandList = new tList<ShellCommand>;
[5093]63
[5113]64  this->rebuildText();
65  this->completionList = NULL;
[5093]66
[5113]67  // EVENT-Handler subscription of '`' to all States, and all other keyboard commands to ES_SEHLL
[5093]68  EventHandler* evh = EventHandler::getInstance();
[5096]69  evh->subscribe(this, ES_ALL, SDLK_BACKQUOTE);
[5119]70  for (int i = 1; i < SDLK_LAST; i++)
[5095]71    evh->subscribe(this, ES_SHELL, i);
[5068]72}
[4320]73
[5068]74Shell* Shell::singletonRef = NULL;
[1853]75
[3245]76/**
[4838]77 * standard deconstructor
[5068]78 */
79Shell::~Shell ()
[3543]80{
[5099]81  // delete the displayable Buffers
[5080]82  for (int i = 0; i < this->bufferDisplaySize; i++)
83    delete this->bufferText[i];
[5113]84  delete[] this->bufferText;
[5093]85
[5099]86  // delete the inputLine
[5079]87
[5068]88  Shell::singletonRef = NULL;
[3543]89}
[5068]90
[5119]91/**
92 * activates the shell
93 *
94 * This also feeds the Last few lines from the main buffers into the displayBuffer
95 */
[5113]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);
[5118]104
[5175]105  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
[5118]106  for (int i = 0; i < this->bufferDisplaySize; i++)
[5175]107    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true);
[5113]108}
109
[5119]110/**
111 * deactiveates the Shell.
112 */
[5113]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);
[5118]121
[5175]122  ShellBuffer::getInstance()->getBufferIterator()->lastElement();
[5157]123  for (int i = 0; i < this->bufferDisplaySize; i++)
[5175]124    this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false);
[5113]125}
126
[5158]127
[5119]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 */
[5113]136void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing)
137{
138  this->textSize = textSize;
139  this->lineSpacing = lineSpacing;
140
141  this->rebuildText();
142}
143
[5127]144/**
145 * rebuilds the Text's
146 *
147 * use this function, if you changed the Font/Size or something else.
148 */
[5113]149void Shell::rebuildText()
150{
[5179]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);
[5113]157
158  this->setBufferDisplaySize(this->bufferDisplaySize);
159}
160
[5074]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 */
[5072]165void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize)
166{
[5080]167  if (this->bufferText != NULL)
[5072]168  {
[5080]169    for (unsigned int i = 0; i < this->bufferDisplaySize; i++)
170      delete this->bufferText[i];
[5113]171    delete[] this->bufferText;
[5072]172  }
[5080]173
174  this->bufferText = new Text*[bufferDisplaySize];
175  for (unsigned int i = 0; i < bufferDisplaySize; i++)
[5072]176  {
[5122]177    this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC);
[5121]178    this->bufferText[i]->setColor(1, 0, 0);
[5080]179    this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT);
[5120]180    this->bufferText[i]->setRelCoor2D(calculateLinePosition(i));
[5080]181    this->bufferText[i]->setText(NULL);
[5089]182    this->bufferText[i]->setParent2D(this);
[5072]183  }
[5113]184  this->bufferDisplaySize = bufferDisplaySize;
[5111]185
[5113]186  this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1);
[5072]187}
[5068]188
189/**
190 * deletes all the Buffers
191 */
[5175]192void Shell::flush()
[5068]193{
[5072]194  // remove all chars from the BufferTexts.
[5080]195  if (this->bufferText)
[5125]196    for (int i = 0; i < this->bufferDisplaySize; i++)
[5080]197    {
[5125]198      this->bufferText[i]->setText(NULL, true);
[5080]199    }
[5068]200
[5072]201
[5175]202    // BUFFER FLUSHING
[5068]203}
204
205/**
[5118]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];
[5113]214
[5118]215    Text* swapText;
216    Text* moveText = this->bufferText[0];
[5120]217    this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10);
[5118]218    for (unsigned int i = 1; i < this->bufferDisplaySize; i++)
219    {
220      if ( i < this->bufferDisplaySize-1)
[5120]221        this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5);
[5118]222      swapText = this->bufferText[i];
223      this  ->bufferText[i] = moveText;
224      moveText = swapText;
225    }
[5120]226    lastText->setRelCoor2D(this->calculateLinePosition(0));
[5118]227    this->bufferText[0] = lastText;
228
[5122]229    this->bufferText[0]->setText(text, true);
[5118]230  }
[5068]231}
232
233/**
[5166]234 * clears the Shell (empties all buffers)
235 */
[5130]236void Shell::clear()
237{
[5175]238  this->flush();
239  ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL);
[5130]240}
241
[5097]242
243
[5069]244/**
245 * listens for some event
246 * @param event the Event happened
247 */
248void Shell::process(const Event &event)
249{
[5093]250  if (event.bPressed)
251  {
[5141]252    PRINTF(5)("Shell received command %s\n", SDLKToKeyname(event.type));
[5093]253    if (event.type == SDLK_BACKQUOTE)
254    {
255      if (EventHandler::getInstance()->getState() == ES_GAME)
[5113]256        this->activate();
[5093]257      else
[5113]258        this->deactivate();
[5093]259    }
[5119]260    else if (event.type == SDLK_F1)
261      this->help();
262    else if (event.type == SDLK_F2)
263      this->debug();
[5093]264    else if (event.type == SDLK_TAB)
[5179]265      ;//this->autoComplete();
[5093]266    else if (event.type == SDLK_BACKSPACE)
[5095]267    {
268      this->delayed = this->repeatDelay;
269      this->pressedKey = SDLK_BACKSPACE;
[5179]270      this->shellInput->removeCharacters(1);
[5095]271    }
[5096]272    else if (event.type == SDLK_RETURN)
[5179]273      this->shellInput->executeCommand();
[5129]274    /*
275    else if (event.type == SDLK_UP)
276    {
277//      this->flushInputLine();
278      tIterator<char>* iterator = this->commandList->getIterator();
279      char* command = iterator->lastElement();
280      while (command)
281      {
282        if (!strcmp (command, inputLine))
283        {
284          inputLine = iterator->prevElement();
285          return;
286        }
287        command = iterator->prevElement();
288      }
289      inputLine = iterator->lastElement();
290    }
291    */
[5097]292    else if (likely(event.type < 127))
[5095]293    {
[5128]294      Uint8 *keystate = SDL_GetKeyState(NULL);
[5095]295      this->delayed = this->repeatDelay;
[5128]296      if (unlikely( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] ))
297      {
298        this->pressedKey = event.type-32;
[5179]299        this->shellInput->addCharacter(event.type-32);
[5128]300      }
301      else
302      {
303        this->pressedKey = event.type;
[5179]304        this->shellInput->addCharacter(event.type);
[5128]305      }
[5095]306    }
[5093]307  }
[5095]308  else // if(!event.bPressed)
309  {
[5128]310    if (this->pressedKey == event.type || (this->pressedKey == event.type - 32))
[5113]311    {
[5095]312      this->pressedKey = SDLK_FIRST;
[5113]313      this->delayed = 0.0;
314    }
[5095]315  }
[5069]316}
317
[5068]318/**
319 * ticks the Shell for dt Seconds
320 * @param dt the elapsed time since the last tick();
321 */
[5095]322void Shell::tick(float dt)
323{
324  if (this->delayed > 0.0)
325    this->delayed -= dt;
326  else if (this->pressedKey != SDLK_FIRST )
327  {
[5097]328    this->delayed = this->repeatRate;
[5095]329    if (this->pressedKey == SDLK_BACKSPACE)
[5179]330      this->shellInput->removeCharacters(1);
[5095]331    else if (pressedKey < 127)
[5179]332      this->shellInput->addCharacter(this->pressedKey);
[5095]333  }
334}
[5068]335/**
336 * displays the Shell
337 */
338void Shell::draw() const
339{
[5099]340  glPushMatrix();
341  // transform for alignment.
342  // setting the Blending effects
343
344  glColor4f(0.0f, 0.0f, 0.8f, .4);
345  glEnable(GL_BLEND);
346  glDisable(GL_TEXTURE_2D);
347  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
348
[5158]349  glBindTexture(GL_TEXTURE_2D, 0);
350  glBegin(GL_TRIANGLE_STRIP);
[5099]351
[5158]352  glTexCoord2f(0, 0);
[5099]353  glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
354
[5158]355  glTexCoord2f(1, 0);
[5113]356  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().);
[5099]357
[5158]358  glTexCoord2f(0, 1);
359  glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
360
361  glTexCoord2f(1, 1);
[5113]362  glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight);
[5099]363
364  glEnd();
[5068]365}
366
367/**
[5166]368 * prints out some nice help about the Shell
369 */
[5119]370void Shell::help() const
371{
372  PRINT(0)("Help for the most important Shell-commands\n");
373  PRINT(0)("F1 - HELP; F2 - DEBUG; ` - open/close shell\n");
374  PRINT(0)("input order:\n");
375  PRINT(0)("ClassName::objectName function [parameter1, [parameter2 ...]]  or\n");
376  PRINT(0)("Command [parameter]\n");
377}
378
[5120]379
380///////////////////////
381// HELPER FUNCTIONS  //
382///////////////////////
[5166]383
384/**
385 * calculates the position of a Buffer-Display Line
386 * @param lineNumber the lineNumber from the bottom to calculate the position from
387 * @returns the Position of the Line.
388 */
[5120]389Vector Shell::calculateLinePosition(unsigned int lineNumber)
390{
[5124]391  return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0);
[5120]392}
393
394
395
[5113]396/**
[5068]397 * displays some nice output from the Shell
398 */
399void Shell::debug() const
400{
[5119]401  PRINT(3)("Debugging output to console (not this shell)\n");
402
[5096]403  if (this->pressedKey != SDLK_FIRST)
404    printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay);
[5119]405
406
[5177]407  ShellBuffer::getInstance()->debug();
[5068]408}
[5166]409
410
411
412// void Shell::testI (int i)
413// {
414//   PRINTF(3)("This is the Test for one Int '%d'\n", i);
415// }
416//
417// void Shell::testS (const char* s)
418// {
419//   PRINTF(3)("This is the Test for one String '%s'\n", s);
420// }
421//
422// void Shell::testB (bool b)
423// {
424//   PRINTF(3)("This is the Test for one Bool: ");
425//   if (b)
426//     PRINTF(3)("true\n");
427//   else
428//     PRINTF(3)("false\n");
429// }
430//
431// void Shell::testF (float f)
432// {
433//   PRINTF(3)("This is the Test for one Float '%f'\n", f);
434// }
435//
436// void Shell::testSF (const char* s, float f)
437// {
438//   PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f);
439// }
Note: See TracBrowser for help on using the repository browser.