From 2fc6cc22a29a53dc5127eafa2719d11e9bb38c42 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 22 Jan 2004 02:38:13 +0000 Subject: Updated with various bits from espec (memory menu, GUI, etc) --- src/gui.c | 299 +++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 247 insertions(+), 52 deletions(-) (limited to 'src/gui.c') diff --git a/src/gui.c b/src/gui.c index 9cbacf2..19d6135 100644 --- a/src/gui.c +++ b/src/gui.c @@ -33,11 +33,11 @@ static const char ident[]="$Id$"; #include "gui.h" #include "gfx.h" +#include "util.h" #include "exit.h" static const char ident_h[]=EZX81_GUI_H; - /* ---------------------------------------- MACROS */ #ifndef TRUE @@ -48,57 +48,185 @@ static const char ident_h[]=EZX81_GUI_H; #define FALSE 0 #endif -/* Must match gfx.c -*/ -#define SCR_W 320 -#define SCR_H 200 +#define WHITE GFXRGB(255,255,255) +#define BLACK GFXRGB(0,0,0) +#define RED GFXRGB(255,100,100) +#define GREY GFXRGB(160,160,160) +#define LOGREY GFXRGB(100,100,100) +#define GREEN GFXRGB(100,255,100) /* ---------------------------------------- STATICS */ -static Uint32 white; -static Uint32 black; -static Uint32 pale; -static Uint32 red; /* ---------------------------------------- PRIVATE FUNCTIONS */ -static void Init() +static void Trim(char *p,size_t len) { - static int init=FALSE; - - if (init) - return; + if (strlen(p)>len) + { + p[len]=0; + p[len-1]='.'; + p[len-2]='.'; + } +} - init=TRUE; - white=GFXRGB(255,255,255); - black=GFXRGB(0,0,0); - pale=GFXRGB(200,200,200); - red=GFXRGB(255,100,100); +static void Centre(const char *p, int y, Uint32 col) +{ + GFXPrint((GFX_WIDTH-strlen(p)*8)/2,y,col,"%s",p); } -static void *Malloc(size_t size) +static void Box(const char *title, int x, int y, int width, int height) { - void *p=malloc(size); + GFXRect(x+1,y+1, + width-2,height-2, + BLACK,TRUE); + + GFXRect(x+1,y+1, + width-2,11, + LOGREY,TRUE); - if (!p) - Exit("malloc failed for %lu bytes\n",(unsigned long)size); + GFXRect(x,y, + width,height, + GREY,FALSE); - return p; + Centre(title,y+2,GREEN); + GFXHLine(x,x+width-1,y+11,GREY); } -static void Centre(const char *p, int y, Uint32 col) +static int DoList(const char *title, int no, char * const list[], int *option) { - GFXPrint((SCR_W-strlen(p)*8)/2,y,col,"%s",p); + static const int max=GFX_HEIGHT/8-8; + SDL_Event *e; + int top; + int cur; + int done; + int f; + + if (no==0) + return -1; + + top=0; + cur=0; + + done=FALSE; + + while(!done) + { + Box(title,7,7,GFX_WIDTH-14,GFX_HEIGHT-14); + + if (option) + { + Centre("Cursors to move, RETURN to accept", + GFX_HEIGHT-44,WHITE); + Centre("SPACE toggles, I inverts all", + GFX_HEIGHT-36,WHITE); + Centre("S select all, C clear all", + GFX_HEIGHT-28,WHITE); + } + else + Centre("Cursors and RETURN to select",GFX_HEIGHT-28,WHITE); + + Centre("ESCAPE to cancel",GFX_HEIGHT-20,WHITE); + + for(f=0;fkey.keysym.sym) + { + case SDLK_RETURN: + done=TRUE; + break; + + case SDLK_SPACE: + if (option) + option[cur]=!option[cur]; + break; + + case SDLK_i: + if (option) + for(f=0;f0) + { + cur--; + + if (curtop+max-2) + top=cur-max+2; + } + break; + + default: + break; + } + } + + return cur; } /* ---------------------------------------- EXPORTED INTERFACES */ -void GUIMessage(const char *title, const char *format,...) +int GUIMessage(GUIBoxType type, const char *title, const char *format,...) { char buff[1025]; va_list va; @@ -109,8 +237,9 @@ void GUIMessage(const char *title, const char *format,...) int width; int height; int x,y; + int ret; - Init(); + ret=FALSE; va_start(va,format); vsprintf(buff,format,va); @@ -129,66 +258,96 @@ void GUIMessage(const char *title, const char *format,...) line=Malloc(sizeof *line * no); + width=16; + line[0]=strtok(buff,"\n"); - width=strlen(line[0]); + Trim(line[0],38); + + if (strlen(line[0])>width) + width=strlen(line[0]); for(f=1;fwidth) width=strlen(line[f]); } - width=(width*8)+16; - height=(no+3)*10; - - if (width>(SCR_W-10)) - width=SCR_W-10; + width=(width*8)+18; + height=(no+5)*10; - if (height>(SCR_H-10)) - height=SCR_H-10; + if (width>(GFX_WIDTH-10)) + width=GFX_WIDTH-10; - y=(SCR_H-height)/2; - x=(SCR_W-width)/2; + if (height>(GFX_HEIGHT-10)) + height=GFX_HEIGHT-10; - GFXRect(x-1,y-1,width+2,height+2,white,FALSE); - GFXRect(x,y,width,height,black,TRUE); + y=(GFX_HEIGHT-height)/2; + x=(GFX_WIDTH-width)/2; - Centre(title,y+2,white); - GFXHLine(x+2,x+width-4,y+10,white); + Box(title,x,y,width,height); for(f=0;fkey.keysym.sym==SDLK_y) + { + ret=TRUE; + break; + } + else if (e->key.keysym.sym==SDLK_n) + { + ret=FALSE; + break; + } + } + } + else + GFXWaitKey(); + free(line); + + return ret; } const char *GUIInputString(const char *prompt, const char *orig) { - static const int y_pos=SCR_H-8; + static const int y_pos=GFX_HEIGHT-8; static char buff[41]; size_t len; int done=FALSE; + unsigned char c; SDL_Event *e; - Init(); - buff[0]=0; strncat(buff,orig,40); len=strlen(buff); + SDL_EnableUNICODE(1); + while(!done) { - GFXRect(0,y_pos,SCR_W,8,black,TRUE); - GFXPrint(0,y_pos,white,"%s %s%c",prompt,buff,FONT_CURSOR); + GFXRect(0,y_pos,GFX_WIDTH,8,BLACK,TRUE); + GFXPrint(0,y_pos,WHITE,"%s %s%c",prompt,buff,FONT_CURSOR); GFXEndFrame(FALSE); e=GFXWaitKey(); @@ -213,17 +372,53 @@ const char *GUIInputString(const char *prompt, const char *orig) break; default: - if (len<40 && isprint(e->key.keysym.sym)) + c=(unsigned char)e->key.keysym.unicode; + + if (len<40 && isprint(c)) { - buff[len++]=(char)e->key.keysym.sym; + buff[len++]=c; buff[len]=0; } break; } } + SDL_EnableUNICODE(0); + return buff; } +int GUIListSelect(const char *title, int no, char * const list[]) +{ + return DoList(title,no,list,NULL); +} + + +int GUIListOption(const char *title, int no, char * const list[], int option[]) +{ + int *o; + int sel; + int f; + + if (no==0) + return FALSE; + + o=Malloc(no * sizeof *o); + + for(f=0;f