diff --git a/src/graphics/gui/logGui.cpp b/src/graphics/gui/logGui.cpp index 82022cc..351198c 100644 --- a/src/graphics/gui/logGui.cpp +++ b/src/graphics/gui/logGui.cpp @@ -95,6 +95,12 @@ namespace lunarium ImGui::End(); return; } + + // NOTE: DID not work + // if (ImGui::IsWindowCollapsed()) + // { + // ImGui::SetWindowPos(ImVec2(0, height - 20), ImGuiCond_Always); + // } } else { diff --git a/src/graphics/gui/luaConsole.cpp b/src/graphics/gui/luaConsole.cpp index 7d06f1a..c28aef6 100644 --- a/src/graphics/gui/luaConsole.cpp +++ b/src/graphics/gui/luaConsole.cpp @@ -44,6 +44,42 @@ namespace lunarium { return OpRes::OK(); } + + int LuaConsole::MyCallback(ImGuiInputTextCallbackData* data) + { + if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) + { + bool changeCommand = false; + if (data->EventKey == ImGuiKey_UpArrow) + { + mpInstance->mRecalledCommand--; + if (mpInstance->mRecalledCommand <= -1) + { + mpInstance->mRecalledCommand = mpInstance->mCommandHistory.size() - 1; + } + changeCommand = true; + } + else if (data->EventKey == ImGuiKey_DownArrow) + { + mpInstance->mRecalledCommand++; + if (mpInstance->mRecalledCommand >= mpInstance->mCommandHistory.size()) + { + mpInstance->mRecalledCommand = 0; + } + changeCommand = true; + } + + if (changeCommand) + { + memset(mpInstance->mBuffer, 0, LUA_CON_BUFFER_SIZE); + strcpy(mpInstance->mBuffer, mpInstance->mCommandHistory[mpInstance->mRecalledCommand].c_str()); + data->DeleteChars(0, data->BufTextLen); + data->InsertChars(0, mpInstance->mBuffer); + data->SelectAll(); + } + } + return 0; + } void LuaConsole::Show() { @@ -51,6 +87,11 @@ namespace lunarium if (!mbShow) return; + if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true)) + { + mpInstance->mRecalledCommand = -1; + } + int x, y; Core::MainWindow().GetPosition(&x, &y); int width, height; @@ -86,8 +127,15 @@ namespace lunarium { const char* msg = mCommandHistory[i].c_str(); int len = strlen(msg); - - ImGui::TextUnformatted(msg); + + if (i == mRecalledCommand) + { + ImGui::TextColored(ImVec4(0.2f, 0.9f, 0.5f, 1.0f), msg); + } + else + { + ImGui::TextUnformatted(msg); + } } if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) @@ -96,7 +144,8 @@ namespace lunarium ImGui::EndChild(); ImGui::Separator(); ImGui::BeginChild("console", ImVec2(0, 20)); - if (ImGui::InputText("command", mBuffer, LUA_CON_BUFFER_SIZE, ImGuiInputTextFlags_EnterReturnsTrue)) + if (ImGui::InputText("command", mBuffer, LUA_CON_BUFFER_SIZE, ImGuiInputTextFlags_EnterReturnsTrue + | ImGuiInputTextFlags_CallbackHistory, LuaConsole::MyCallback)) { mCommandHistory.push_back(mBuffer); memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); @@ -109,44 +158,6 @@ namespace lunarium ImGui::EndChild(); ImGui::End(); - - // 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) - { - memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); - strcpy(mBuffer, mCommandHistory[mRecalledCommand].c_str()); - } } void LuaConsole::SetShow(bool show) diff --git a/src/graphics/gui/luaConsole.h b/src/graphics/gui/luaConsole.h index 3490ac1..2c83dfb 100644 --- a/src/graphics/gui/luaConsole.h +++ b/src/graphics/gui/luaConsole.h @@ -13,6 +13,8 @@ #include #include +struct ImGuiInputTextCallbackData; + namespace lunarium { const int LUA_CON_BUFFER_SIZE = 64; @@ -34,6 +36,7 @@ namespace lunarium std::string GetLastCommand(); bool GetNewCommand(std::string& command); // returns true if there is a new and false if not + static int MyCallback(ImGuiInputTextCallbackData* data); private: static LuaConsole* mpInstance;