Console behavior split into it's own base class (so that the Core and Editor can have different looking console panels).

Gui_Panel_Refactor
Joeyrp 4 years ago
parent 6e2f676f11
commit fed77546dc

@ -27,7 +27,7 @@ set(LUNARIUM_SRC
"src/core/core.cpp" "src/core/core.cpp"
"src/core/state.cpp" "src/core/state.cpp"
"src/core/version.cpp" "src/core/version.cpp"
"src/core/console.cpp" "src/core/core_console.cpp"
"src/core/iRunMode.cpp" "src/core/iRunMode.cpp"
"src/window/window.cpp" "src/window/window.cpp"
"src/graphics/opengl/glGraphics.cpp" "src/graphics/opengl/glGraphics.cpp"

@ -9,7 +9,7 @@
#include "core.h" #include "core.h"
#include "version.h" #include "version.h"
#include "console.h" #include "core_console.h"
#include <gui/dearimgui/imgui.h> #include <gui/dearimgui/imgui.h>
#include <LunariumConfig.h> #include <LunariumConfig.h>
@ -134,6 +134,7 @@ namespace lunarium
// Init the Debug log window // Init the Debug log window
OpRes result; OpRes result;
mPanels[gui::PanelType::PT_CORE_CONSOLE] = new CoreConsole;
// result = LogGui::GetInstance().Initialize(); // result = LogGui::GetInstance().Initialize();
// if (Failed(result)) // if (Failed(result))
// { // {
@ -231,13 +232,11 @@ namespace lunarium
} }
// RUN MODE // RUN MODE
mPanels[gui::PanelType::PT_CORE_CONSOLE] = nullptr;
const char* types[] = { "game", "editor", "test" }; const char* types[] = { "game", "editor", "test" };
Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running in mode: %s", types[mState.Mode]); Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running in mode: %s", types[mState.Mode]);
if (RunMode::MODE_TEST == mState.Mode) if (RunMode::MODE_TEST == mState.Mode)
{ {
mpRunMode = new Tester; mpRunMode = new Tester;
mPanels[gui::PanelType::PT_CORE_CONSOLE] = new Console;
} }
else if (RunMode::MODE_EDITOR == mState.Mode) else if (RunMode::MODE_EDITOR == mState.Mode)
@ -247,6 +246,9 @@ namespace lunarium
return; return;
#else #else
mpRunMode = new editor::Editor; mpRunMode = new editor::Editor;
delete mPanels[gui::PanelType::PT_CORE_CONSOLE];
mPanels[gui::PanelType::PT_CORE_CONSOLE] = nullptr;
// Editor has it's own console
// LogGui::GetInstance().SetStickToWindow(false); // LogGui::GetInstance().SetStickToWindow(false);
// LuaConsole::GetInstance().SetStickToWindow(false); // LuaConsole::GetInstance().SetStickToWindow(false);
#endif #endif
@ -295,7 +297,7 @@ namespace lunarium
glfwSetWindowTitle(mpWindow->GetWindow(), title.c_str()); glfwSetWindowTitle(mpWindow->GetWindow(), title.c_str());
// Get pointers to gui panels // Get pointers to gui panels
Console* con = (Console*)mPanels[gui::PanelType::PT_CORE_CONSOLE]; CoreConsole* con = (CoreConsole*)mPanels[gui::PanelType::PT_CORE_CONSOLE];
// Poll input // Poll input
Window::PollEvents(); Window::PollEvents();

