Key repeat in menus

This commit is contained in:
laochailan 2012-01-07 13:20:16 +01:00
parent 89a019f1fb
commit cbbecc4bec
3 changed files with 100 additions and 63 deletions

View file

@ -36,34 +36,51 @@ void create_menu(MenuData *menu) {
menu->selected = -1;
}
static void key_action(MenuData *menu, int sym) {
if(sym == tconfig.intval[KEY_DOWN] || sym == SDLK_DOWN) {
do {
if(++menu->cursor >= menu->ecount)
menu->cursor = 0;
} while(menu->entries[menu->cursor].action == NULL);
} else if(sym == tconfig.intval[KEY_UP] || sym == SDLK_UP) {
do {
if(--menu->cursor < 0)
menu->cursor = menu->ecount - 1;
} while(menu->entries[menu->cursor].action == NULL);
} else if((sym == tconfig.intval[KEY_SHOT] || sym == SDLK_RETURN) && menu->entries[menu->cursor].action) {
menu->quit = 1;
menu->selected = menu->cursor;
} else if(sym == SDLK_ESCAPE && menu->type == MT_Transient) {
menu->quit = 1;
}
}
void menu_input(MenuData *menu) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
int sym = event.key.keysym.sym;
global_processevent(&event);
if(event.type == SDL_KEYDOWN) {
if(sym == tconfig.intval[KEY_DOWN] || sym == SDLK_DOWN) {
do {
if(++menu->cursor >= menu->ecount)
menu->cursor = 0;
} while(menu->entries[menu->cursor].action == NULL);
} else if(sym == tconfig.intval[KEY_UP] || sym == SDLK_UP) {
do {
if(--menu->cursor < 0)
menu->cursor = menu->ecount - 1;
} while(menu->entries[menu->cursor].action == NULL);
} else if((sym == tconfig.intval[KEY_SHOT] || sym == SDLK_RETURN) && menu->entries[menu->cursor].action) {
menu->quit = 1;
menu->selected = menu->cursor;
} else if(sym == SDLK_ESCAPE && menu->type == MT_Transient) {
menu->quit = 1;
}
key_action(menu,sym);
menu->lastkey = sym;
} else if(event.type == SDL_QUIT) {
exit(1);
}
}
Uint8 *keys = SDL_GetKeyState(NULL);
if(keys[menu->lastkey])
menu->keypressed++;
else
menu->keypressed = 0;
if(menu->keypressed > KEYREPEAT_TIME) {
key_action(menu, menu->lastkey);
menu->keypressed = KEYREPEAT_TIME-10;
}
}
void menu_logic(MenuData *menu) {

View file

@ -10,6 +10,7 @@
#define IMENU_BLUR 0.05
#define FADE_TIME 15
#define KEYREPEAT_TIME 40
typedef struct {
char *name;
@ -29,6 +30,9 @@ typedef struct MenuData{
int cursor;
int selected;
int lastkey;
int keypressed;
MenuEntry *entries;
int ecount;

View file

@ -483,6 +483,55 @@ void binding_input(MenuData *menu, OptionBinding *b)
}
}
static void options_key_action(MenuData *menu, int sym) {
if(sym == tconfig.intval[KEY_DOWN] || sym == SDLK_DOWN) {
menu->drawdata[3] = 10;
do {
menu->cursor++;
if(menu->cursor >= menu->ecount)
menu->cursor = 0;
} while(!menu->entries[menu->cursor].action);
} else if(sym == tconfig.intval[KEY_UP] || sym == SDLK_UP) {
menu->drawdata[3] = 10;
do {
menu->cursor--;
if(menu->cursor < 0)
menu->cursor = menu->ecount - 1;
} while(!menu->entries[menu->cursor].action);
} else if((sym == tconfig.intval[KEY_SHOT] || sym == SDLK_RETURN) && menu->entries[menu->cursor].action) {
menu->selected = menu->cursor;
OptionBinding *binds = (OptionBinding*)menu->context;
OptionBinding *bind = &(binds[menu->selected]);
if(bind->enabled) switch(bind->type)
{
case BT_IntValue: binding_setnext(bind); break;
case BT_KeyBinding: bind->blockinput = True; break;
}
else
menu->quit = 1;
} else if(sym == tconfig.intval[KEY_LEFT] || sym == SDLK_LEFT) {
menu->selected = menu->cursor;
OptionBinding *binds = (OptionBinding*)menu->context;
OptionBinding *bind = &(binds[menu->selected]);
if(bind->enabled && bind->type == BT_IntValue)
binding_setprev(bind);
} else if(sym == tconfig.intval[KEY_RIGHT] || sym == SDLK_RIGHT) {
menu->selected = menu->cursor;
OptionBinding *binds = (OptionBinding*)menu->context;
OptionBinding *bind = &(binds[menu->selected]);
if(bind->enabled && bind->type == BT_IntValue)
binding_setnext(bind);
} else if(sym == SDLK_ESCAPE) {
menu->quit = 2;
}
menu->cursor = (menu->cursor % menu->ecount) + menu->ecount*(menu->cursor < 0);
}
void options_menu_input(MenuData *menu) {
SDL_Event event;
OptionBinding *b;
@ -498,56 +547,23 @@ void options_menu_input(MenuData *menu) {
global_processevent(&event);
if(event.type == SDL_KEYDOWN) {
if(sym == tconfig.intval[KEY_DOWN] || sym == SDLK_DOWN) {
menu->drawdata[3] = 10;
do {
menu->cursor++;
if(menu->cursor >= menu->ecount)
menu->cursor = 0;
} while(!menu->entries[menu->cursor].action);
} else if(sym == tconfig.intval[KEY_UP] || sym == SDLK_UP) {
menu->drawdata[3] = 10;
do {
menu->cursor--;
if(menu->cursor < 0)
menu->cursor = menu->ecount - 1;
} while(!menu->entries[menu->cursor].action);
} else if((sym == tconfig.intval[KEY_SHOT] || sym == SDLK_RETURN) && menu->entries[menu->cursor].action) {
menu->selected = menu->cursor;
OptionBinding *binds = (OptionBinding*)menu->context;
OptionBinding *bind = &(binds[menu->selected]);
if(bind->enabled) switch(bind->type)
{
case BT_IntValue: binding_setnext(bind); break;
case BT_KeyBinding: bind->blockinput = True; break;
}
else
menu->quit = 1;
} else if(sym == tconfig.intval[KEY_LEFT] || sym == SDLK_LEFT) {
menu->selected = menu->cursor;
OptionBinding *binds = (OptionBinding*)menu->context;
OptionBinding *bind = &(binds[menu->selected]);
if(bind->enabled && bind->type == BT_IntValue)
binding_setprev(bind);
} else if(sym == tconfig.intval[KEY_RIGHT] || sym == SDLK_RIGHT) {
menu->selected = menu->cursor;
OptionBinding *binds = (OptionBinding*)menu->context;
OptionBinding *bind = &(binds[menu->selected]);
if(bind->enabled && bind->type == BT_IntValue)
binding_setnext(bind);
} else if(sym == SDLK_ESCAPE) {
menu->quit = 2;
}
menu->cursor = (menu->cursor % menu->ecount) + menu->ecount*(menu->cursor < 0);
options_key_action(menu, sym);
menu->lastkey = sym;
} else if(event.type == SDL_QUIT) {
exit(1);
}
}
Uint8 *keys = SDL_GetKeyState(NULL);
if(keys[menu->lastkey])
menu->keypressed++;
else
menu->keypressed = 0;
if(menu->keypressed > KEYREPEAT_TIME) {
options_key_action(menu, menu->lastkey);
menu->keypressed = KEYREPEAT_TIME-10;
}
}
int options_menu_loop(MenuData *menu) {