CConsole improvements: Add selectable logger text

CConsole now uses a dedicated class for logging text (modified CTextEditor class (CTextLogger)). The class uses an ImDraw list with a character vector to draw the text.

Text could be selected by double click (word), triple click (line), a selection by dragging the cursor, or everything with 'ctrl + a'.
This commit is contained in:
Kawe Mazidjatari 2022-06-20 10:31:09 +02:00
parent 1b81f2d1eb
commit 1764f039cf
7 changed files with 32 additions and 53 deletions

View File

@ -55,6 +55,7 @@
#if !defined(DEDICATED) && !defined(SDKLAUNCHER) && !defined (NETCONSOLE)
#include "thirdparty/imgui/include/imgui.h"
#include "thirdparty/imgui/include/imgui_stdlib.h"
#include "thirdparty/imgui/include/imgui_logger.h"
#include "thirdparty/imgui/include/imgui_editor.h"
#include "thirdparty/imgui/include/imgui_utility.h"
#include "thirdparty/imgui/include/imgui_internal.h"

View File

@ -92,7 +92,7 @@ void CBaseFileSystem::Warning(CBaseFileSystem* pFileSystem, FileWarningLevel_t l
wconsole->debug(szBuf);
#ifndef DEDICATED
iconsole->debug(szBuf);
g_pConsole->m_ivConLog.push_back(CConLog(g_spd_sys_w_oss.str(), ImVec4(1.00f, 1.00f, 0.00f, 1.00f)));
g_pConsole->AddLog(CConLog(g_spd_sys_w_oss.str(), ImVec4(1.00f, 1.00f, 0.00f, 1.00f)));
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();

View File

@ -28,7 +28,6 @@ History:
//-----------------------------------------------------------------------------
CConsole::CConsole(void)
{
ClearLog();
memset(m_szInputBuf, '\0', sizeof(m_szInputBuf));
m_nHistoryPos = -1;
@ -161,16 +160,16 @@ void CConsole::Think(void)
{
for (;;) // Loop running at 100-tps.
{
if (m_ivConLog.size() > con_max_size_logvector->GetSizeT())
if (m_Logger.GetTotalLines() > con_max_size_logvector->GetInt())
{
while (m_ivConLog.size() > con_max_size_logvector->GetSizeT() / 4 * 3)
while (m_Logger.GetTotalLines() > con_max_size_logvector->GetInt() / 4 * 3)
{
m_ivConLog.erase(m_ivConLog.begin());
m_Logger.RemoveLine(0);
m_nScrollBack++;
}
}
while (static_cast<int>(m_vsvHistory.size()) > 512)
while (m_vsvHistory.size() > 512)
{
m_vsvHistory.erase(m_vsvHistory.begin());
}
@ -227,10 +226,9 @@ void CConsole::BasePanel(void)
ImGui::Separator();
///////////////////////////////////////////////////////////////////////
ImGui::BeginChild("ScrollingRegion", ImVec2(0, -flFooterHeightReserve), true, ImGuiWindowFlags_AlwaysVerticalScrollbar);
ImGui::BeginChild("ScrollingRegion", ImVec2(0, -flFooterHeightReserve), true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysVerticalScrollbar);
m_Logger.Render();
if (m_bCopyToClipBoard) { ImGui::LogToClipboard(); }
ColorLog();
if (m_bCopyToClipBoard)
{
ImGui::LogToClipboard();
@ -777,12 +775,21 @@ int CConsole::TextEditCallbackStub(ImGuiInputTextCallbackData* iData)
return pConsole->TextEditCallback(iData);
}
//-----------------------------------------------------------------------------
// Purpose: adds logs to the vector
// Input : &conLog -
//-----------------------------------------------------------------------------
void CConsole::AddLog(const CConLog& conLog)
{
m_Logger.InsertText(conLog);
}
//-----------------------------------------------------------------------------
// Purpose: adds logs to the vector
// Input : *fmt -
// ... -
//-----------------------------------------------------------------------------
void CConsole::AddLog(ImVec4 color, const char* fmt, ...) IM_FMTARGS(2)
void CConsole::AddLog(const ImVec4& color, const char* fmt, ...) IM_FMTARGS(2)
{
char buf[1024];
va_list args{};
@ -790,35 +797,16 @@ void CConsole::AddLog(ImVec4 color, const char* fmt, ...) IM_FMTARGS(2)
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
buf[IM_ARRAYSIZE(buf) - 1] = 0;
va_end(args);
m_ivConLog.push_back(CConLog(Strdup(buf), color));
m_Logger.InsertText(CConLog(Strdup(buf), color));
}
//-----------------------------------------------------------------------------
// Purpose: clears the entire vector
// Purpose: clears the entire log vector
//-----------------------------------------------------------------------------
void CConsole::ClearLog(void)
{
//for (int i = 0; i < m_ivConLog.size(); i++) { free(m_ivConLog[i]); }
m_ivConLog.clear();
}
//-----------------------------------------------------------------------------
// Purpose: colors important logs
//-----------------------------------------------------------------------------
void CConsole::ColorLog(void) const
{
for (size_t i = 0; i < m_ivConLog.size(); i++)
{
if (!m_itFilter.PassFilter(m_ivConLog[i].m_svConLog.c_str()))
{
continue;
}
ImGui::PushStyleColor(ImGuiCol_Text, m_ivConLog[i].m_imColor);
ImGui::TextWrapped(m_ivConLog[i].m_svConLog.c_str());
ImGui::PopStyleColor();
///////////////////////////////////////////////////////////////////////
}
m_Logger.RemoveLine(0, m_Logger.GetTotalLines());
}
//-----------------------------------------------------------------------------

View File

@ -2,6 +2,7 @@
#ifndef DEDICATED
#include "common/sdkdefs.h"
#include "windows/resource.h"
#include "thirdparty/imgui/include/imgui_logger.h"
struct CSuggest
{
@ -22,17 +23,6 @@ struct CSuggest
int m_nFlags;
};
struct CConLog
{
CConLog(const string& svConLog, const ImVec4& imColor)
{
m_svConLog = svConLog;
m_imColor = imColor;
}
string m_svConLog;
ImVec4 m_imColor;
};
class CConsole
{
private:
@ -63,6 +53,7 @@ private:
ImVec2 m_ivSuggestWindowPos;
ImVec2 m_ivSuggestWindowSize;
CTextLogger m_Logger;
ImGuiInputTextFlags m_nInputFlags =
ImGuiInputTextFlags_AutoCaretEnd |
@ -82,7 +73,6 @@ private:
public:
bool m_bActivate = false;
vector<CConLog> m_ivConLog;
vector<CSuggest> m_vsvCommandBases;
///////////////////////////////////////////////////////////////////////////
@ -109,9 +99,9 @@ public:
static int TextEditCallbackStub(ImGuiInputTextCallbackData* pData);
///////////////////////////////////////////////////////////////////////////
void AddLog(ImVec4 color, const char* fmt, ...) IM_FMTARGS(2);
void AddLog(const CConLog& conLog);
void AddLog(const ImVec4& color, const char* fmt, ...) IM_FMTARGS(2);
void ClearLog(void);
void ColorLog(void) const;
///////////////////////////////////////////////////////////////////////////
void SetStyleVar(void);

View File

@ -162,7 +162,7 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...)
}
}
g_pConsole->m_ivConLog.push_back(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), color));
g_pConsole->AddLog(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), color));
g_pLogSystem.AddLog(static_cast<LogType_t>(context), g_spd_sys_w_oss.str());
g_spd_sys_w_oss.str("");
@ -232,7 +232,7 @@ SQRESULT SQVM_WarningFunc(HSQUIRRELVM v, SQInteger a2, SQInteger a3, SQInteger*
#ifndef DEDICATED
iconsole->debug(vmStr); // Emit to in-game console.
g_pConsole->m_ivConLog.push_back(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), ImVec4(1.00f, 1.00f, 0.00f, 0.80f)));
g_pConsole->AddLog(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), ImVec4(1.00f, 1.00f, 0.00f, 0.80f)));
g_pLogSystem.AddLog(LogType_t::WARNING_C, g_spd_sys_w_oss.str());
g_spd_sys_w_oss.str("");

