Extend IConsole::PossibleCommands, add index to callback

`PossibleCommands` now passes the item index to the callback and returns the total number of items.

Add `EmptyPossibleCommandCallback` as default parameter.
This commit is contained in:
Robert Müller 2022-08-09 17:26:57 +02:00
parent 680abb6405
commit f3eb93b83a
5 changed files with 20 additions and 18 deletions

View file

@ -82,14 +82,16 @@ public:
typedef void (*FTeeHistorianCommandCallback)(int ClientID, int FlagMask, const char *pCmd, IResult *pResult, void *pUser); typedef void (*FTeeHistorianCommandCallback)(int ClientID, int FlagMask, const char *pCmd, IResult *pResult, void *pUser);
typedef void (*FPrintCallback)(const char *pStr, void *pUser, ColorRGBA PrintColor); typedef void (*FPrintCallback)(const char *pStr, void *pUser, ColorRGBA PrintColor);
typedef void (*FPossibleCallback)(const char *pCmd, void *pUser); typedef void (*FPossibleCallback)(int Index, const char *pCmd, void *pUser);
typedef void (*FCommandCallback)(IResult *pResult, void *pUserData); typedef void (*FCommandCallback)(IResult *pResult, void *pUserData);
typedef void (*FChainCommandCallback)(IResult *pResult, void *pUserData, FCommandCallback pfnCallback, void *pCallbackUserData); typedef void (*FChainCommandCallback)(IResult *pResult, void *pUserData, FCommandCallback pfnCallback, void *pCallbackUserData);
static void EmptyPossibleCommandCallback(int Index, const char *pCmd, void *pUser) {}
virtual void Init() = 0; virtual void Init() = 0;
virtual const CCommandInfo *FirstCommandInfo(int AccessLevel, int Flagmask) const = 0; virtual const CCommandInfo *FirstCommandInfo(int AccessLevel, int Flagmask) const = 0;
virtual const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) = 0; virtual const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) = 0;
virtual void PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) = 0; virtual int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback = EmptyPossibleCommandCallback, void *pUser = nullptr) = 0;
virtual void ParseArguments(int NumArgs, const char **ppArguments) = 0; virtual void ParseArguments(int NumArgs, const char **ppArguments) = 0;
virtual void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) = 0; virtual void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) = 0;

View file

@ -551,16 +551,21 @@ void CConsole::ExecuteLineStroked(int Stroke, const char *pStr, int ClientID, bo
} }
} }
void CConsole::PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) int CConsole::PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser)
{ {
int Index = 0;
for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext) for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext)
{ {
if(pCommand->m_Flags & FlagMask && pCommand->m_Temp == Temp) if(pCommand->m_Flags & FlagMask && pCommand->m_Temp == Temp)
{ {
if(str_find_nocase(pCommand->m_pName, pStr)) if(str_find_nocase(pCommand->m_pName, pStr))
pfnCallback(pCommand->m_pName, pUser); {
pfnCallback(Index, pCommand->m_pName, pUser);
Index++;
}
} }
} }
return Index;
} }
CConsole::CCommand *CConsole::FindCommand(const char *pName, int FlagMask) CConsole::CCommand *CConsole::FindCommand(const char *pName, int FlagMask)

View file

@ -196,7 +196,7 @@ public:
void Init() override; void Init() override;
const CCommandInfo *FirstCommandInfo(int AccessLevel, int FlagMask) const override; const CCommandInfo *FirstCommandInfo(int AccessLevel, int FlagMask) const override;
const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) override; const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) override;
void PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) override; int PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) override;
void ParseArguments(int NumArgs, const char **ppArguments) override; void ParseArguments(int NumArgs, const char **ppArguments) override;
void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) override; void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) override;

View file

