Skip to content

Commit

Permalink
Merged pull request "Sync with Q2PRO: Input things ": #371
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Jan 30, 2024
2 parents 6d8a013 + f872959 commit 92f8d91
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 80 deletions.
73 changes: 68 additions & 5 deletions src/client/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ static void Con_CheckTop(void)
{
int top = con.current - CON_TOTALLINES + 1;

if (top < 1) {
top = 1;
if (top < 0) {
top = 0;
}
if (con.display < top) {
con.display = top;
Expand Down Expand Up @@ -713,6 +713,8 @@ static int Con_DrawLine(int v, int row, float alpha)
s += line->ts_len;
w -= line->ts_len;
}
if (w < 1)
return x;

switch (line->color) {
case COLOR_ALT:
Expand Down Expand Up @@ -1158,16 +1160,67 @@ static void Con_Paste(char *(*func)(void))
IF_CharEvent(&con.prompt.inputLine, ' ');
break;
default:
if (Q_isprint(c)) {
IF_CharEvent(&con.prompt.inputLine, c);
if (!Q_isprint(c)) {
c = '?';
}
IF_CharEvent(&con.prompt.inputLine, c);
break;
}
}

Z_Free(cbd);
}

// console lines are not necessarily NUL-terminated
static void Con_ClearLine(char *buf, int row)
{
consoleLine_t *line = &con.text[row & CON_TOTALLINES_MASK];
char *s = line->text + line->ts_len;
int w = con.linewidth - line->ts_len;

while (w-- > 0 && *s)
*buf++ = *s++ & 127;
*buf = 0;
}

static void Con_SearchUp(void)
{
char buf[CON_LINEWIDTH + 1];
char *s = con.prompt.inputLine.text;
int top = con.current - CON_TOTALLINES + 1;

if (top < 0)
top = 0;

if (!*s)
return;

for (int row = con.display - 1; row >= top; row--) {
Con_ClearLine(buf, row);
if (Q_stristr(buf, s)) {
con.display = row;
break;
}
}
}

static void Con_SearchDown(void)
{
char buf[CON_LINEWIDTH + 1];
char *s = con.prompt.inputLine.text;

if (!*s)
return;

for (int row = con.display + 1; row <= con.current; row++) {
Con_ClearLine(buf, row);
if (Q_stristr(buf, s)) {
con.display = row;
break;
}
}
}

/*
====================
Key_Console
Expand Down Expand Up @@ -1219,6 +1272,16 @@ void Key_Console(int key)
goto scroll;
}

if (key == K_UPARROW && Key_IsDown(K_CTRL)) {
Con_SearchUp();
return;
}

if (key == K_DOWNARROW && Key_IsDown(K_CTRL)) {
Con_SearchDown();
return;
}

if (key == K_UPARROW || (key == 'p' && Key_IsDown(K_CTRL))) {
Prompt_HistoryUp(&con.prompt);
goto scroll;
Expand Down Expand Up @@ -1252,7 +1315,7 @@ void Key_Console(int key)
}

if (key == K_HOME && Key_IsDown(K_CTRL)) {
con.display = 1;
con.display = 0;
Con_CheckTop();
return;
}
Expand Down
77 changes: 40 additions & 37 deletions src/client/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,50 +643,53 @@ static void m_autosens_changed(cvar_t *self)
autosens_y = 1.0f / V_CalcFov(fov, 4, 3);
}

static const cmdreg_t c_input[] = {
{ "centerview", IN_CenterView },
{ "+moveup", IN_UpDown },
{ "-moveup", IN_UpUp },
{ "+movedown", IN_DownDown },
{ "-movedown", IN_DownUp },
{ "+left", IN_LeftDown },
{ "-left", IN_LeftUp },
{ "+right", IN_RightDown },
{ "-right", IN_RightUp },
{ "+forward", IN_ForwardDown },
{ "-forward", IN_ForwardUp },
{ "+back", IN_BackDown },
{ "-back", IN_BackUp },
{ "+lookup", IN_LookupDown },
{ "-lookup", IN_LookupUp },
{ "+lookdown", IN_LookdownDown },
{ "-lookdown", IN_LookdownUp },
{ "+strafe", IN_StrafeDown },
{ "-strafe", IN_StrafeUp },
{ "+moveleft", IN_MoveleftDown },
{ "-moveleft", IN_MoveleftUp },
{ "+moveright", IN_MoverightDown },
{ "-moveright", IN_MoverightUp },
{ "+speed", IN_SpeedDown },
{ "-speed", IN_SpeedUp },
{ "+attack", IN_AttackDown },
{ "-attack", IN_AttackUp },
{ "+use", IN_UseDown },
{ "-use", IN_UseUp },
{ "impulse", IN_Impulse },
{ "+klook", IN_KLookDown },
{ "-klook", IN_KLookUp },
{ "+mlook", IN_MLookDown },
{ "-mlook", IN_MLookUp },
{ "in_restart", IN_Restart_f },
{ NULL }
};

/*
============
CL_RegisterInput
============
*/
void CL_RegisterInput(void)
{
Cmd_AddCommand("centerview", IN_CenterView);

Cmd_AddCommand("+moveup", IN_UpDown);
Cmd_AddCommand("-moveup", IN_UpUp);
Cmd_AddCommand("+movedown", IN_DownDown);
Cmd_AddCommand("-movedown", IN_DownUp);
Cmd_AddCommand("+left", IN_LeftDown);
Cmd_AddCommand("-left", IN_LeftUp);
Cmd_AddCommand("+right", IN_RightDown);
Cmd_AddCommand("-right", IN_RightUp);
Cmd_AddCommand("+forward", IN_ForwardDown);
Cmd_AddCommand("-forward", IN_ForwardUp);
Cmd_AddCommand("+back", IN_BackDown);
Cmd_AddCommand("-back", IN_BackUp);
Cmd_AddCommand("+lookup", IN_LookupDown);
Cmd_AddCommand("-lookup", IN_LookupUp);
Cmd_AddCommand("+lookdown", IN_LookdownDown);
Cmd_AddCommand("-lookdown", IN_LookdownUp);
Cmd_AddCommand("+strafe", IN_StrafeDown);
Cmd_AddCommand("-strafe", IN_StrafeUp);
Cmd_AddCommand("+moveleft", IN_MoveleftDown);
Cmd_AddCommand("-moveleft", IN_MoveleftUp);
Cmd_AddCommand("+moveright", IN_MoverightDown);
Cmd_AddCommand("-moveright", IN_MoverightUp);
Cmd_AddCommand("+speed", IN_SpeedDown);
Cmd_AddCommand("-speed", IN_SpeedUp);
Cmd_AddCommand("+attack", IN_AttackDown);
Cmd_AddCommand("-attack", IN_AttackUp);
Cmd_AddCommand("+use", IN_UseDown);
Cmd_AddCommand("-use", IN_UseUp);
Cmd_AddCommand("impulse", IN_Impulse);
Cmd_AddCommand("+klook", IN_KLookDown);
Cmd_AddCommand("-klook", IN_KLookUp);
Cmd_AddCommand("+mlook", IN_MLookDown);
Cmd_AddCommand("-mlook", IN_MLookUp);

Cmd_AddCommand("in_restart", IN_Restart_f);
Cmd_Register(c_input);

cl_nodelta = Cvar_Get("cl_nodelta", "0", 0);
cl_maxpackets = Cvar_Get("cl_maxpackets", "30", 0);
Expand Down
9 changes: 5 additions & 4 deletions src/client/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ void Key_SetDest(keydest_t dest)
IN_Activate();
CL_CheckForPause();
}

if (dest == KEY_GAME) {
anykeydown = 0;
}
}

