From 5c961692f7f10024432ff06be1ef569638e29bd4 Mon Sep 17 00:00:00 2001 From: sf17k Date: Sat, 7 May 2011 01:35:31 -0400 Subject: [PATCH] improved text input handling --- Source/Game.cpp | 30 ++++++++++++++++-------------- Source/GameTick.cpp | 36 +++++++++++++++--------------------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/Source/Game.cpp b/Source/Game.cpp index 66aafdf..9cdf9eb 100644 --- a/Source/Game.cpp +++ b/Source/Game.cpp @@ -243,27 +243,30 @@ void Game::inputText(char* str, int* charselected, int* nb_chars) { switch(evenement.type) { case SDL_KEYDOWN: if(evenement.key.keysym.sym == SDLK_ESCAPE) { - for(i=0;i<255;i++){ - str[i]=' '; - } + for(i=0;i<255;i++) + str[i]=0; *nb_chars=0; *charselected=0; waiting=false; } else if(evenement.key.keysym.sym==SDLK_BACKSPACE){ if((*charselected)!=0) { - for(i=(*charselected)-1;i<255;i++){ + for(i=(*charselected)-1;i<255;i++) str[i]=str[i+1]; - } - str[255]=' '; + str[255]=0; (*charselected)--; (*nb_chars)--; } } else if(evenement.key.keysym.sym==SDLK_DELETE){ - for(i=(*charselected);i<255;i++){ - str[i]=str[i+1]; - } - str[255]=' '; - (*nb_chars)--; + if((*charselected)<(*nb_chars)){ + for(i=(*charselected);i<255;i++) + str[i]=str[i+1]; + str[255]=0; + (*nb_chars)--; + } + } else if(evenement.key.keysym.sym==SDLK_HOME){ + (*charselected)=0; + } else if(evenement.key.keysym.sym==SDLK_END){ + (*charselected)=(*nb_chars); } else if(evenement.key.keysym.sym==SDLK_LEFT){ if((*charselected)!=0) (*charselected)--; @@ -272,10 +275,9 @@ void Game::inputText(char* str, int* charselected, int* nb_chars) { (*charselected)++; } else if(evenement.key.keysym.sym==SDLK_RETURN) { waiting=false; - } else if((evenement.key.keysym.unicode<127)&&((*nb_chars)<60)&&(evenement.key.keysym.sym!=SDLK_LSHIFT)&&(evenement.key.keysym.sym!=SDLK_RSHIFT)) { - for(i=255;i>=(*charselected)+1;i--){ + } else if(evenement.key.keysym.unicode>=32&&evenement.key.keysym.unicode<127&&(*nb_chars)<60){ + for(i=255;i>=(*charselected)+1;i--) str[i]=str[i-1]; - } str[*charselected]=evenement.key.keysym.unicode; (*charselected)++; (*nb_chars)++; diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 9314be1..326d9fd 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -2451,7 +2451,7 @@ void Game::MenuTick(){ mainmenu=1; for(int j=0;j<255;j++){ - displaytext[0][j]=' '; + displaytext[0][j]=0; } displaychars[0]=0; displayselected=0; @@ -2536,7 +2536,7 @@ void Game::MenuTick(){ fireSound(firestartsound); for(int i=0;i<255;i++){ - displaytext[0][i]=' '; + displaytext[0][i]=0; } displaychars[0]=0; @@ -6047,13 +6047,6 @@ void Game::Tick(){ if(!winfreeze)leveltime+=multiplier; //keys - if(Input::isKeyDown(SDLK_ESCAPE)){ - chatting=0; - console=0; - freeze=0; - displaychars[0]=0; - } - if(Input::isKeyPressed(SDLK_v)&&debugmode){ freeze=1-freeze; if(freeze){ @@ -6068,9 +6061,8 @@ void Game::Tick(){ inputText(displaytext[0],&displayselected,&displaychars[0]); if(!waiting) { if(displaychars[0]){ - for(int j=0;j<255;j++){ - displaytext[0][j]=' '; - } + for(int j=0;j<255;j++) + displaytext[0][j]=0; displaychars[0]=0; displayselected=0; } @@ -6102,18 +6094,16 @@ void Game::Tick(){ inputText(consoletext[0],&consoleselected,&consolechars[0]); if(!waiting) { archiveselected=0; - cmd_dispatch(this, consoletext[0]); if(consolechars[0]>0){ - + consoletext[0][consolechars[0]]=' '; + cmd_dispatch(this, consoletext[0]); for(int k=14;k>=1;k--){ - for(int j=0;j<255;j++){ + for(int j=0;j<255;j++) consoletext[k][j]=consoletext[k-1][j]; - } consolechars[k]=consolechars[k-1]; } - for(int j=0;j<255;j++){ - consoletext[0][j]=' '; - } + for(int j=0;j<255;j++) + consoletext[0][j]=0; consolechars[0]=0; consoleselected=0; } @@ -6153,11 +6143,15 @@ void Game::Tick(){ if((Input::isKeyPressed(jumpkey)||Input::isKeyPressed(SDLK_SPACE))&&!campaign) if(winfreeze) winfreeze=0; - if((Input::isKeyDown(SDLK_ESCAPE))&&!campaign&&gameon) - if(winfreeze){ + if((Input::isKeyDown(SDLK_ESCAPE))&&!campaign&&gameon){ + if(console){ + console=0; + freeze=0; + }else if(winfreeze){ mainmenu=9; gameon=0; } + } -- 2.39.5