/* $Id: menubuttons.c,v 1.1 2003/12/24 02:42:05 stpohle Exp $ * Menuhandling: buttons */ #include "basic.h" #include "bomberclone.h" #include "menu.h" #include "menugui.h" /* create a button only in the menudatas.. darf all this only when menu_loop * is called */ void menu_create_button (char *name, int x, int y, int w, int id) { int i = menu_getlastitem(menu.items); if (i == -1) { /* first entry in the itemslist */ menu.items = &menuitems[0]; i = 0; } else if (i >= MENU_MAXENTRYS) { /* max items reached, ignore new item */ d_fatal ("menu_create_button: MENU_MAXENTRYS reached. Item Ignored\n"); return; } else { /* add new item to the list */ menuitems[i].next = &menuitems[i+1]; i++; } menuitems[i].type = MENU_button; menuitems[i].pos.w = (1 + (int)((w - menu.buttonimages[0][0]->w - menu.buttonimages[0][2]->w) / menu.buttonimages[0][1]->w)) * menu.buttonimages[0][1]->w + menu.buttonimages[0][0]->w + menu.buttonimages[0][2]->w; if (x != -1) menuitems[i].pos.x = x; else menuitems[i].pos.x = (menu.oldscreenpos.w - 2 * menu.images[0]->w - menuitems[i].pos.w) / 2; menuitems[i].pos.y = y; menuitems[i].state = 0; menuitems[i].id = id; strncpy (menuitems[i].label, name, MENU_TITLELEN); }; /* draw the menuitem button or bool * menuitem->pos.[x|y|w] - Position and X-Size inside the menu * label - Text of the Button/Bool */ void menu_draw_button (_menuitem *mi) { int px, py, i; SDL_Rect dest; if (mi->type != MENU_button && mi->type != MENU_bool) return; dest.x = mi->pos.x; dest.y = mi->pos.y; dest.w = mi->pos.w; dest.h = menu.buttonimages[0][0]->h; menu_draw_background (&dest); /* check the focus of the button */ if (menu.focusvis && mi == menu.focus) mi->state = 1; else if (mi->type == MENU_bool && (*((int*) mi->ptrdata)) > 0) mi->state = 2; // bool else mi->state = 0; // button or bool == FALSE // draw the left side of the button dest.x = MENUOFFSET_X + mi->pos.x; dest.y = MENUOFFSET_Y + mi->pos.y; dest.w = menu.buttonimages[mi->state][0]->w; dest.h = menu.buttonimages[mi->state][0]->h; gfx_blit (menu.buttonimages[mi->state][0], NULL, gfx.screen, &dest, 10000); // draw the center of the button for (i = 0; i < ((mi->pos.w - (menu.buttonimages[mi->state][0]->w + menu.buttonimages[mi->state][2]->w)) / menu.buttonimages[mi->state][1]->w); i++) { dest.x = MENUOFFSET_X + mi->pos.x + menu.buttonimages[mi->state][0]->w + (i * menu.buttonimages[mi->state][1]->w); dest.y = MENUOFFSET_Y + mi->pos.y; dest.w = menu.buttonimages[mi->state][1]->w; dest.h = menu.buttonimages[mi->state][1]->h; gfx_blit (menu.buttonimages[mi->state][1], NULL, gfx.screen, &dest, 10000); } // draw the right side of the button dest.x = MENUOFFSET_X + mi->pos.x + mi->pos.w - menu.buttonimages[mi->state][2]->w; dest.y = MENUOFFSET_Y + mi->pos.y; dest.w = menu.buttonimages[mi->state][2]->w; dest.h = menu.buttonimages[mi->state][2]->h; gfx_blit (menu.buttonimages[mi->state][2], NULL, gfx.screen, &dest, 10000); // calculate the center of the button px = (mi->pos.w - (strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x)) / 2 + mi->pos.x; py = (menu.buttonimages[mi->state][0]->h - font[MENU_BUTTON_FONTSIZE].size.y) / 2 + mi->pos.y; if (mi->type == MENU_bool && mi->state == 2) // disabled bool == FALSE font_gfxdraw (MENUOFFSET_X + px, MENUOFFSET_Y + py, mi->label, MENU_BUTTON_FONTSIZE, COLOR_black, 10000); else font_gfxdraw (MENUOFFSET_X + px, MENUOFFSET_Y + py, mi->label, MENU_BUTTON_FONTSIZE, COLOR_yellow, 10000); }; /* handle the event on the button * Return: 1 - if the button was pressed (With Enter) */ int menu_event_button (_menuitem *mi, SDL_Event *event) { switch (event->type) { case (SDL_KEYDOWN): /* key was pressed */ if (event->key.keysym.sym == SDLK_LEFT || event->key.keysym.sym == SDLK_UP) menu_focus_prev (); else if (event->key.keysym.sym == SDLK_RIGHT || event->key.keysym.sym == SDLK_DOWN) menu_focus_next (); else if (event->key.keysym.sym == SDLK_RETURN || event->key.keysym.sym == SDLK_LCTRL || event->key.keysym.sym == SDLK_RCTRL) return 1; break; } return 0; };