Use emplace_back for CCommand, fix member variable names

This commit is contained in:
Robert Müller 2022-05-24 21:19:33 +02:00 committed by heinrich5991
parent d0b5d096f5
commit 476af1286d
2 changed files with 16 additions and 10 deletions

View file

@ -42,7 +42,7 @@ CChat::CChat()
void CChat::RegisterCommand(const char *pName, const char *pParams, int flags, const char *pHelp) void CChat::RegisterCommand(const char *pName, const char *pParams, int flags, const char *pHelp)
{ {
m_Commands.push_back(CCommand{pName, pParams}); m_Commands.emplace_back(pName, pParams);
} }
void CChat::RebuildChat() void CChat::RebuildChat()
@ -380,7 +380,7 @@ bool CChat::OnInput(IInput::CEvent Event)
auto &Command = m_Commands[Index]; auto &Command = m_Commands[Index];
if(str_startswith(Command.pName, pCommandStart)) if(str_startswith(Command.m_pName, pCommandStart))
{ {
pCompletionCommand = &Command; pCompletionCommand = &Command;
m_CompletionChosen = Index + SearchType * NumCommands; m_CompletionChosen = Index + SearchType * NumCommands;
@ -397,10 +397,10 @@ bool CChat::OnInput(IInput::CEvent Event)
// add the command // add the command
str_append(aBuf, "/", sizeof(aBuf)); str_append(aBuf, "/", sizeof(aBuf));
str_append(aBuf, pCompletionCommand->pName, sizeof(aBuf)); str_append(aBuf, pCompletionCommand->m_pName, sizeof(aBuf));
// add separator // add separator
const char *pSeparator = pCompletionCommand->pParams[0] == '\0' ? "" : " "; const char *pSeparator = pCompletionCommand->m_pParams[0] == '\0' ? "" : " ";
str_append(aBuf, pSeparator, sizeof(aBuf)); str_append(aBuf, pSeparator, sizeof(aBuf));
if(*pSeparator) if(*pSeparator)
str_append(aBuf, pSeparator, sizeof(aBuf)); str_append(aBuf, pSeparator, sizeof(aBuf));
@ -408,7 +408,7 @@ bool CChat::OnInput(IInput::CEvent Event)
// add part after the name // add part after the name
str_append(aBuf, m_Input.GetString() + m_PlaceholderOffset + m_PlaceholderLength, sizeof(aBuf)); str_append(aBuf, m_Input.GetString() + m_PlaceholderOffset + m_PlaceholderLength, sizeof(aBuf));
m_PlaceholderLength = str_length(pSeparator) + str_length(pCompletionCommand->pName) + 1; m_PlaceholderLength = str_length(pSeparator) + str_length(pCompletionCommand->m_pName) + 1;
m_OldChatStringLength = m_Input.GetLength(); m_OldChatStringLength = m_Input.GetLength();
m_Input.Set(aBuf); // TODO: Use Add instead m_Input.Set(aBuf); // TODO: Use Add instead
m_Input.SetCursorOffset(m_PlaceholderOffset + m_PlaceholderLength); m_Input.SetCursorOffset(m_PlaceholderOffset + m_PlaceholderLength);

View file

@ -93,12 +93,18 @@ class CChat : public CComponent
struct CCommand struct CCommand
{ {
const char *pName; const char *m_pName;
const char *pParams; const char *m_pParams;
bool operator<(const CCommand &Other) const { return str_comp(pName, Other.pName) < 0; } CCommand() {}
bool operator<=(const CCommand &Other) const { return str_comp(pName, Other.pName) <= 0; } CCommand(const char *pName, const char *pParams) :
bool operator==(const CCommand &Other) const { return str_comp(pName, Other.pName) == 0; } m_pName(pName), m_pParams(pParams)
{
}
bool operator<(const CCommand &Other) const { return str_comp(m_pName, Other.m_pName) < 0; }
bool operator<=(const CCommand &Other) const { return str_comp(m_pName, Other.m_pName) <= 0; }
bool operator==(const CCommand &Other) const { return str_comp(m_pName, Other.m_pName) == 0; }
}; };
std::vector<CCommand> m_Commands; std::vector<CCommand> m_Commands;