From 7628794d1035ddb93988f9305c4a0977a44b818f Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Tue, 28 Sep 2021 15:18:38 -0400 Subject: [PATCH] LuaConsole now retains input focus after pressing enter. But must press escape to use command recall. --- src/core/core.cpp | 5 ++- src/graphics/gui/logGui.cpp | 7 +++- src/graphics/gui/luaConsole.cpp | 64 +++++++++++++++++++++++++++------ src/graphics/gui/luaConsole.h | 1 + src/scripting/scriptManager.cpp | 7 ++-- 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 0b8f1d5..5f70cf4 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -308,7 +308,10 @@ namespace lunarium { // Logger::Log(LogCategory::CORE, LogLevel::INFO, "New LUA command: %s", command.c_str()); OpRes result = ScriptManager::RunScript(command.c_str()); - // NOTE: Ignoring script result + if (Failed(result)) + { + Logger::Log(LogCategory::CORE, LogLevel::INFO_VERBOSE, result.Description.c_str()); + } } } diff --git a/src/graphics/gui/logGui.cpp b/src/graphics/gui/logGui.cpp index 4dcbe48..82022cc 100644 --- a/src/graphics/gui/logGui.cpp +++ b/src/graphics/gui/logGui.cpp @@ -125,7 +125,7 @@ namespace lunarium ImVec4 color(1.0f, 1.0f, 1.0f, 1.0f); - if (mMsgHistory[i].find("WANRING") != std::string::npos) + if (mMsgHistory[i].find("WARNING") != std::string::npos) { color = ImVec4(0.75f, 0.75f, 0.0f, 1.0f); } @@ -135,6 +135,11 @@ namespace lunarium color = ImVec4(1.0f, 0.0f, 0.0f, 1.0f); } + if (mMsgHistory[i].find("[LUA]") != std::string::npos) + { + color = ImVec4(0.25f, 0.5f, 0.95f, 1.0f); + } + const char* msg = mMsgHistory[i].c_str(); int len = strlen(msg); diff --git a/src/graphics/gui/luaConsole.cpp b/src/graphics/gui/luaConsole.cpp index ff6375e..7d06f1a 100644 --- a/src/graphics/gui/luaConsole.cpp +++ b/src/graphics/gui/luaConsole.cpp @@ -19,7 +19,7 @@ namespace lunarium { LuaConsole* LuaConsole::mpInstance = nullptr; LuaConsole::LuaConsole() - : mbShow(false), mbStickToWindow(true), mbNewCommand(false) + : mbShow(false), mbStickToWindow(true), mbNewCommand(false), mRecalledCommand(-1) { memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); } @@ -55,11 +55,13 @@ namespace lunarium Core::MainWindow().GetPosition(&x, &y); int width, height; Core::MainWindow().GetFramebufferSize(&width, &height); + + float myHeight = height / 3.0f; if (mbStickToWindow) { ImGui::SetNextWindowPos(ImVec2(x, y), ImGuiCond_Always); - ImGui::SetNextWindowSize(ImVec2(width / 3.0f, height / 3.0f), ImGuiCond_Always); + ImGui::SetNextWindowSize(ImVec2(width / 3.0f, myHeight + 20), ImGuiCond_Always); if (!ImGui::Begin("LUA Console", &mbShow, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings)) { @@ -70,7 +72,7 @@ namespace lunarium else { ImGui::SetNextWindowPos(ImVec2(x, y), ImGuiCond_FirstUseEver); - ImGui::SetNextWindowSize(ImVec2(width / 3.0f, height / 3.0f), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowSize(ImVec2(width / 3.0f, myHeight + 20), ImGuiCond_FirstUseEver); if (!ImGui::Begin("LUA Console", &mbShow)) { ImGui::End(); @@ -79,7 +81,7 @@ namespace lunarium } - ImGui::BeginChild("history", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); + ImGui::BeginChild("history", ImVec2(0, myHeight - 45), false, ImGuiWindowFlags_AlwaysVerticalScrollbar); for (int i = 0; i < mCommandHistory.size(); i++) { const char* msg = mCommandHistory[i].c_str(); @@ -88,20 +90,62 @@ namespace lunarium ImGui::TextUnformatted(msg); } + if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) + ImGui::SetScrollHereY(1.0f); ImGui::EndChild(); ImGui::Separator(); - ImGui::InputText("command", mBuffer, LUA_CON_BUFFER_SIZE); + ImGui::BeginChild("console", ImVec2(0, 20)); + if (ImGui::InputText("command", mBuffer, LUA_CON_BUFFER_SIZE, ImGuiInputTextFlags_EnterReturnsTrue)) + { + mCommandHistory.push_back(mBuffer); + memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); + mbNewCommand = true; - if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) - ImGui::SetScrollHereY(1.0f); + // Doesn't keep the focus on the input text field + // Might be another way to do it now + ImGui::SetKeyboardFocusHere(-1); + } + + ImGui::EndChild(); ImGui::End(); - if (Core::Input().IsKeyPressed(KeyCode::RETURN, true) && strlen(mBuffer) > 1) + // if (Core::Input().IsKeyPressed(KeyCode::RETURN, true) && strlen(mBuffer) > 1) + // { + // mCommandHistory.push_back(mBuffer); + // memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); + // mbNewCommand = true; + + // // Doesn't keep the focus on the input text field + // // Might be another way to do it now + // ImGui::SetKeyboardFocusHere(-1); + // } + + bool changeCommand = false; + if (Core::Input().IsKeyPressed(KeyCode::UP, true)) + { + mRecalledCommand--; + if (mRecalledCommand <= -1) + { + mRecalledCommand = mCommandHistory.size() - 1; + } + changeCommand = true; + } + + if (Core::Input().IsKeyPressed(KeyCode::DOWN, true)) + { + mRecalledCommand++; + if (mRecalledCommand >= mCommandHistory.size()) + { + mRecalledCommand = 0; + } + changeCommand = true; + } + + if (changeCommand) { - mCommandHistory.push_back(mBuffer); memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); - mbNewCommand = true; + strcpy(mBuffer, mCommandHistory[mRecalledCommand].c_str()); } } diff --git a/src/graphics/gui/luaConsole.h b/src/graphics/gui/luaConsole.h index da3a5fd..3490ac1 100644 --- a/src/graphics/gui/luaConsole.h +++ b/src/graphics/gui/luaConsole.h @@ -42,6 +42,7 @@ namespace lunarium char mBuffer[LUA_CON_BUFFER_SIZE]; std::vector mCommandHistory; bool mbNewCommand; + int mRecalledCommand; private: LuaConsole(); diff --git a/src/scripting/scriptManager.cpp b/src/scripting/scriptManager.cpp index 317d305..107c6ec 100644 --- a/src/scripting/scriptManager.cpp +++ b/src/scripting/scriptManager.cpp @@ -10,6 +10,7 @@ #include "scriptManager.h" #include +#include namespace lunarium { @@ -55,8 +56,10 @@ namespace lunarium if (!result.valid()) { sol::error err = result; - Logger::Log(mpInstance->mCat, LogLevel::WARNING, "LUA script has errors: %s", err.what()); - return OpRes::Fail(err.what()); + Logger::Log(mpInstance->mCat, LogLevel::WARNING, "LUA script has errors"); + std::ostringstream oss; + oss << "LUA code has error. CODE:\n" << code << "\nEND CODE -- ERROR: " << err.what(); + return OpRes::Fail(oss.str().c_str()); } return OpRes::OK();