@ -0,0 +1,72 @@
/******************************************************************************
* File - core_console.cpp
* Author - Joey Pollack
* Date - 2022/02/10 (y/m/d)
* Mod Date - 2022/02/10 (y/m/d)
* Description - console for the game mode
******************************************************************************/
#include "core_console.h"
#include <gui/dearimgui/imgui.h>
#include <gui/dearimgui/imgui_internal.h> // To use the DockWindowXXX methods
namespace lunarium
{
CoreConsole::CoreConsole()
: Console(gui::PanelType::PT_CORE_CONSOLE, "Core Console"), mDockIsInit(false)
{
}
bool CoreConsole::DoFrame()
{
InitDock();
if (!mIsOpen)
return false;
ImGuiViewport* pView = ImGui::GetMainViewport();
float myHeight = pView->WorkSize.y / 3.0f;
float y = pView->WorkPos.y + (myHeight * 2);
float alpha = IsFocused() ? 0.75f : 0.5f;
ImGui::SetNextWindowPos(ImVec2(pView->WorkPos.x, y), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(pView->WorkSize.x, myHeight), ImGuiCond_Always);
ImGui::SetNextWindowBgAlpha(alpha);
if (!ImGui::Begin(GetName(), &mIsOpen, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoCollapse))
{
ImGui::End();
return mIsOpen;
}
Console::DoFrame();
ImGui::End();
return mIsOpen;
}
void CoreConsole::InitDock()
{
ImGuiViewport* Viewport = ImGui::GetMainViewport();
// ImGuiID mainID = ImGui::DockSpace(ImGui::GetID("Core Dockspace"));
ImGuiID mainID = ImGui::DockSpaceOverViewport(Viewport, ImGuiDockNodeFlags_PassthruCentralNode);
if (!ImGui::DockBuilderGetNode(mainID) || !mDockIsInit)
{
mDockIsInit = true;
ImGui::DockBuilderRemoveNode(mainID);
ImGui::DockBuilderAddNode(mainID, ImGuiDockNodeFlags_DockSpace);
ImGui::DockBuilderSetNodeSize(mainID, Viewport->Size);
//ImGui::DockBuilderSetNodePos(mDockSpaces.Main, Viewport->WorkPos);
ImGuiID bottom = ImGui::DockBuilderSplitNode(mainID, ImGuiDir_Down, 0.35f, nullptr, nullptr);
ImGui::DockBuilderFinish(mainID);
// Dock Panels
//ImGui::DockBuilderDockWindow(GetName(), bottom);
}
}
}

@ -0,0 +1,28 @@
/******************************************************************************
* File - core_console.h
* Author - Joey Pollack
* Date - 2022/02/10 (y/m/d)
* Mod Date - 2022/02/10 (y/m/d)
* Description - console for the game mode
******************************************************************************/
#ifndef CORE_CONSOLE_H_
#define CORE_CONSOLE_H_
#include <gui/console.h>
namespace lunarium
{
class CoreConsole : public gui::Console
{
public:
CoreConsole();
bool DoFrame() override;
private:
void InitDock();
bool mDockIsInit;
};
}
#endif // CORE_CONSOLE_H_

@ -4,6 +4,7 @@ set(GUI_SRC
"gui.cpp" "gui.cpp"
"file_browser.cpp" "file_browser.cpp"
"panel.cpp" "panel.cpp"
"console.cpp"
) )
add_library(gui ${GUI_SRC}) add_library(gui ${GUI_SRC})

