Skip to content

Commit

Permalink
restore old chat code (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfomin authored Dec 5, 2024
1 parent f88fe30 commit bef20d0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 65 deletions.
10 changes: 0 additions & 10 deletions src/i_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,16 +722,6 @@ static int GetTypedChar(SDL_Keysym *sym)
{
int result = TranslateKey(sym);

switch (result)
{
case KEY_BACKSPACE:
case KEY_ESCAPE:
case KEY_ENTER:
return 0;
default:
break;
}

// If shift is held down, apply the original uppercase
// translation table used under DOS.
if ((SDL_GetModState() & KMOD_SHIFT) != 0 && result >= 0
Expand Down
2 changes: 1 addition & 1 deletion src/mn_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -4696,7 +4696,7 @@ int MN_StringWidth(const char *string)
continue;
}
c = M_ToUpper(c) - HU_FONTSTART;
if (c < 0 || c > HU_FONTSIZE || hu_font[c] == NULL)
if (c < 0 || c >= HU_FONTSIZE || hu_font[c] == NULL)
{
w += SPACEWIDTH;
continue;
Expand Down
158 changes: 104 additions & 54 deletions src/st_widgets.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "hu_command.h"
#include "hu_coordinates.h"
#include "hu_obituary.h"
#include "i_input.h"
#include "i_timer.h"
#include "i_video.h"
#include "m_array.h"
Expand Down Expand Up @@ -181,6 +180,52 @@ static void UpdateAnnounceMessage(sbe_widget_t *widget, player_t *player)
}
}

// key tables
// jff 5/10/98 french support removed,
// as it was not being used and couldn't be easily tested
//

static const char shiftxform[] =
{
0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31,
' ', '!', '"', '#', '$', '%', '&',
'"', // shift-'
'(', ')', '*', '+',
'<', // shift-,
'_', // shift--
'>', // shift-.
'?', // shift-/
')', // shift-0
'!', // shift-1
'@', // shift-2
'#', // shift-3
'$', // shift-4
'%', // shift-5
'^', // shift-6
'&', // shift-7
'*', // shift-8
'(', // shift-9
':',
':', // shift-;
'<',
'+', // shift-=
'>', '?', '@',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'[', // shift-[
'!', // shift-backslash - OH MY GOD DOES WATCOM SUCK
']', // shift-]
'"', '_',
'\'', // shift-`
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'{', '|', '}', '~', 127
};

typedef struct
{
char string[HU_MAXLINELENGTH];
Expand All @@ -197,8 +242,6 @@ static void ClearChatLine(chatline_t *line)

static boolean AddKeyToChatLine(chatline_t *line, char ch)
{
ch = M_ToUpper(ch);

if (ch >= ' ' && ch <= '_')
{
if (line->pos == HU_MAXLINELENGTH - 1)
Expand Down Expand Up @@ -255,20 +298,28 @@ void ST_UpdateChatMessage(void)
{
chat_dest[p] = ch;
}
else if (AddKeyToChatLine(&lines[p], ch) && ch == KEY_ENTER)
else
{
if (lines[p].pos
&& (chat_dest[p] == consoleplayer + 1
|| chat_dest[p] == HU_BROADCAST))
if (ch >= 'a' && ch <= 'z')
{
M_snprintf(message_string, sizeof(message_string), "%s%s",
*player_names[p], lines[p].string);
ch = (char)shiftxform[(unsigned char)ch];
}

if (AddKeyToChatLine(&lines[p], ch) && ch == KEY_ENTER)
{
if (lines[p].pos && (chat_dest[p] == consoleplayer + 1
|| chat_dest[p] == HU_BROADCAST))
{
M_snprintf(message_string, sizeof(message_string),
"%s%s", *player_names[p], lines[p].string);

S_StartSoundPitch(
0, gamemode == commercial ? sfx_radio : sfx_tink,
PITCH_NONE);
S_StartSoundPitch(0,
gamemode == commercial ? sfx_radio
: sfx_tink,
PITCH_NONE);
}
ClearChatLine(&lines[p]);
}
ClearChatLine(&lines[p]);
}
players[p].cmd.chatchar = 0;
}
Expand Down Expand Up @@ -335,42 +386,48 @@ char ST_DequeueChatChar(void)

static chatline_t chatline;

static void StartChatInput(int dest)
{
chat_on = true;
ClearChatLine(&chatline);
QueueChatChar(dest);
I_StartTextInput();
}

static void StopChatInput(void)
{
chat_on = false;
I_StopTextInput();
}

boolean ST_MessagesResponder(event_t *ev)
{
if (ev->type == ev_text)
{
return false;
}

static char lastmessage[HU_MAXLINELENGTH + 1];

boolean eatkey = false;
static boolean shiftdown = false;
static boolean altdown = false;
int ch;
int numplayers;

static int num_nobrainers = 0;

ch = (ev->type == ev_keydown) ? ev->data1.i : 0;

numplayers = 0;
for (int p = 0; p < MAXPLAYERS; p++)
{
numplayers += playeringame[p];
}

if (ev->data1.i == KEY_RSHIFT)
{
shiftdown = ev->type == ev_keydown;
return false;
}

if (ev->data1.i == KEY_RALT)
{
altdown = ev->type != ev_keyup;
altdown = ev->type == ev_keydown;
return false;
}

if (M_InputActivated(input_chat_backspace))
{
ch = KEY_BACKSPACE;
}

if (!chat_on)
{
if (M_InputActivated(input_chat_enter)) // phares
Expand All @@ -385,8 +442,9 @@ boolean ST_MessagesResponder(event_t *ev)
}
else if (netgame && M_InputActivated(input_chat))
{
eatkey = true;
StartChatInput(HU_BROADCAST);
eatkey = chat_on = true;
ClearChatLine(&chatline);
QueueChatChar(HU_BROADCAST);
}
else if (netgame && numplayers > 2) // killough 11/98: simplify
{
Expand All @@ -406,8 +464,9 @@ boolean ST_MessagesResponder(event_t *ev)
}
else if (playeringame[p])
{
eatkey = true;
StartChatInput(p + 1);
eatkey = chat_on = true;
ClearChatLine(&chatline);
QueueChatChar((char)(p + 1));
break;
}
}
Expand All @@ -416,15 +475,18 @@ boolean ST_MessagesResponder(event_t *ev)
} // jff 2/26/98 no chat functions if message review is displayed
else
{
if (M_InputActivated(input_chat_enter))
{
ch = KEY_ENTER;
}

// send a macro
if (altdown)
{
int ch = (ev->type == ev_keydown) ? ev->data1.i : 0;

ch = ch - '0';
if (ch < 0 || ch > 9)
{
return true;
return false;
}
const char *macromessage = chat_macros[ch];

Expand All @@ -439,34 +501,26 @@ boolean ST_MessagesResponder(event_t *ev)
QueueChatChar(KEY_ENTER); // phares

// leave chat mode and notify that it was sent
StopChatInput();
chat_on = false;
M_StringCopy(lastmessage, chat_macros[ch], sizeof(lastmessage));
displaymsg("%s", lastmessage);
eatkey = true;
}
else
{
int ch = (ev->type == ev_keydown) ? ev->data1.i : 0;

int txt = 0;
if (ev->type == ev_text)
{
txt = ev->data1.i;
}
else if (ch == KEY_ENTER || ch == KEY_BACKSPACE)
if (shiftdown || (ch >= 'a' && ch <= 'z'))
{
txt = ch;
ch = shiftxform[ch];
}

eatkey = AddKeyToChatLine(&chatline, txt);
eatkey = AddKeyToChatLine(&chatline, ch);
if (eatkey)
{
QueueChatChar(txt);
QueueChatChar(ch);
}

if (ch == KEY_ENTER) // phares
{
StopChatInput();
chat_on = false;
if (chatline.pos)
{
M_StringCopy(lastmessage, chatline.string,
Expand All @@ -476,11 +530,7 @@ boolean ST_MessagesResponder(event_t *ev)
}
else if (ch == KEY_ESCAPE) // phares
{
StopChatInput();
}
else
{
eatkey = true;
chat_on = false;
}
}
}
Expand Down

8 comments on commit bef20d0

@fabiangreffrath
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we should handle altdown in the i_input.c code, e.g. using SDL_GetModState() and setting event->data4 for this, or similar.

@rfomin
Copy link
Collaborator Author

@rfomin rfomin commented on bef20d0 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if SDL_GetModState() would help, the problem is that SDL_TEXTINPUT allows alt-key combinations for character input.

I thought about rewriting the chat widget, Doom chat was never good. But it's only worth it if we improve the multiplayer significantly (at least solve the frame interpolation problem).

@fabiangreffrath
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the problem is to tell apart the character that is reached by pressing Alt+Number from the macro that is reached by the same keys?

@rfomin
Copy link
Collaborator Author

@rfomin rfomin commented on bef20d0 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the problem is to tell apart the character that is reached by pressing Alt+Number from the macro that is reached by the same keys?

Yes.

Also I don't know which special keys (like Enter, Esc, Alt) trigger SDL_TEXTINPUT events, it's not documented. Looks like keyboard input has been improved in SDL3: https://wiki.libsdl.org/SDL3/BestKeyboardPractices

@fabiangreffrath
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds pretty much like what we already have in SDL2. I'd say we are already following best practice in this regard.

However, this article precisely describes our dilemma: You either track key presses or you receive text, but not both at the same time.

@rfomin
Copy link
Collaborator Author

@rfomin rfomin commented on bef20d0 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You either track key presses or you receive text, but not both at the same time.

Yes, that's why I think we should just rewrite it. Allow cursor movement, a little chat history, and so on. And remove the macros.

But all this work is pointless, as multiplayer is almost unplayable. No interpolation, horrible lags, etc. If we fix multiplayer, it would be interesting to integrate it into the menu, rewrite the chat, maybe even add a "modern" coop mode.

@fabiangreffrath
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all very noble goals!

However, I don't want to let the "perfect become the enemy of the good." In this case, not fix all the little annoyances that we have now, because we are planning to replace everything with something better anyway.

@rfomin
Copy link
Collaborator Author

@rfomin rfomin commented on bef20d0 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But SDL_TEXTINPUT and Alt-Num macros are not working well. I think the old chat is broken - no cursor movement, Del and Esc don't work. I don't feel motivated to improve it without at least fixing the mutiplayer interpolation.

Please sign in to comment.