home *** CD-ROM | disk | FTP | other *** search
- #include "ConsoleFrontEnd.h"
- #include "ConsoleInfo.h"
- #include "Display.h"
- #include "Renderer.h"
- #include "vectormath.h"
- #include "SDL.h"
- #include "DisplayInfo.h"
- #include "Input.h"
- #include "Tokenizer.h"
- #include "TextureHandler.h"
- #include "ShaderHandler.h"
- #include "FontHandler.h"
- #include "Game.h"
- #include "version.h"
- #include <string.h>
-
- #include "log.h"
-
-
- #define CONSOLE_FRONT_END_FONT_SCALE 0.7f
- #define cprint(x,y,str) drawScaledString(x, y, CONSOLE_FRONT_END_FONT_SCALE, CONSOLE_FRONT_END_FONT_SCALE, font, str);
-
- ConsoleFrontEnd* consoleFrontEnd=NULL;
- //con_t con;
- /*
- bool initConsoleFE(){
- }
-
- bool shutdownConsoleFE(){
- }
- */
-
- ConsoleFrontEnd::ConsoleFrontEnd(Console* console){
- this->console=console;
-
- promptStr=">> ";
- inputStr=new char[CON_MAX_STRING_LENGTH];
- inputStr[0]='\0';
- inputStrIndex=0;
-
- for(int i=0;i<CON_MAX_HISTORY_LINES;i++){
- historyLines[i] = NULL;
- }
- inputHistoryIndex=-1;
- scrollIndex=0;
-
- isActive=false;
- height = DISPLAY_VSCREEN_HEIGHT/3;
- lineSpacing=1;
-
- // ConsoleInfo::cvar.con_FE_font->updateVar();
- font=FontHandler::getFont("gui/fonts/courier_small_bold.font");
- if(font==NULL){
- error("(in ConsoleFrontEnd::ConsoleFrontEnd()): couldn't load console font.\n\n");
- }
- /*
- ConsoleInfo::cvar.console_backgroundTexture->updateVar();
- backgroundTex=TextureHandler::getTexture(ConsoleInfo::var.backgroundTexture);
- if(backgroundTex==NULL){
- error("(in ConsoleFrontEnd::ConsoleFrontEnd()): couldn't load background texture '%s'.\n\n", ConsoleInfo::var.backgroundTexture);
- }
- */
- backgroundShader=ShaderHandler::getShader("gui/console/console_background.shader");
- // if(backgroundShader==NULL){
- // error("(in ConsoleFrontEnd::ConsoleFrontEnd()): couldn't load background Shader '%s'.\n\n", "arsch");
- // }
-
- // NOT USED (ConsoleInfo::var.xyColor is used!!!
- vectorInit4d(1.0f, 1.0f, 1.0f, 0.8f, backgroundColor);
- vectorInit4d(0.0f, 0.5f, 1.0f, 1.0f, fontColor);
- }
-
-
- ConsoleFrontEnd::~ConsoleFrontEnd(){
- delete[] inputStr;
-
- if(font!=NULL)
- FontHandler::releaseFont(font);
-
- if(backgroundTex!=NULL)
-
- TextureHandler::releaseTexture(backgroundTex);
-
- if(backgroundShader!=NULL)
- ShaderHandler::releaseShader(backgroundShader);
-
- clearHistory();
- }
-
- void ConsoleFrontEnd::activate(){
- SDL_EnableUNICODE(1);
- SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
- height = Display::info.var.height/3;
- isActive=true;
- }
-
- void ConsoleFrontEnd::deactivate(){
- SDL_EnableUNICODE(0);
-
- SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
- isActive=false;
- }
-
- void ConsoleFrontEnd::receiveKey(SDL_keysym* keysym){
- int i,j;
- SDLKey key=keysym->sym;
-
- if(key==SDLK_ESCAPE){
- //deactivate();
- }else if(key==SDLK_RIGHT){
- if(inputStr[inputStrIndex]!='\0'){
- inputStrIndex++;
- }
- }else if(key==SDLK_LEFT){
- if(inputStrIndex>0){
- inputStrIndex--;
- }
- }else if(key==SDLK_UP){
- if(inputHistoryIndex+1<CON_MAX_HISTORY_LINES && historyLines[inputHistoryIndex+1]!=NULL){
- inputHistoryIndex++;
- strcpy(inputStr, historyLines[inputHistoryIndex]);
- inputStrIndex=strlen(inputStr);
- }
- }else if(key==SDLK_DOWN){
- if(inputHistoryIndex>0 && historyLines[inputHistoryIndex-1]!=NULL){
- inputHistoryIndex--;
- strcpy(inputStr, historyLines[inputHistoryIndex]);
- inputStrIndex=strlen(inputStr);
- }else if(inputHistoryIndex==0){
- inputHistoryIndex--; // -1 dann!
- inputStr[0]='\0';
- inputStrIndex=0;
- }
- }else if(key==SDLK_PAGEDOWN && !(keysym->mod & KMOD_CTRL) ){ // scroll down one line
- if(scrollIndex>0)
- scrollIndex--;
- }else if(key==SDLK_PAGEUP && !(keysym->mod & KMOD_CTRL) ){ // scroll up one line
- if(scrollIndex<CON_MAX_LINES && console->lines[scrollIndex]!=NULL) // THINKABOUTME: scheisse, wenn unterwegs eine line NULL ist
- scrollIndex++;
- }else if(key==SDLK_PAGEDOWN && (keysym->mod & KMOD_CTRL) ){ // scroll down 10 lines
- scrollIndex = scrollIndex-10>0 ? scrollIndex-10 : 0;
- }else if(key==SDLK_PAGEUP && (keysym->mod & KMOD_CTRL) ){ // scroll up 10 lines
- i=0;
- while(i<10 && scrollIndex<CON_MAX_LINES && console->lines[scrollIndex]!=NULL){ // THINKABOUTME: scheisse, wenn unterwegs eine line NULL ist
- scrollIndex++;
- i++;
- }
- }else if(key==SDLK_HOME && (keysym->mod & KMOD_CTRL) ){ // scroll to top
- while(scrollIndex<CON_MAX_LINES && console->lines[scrollIndex]!=NULL){ // THINKABOUTME: scheisse, wenn unterwegs eine line NULL ist
- scrollIndex++;
- }
- }else if(key==SDLK_END && (keysym->mod & KMOD_CTRL) ){ // scroll to bottom
- scrollIndex=0;
-
- }else if(key==SDLK_HOME && !(keysym->mod & KMOD_CTRL) ){
- inputStrIndex=0;
- }else if(key==SDLK_END && !(keysym->mod & KMOD_CTRL) ){
- inputStrIndex=strlen(inputStr);
- }else if(key==SDLK_RETURN){ // enter
- if(strlen(inputStr) > 0)
- appendHistoryLine(inputStr);
-
- char buff[CON_MAX_STRING_LENGTH];
- strncpy(buff, inputStr, CON_MAX_STRING_LENGTH);
- inputStr[0] = '\0';
- inputStrIndex = 0;
- inputHistoryIndex = -1;
-
- if(buff[0] == '/'){
- sendToConsole(buff+1);
- }else{
- if( !Game::wasInit() || strlen(buff) == 0){
- sendToConsole(buff);
- }else{
- Game::chatMessageAll(buff);
- }
- }
- }else if(key==SDLK_BACKSPACE){ // backspace
- if(inputStrIndex<=0)
- return;
-
- i=inputStrIndex-1;
- while(inputStr[i]!='\0'){
- inputStr[i]=inputStr[i+1];
- i++;
- }
- inputStrIndex-=1;
- }else if(key==SDLK_DELETE){ // del
- if(inputStr[inputStrIndex]=='\0')
- return;
-
- i=inputStrIndex;
- while(inputStr[i]!='\0'){
- inputStr[i]=inputStr[i+1];
- i++;
- }
- }else if(key==SDLK_TAB){ // tab -> auto complete
- if(strlen(inputStr)>0){
- // prepend a '/' if not already done
- if(inputStr[0] != '/'){
- int l=strlen(inputStr);
- if(l < CON_MAX_STRING_LENGTH){
- for(i=l+1 ; i>=1 ; i--){
- inputStr[i] = inputStr[i-1];
- }
- inputStr[0] = '/';
- }
- // printf("string: '%s'\n", inputStr);
- }
-
- // ignore the '/' for testing...
- char* cmpStr;
- cmpStr = inputStr + 1;
-
- console->print("TAB suggests:\n");
- int count=0;
- const char* names[CON_MAX_CCMDS+CON_MAX_CVARS+CON_MAX_ALIASES];
- for(i=0;i<CON_MAX_CCMDS;i++){
- if(console->cCmds[i]!=NULL && !strncmp(cmpStr, console->cCmds[i]->name, strlen(cmpStr))){
- console->print(" -> %s (%s)\n", console->cCmds[i]->name, console->cCmds[i]->infoStr);
- names[count]=console->cCmds[i]->name;
- count++;
- }
- }
- for(i=0;i<CON_MAX_CVARS;i++){
- if(console->cVars[i]!=NULL && !strncmp(cmpStr, console->cVars[i]->name, strlen(cmpStr))){
- console->print(" -> %s\n", console->cVars[i]->name /*, console->cVars[i]->infoStr*/);
- names[count]=console->cVars[i]->name;
- count++;
- }
- }
- for(i=0;i<CON_MAX_ALIASES;i++){
- if(console->aliases[i]!=NULL && !strncmp(cmpStr, console->aliases[i]->name, strlen(cmpStr))){
- console->print(" -> %s (alias for '%s')\n", console->aliases[i]->name , console->aliases[i]->string);
- names[count] = console->aliases[i]->name;
- count++;
- }
- }
-
- if(count>0){
- int firstDiff=99999;
- for(i=0;i<count;i++){
-
- for(j=0;j<count;j++){
- int n = strlen(cmpStr);
- while(!strncmp(names[i], names[j], n) && (unsigned int)n<=strlen(names[i]) && (unsigned int)n<=strlen(names[j])){
- n++;
- }
- firstDiff = n<firstDiff ? n : firstDiff;
- }
- }
-
- strncpy(cmpStr, names[0], firstDiff);
- cmpStr[firstDiff-1] = '\0';
- }
- inputStrIndex = strlen(inputStr);
- }
- }else{ // add char to inputStr
- int l=strlen(inputStr);
- if(l < CON_MAX_STRING_LENGTH){
- char ch;
- if ( (keysym->unicode & 0xFF80) == 0 && keysym->unicode!=0) {
- ch = keysym->unicode & 0x7F;
- for(i=l+1;i>inputStrIndex;i--){
- inputStr[i]=inputStr[i-1];
- }
-
- inputStr[inputStrIndex]=ch;
- inputStrIndex++;
- //inputStr[inputStrIndex+1]='\0';
- //inputStrIndex = inputStrIndex >= CON_MAX_LINE_LENGTH-1 ? CON_MAX_LINE_LENGTH-1 : inputStrIndex+1;
- }else{
- // warn("(in ConsoleFrontEnd::recieveKey()): unicode of key not known!\n\n");
- }
- }
- }
-
- }
-
-
- void ConsoleFrontEnd::sendToConsole(char* str){
- console->input(str);
- }
-
- void ConsoleFrontEnd::clearInputString(){
- inputStr[0]='\0';
- inputStrIndex=0;
- inputHistoryIndex=-1;
- }
-
- void ConsoleFrontEnd::draw(){
- height = DISPLAY_VSCREEN_HEIGHT/3;
-
- Renderer::beginDrawing();
-
- drawBackground();
- drawLines();
- drawPrompt();
-
- Renderer::endDrawing();
- }
-
- void ConsoleFrontEnd::drawBackground(){
- backgroundShader->setup(SDL_GetTicks());
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 1.0f);
- if(_glMultiTexCoord2fARB!=NULL)
- _glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, 1.0f);
- glVertex2i(0, DISPLAY_VSCREEN_HEIGHT );
-
- glTexCoord2f(1.0, 1.0);
- if(_glMultiTexCoord2fARB!=NULL)
- _glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0f, 1.0f);
- glVertex2i(DISPLAY_VSCREEN_WIDTH, DISPLAY_VSCREEN_HEIGHT );
-
-
- glTexCoord2f(1.0, 0.0);
- if(_glMultiTexCoord2fARB!=NULL)
- _glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0f, 0.0f);
- glVertex2i(DISPLAY_VSCREEN_WIDTH, DISPLAY_VSCREEN_HEIGHT - height);
-
-
- glTexCoord2f(0.0, 0.0);
- if(_glMultiTexCoord2fARB!=NULL)
- _glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, 0.0f);
- glVertex2i(0, DISPLAY_VSCREEN_HEIGHT - height);
- glEnd();
- backgroundShader->setdown();
-
-
- glColor4fv(ConsoleInfo::var.fontColor);
- glBegin(GL_LINES);
- glVertex2i(0, DISPLAY_VSCREEN_HEIGHT - height);
- glVertex2i(DISPLAY_VSCREEN_WIDTH, DISPLAY_VSCREEN_HEIGHT - height);
- glEnd();
- // renderBitmapFormatString(screen.width-10, DisplayInfo::var.height-height+5, GLUT_BITMAP_TIMES_ROMAN_10, TEXT_ALIGN_RIGHT, "MAUSS version: %s", MAUSS_VERSION);
- drawScaledAndAlignedFormatString(DISPLAY_VSCREEN_WIDTH-3, DISPLAY_VSCREEN_HEIGHT-height+3, CONSOLE_FRONT_END_FONT_SCALE*0.9f, CONSOLE_FRONT_END_FONT_SCALE*0.9f,
- font, TEXT_ALIGN_RIGHT, "FWP v%s (%s)", FWP_VERSION_STRING, FWP_OS_STRING);
-
- }
-
- void ConsoleFrontEnd::drawPrompt(){
-
- glColor4fv(ConsoleInfo::var.fontColor);
- drawScaledFormatString(10, DISPLAY_VSCREEN_HEIGHT-height+5, CONSOLE_FRONT_END_FONT_SCALE, CONSOLE_FRONT_END_FONT_SCALE,
- font, "%s%s", promptStr, inputStr);
-
- if((SDL_GetTicks()/500)%2){
- char buff[CON_MAX_STRING_LENGTH];
- strncpy(buff, inputStr, inputStrIndex);
- buff[inputStrIndex] = '\0';
- float xOffs = (getStringWidth(font, promptStr)+getStringWidth(font, buff))*CONSOLE_FRONT_END_FONT_SCALE;
- drawScaledString(10+(int)xOffs, DISPLAY_VSCREEN_HEIGHT-height+5, CONSOLE_FRONT_END_FONT_SCALE, CONSOLE_FRONT_END_FONT_SCALE, font, "_");
- }
- }
-
- void ConsoleFrontEnd::drawLines(){
- int i;
- int n = (int)( height/(lineSpacing + font->height*CONSOLE_FRONT_END_FONT_SCALE) );
- int y = DISPLAY_VSCREEN_HEIGHT - height + 10;
- char* p;
- //char* pMin;
- float charWidth = font->chars[(int)'A']->width*CONSOLE_FRONT_END_FONT_SCALE; // FIXME: das muss es nicht geben!!!
- unsigned int maxCharsPerLine = (int)(DISPLAY_VSCREEN_WIDTH/charWidth) -2;
-
- int stop = scrollIndex+n <= CON_MAX_LINES ? scrollIndex+n : CON_MAX_LINES;
-
- for(i=scrollIndex; i<stop; i++){
- if(console->lines[i]==NULL){
- y+= (int)( lineSpacing + font->height*CONSOLE_FRONT_END_FONT_SCALE );
- continue;
- }
-
- glColor4fv(ConsoleInfo::var.fontColor);
- if(strlen(console->lines[i]) <= maxCharsPerLine){
- y += (int)( (lineSpacing + font->height*CONSOLE_FRONT_END_FONT_SCALE) );
- cprint(10, y, console->lines[i]);
- }else{ // FIXME: sch�er machen
- char buff[256];
-
- int numLines = (int)( strlen(console->lines[i])/maxCharsPerLine ) + 1;
- y += (int)( lineSpacing + font->height*CONSOLE_FRONT_END_FONT_SCALE ) * numLines;
- p = console->lines[i];
- while(p < console->lines[i] + strlen(console->lines[i])){
- strncpy(buff, p, maxCharsPerLine);
- buff[maxCharsPerLine]='\0';
- cprint(10, y, buff);
- y -= (int)( lineSpacing + font->height*CONSOLE_FRONT_END_FONT_SCALE );
-
- p += maxCharsPerLine;
- }
- y += (int)( lineSpacing + font->height*CONSOLE_FRONT_END_FONT_SCALE ) * numLines;
- }
- }
- }
-
-
-
-
- void ConsoleFrontEnd::clearHistory(){
- int i;
-
- for(i=0;i<CON_MAX_HISTORY_LINES;i++){
- if(historyLines[i]!=NULL){
- delete[] historyLines[i];
- historyLines[i]=NULL;
- }
- }
- }
-
- void ConsoleFrontEnd::appendHistoryLine(const char* line){
- int i;
-
- if(historyLines[CON_MAX_HISTORY_LINES-1]!=NULL) // delete last line
- delete[] historyLines[CON_MAX_HISTORY_LINES-1];
-
- for(i=CON_MAX_HISTORY_LINES-1; i>0; i--){ // move all one position up
- historyLines[i]=historyLines[i-1];
- }
-
- // int t=strlen(line);
- historyLines[0]=newString(line);//new char[strlen(line)+1];
- // historyLines[0]=strncpy(historyLines[0], line, strlen(line)+1);
- }
-
-
-