Recalling commands in the Lua Console now works without pressing escape.

Gui_Panel_Refactor
Joeyrp 4 years ago
parent 7628794d10
commit 5f56810e00

@ -95,6 +95,12 @@ namespace lunarium
ImGui::End(); ImGui::End();
return; return;
} }
// NOTE: DID not work
// if (ImGui::IsWindowCollapsed())
// {
// ImGui::SetWindowPos(ImVec2(0, height - 20), ImGuiCond_Always);
// }
} }
else else
{ {

@ -45,12 +45,53 @@ namespace lunarium
return OpRes::OK(); 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() void LuaConsole::Show()
{ {
// TODO: LuaConsole::Show // TODO: LuaConsole::Show
if (!mbShow) if (!mbShow)
return; return;
if (Core::Input().IsKeyDown(KeyCode::ESCAPE, true))
{
mpInstance->mRecalledCommand = -1;
}
int x, y; int x, y;
Core::MainWindow().GetPosition(&x, &y); Core::MainWindow().GetPosition(&x, &y);
int width, height; int width, height;
@ -87,8 +128,15 @@ namespace lunarium
const char* msg = mCommandHistory[i].c_str(); const char* msg = mCommandHistory[i].c_str();
int len = strlen(msg); int len = strlen(msg);
if (i == mRecalledCommand)
{
ImGui::TextColored(ImVec4(0.2f, 0.9f, 0.5f, 1.0f), msg);
}
else
{
ImGui::TextUnformatted(msg); ImGui::TextUnformatted(msg);
} }
}
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
ImGui::SetScrollHereY(1.0f); ImGui::SetScrollHereY(1.0f);
@ -96,7 +144,8 @@ namespace lunarium
ImGui::EndChild(); ImGui::EndChild();
ImGui::Separator(); ImGui::Separator();
ImGui::BeginChild("console", ImVec2(0, 20)); 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); mCommandHistory.push_back(mBuffer);
memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); memset(mBuffer, 0, LUA_CON_BUFFER_SIZE);
@ -109,44 +158,6 @@ namespace lunarium
ImGui::EndChild(); ImGui::EndChild();
ImGui::End(); 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) void LuaConsole::SetShow(bool show)

@ -13,6 +13,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
struct ImGuiInputTextCallbackData;
namespace lunarium namespace lunarium
{ {
const int LUA_CON_BUFFER_SIZE = 64; const int LUA_CON_BUFFER_SIZE = 64;
@ -34,6 +36,7 @@ namespace lunarium
std::string GetLastCommand(); std::string GetLastCommand();
bool GetNewCommand(std::string& command); // returns true if there is a new and false if not bool GetNewCommand(std::string& command); // returns true if there is a new and false if not
static int MyCallback(ImGuiInputTextCallbackData* data);
private: private:
static LuaConsole* mpInstance; static LuaConsole* mpInstance;

Loading…
Cancel
Save