From 2d776fcd61be168e1ff0ff2e4a5e71835e7cea19 Mon Sep 17 00:00:00 2001 From: oroborus Date: Sat, 28 Jun 2008 16:38:35 +0000 Subject: - prekopanie adresarovej struktury, prva cast git-svn-id: http://opensvn.csie.org/tuxanci_ng@77 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/widget/widget_textfield.c | 194 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 src/widget/widget_textfield.c (limited to 'src/widget/widget_textfield.c') diff --git a/src/widget/widget_textfield.c b/src/widget/widget_textfield.c new file mode 100644 index 0000000..f9186c9 --- /dev/null +++ b/src/widget/widget_textfield.c @@ -0,0 +1,194 @@ + +#include +#include "base/main.h" +#include "client/interface.h" +#include "client/font.h" +#include "client/image.h" +#include "widget_textfield.h" + +widget_textfield_t* newWidgetTextfield(char *text, int x, int y) +{ + widget_textfield_t *new; + + new = malloc( sizeof(widget_textfield_t) ); + strcpy(new->text, text); + new->x = x; + new->y = y; + new->time = 0; + new->timeBlick = 0; + new->active = FALSE; + getTextSize(text, &new->w, &new->h); + + return new; +} + +static void drawBackground(widget_textfield_t *p) +{ + static SDL_Surface *g_textfield0 = NULL; + static SDL_Surface *g_textfield1 = NULL; + int x, y; + + getMousePosition(&x, &y); + + if( g_textfield0 == NULL ) + { + g_textfield0 = addImageData("button0.png", IMAGE_ALPHA, "textfiled0", IMAGE_GROUP_BASE); + } + + if( g_textfield1 == NULL ) + { + g_textfield1 = addImageData("button1.png", IMAGE_ALPHA, "textfiled1", IMAGE_GROUP_BASE); + } + + if( p->active ) + { + drawImage(g_textfield1, p->x, p->y, 0, 0, g_textfield1->w, g_textfield1->h); + } + else + { + drawImage(g_textfield0, p->x, p->y, 0, 0, g_textfield0->w, g_textfield0->h); + } +} + +static void drawText(widget_textfield_t *p) +{ + char str[STR_SIZE]; + + strcpy(str, p->text); + + if( p->timeBlick > WIDGET_TEXTFIELD_TIME_BLICK_CURSOR/2 && + p->active == TRUE ) + { + //strcat(str, ">"); + strcat(str, "\f"); + } + + p->timeBlick++; + + if( p->timeBlick == WIDGET_TEXTFIELD_TIME_BLICK_CURSOR ) + { + p->timeBlick = 0; + } + + //drawFont(str, p->x+WIDGET_TEXTFIELD_TEXT_OFFSET_X, p->y+p->h/2, COLOR_WHITE); + drawFont(str, p->x+WIDGET_TEXTFIELD_TEXT_OFFSET_X, p->y+WIDGET_TEXTFIELD_HEIGHT/2-p->h/2, COLOR_WHITE); + //printf("p->y: %d\nheight: %d\n p->h: %d\n", p->y, WIDGET_TEXTFIELD_HEIGHT, p->h); +} + +void drawWidgetTextfield(widget_textfield_t *p) +{ + drawBackground(p); + drawText(p); +} + +static void readKey(widget_textfield_t *p) +{ + Uint8 *mapa; + int len; + int i; + + if( p->time > 0 ) + { + p->time--; + return; + } + + if( p->active == FALSE ) + { + return; + } + + mapa = SDL_GetKeyState(NULL); + len = strlen(p->text); + + // mazanie posledneho klavesu + if( mapa[SDLK_BACKSPACE] == SDL_PRESSED ) + { + if( len > 0 ) + { + p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; + p->text[len-1]='\0'; + getTextSize(p->text, &p->w, &p->h); + } + + return; + } + + if( p->w > WIDGET_TEXTFIELD_WIDTH-40 ) + { + return; + } + + // klavesy abecedy + for(i=SDLK_SPACE /*SDLK_a*/;i<=SDLK_z;i++) + { + //if(widthtime = WIDGET_TEXTFIELD_TIME_READ_KEY; + + strcat( p->text, SDL_GetKeyName(i) ); + getTextSize(p->text, &p->w, &p->h); + + if( mapa[SDLK_LSHIFT] == SDL_PRESSED || + mapa[SDLK_RSHIFT] == SDL_PRESSED) + { + p->text[ strlen( p->text ) - 1 ] -= 32; + getTextSize(p->text, &p->w, &p->h); + return; + } + } + } + + // aby ve jmene bulanka fungovaly take cislice alfanumericke klavesnice + for( i = SDLK_0 ; i <= SDLK_9 ; i++ ) + { + if( mapa[i] == SDL_PRESSED && len < STR_SIZE ) + { + p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; + strcat( p->text, SDL_GetKeyName(i) ); + getTextSize(p->text, &p->w, &p->h); + return; + } + } + + // aby ve jmene bulanka fungovaly take cislice napsane na numericke klavesnici + for( i=SDLK_KP0 ; i<=SDLK_KP9 ; i++ ) + { + if( mapa[i] == SDL_PRESSED && len < STR_SIZE ) + { + p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; + strncat( p->text, SDL_GetKeyName(i)+1, 1 ); // napr. "[4]" + getTextSize(p->text, &p->w, &p->h); + return; + } + } +} + +void eventWidgetTextfield(widget_textfield_t *p) +{ + int x, y; + + getMousePosition(&x, &y); + + if( isMouseClicked() ) + { + if( x >= p->x && x <= p->x+WIDGET_TEXTFIELD_WIDTH && + y >= p->y && y <= p->y+WIDGET_TEXTFIELD_HEIGHT ) + { + p->active = TRUE; + } + else + { + p->active = FALSE; + } + } + + readKey(p); +} + +void destroyWidgetTextfield(widget_textfield_t *p) +{ + free(p); +} + -- cgit v1.2.3