CTRL+letter actions in console + cursor can be inside edit line

This commit is contained in:
BjossiAlfreds 2024-07-31 21:41:40 +00:00
parent 29eb6a601f
commit ced8be673e
4 changed files with 117 additions and 46 deletions

View file

@ -471,6 +471,8 @@ Con_DrawInput(void)
int i; int i;
float scale; float scale;
char *text; char *text;
char ch;
int txtlen;
if (cls.key_dest == key_menu) if (cls.key_dest == key_menu)
{ {
@ -486,28 +488,38 @@ Con_DrawInput(void)
scale = SCR_GetConsoleScale(); scale = SCR_GetConsoleScale();
text = key_lines[edit_line]; text = key_lines[edit_line];
/* add the cursor frame */
text[key_linepos] = 10 + ((int)(cls.realtime >> 8) & 1);
/* fill out remainder with spaces */
for (i = key_linepos + 1; i < con.linewidth; i++)
{
text[i] = ' ';
}
/* prestep if horizontally scrolling */ /* prestep if horizontally scrolling */
if (key_linepos >= con.linewidth) if (key_linepos >= con.linewidth)
{ {
text += 1 + key_linepos - con.linewidth; text += 1 + key_linepos - con.linewidth;
} }
txtlen = strlen(text);
for (i = 0; i < con.linewidth; i++) for (i = 0; i < con.linewidth; i++)
{ {
Draw_CharScaled(((i + 1) << 3) * scale, con.vislines - 22 * scale, text[i], scale); if (i == key_linepos)
} {
if (cls.realtime & 8)
{
ch = 11;
}
else
{
ch = (text[i] == '\0') ? 10 : text[i];
}
}
else if (i >= txtlen)
{
ch = ' ';
}
else
{
ch = text[i];
}
/* remove cursor */ Draw_CharScaled(((i + 1) << 3) * scale, con.vislines - 22 * scale, ch, scale);
key_lines[edit_line][key_linepos] = 0; }
} }
/* /*

View file

@ -29,6 +29,8 @@
#include "header/client.h" #include "header/client.h"
void IN_GetClipboardText(char *out, size_t n);
static cvar_t *cfg_unbindall; static cvar_t *cfg_unbindall;
/* /*
@ -346,6 +348,8 @@ CompleteMapNameCommand(void)
void void
Key_Console(int key) Key_Console(int key)
{ {
char txt[2], cliptext[256];
/* /*
* Ignore keypad in console to prevent duplicate * Ignore keypad in console to prevent duplicate
* entries through key presses processed as a * entries through key presses processed as a
@ -375,13 +379,25 @@ Key_Console(int key)
break; break;
} }
if (key == 'l') if (keydown[K_CTRL])
{ {
if (keydown[K_CTRL]) if (key == 'l')
{ {
Cbuf_AddText("clear\n"); Cbuf_AddText("clear\n");
return; return;
} }
if (key == 'v')
{
IN_GetClipboardText(cliptext, sizeof(cliptext));
if (*cliptext != '\0')
{
key_linepos += Q_strins(key_lines[edit_line], cliptext, key_linepos, MAXCMDLINE);
}
return;
}
} }
if ((key == K_ENTER) || (key == K_KP_ENTER)) if ((key == K_ENTER) || (key == K_KP_ENTER))
@ -423,9 +439,7 @@ Key_Console(int key)
return; return;
} }
if ((key == K_BACKSPACE) || (key == K_LEFTARROW) || if (key == K_LEFTARROW)
(key == K_KP_LEFTARROW) ||
((key == 'h') && (keydown[K_CTRL])))
{ {
if (key_linepos > 1) if (key_linepos > 1)
{ {
@ -435,11 +449,35 @@ Key_Console(int key)
return; return;
} }
if (key == K_RIGHTARROW)
{
if (key_lines[edit_line][key_linepos] != '\0')
{
key_linepos++;
}
return;
}
if ((key == K_BACKSPACE) ||
((key == 'h') && (keydown[K_CTRL])))
{
if (key_linepos > 1)
{
Q_strdel(key_lines[edit_line], key_linepos - 1, 1);
key_linepos--;
}
return;
}
if (key == K_DEL) if (key == K_DEL)
{ {
memmove(key_lines[edit_line] + key_linepos, if (key_lines[edit_line][key_linepos] != '\0')
key_lines[edit_line] + key_linepos + 1, {
sizeof(key_lines[edit_line]) - key_linepos - 1); Q_strdel(key_lines[edit_line], key_linepos, 1);
}
return; return;
} }
@ -547,32 +585,10 @@ Key_Console(int key)
return; /* non printable character */ return; /* non printable character */
} }
if (key_linepos < MAXCMDLINE - 1) *txt = key;
{ *(txt + 1) = '\0';
int last;
int length;
length = strlen(key_lines[edit_line]); key_linepos += Q_strins(key_lines[edit_line], txt, key_linepos, MAXCMDLINE);
if (length >= MAXCMDLINE - 1)
{
return;
}
last = key_lines[edit_line][key_linepos];
memmove(key_lines[edit_line] + key_linepos + 1,
key_lines[edit_line] + key_linepos,
length - key_linepos);
key_lines[edit_line][key_linepos] = key;
key_linepos++;
if (!last)
{
key_lines[edit_line][key_linepos] = 0;
}
}
} }
qboolean chat_team; qboolean chat_team;
@ -1009,6 +1025,10 @@ Key_ReadConsoleHistory()
} }
} }
/* input line is always blank */
key_linepos = 1;
strcpy (key_lines[edit_line], "]");
fclose(f); fclose(f);
} }
@ -1045,6 +1065,7 @@ Key_Init(void)
consolekeys[i] = true; consolekeys[i] = true;
} }
consolekeys[K_DEL] = true;
consolekeys[K_ENTER] = true; consolekeys[K_ENTER] = true;
consolekeys[K_KP_ENTER] = true; consolekeys[K_KP_ENTER] = true;
consolekeys[K_TAB] = true; consolekeys[K_TAB] = true;
@ -1380,6 +1401,12 @@ Key_Event(int key, qboolean down, qboolean special)
return; return;
} }
/* FIXME: Better way to do CTRL+letter actions in the console */
if (keydown[K_CTRL] && cls.key_dest == key_console && key >= 'a' && key <= 'z')
{
special = true;
}
/* All input subsystems handled after this point only /* All input subsystems handled after this point only
care for key down events (=> if(!down) returns above). */ care for key down events (=> if(!down) returns above). */

View file

@ -2412,3 +2412,19 @@ IN_Shutdown(void)
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
void
IN_GetClipboardText(char *out, size_t n)
{
char *s = SDL_GetClipboardText();
if (!s || *s == '\0')
{
*out = '\0';
return;
}
Q_strlcpy(out, s, n - 1);
SDL_free(s);
}

View file

@ -2410,3 +2410,19 @@ IN_Shutdown(void)
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
void
IN_GetClipboardText(char *out, size_t n)
{
char *s = SDL_GetClipboardText();
if (!s || *s == '\0')
{
*out = '\0';
return;
}
Q_strlcpy(out, s, n - 1);
SDL_free(s);
}