You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
6.3 KiB
183 lines
6.3 KiB
/* $Id: menuentrys.c,v 1.3 2003/12/28 01:21:43 stpohle Exp $
|
|
* Menuhandling: entry */
|
|
|
|
|
|
#include "basic.h"
|
|
#include "bomberclone.h"
|
|
#include "menu.h"
|
|
#include "menugui.h"
|
|
|
|
|
|
/* save the data into the pointer */
|
|
void menu_entry_save (_menuitem *mi) {
|
|
int tmp;
|
|
switch (mi->type) {
|
|
case (MENU_entryint16):
|
|
sscanf (mi->keybi.text, "%d", &tmp);
|
|
*(Sint16*)mi->ptrdata = (Sint16) tmp;
|
|
break;
|
|
case (MENU_entryint32):
|
|
sscanf (mi->keybi.text, "%d", (Sint32*)mi->ptrdata);
|
|
break;
|
|
case (MENU_entrytext):
|
|
strncpy (mi->ptrdata, mi->keybi.text, mi->len);
|
|
break;
|
|
case (MENU_entryfloat):
|
|
sscanf (mi->keybi.text, "%f", (float*)mi->ptrdata);
|
|
break;
|
|
}
|
|
};
|
|
|
|
|
|
/* restore the data into the pointer */
|
|
void menu_entry_restore (_menuitem *mi) {
|
|
switch (mi->type) {
|
|
case (MENU_entryint16):
|
|
sprintf (mi->keybi.text, "%d", *(Sint16*)mi->ptrdata);
|
|
break;
|
|
case (MENU_entryint32):
|
|
sprintf (mi->keybi.text, "%d", *(Sint32*)mi->ptrdata);
|
|
break;
|
|
case (MENU_entrytext):
|
|
sprintf (mi->keybi.text, "%s", (char*)mi->ptrdata);
|
|
break;
|
|
case (MENU_entryfloat):
|
|
sprintf (mi->keybi.text, "%f", *(float*)mi->ptrdata);
|
|
break;
|
|
}
|
|
mi->keybi.curpos = strlen (mi->keybi.text);
|
|
};
|
|
|
|
|
|
/* create a entryimages only in the menudatas.. darf all this only when menu_loop
|
|
* is called */
|
|
void menu_create_entry (char *name, int x, int y, int w, void *data, int len, int typ, int id) {
|
|
int i = menu_getlastitem(menu.items);
|
|
|
|
if (typ != MENU_entrytext && typ != MENU_entryint16 && typ != MENU_entryint32 && typ != MENU_entryfloat)
|
|
return;
|
|
|
|
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_entry: MENU_MAXENTRYS reached. Item Ignored\n");
|
|
return;
|
|
}
|
|
else { /* add new item to the list */
|
|
menuitems[i].next = &menuitems[i+1];
|
|
i++;
|
|
}
|
|
|
|
menuitems[i].type = typ;
|
|
menuitems[i].pos.w = (1 + (int)((w - (strlen (name) * font[MENU_BUTTON_FONTSIZE].size.x) - 8 - menu.entryimages[0][0]->w - menu.entryimages[0][2]->w) / menu.entryimages[0][1]->w)) * menu.entryimages[0][1]->w + menu.entryimages[0][0]->w + menu.entryimages[0][2]->w + (strlen (name) * font[MENU_BUTTON_FONTSIZE].size.x) + 8;
|
|
|
|
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].len = len;
|
|
menuitems[i].ptrdata = (char *) data;
|
|
menuitems[i].id = id;
|
|
strncpy (menuitems[i].label, name, MENU_TITLELEN);
|
|
|
|
if (typ == MENU_entrytext)
|
|
keybinput_new (&menuitems[i].keybi, KEYBI_text, len);
|
|
else if (typ == MENU_entryint16)
|
|
keybinput_new (&menuitems[i].keybi, KEYBI_int, len);
|
|
else if (typ == MENU_entryint32)
|
|
keybinput_new (&menuitems[i].keybi, KEYBI_int, len);
|
|
else
|
|
keybinput_new (&menuitems[i].keybi, KEYBI_float, 10);
|
|
|
|
menu_entry_restore (&menuitems[i]);
|
|
};
|
|
|
|
|
|
/* 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_entry (_menuitem *mi) {
|
|
int px, py, i;
|
|
SDL_Rect dest;
|
|
|
|
if (mi->type != MENU_entrytext && mi->type != MENU_entryint16 && mi->type != MENU_entryint32 && mi->type != MENU_entryfloat)
|
|
return;
|
|
|
|
dest.x = mi->pos.x;
|
|
dest.y = mi->pos.y;
|
|
dest.w = mi->pos.w;
|
|
dest.h = menu.entryimages[0][0]->h;
|
|
menu_draw_background (&dest);
|
|
|
|
/* check the focus of the button */
|
|
if (menu.focusvis && mi == menu.focus)
|
|
mi->state = 1;
|
|
else
|
|
mi->state = 0;
|
|
|
|
// draw the left side of the button
|
|
dest.x = MENUOFFSET_X + mi->pos.x + (strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x) + 8;
|
|
dest.y = MENUOFFSET_Y + mi->pos.y;
|
|
dest.w = menu.entryimages[mi->state][0]->w;
|
|
dest.h = menu.entryimages[mi->state][0]->h;
|
|
gfx_blit (menu.entryimages[mi->state][0], NULL, gfx.screen, &dest, 10000);
|
|
/* draw the center of the button
|
|
* checkt first if there is something wrong */
|
|
if (mi->pos.w < ((strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x) + 8) - (menu.entryimages[mi->state][0]->w + menu.entryimages[mi->state][2]->w))
|
|
d_fatal ("menuentry Error with Element: %s\n", mi->label);
|
|
else
|
|
for (i = 0; i < ((mi->pos.w - ((strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x) + 8) - (menu.entryimages[mi->state][0]->w + menu.entryimages[mi->state][2]->w)) / menu.entryimages[mi->state][1]->w); i++) {
|
|
dest.x = MENUOFFSET_X + mi->pos.x + (strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x) + 8 + menu.entryimages[mi->state][0]->w + (i * menu.entryimages[mi->state][1]->w);
|
|
dest.y = MENUOFFSET_Y + mi->pos.y;
|
|
dest.w = menu.entryimages[mi->state][1]->w;
|
|
dest.h = menu.entryimages[mi->state][1]->h;
|
|
gfx_blit (menu.entryimages[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.entryimages[mi->state][2]->w;
|
|
dest.y = MENUOFFSET_Y + mi->pos.y;
|
|
dest.w = menu.entryimages[mi->state][2]->w;
|
|
dest.h = menu.entryimages[mi->state][2]->h;
|
|
gfx_blit (menu.entryimages[mi->state][2], NULL, gfx.screen, &dest, 10000);
|
|
|
|
// calculate the center of the button
|
|
px = (strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x) + 8 + (mi->pos.w - (strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x) - 8 - (strlen (mi->keybi.text) * font[MENU_BUTTON_FONTSIZE].size.x)) / 2 + mi->pos.x;
|
|
py = (menu.entryimages[mi->state][0]->h - font[MENU_BUTTON_FONTSIZE].size.y) / 2 + mi->pos.y;
|
|
font_gfxdraw (MENUOFFSET_X + px, MENUOFFSET_Y + py, mi->keybi.text, MENU_BUTTON_FONTSIZE, COLOR_yellow, 10000);
|
|
font_gfxdraw (MENUOFFSET_X + mi->pos.x, MENUOFFSET_Y + py, mi->label, MENU_BUTTON_FONTSIZE, COLOR_yellow, 10000);
|
|
};
|
|
|
|
|
|
/* handle the event on the button
|
|
* on ESC - Reload Old Data
|
|
* on lose focus - Save Data
|
|
*/
|
|
int menu_event_entry (_menuitem *mi, SDL_Event *event) {
|
|
switch (event->type) {
|
|
case (SDL_KEYDOWN): /* key was pressed */
|
|
if (event->key.keysym.sym == SDLK_UP)
|
|
menu_focus_prev ();
|
|
else if (event->key.keysym.sym == SDLK_DOWN)
|
|
menu_focus_next ();
|
|
else if (event->key.keysym.sym == SDLK_RETURN || event->key.keysym.sym == SDLK_RCTRL || event->key.keysym.sym == SDLK_RCTRL)
|
|
menu_entry_save (mi);
|
|
else if (event->key.keysym.sym == SDLK_ESCAPE)
|
|
menu_entry_restore (mi);
|
|
else {
|
|
keybinput_loop (&mi->keybi, event);
|
|
menu_draw_entry (mi);
|
|
}
|
|
break;
|
|
case (SDL_KEYUP):
|
|
keybinput_loop (&mi->keybi, event);
|
|
menu_draw_entry (mi);
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
};
|