/*
Expand Down Expand Up @@ -664,7 +668,7 @@ void Key_Event(unsigned key, bool down, unsigned time)

if (cls.key_dest == KEY_GAME &&
cl.frame.ps.stats[STAT_LAYOUTS] &&
cls.demo.playback == false) {
!cls.demo.playback) {
if (keydown[key] == 2) {
// force main menu if escape is held
UI_OpenMenu(UIMENU_GAME);
Expand Down Expand Up @@ -771,9 +775,6 @@ void Key_Event(unsigned key, bool down, unsigned time)
return;
}

if (cls.key_dest == KEY_GAME)
return;

if (!down) {
if (cls.key_dest & KEY_MENU)
UI_KeyEvent(key, down);
Expand Down
2 changes: 1 addition & 1 deletion src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2852,7 +2852,7 @@ CL_Activate
void CL_Activate(active_t active)
{
if (cls.active != active) {
Com_DDDPrintf("%s: %u\n", __func__, active);
Com_DDPrintf("%s: %u\n", __func__, active);
cls.active = active;
cls.disable_screen = 0;
Key_ClearStates();
Expand Down
50 changes: 17 additions & 33 deletions src/common/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
static cvar_t *com_completion_mode;
static cvar_t *com_completion_treshold;

static void Prompt_ShowMatches(commandPrompt_t *prompt, char **matches,
int start, int end)
static void Prompt_ShowMatches(commandPrompt_t *prompt, char **matches, int count)
{
int count = end - start;
int numCols = 7, numLines;
int i, j, k;
size_t maxlen, len, total;
size_t colwidths[6];
char *match;

// determine number of columns needed
do {
numCols--;
numLines = (count + numCols - 1) / numCols;
total = 0;
for (i = 0; i < numCols; i++) {
k = start + numLines * i;
if (k >= end) {
break;
}
maxlen = 0;
for (j = k; j < k + numLines && j < end; j++) {
k = min((i + 1) * numLines, count);
for (j = i * numLines; j < k; j++) {
len = strlen(matches[j]);
maxlen = max(maxlen, len);
}
Expand All @@ -67,19 +61,11 @@ static void Prompt_ShowMatches(commandPrompt_t *prompt, char **matches,

for (i = 0; i < numLines; i++) {
for (j = 0; j < numCols; j++) {
k = start + j * numLines + i;
if (k >= end) {
k = j * numLines + i;
if (k >= count) {
break;
}
match = matches[k];
prompt->printf("%s", match);
len = strlen(match);
if (len < colwidths[j]) {
// pad with spaces
for (k = 0; k < colwidths[j] - len; k++) {
prompt->printf(" ");
}
}
prompt->printf("%*s", -(int)colwidths[j], matches[k]);
}
prompt->printf("\n");
}
Expand All @@ -92,36 +78,34 @@ static void Prompt_ShowIndividualMatches(
int numAliases,
int numCvars)
{
int offset = 0;

if (numCommands) {
qsort(matches + offset, numCommands, sizeof(matches[0]), SortStrcmp);
qsort(matches, numCommands, sizeof(matches[0]), SortStrcmp);

prompt->printf("\n%i possible command%s:\n",
numCommands, numCommands != 1 ? "s" : "");

Prompt_ShowMatches(prompt, matches, offset, offset + numCommands);
offset += numCommands;
Prompt_ShowMatches(prompt, matches, numCommands);
matches += numCommands;
}

if (numCvars) {
qsort(matches + offset, numCvars, sizeof(matches[0]), SortStrcmp);
qsort(matches, numCvars, sizeof(matches[0]), SortStrcmp);

prompt->printf("\n%i possible variable%s:\n",
numCvars, numCvars != 1 ? "s" : "");

Prompt_ShowMatches(prompt, matches, offset, offset + numCvars);
offset += numCvars;
Prompt_ShowMatches(prompt, matches, numCvars);
matches += numCvars;
}

if (numAliases) {
qsort(matches + offset, numAliases, sizeof(matches[0]), SortStrcmp);
qsort(matches, numAliases, sizeof(matches[0]), SortStrcmp);

prompt->printf("\n%i possible alias%s:\n",
numAliases, numAliases != 1 ? "es" : "");

Prompt_ShowMatches(prompt, matches, offset, offset + numAliases);
offset += numAliases;
Prompt_ShowMatches(prompt, matches, numAliases);
matches += numAliases;
}
}

Expand Down Expand Up @@ -340,7 +324,7 @@ void Prompt_CompleteCommand(commandPrompt_t *prompt, bool backslash)
case 1:
multi:
// print in multiple columns
Prompt_ShowMatches(prompt, sorted, 0, ctx.count);
Prompt_ShowMatches(prompt, sorted, ctx.count);
break;
case 2:
default:
Expand Down Expand Up @@ -549,7 +533,7 @@ void Prompt_SaveHistory(commandPrompt_t *prompt, const char *filename, int lines
}
for (; i < prompt->inputLineNum; i++) {
s = prompt->history[i & HISTORY_MASK];
if (s) {
if (s && *s) {
FS_FPrintf(f, "%s\n", s);
}
}
Expand Down

0 comments on commit 92f8d91

Please sign in to comment.