View File

@ -172,7 +172,7 @@ void DevMsg(eDLL_T context, const char* fmt, ...)
break;
}
g_pConsole->m_ivConLog.push_back(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), color));
g_pConsole->AddLog(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), color));
g_pLogSystem.AddLog(tLog, g_spd_sys_w_oss.str());
g_spd_sys_w_oss.str("");
@ -247,7 +247,7 @@ void Warning(eDLL_T context, const char* fmt, ...)
#ifndef DEDICATED
iconsole->info(svOut);
g_pConsole->m_ivConLog.push_back(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), ImVec4(1.00f, 1.00f, 0.00f, 0.80f)));
g_pConsole->AddLog(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), ImVec4(1.00f, 1.00f, 0.00f, 0.80f)));
g_pLogSystem.AddLog(LogType_t::WARNING_C, g_spd_sys_w_oss.str());
g_spd_sys_w_oss.str("");
@ -322,7 +322,7 @@ void Error(eDLL_T context, const char* fmt, ...)
#ifndef DEDICATED
iconsole->info(svOut);
g_pConsole->m_ivConLog.push_back(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), ImVec4(1.00f, 0.00f, 0.00f, 1.00f)));
g_pConsole->AddLog(CConLog(PrintPercentageEscape(g_spd_sys_w_oss.str()), ImVec4(1.00f, 0.00f, 0.00f, 1.00f)));
g_pLogSystem.AddLog(LogType_t::ERROR_C, g_spd_sys_w_oss.str());
g_spd_sys_w_oss.str("");

View File

@ -32,7 +32,7 @@ int HQHull_PrintFunc(const char* fmt, ...)
#ifndef DEDICATED
iconsole->debug(buf);
g_pConsole->m_ivConLog.push_back(CConLog(g_spd_sys_w_oss.str(), ImVec4(0.81f, 0.81f, 0.81f, 1.00f)));
g_pConsole->AddLog(CConLog(g_spd_sys_w_oss.str(), ImVec4(0.81f, 0.81f, 0.81f, 1.00f)));
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();