@ -12,13 +12,45 @@
#include <core/core.h> #include <core/core.h>
#include <input/inputManager.h> #include <input/inputManager.h>
#include <cstring> #include <cstring>
#include <sstream>
namespace lunarium namespace lunarium
{ {
Console::Console() namespace gui
: Panel(gui::PanelType::PT_CORE_CONSOLE, "Core Console", gui::PanelDockZone::DDZ_NONE, false), {
mbNewCommand(false), mRecalledCommand(-1), mAlpha(0.5f), mIsFocused(false) ////////////////////////////////////////////////////////////
// GUI LOG LISTENER
////////////////////////////////////////////////////////////
GuiListener::GuiListener(Console* pGui, uint32_t acceptedLogLevels, uint32_t acceptedLogCategories, const char* myName)
: LogListener(acceptedLogLevels, acceptedLogCategories, myName), mpConsole(pGui)
{
}
bool GuiListener::Log(LogMessage& message)
{
if (!LevelIsSet(message.LogLevel) ||
!CategoryIsSet(message.LogCategory))
return false;
std::ostringstream oss;
oss << std::endl << Logger::TimeStamp() << Logger::GetCategoryName(message.LogCategory)
<< Logger::GetLevelName(message.LogLevel) << message.Message << std::flush;
mpConsole->mMsgHistory.push_back(oss.str());
return true;
}
////////////////////////////////////////////////////////////
// CONSOLE
////////////////////////////////////////////////////////////
Console::Console(PanelType type, const char* name)
: Panel(type, name, gui::PanelDockZone::DDZ_NONE, false),
mbNewCommand(false), mRecalledCommand(-1), mIsFocused(false), mListener(nullptr),
mbOglDebug(false), mbInfoVerbose(false)
{ {
memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); memset(mBuffer, 0, LUA_CON_BUFFER_SIZE);
Logger::GetInstance()->AddListener(new GuiListener(this));
} }
bool Console::IsFocused() const bool Console::IsFocused() const
@ -33,49 +65,65 @@ namespace lunarium
bool Console::DoFrame() bool Console::DoFrame()
{ {
if (!mIsOpen)
return false;
if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true)) if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true))
{ {
mRecalledCommand = -1; mRecalledCommand = -1;
} }
ImGuiViewport* pView = ImGui::GetMainViewport();
float myHeight = pView->WorkSize.y / 3.0f;
float y = pView->WorkPos.y + (myHeight * 2);
ImGui::SetNextWindowPos(ImVec2(pView->WorkPos.x, y), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(pView->WorkSize.x, myHeight), ImGuiCond_Always);
ImGui::SetNextWindowBgAlpha(mAlpha);
if (!ImGui::Begin("Console", &mIsOpen, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse))
{
ImGui::End();
return mIsOpen;
}
float history_height = myHeight - 45; float history_height = ImGui::GetWindowSize().y - 45;
ImGui::BeginChild("history", ImVec2(0, history_height), false, ImGuiWindowFlags_AlwaysVerticalScrollbar); ImGui::BeginChild("history", ImVec2(0, history_height), false, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_NoMove);
mIsFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); mIsFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);
for (int i = 0; i < mCommandHistory.size(); i++) // for (int i = 0; i < mCommandHistory.size(); i++)
// {
// const char* msg = mCommandHistory[i].c_str();
// int len = strlen(msg);
// if (i == mRecalledCommand)
// {
// ImGui::TextColored(ImVec4(0.2f, 0.9f, 0.5f, 1.0f), msg);
// }
// else
// {
// ImGui::TextUnformatted(msg);
// }
// }
for (int i = 0; i < mMsgHistory.size(); i++)
{ {
const char* msg = mCommandHistory[i].c_str(); if ((mMsgHistory[i].find("[OGL_DEBUG]") != std::string::npos && !mbOglDebug)
int len = strlen(msg); || (mMsgHistory[i].find("[INFO_VERBOSE]") != std::string::npos && !mbInfoVerbose))
{
continue;
}
ImVec4 color(1.0f, 1.0f, 1.0f, 1.0f);
if (i == mRecalledCommand) if (mMsgHistory[i].find("[LUA]") != std::string::npos)
{ {
ImGui::TextColored(ImVec4(0.2f, 0.9f, 0.5f, 1.0f), msg); color = ImVec4(0.25f, 0.5f, 0.95f, 1.0f);
} }
else
if (mMsgHistory[i].find("WARNING") != std::string::npos)
{
color = ImVec4(0.75f, 0.75f, 0.0f, 1.0f);
}
if (mMsgHistory[i].find("ERROR") != std::string::npos)
{ {
ImGui::TextUnformatted(msg); color = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
} }
const char* msg = mMsgHistory[i].c_str();
int len = strlen(msg);
ImGui::PushTextWrapPos(ImGui::GetWindowWidth());
ImGui::TextColored(color, msg);
ImGui::PopTextWrapPos();
} }
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
@ -99,9 +147,8 @@ namespace lunarium
} }
ImGui::EndChild(); ImGui::EndChild();
ImGui::End();
mAlpha = mIsFocused ? 0.75f : 0.5f; //mAlpha = mIsFocused ? 0.75f : 0.5f;
return mIsOpen; return mIsOpen;
} }
@ -166,4 +213,4 @@ namespace lunarium
return 0; return 0;
} }
} }}

@ -11,15 +11,30 @@
#define CONSOLE_H_ #define CONSOLE_H_
#include <gui/panel.h> #include <gui/panel.h>
#include <utils/logger.h>
#include <vector> #include <vector>
namespace lunarium namespace lunarium
{ {
namespace gui
{
class Console;
class GuiListener : public LogListener
{
public:
GuiListener(Console* pCon, uint32_t acceptedLogLevels = LogLevel::ANY, uint32_t acceptedLogCategories = LogLevel::ANY, const char* myName = "Gui Listener");
virtual bool Log(LogMessage& message);
private:
Console* mpConsole;
};
const int LUA_CON_BUFFER_SIZE = 64; const int LUA_CON_BUFFER_SIZE = 64;
class Console : public gui::Panel class Console : public Panel
{ {
public: public:
Console(); Console(PanelType type, const char* name);
virtual void Update(float dt); virtual void Update(float dt);
virtual bool DoFrame(); virtual bool DoFrame();
@ -38,14 +53,21 @@ namespace lunarium
std::vector<std::string> mCommandHistory; std::vector<std::string> mCommandHistory;
bool mbNewCommand; bool mbNewCommand;
int mRecalledCommand; int mRecalledCommand;
float mAlpha; // float mAlpha;
bool mIsFocused; bool mIsFocused;
// LOG STUFF
std::vector<std::string> mMsgHistory;
friend GuiListener;
GuiListener* mListener;
bool mbOglDebug;
bool mbInfoVerbose;
private: private:
void CheckFocus(); void CheckFocus();
}; };
} }}
#endif // CONSOLE_H_ #endif // CONSOLE_H_
Loading…
Cancel
Save