Add sprinting and other binddt functions

This commit is contained in:
Ryan Baldwin 2022-08-19 21:42:57 -07:00
parent 96a7b5d33d
commit 05d82bc901
5 changed files with 126 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -271,6 +271,7 @@ void Host_WriteConfiguration (void)
}
Key_WriteBindings (f);
Key_WriteDTBindings (f);
Cvar_WriteVariables (f);
fclose (f);

View File

@ -40,6 +40,7 @@ keydest_t key_dest;
int key_count; // incremented every key event
char *keybindings[256];
char *dtbindings[256];
qboolean consolekeys[256]; // if true, can't be rebound while in console
qboolean menubound[256]; // if true, can't be rebound while in menu
int keyshift[256]; // key to map to if shift held down in console
@ -466,6 +467,34 @@ void Key_SetBinding (int keynum, char *binding)
keybindings[keynum] = new;
}
/*
===================
Key_SetDTBinding
===================
*/
void Key_SetDTBinding (int keynum, char *binding)
{
char *new;
int l;
if (keynum == -1)
return;
// free old bindings
if (dtbindings[keynum])
{
Z_Free (dtbindings[keynum]);
dtbindings[keynum] = NULL;
}
// allocate memory for new binding
l = Q_strlen (binding);
new = Z_Malloc (l+1);
Q_strcpy (new, binding);
new[l] = 0;
dtbindings[keynum] = new;
}
/*
===================
Key_Unbind_f
@ -546,6 +575,51 @@ void Key_Bind_f (void)
Key_SetBinding (b, cmd);
}
/*
===================
Key_Binddt_f
===================
*/
void Key_Binddt_f (void)
{
int i, c, b;
char cmd[1024];
c = Cmd_Argc();
if (c != 2 && c != 3)
{
Con_Printf ("binddt <key> [command] : attach a command to a double tap key\n");
return;
}
b = Key_StringToKeynum (Cmd_Argv(1));
if (b==-1)
{
Con_Printf ("\"%s\" isn't a valid key\n", Cmd_Argv(1));
return;
}
if (c == 2)
{
if (dtbindings[b])
Con_Printf ("\"%s\" = \"%s\"\n", Cmd_Argv(1), dtbindings[b] );
else
Con_Printf ("\"%s\" is not bound\n", Cmd_Argv(1) );
return;
}
// copy the rest of the command line
cmd[0] = 0; // start out with a null string
for (i=2 ; i< c ; i++)
{
if (i > 2)
strcat (cmd, " ");
strcat (cmd, Cmd_Argv(i));
}
Key_SetDTBinding (b, cmd);
}
/*
============
Key_WriteBindings
@ -563,6 +637,22 @@ void Key_WriteBindings (FILE *f)
fprintf (f, "bind \"%s\" \"%s\"\n", Key_KeynumToString(i), keybindings[i]);
}
/*
============
Key_WriteDTBindings
Writes lines containing "binddt key value"
============
*/
void Key_WriteDTBindings (FILE *f)
{
int i;
for (i=0 ; i<256 ; i++)
if (dtbindings[i])
if (*dtbindings[i])
fprintf (f, "binddt \"%s\" \"%s\"\n", Key_KeynumToString(i), dtbindings[i]);
}
/*
===================
@ -634,6 +724,7 @@ void Key_Init (void)
// register our functions
//
Cmd_AddCommand ("bind",Key_Bind_f);
Cmd_AddCommand ("binddt",Key_Binddt_f);
Cmd_AddCommand ("unbind",Key_Unbind_f);
Cmd_AddCommand ("unbindall",Key_Unbindall_f);
@ -648,11 +739,19 @@ Called by the system between frames for both key up and key down events
Should NOT be called during an interrupt!
===================
*/
int lastkey;
double lastkeytime;
int oldkey;
double oldkeytime;
void Key_Event (int key, qboolean down)
{
char *kb;
char cmd[1024];
oldkey = lastkey;
keydown[key] = down;
lastkey = key;
keydown[key] = down;
if (!down)
@ -668,6 +767,8 @@ void Key_Event (int key, qboolean down)
// update auto-repeat status
if (down)
{
oldkeytime = lastkeytime;
lastkeytime = Sys_FloatTime();
key_repeats[key]++;
if (key != K_BACKSPACE && key != K_PAUSE && key_repeats[key] > 1)
{
@ -750,6 +851,28 @@ void Key_Event (int key, qboolean down)
|| (key_dest == key_console && !consolekeys[key])
|| (key_dest == key_game && ( !con_forcedup || !consolekeys[key] ) ) )
{
if (oldkey == key && ((oldkeytime + 0.3) > lastkeytime))
{
kb = dtbindings[key];
if (kb)
{
if (kb[0] == '+')
{ // button commands add keynum as a parm
sprintf (cmd, kb, key);
Cbuf_AddText (cmd);
}
else
{
Cbuf_AddText (kb);
Cbuf_AddText ("\n");
}
}
oldkey = 0;
oldkeytime = 0;
lastkeytime = 0;
lastkey = 0;
}
kb = keybindings[key];
if (kb)
{

View File

@ -124,6 +124,7 @@ typedef enum {key_game, key_console, key_message, key_menu, key_menu_pause} keyd
extern keydest_t key_dest;
extern char *keybindings[256];
extern char *dtbindings[256];
extern int key_repeats[256];
extern int key_count; // incremented every key event
extern int key_lastpress;
@ -132,4 +133,5 @@ void Key_Event (int key, qboolean down);
void Key_Init (void);
void Key_WriteBindings (FILE *f);
void Key_SetBinding (int keynum, char *binding);
void Key_WriteDTBindings (FILE *f);
void Key_ClearStates (void);