gui code refactored and moved into its own library
the LuaConsole and LogGui have been merged into a single Console panel. The appearance looks good and the lua console part works. Still need to add the log history and log filter options.Gui_Panel_Refactor
parent
e05f3bb20a
commit
6e2f676f11
@ -0,0 +1,169 @@
|
||||
/******************************************************************************
|
||||
* File - console.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/02/09 (y/m/d)
|
||||
* Mod Date - 2022/02/09 (y/m/d)
|
||||
* Description - Engine console for viewing the debug log and entering lua
|
||||
commands
|
||||
******************************************************************************/
|
||||
|
||||
#include "console.h"
|
||||
#include <gui/dearimgui/imgui.h>
|
||||
#include <core/core.h>
|
||||
#include <input/inputManager.h>
|
||||
#include <cstring>
|
||||
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)
|
||||
{
|
||||
memset(mBuffer, 0, LUA_CON_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
bool Console::IsFocused() const
|
||||
{
|
||||
return mIsFocused;
|
||||
}
|
||||
|
||||
void Console::Update(float dt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
mIsFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
|
||||
ImGui::SetScrollHereY(1.0f);
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::Separator();
|
||||
ImGui::BeginChild("input", ImVec2(0, 20), false, ImGuiWindowFlags_NoScrollbar);
|
||||
mIsFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);
|
||||
if (ImGui::InputText("command", mBuffer, LUA_CON_BUFFER_SIZE, ImGuiInputTextFlags_EnterReturnsTrue
|
||||
| ImGuiInputTextFlags_CallbackHistory, Console::MyCallback, (void*)this))
|
||||
{
|
||||
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);
|
||||
//mAlpha = ImGui::IsWindowFocused() ? 0.75f : 0.1f;
|
||||
}
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::End();
|
||||
|
||||
mAlpha = mIsFocused ? 0.75f : 0.5f;
|
||||
|
||||
return mIsOpen;
|
||||
}
|
||||
|
||||
const std::vector<std::string>* Console::GetCommandHistory() const
|
||||
{
|
||||
return &mCommandHistory;
|
||||
}
|
||||
|
||||
std::string Console::GetLastCommand()
|
||||
{
|
||||
return mCommandHistory.back();
|
||||
}
|
||||
|
||||
bool Console::GetNewCommand(std::string& command)
|
||||
{
|
||||
if (mbNewCommand)
|
||||
{
|
||||
command = mCommandHistory.back();
|
||||
mbNewCommand = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int Console::MyCallback(ImGuiInputTextCallbackData* data)
|
||||
{
|
||||
if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory)
|
||||
{
|
||||
bool changeCommand = false;
|
||||
Console* pConsole = (Console*)data->UserData;
|
||||
if (data->EventKey == ImGuiKey_UpArrow)
|
||||
{
|
||||
pConsole->mRecalledCommand--;
|
||||
if (pConsole->mRecalledCommand <= -1)
|
||||
{
|
||||
pConsole->mRecalledCommand = pConsole->mCommandHistory.size() - 1;
|
||||
}
|
||||
changeCommand = true;
|
||||
}
|
||||
else if (data->EventKey == ImGuiKey_DownArrow)
|
||||
{
|
||||
pConsole->mRecalledCommand++;
|
||||
if (pConsole->mRecalledCommand >= pConsole->mCommandHistory.size())
|
||||
{
|
||||
pConsole->mRecalledCommand = 0;
|
||||
}
|
||||
changeCommand = true;
|
||||
}
|
||||
|
||||
if (changeCommand)
|
||||
{
|
||||
memset(pConsole->mBuffer, 0, LUA_CON_BUFFER_SIZE);
|
||||
strcpy(pConsole->mBuffer, pConsole->mCommandHistory[pConsole->mRecalledCommand].c_str());
|
||||
data->DeleteChars(0, data->BufTextLen);
|
||||
data->InsertChars(0, pConsole->mBuffer);
|
||||
data->SelectAll();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
/******************************************************************************
|
||||
* File - console.h
|
||||
* Author - Joey Pollack
|
||||
* Date - 2022/02/09 (y/m/d)
|
||||
* Mod Date - 2022/02/09 (y/m/d)
|
||||
* Description - Engine console for viewing the debug log and entering lua
|
||||
commands
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef CONSOLE_H_
|
||||
#define CONSOLE_H_
|
||||
|
||||
#include <gui/panel.h>
|
||||
#include <vector>
|
||||
|
||||
namespace lunarium
|
||||
{
|
||||
const int LUA_CON_BUFFER_SIZE = 64;
|
||||
class Console : public gui::Panel
|
||||
{
|
||||
public:
|
||||
Console();
|
||||
virtual void Update(float dt);
|
||||
virtual bool DoFrame();
|
||||
|
||||
bool IsFocused() const;
|
||||
|
||||
// LUA COMMAND STUFF
|
||||
const std::vector<std::string>* GetCommandHistory() const;
|
||||
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:
|
||||
// LUA COMMAND STUFF
|
||||
char mBuffer[LUA_CON_BUFFER_SIZE];
|
||||
std::vector<std::string> mCommandHistory;
|
||||
bool mbNewCommand;
|
||||
int mRecalledCommand;
|
||||
float mAlpha;
|
||||
bool mIsFocused;
|
||||
|
||||
private:
|
||||
void CheckFocus();
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif // CONSOLE_H_
|
||||
@ -0,0 +1,22 @@
|
||||
|
||||
# Source Files
|
||||
set(GUI_SRC
|
||||
"gui.cpp"
|
||||
"file_browser.cpp"
|
||||
"panel.cpp"
|
||||
)
|
||||
|
||||
add_library(gui ${GUI_SRC})
|
||||
target_link_libraries(gui utils assets dearimgui glfw)
|
||||
|
||||
target_include_directories(gui
|
||||
PUBLIC "${PROJECT_BINARY_DIR}"
|
||||
PUBLIC ../../
|
||||
PUBLIC ../../internal_libs
|
||||
PUBLIC ../../run_modes
|
||||
PUBLIC ../../../external/glm
|
||||
PUBLIC ../../../external/glad/include
|
||||
PUBLIC ../../../external/glfw/include
|
||||
PUBLIC ../../../external/box2d/include
|
||||
PUBLIC ../../../external/pugixml/src
|
||||
)
|
||||
@ -1,7 +1,7 @@
|
||||
add_library(dearimgui imgui.cpp imgui_demo.cpp imgui_widgets.cpp imgui_tables.cpp imgui_draw.cpp imgui_impl_glfw.cpp imgui_impl_opengl3.cpp)
|
||||
|
||||
target_include_directories(dearimgui
|
||||
PUBLIC ../../../external/glfw/include
|
||||
PUBLIC ../../../../external/glfw/include
|
||||
)
|
||||
|
||||
# message( " current source dir: ${CMAKE_CURRENT_SOURCE_DIR}" )
|
||||
Loading…
Reference in New Issue