@ -161,12 +161,11 @@ void CGameConsole::CInstance::ExecuteLine(const char *pLine)
} }
} }
void CGameConsole::CInstance::PossibleCommandsCompleteCallback(const char *pStr, void *pUser) void CGameConsole::CInstance::PossibleCommandsCompleteCallback(int Index, const char *pStr, void *pUser)
{ {
CGameConsole::CInstance *pInstance = (CGameConsole::CInstance *)pUser; CGameConsole::CInstance *pInstance = (CGameConsole::CInstance *)pUser;
if(pInstance->m_CompletionChosen == pInstance->m_CompletionEnumerationCount) if(pInstance->m_CompletionChosen == Index)
pInstance->m_Input.Set(pStr); pInstance->m_Input.Set(pStr);
pInstance->m_CompletionEnumerationCount++;
} }
void CGameConsole::CInstance::OnInput(IInput::CEvent Event) void CGameConsole::CInstance::OnInput(IInput::CEvent Event)
@ -335,16 +334,13 @@ void CGameConsole::CInstance::OnInput(IInput::CEvent Event)
m_CompletionChosen--; m_CompletionChosen--;
else if(!m_ReverseTAB) else if(!m_ReverseTAB)
m_CompletionChosen++; m_CompletionChosen++;
m_CompletionEnumerationCount = 0; int CompletionEnumerationCount = m_pGameConsole->m_pConsole->PossibleCommands(m_aCompletionBuffer, m_CompletionFlagmask, m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(), PossibleCommandsCompleteCallback, this);
m_pGameConsole->m_pConsole->PossibleCommands(m_aCompletionBuffer, m_CompletionFlagmask, m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(), PossibleCommandsCompleteCallback, this);
m_CompletionUsed = true; m_CompletionUsed = true;
// handle wrapping // handle wrapping
if(m_CompletionEnumerationCount && (m_CompletionChosen >= m_CompletionEnumerationCount || m_CompletionChosen < 0)) if(CompletionEnumerationCount && (m_CompletionChosen >= CompletionEnumerationCount || m_CompletionChosen < 0))
{ {
m_CompletionChosen = (m_CompletionChosen + m_CompletionEnumerationCount) % m_CompletionEnumerationCount; m_CompletionChosen = (m_CompletionChosen + CompletionEnumerationCount) % CompletionEnumerationCount;
m_CompletionEnumerationCount = 0;
m_pGameConsole->m_pConsole->PossibleCommands(m_aCompletionBuffer, m_CompletionFlagmask, m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(), PossibleCommandsCompleteCallback, this); m_pGameConsole->m_pConsole->PossibleCommands(m_aCompletionBuffer, m_CompletionFlagmask, m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(), PossibleCommandsCompleteCallback, this);
} }
} }
@ -487,7 +483,7 @@ struct CRenderInfo
float m_Width; float m_Width;
}; };
void CGameConsole::PossibleCommandsRenderCallback(const char *pStr, void *pUser) void CGameConsole::PossibleCommandsRenderCallback(int Index, const char *pStr, void *pUser)
{ {
CRenderInfo *pInfo = static_cast<CRenderInfo *>(pUser); CRenderInfo *pInfo = static_cast<CRenderInfo *>(pUser);

View file

@ -40,7 +40,6 @@ class CGameConsole : public CComponent
CLineInput m_Input; CLineInput m_Input;
const char *m_pName; const char *m_pName;
int m_Type; int m_Type;
int m_CompletionEnumerationCount;
int m_BacklogCurPage; int m_BacklogCurPage;
CGameConsole *m_pGameConsole; CGameConsole *m_pGameConsole;
@ -74,7 +73,7 @@ class CGameConsole : public CComponent
void PrintLine(const char *pLine, int Len, ColorRGBA PrintColor); void PrintLine(const char *pLine, int Len, ColorRGBA PrintColor);
const char *GetString() const { return m_Input.GetString(); } const char *GetString() const { return m_Input.GetString(); }
static void PossibleCommandsCompleteCallback(const char *pStr, void *pUser); static void PossibleCommandsCompleteCallback(int Index, const char *pStr, void *pUser);
}; };
class IConsole *m_pConsole; class IConsole *m_pConsole;
@ -106,7 +105,7 @@ class CGameConsole : public CComponent
void Toggle(int Type); void Toggle(int Type);
void Dump(int Type); void Dump(int Type);
static void PossibleCommandsRenderCallback(const char *pStr, void *pUser); static void PossibleCommandsRenderCallback(int Index, const char *pStr, void *pUser);
static void ConToggleLocalConsole(IConsole::IResult *pResult, void *pUserData); static void ConToggleLocalConsole(IConsole::IResult *pResult, void *pUserData);
static void ConToggleRemoteConsole(IConsole::IResult *pResult, void *pUserData); static void ConToggleRemoteConsole(IConsole::IResult *pResult, void *pUserData);
static void ConClearLocalConsole(IConsole::IResult *pResult, void *pUserData); static void ConClearLocalConsole(IConsole::IResult *pResult, void *pUserData);