diff --git a/CMakeLists.txt b/CMakeLists.txt index fa90e76..e2d5256 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ set(LUNARIUM_SRC "src/core/core.cpp" "src/core/state.cpp" "src/core/version.cpp" -"src/core/console.cpp" +"src/core/core_console.cpp" "src/core/iRunMode.cpp" "src/window/window.cpp" "src/graphics/opengl/glGraphics.cpp" diff --git a/src/core/core.cpp b/src/core/core.cpp index 9635e47..68af891 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -9,7 +9,7 @@ #include "core.h" #include "version.h" -#include "console.h" +#include "core_console.h" #include #include @@ -134,6 +134,7 @@ namespace lunarium // Init the Debug log window OpRes result; + mPanels[gui::PanelType::PT_CORE_CONSOLE] = new CoreConsole; // result = LogGui::GetInstance().Initialize(); // if (Failed(result)) // { @@ -231,13 +232,11 @@ namespace lunarium } // RUN MODE - mPanels[gui::PanelType::PT_CORE_CONSOLE] = nullptr; const char* types[] = { "game", "editor", "test" }; Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running in mode: %s", types[mState.Mode]); if (RunMode::MODE_TEST == mState.Mode) { mpRunMode = new Tester; - mPanels[gui::PanelType::PT_CORE_CONSOLE] = new Console; } else if (RunMode::MODE_EDITOR == mState.Mode) @@ -247,6 +246,9 @@ namespace lunarium return; #else 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); // LuaConsole::GetInstance().SetStickToWindow(false); #endif @@ -295,7 +297,7 @@ namespace lunarium glfwSetWindowTitle(mpWindow->GetWindow(), title.c_str()); // 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 Window::PollEvents(); diff --git a/src/core/core_console.cpp b/src/core/core_console.cpp new file mode 100644 index 0000000..5a7c772 --- /dev/null +++ b/src/core/core_console.cpp @@ -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 +#include // 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); + } + } +} \ No newline at end of file diff --git a/src/core/core_console.h b/src/core/core_console.h new file mode 100644 index 0000000..8b7148b --- /dev/null +++ b/src/core/core_console.h @@ -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 + +namespace lunarium +{ + class CoreConsole : public gui::Console + { + public: + CoreConsole(); + bool DoFrame() override; + + private: + void InitDock(); + bool mDockIsInit; + }; +} + +#endif // CORE_CONSOLE_H_ \ No newline at end of file diff --git a/src/internal_libs/gui/CMakeLists.txt b/src/internal_libs/gui/CMakeLists.txt index 2db9096..0c2517b 100644 --- a/src/internal_libs/gui/CMakeLists.txt +++ b/src/internal_libs/gui/CMakeLists.txt @@ -4,6 +4,7 @@ set(GUI_SRC "gui.cpp" "file_browser.cpp" "panel.cpp" +"console.cpp" ) add_library(gui ${GUI_SRC}) diff --git a/src/core/console.cpp b/src/internal_libs/gui/console.cpp similarity index 57% rename from src/core/console.cpp rename to src/internal_libs/gui/console.cpp index 8fb82e8..5b3a119 100644 --- a/src/core/console.cpp +++ b/src/internal_libs/gui/console.cpp @@ -12,13 +12,45 @@ #include #include #include +#include namespace lunarium { - Console::Console() - : Panel(gui::PanelType::PT_CORE_CONSOLE, "Core Console", gui::PanelDockZone::DDZ_NONE, false), - mbNewCommand(false), mRecalledCommand(-1), mAlpha(0.5f), mIsFocused(false) + namespace gui + { + //////////////////////////////////////////////////////////// + // 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); + Logger::GetInstance()->AddListener(new GuiListener(this)); } bool Console::IsFocused() const @@ -33,49 +65,65 @@ namespace lunarium bool Console::DoFrame() { - if (!mIsOpen) - return false; if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true)) { 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; - ImGui::BeginChild("history", ImVec2(0, history_height), false, ImGuiWindowFlags_AlwaysVerticalScrollbar); + float history_height = ImGui::GetWindowSize().y - 45; + ImGui::BeginChild("history", ImVec2(0, history_height), false, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_NoMove); 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(); - int len = strlen(msg); + if ((mMsgHistory[i].find("[OGL_DEBUG]") != std::string::npos && !mbOglDebug) + || (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()) @@ -99,9 +147,8 @@ namespace lunarium } ImGui::EndChild(); - ImGui::End(); - mAlpha = mIsFocused ? 0.75f : 0.5f; + //mAlpha = mIsFocused ? 0.75f : 0.5f; return mIsOpen; } @@ -166,4 +213,4 @@ namespace lunarium return 0; } -} \ No newline at end of file +}} \ No newline at end of file diff --git a/src/core/console.h b/src/internal_libs/gui/console.h similarity index 62% rename from src/core/console.h rename to src/internal_libs/gui/console.h index 1f3ec54..302010d 100644 --- a/src/core/console.h +++ b/src/internal_libs/gui/console.h @@ -11,15 +11,30 @@ #define CONSOLE_H_ #include +#include #include 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; - class Console : public gui::Panel + class Console : public Panel { public: - Console(); + Console(PanelType type, const char* name); virtual void Update(float dt); virtual bool DoFrame(); @@ -38,14 +53,21 @@ namespace lunarium std::vector mCommandHistory; bool mbNewCommand; int mRecalledCommand; - float mAlpha; + // float mAlpha; bool mIsFocused; + // LOG STUFF + std::vector mMsgHistory; + friend GuiListener; + GuiListener* mListener; + bool mbOglDebug; + bool mbInfoVerbose; + private: void CheckFocus(); }; -} +}} #endif // CONSOLE_H_ \ No newline at end of file