/** * @brief Opens the keybindings menu. */ static void opt_keybinds( unsigned int wid ) { int w, h, lw, bw, bh; /* Get dimensions. */ menuKeybinds_getDim( wid, &w, &h, &lw, NULL, &bw, &bh ); /* Close button. */ window_addButton( wid, -20, 20, bw, bh, "btnClose", "Close", opt_close ); /* Set button. */ window_addButton( wid, -20 - bw - 20, 20, bw, bh, "btnSet", "Set Key", opt_setKey ); /* Restore deafaults button. */ window_addButton( wid, -20, 20+bh+20, bw, bh, "btnDefaults", "Defaults", opt_keyDefaults ); /* Text stuff. */ window_addText( wid, 20+lw+20, -40, w-(20+lw+20), 30, 1, "txtName", NULL, &cDConsole, NULL ); window_addText( wid, 20+lw+20, -90, w-(20+lw+20), h-70-60-bh, 0, "txtDesc", &gl_smallFont, NULL, NULL ); /* Generate the list. */ menuKeybinds_genList( wid ); }
/** * @brief Opens the keybindings menu. */ void opt_menuKeybinds (void) { unsigned int wid; int w, h; int bw, bh; int lw; /* Dimensions. */ w = 500; h = 300; /* Create the window. */ wid = window_create( "Keybindings", -1, -1, w, h ); menuKeybinds_getDim( wid, &w, &h, &lw, NULL, &bw, &bh ); /* Close button. */ window_addButton( wid, -20, 20, bw, bh, "btnClose", "Close", window_close ); /* Set button. */ window_addButton( wid, -20 - bw - 20, 20, bw, bh, "btnSet", "Set Key", opt_setKey ); /* Text stuff. */ window_addText( wid, 20+lw+20, -40, w-(20+lw+20), 30, 1, "txtName", NULL, &cDConsole, NULL ); window_addText( wid, 20+lw+20, -90, w-(20+lw+20), h-70-60-bh, 0, "txtDesc", &gl_smallFont, NULL, NULL ); /* Generate the list. */ menuKeybinds_genList( wid ); }
/** * @brief Unsets the key. */ static void opt_unsetKey( unsigned int wid, char *str ) { (void) str; unsigned int parent; /* Unsets the keybind. */ input_setKeybind( opt_selectedKeybind, KEYBIND_NULL, 0, 0 ); /* Close window. */ window_close( wid, NULL ); /* Update parent window. */ parent = window_getParent( wid ); window_destroyWidget( parent, "lstKeybinds" ); menuKeybinds_genList( parent ); }
/** * @brief Restores the key defaults. */ static void opt_keyDefaults( unsigned int wid, char *str ) { (void) str; /* Ask user if he wants to. */ if (!dialogue_YesNoRaw( "Restore Defaults", "Are you sure you want to restore default keybindings?" )) return; /* Restore defaults. */ input_setDefault(); /* Regenerate list widget. */ window_destroyWidget( wid, "lstKeybinds" ); menuKeybinds_genList( wid ); /* Alert user it worked. */ dialogue_msgRaw( "Defaults Restored", "Keybindings restored to defaults."); }
/** * @brief Tries to set the key from an event. */ static int opt_setKeyEvent( unsigned int wid, SDL_Event *event ) { unsigned int parent; KeybindType type; int key; SDLMod mod, ev_mod; const char *str; int pos, off; /* See how to handle it. */ switch (event->type) { case SDL_KEYDOWN: key = event->key.keysym.sym; /* If control key make player hit twice. */ if (((key == SDLK_NUMLOCK) || (key == SDLK_CAPSLOCK) || (key == SDLK_SCROLLOCK) || (key == SDLK_RSHIFT) || (key == SDLK_LSHIFT) || (key == SDLK_RCTRL) || (key == SDLK_LCTRL) || (key == SDLK_RALT) || (key == SDLK_LALT) || (key == SDLK_RMETA) || (key == SDLK_LMETA) || (key == SDLK_LSUPER) || (key == SDLK_RSUPER)) && (opt_lastKeyPress != key)) { opt_lastKeyPress = key; return 0; } type = KEYBIND_KEYBOARD; if (window_checkboxState( wid, "chkAny" )) mod = NMOD_ALL; else { ev_mod = event->key.keysym.mod; mod = 0; if (ev_mod & (KMOD_LSHIFT | KMOD_RSHIFT)) mod |= NMOD_SHIFT; if (ev_mod & (KMOD_LCTRL | KMOD_RCTRL)) mod |= NMOD_CTRL; if (ev_mod & (KMOD_LALT | KMOD_RALT)) mod |= NMOD_ALT; if (ev_mod & (KMOD_LMETA | KMOD_RMETA)) mod |= NMOD_META; } /* Set key. */ opt_lastKeyPress = key; break; case SDL_JOYAXISMOTION: if (event->jaxis.value > 0) type = KEYBIND_JAXISPOS; else if (event->jaxis.value < 0) type = KEYBIND_JAXISNEG; else return 0; /* Not handled. */ key = event->jaxis.axis; mod = NMOD_ALL; break; case SDL_JOYBUTTONDOWN: type = KEYBIND_JBUTTON; key = event->jbutton.button; mod = NMOD_ALL; break; /* Not handled. */ default: return 0; } /* Warn if already bound. */ str = input_keyAlreadyBound( type, key, mod ); if ((str != NULL) && strcmp(str, opt_selectedKeybind)) dialogue_alert( "Key '%s' overlaps with key '%s' that was just set. " "You may want to correct this.", str, opt_selectedKeybind ); /* Set keybinding. */ input_setKeybind( opt_selectedKeybind, type, key, mod ); /* Close window. */ window_close( wid, NULL ); /* Update parent window. */ parent = window_getParent( wid ); pos = toolkit_getListPos( parent, "lstKeybinds" ); off = toolkit_getListOffset( parent, "lstKeybinds" ); window_destroyWidget( parent, "lstKeybinds" ); menuKeybinds_genList( parent ); toolkit_setListPos( parent, "lstKeybinds", pos ); toolkit_setListOffset( parent, "lstKeybinds", off ); return 0; }