added auto completion of nicknames and highlighted nicknames in chat. Closes #218

This commit is contained in:
oy 2010-12-16 02:31:12 +01:00
parent 1d7df01e9a
commit 6063b4c14d
3 changed files with 64 additions and 0 deletions

View file

@ -35,6 +35,10 @@ void CChat::OnReset()
m_Show = false;
m_InputUpdate = false;
m_ChatStringOffset = 0;
m_CompletionChosen = -1;
m_aCompletionBuffer[0] = 0;
m_PlaceholderOffset = 0;
m_PlaceholderLength = 0;
}
void CChat::OnRelease()
@ -104,8 +108,58 @@ bool CChat::OnInput(IInput::CEvent e)
m_Mode = MODE_NONE;
m_pClient->OnRelease();
}
if(e.m_Flags&IInput::FLAG_PRESS && e.m_Key == KEY_TAB)
{
// fill the completion buffer
if(m_CompletionChosen < 0)
{
const char *pCursor = m_Input.GetString()+m_Input.GetCursorOffset();
for(int Count = 0; Count < m_Input.GetCursorOffset() && *(pCursor-1) != ' '; --pCursor, ++Count);
m_PlaceholderOffset = pCursor-m_Input.GetString();
for(m_PlaceholderLength = 0; *pCursor && *pCursor != ' '; ++pCursor)
++m_PlaceholderLength;
str_copy(m_aCompletionBuffer, m_Input.GetString()+m_PlaceholderOffset, min(static_cast<int>(sizeof(m_aCompletionBuffer)), m_PlaceholderLength+1));
}
// find next possible name
const char *pCompletionString = 0;
m_CompletionChosen = (m_CompletionChosen+1)%MAX_CLIENTS;
for(int i = 0; i < MAX_CLIENTS; ++i)
{
int Index = (m_CompletionChosen+i)%MAX_CLIENTS;
if(!m_pClient->m_Snap.m_paPlayerInfos[Index])
continue;
if(str_find_nocase(m_pClient->m_aClients[Index].m_aName, m_aCompletionBuffer))
{
pCompletionString = m_pClient->m_aClients[Index].m_aName;
m_CompletionChosen = Index;
break;
}
}
// insert the name
if(pCompletionString)
{
char aBuf[256];
str_copy(aBuf, m_Input.GetString(), min(static_cast<int>(sizeof(aBuf)), m_PlaceholderOffset+1));
str_append(aBuf, pCompletionString, sizeof(aBuf));
str_append(aBuf, m_Input.GetString()+m_PlaceholderOffset+m_PlaceholderLength, sizeof(aBuf));
m_PlaceholderLength = str_length(pCompletionString);
m_OldChatStringLength = m_Input.GetLength();
m_Input.Set(aBuf);
m_Input.SetCursorOffset(m_PlaceholderOffset+m_PlaceholderLength);
m_InputUpdate = true;
}
}
else
{
// reset name completion process
if(e.m_Flags&IInput::FLAG_PRESS && e.m_Key != KEY_TAB)
m_CompletionChosen = -1;
m_OldChatStringLength = m_Input.GetLength();
m_Input.ProcessInput(e);
m_InputUpdate = true;
@ -129,6 +183,7 @@ void CChat::EnableMode(int Team)
m_Input.Clear();
Input()->ClearEvents();
m_CompletionChosen = -1;
}
}
@ -167,6 +222,7 @@ void CChat::AddLine(int ClientId, int Team, const char *pLine)
m_aLines[m_CurrentLine].m_ClientId = ClientId;
m_aLines[m_CurrentLine].m_Team = Team;
m_aLines[m_CurrentLine].m_NameColor = -2;
m_aLines[m_CurrentLine].m_Highlighted = str_find_nocase(pLine, m_pClient->m_aClients[m_pClient->m_Snap.m_LocalCid].m_aName) != 0;
if(ClientId == -1) // server message
{
@ -309,6 +365,8 @@ void CChat::OnRender()
// render line
if(m_aLines[r].m_ClientId == -1)
TextRender()->TextColor(1.0f, 1.0f, 0.5f, 1.0f); // system
else if(m_aLines[r].m_Highlighted)
TextRender()->TextColor(1.0f, 0.5f, 0.5f, 1.0f); // highlighted
else if(m_aLines[r].m_Team)
TextRender()->TextColor(0.65f, 1.0f, 0.65f, 1.0f); // team message
else

View file

@ -23,6 +23,7 @@ class CChat : public CComponent
int m_NameColor;
char m_aName[64];
char m_aText[512];
bool m_Highlighted;
};
CLine m_aLines[MAX_LINES];
@ -41,6 +42,10 @@ class CChat : public CComponent
bool m_InputUpdate;
int m_ChatStringOffset;
int m_OldChatStringLength;
int m_CompletionChosen;
char m_aCompletionBuffer[256];
int m_PlaceholderOffset;
int m_PlaceholderLength;
static void ConSay(IConsole::IResult *pResult, void *pUserData);
static void ConSayTeam(IConsole::IResult *pResult, void *pUserData);

View file

@ -28,6 +28,7 @@ public:
const char *GetString() const { return m_Str; }
int GetLength() const { return m_Len; }
int GetCursorOffset() const { return m_CursorPos; }
void SetCursorOffset(int Offset) { m_CursorPos = Offset > m_Len ? m_Len : Offset < 0 ? 0 : Offset; }
};
#endif