Skip to content

Commit

Permalink
[dbgui] further improved the lua console. in addition, gained access …
Browse files Browse the repository at this point in the history
…to imgui's InputText function overload for a std::string, which makes life super easy
  • Loading branch information
harrand committed Aug 6, 2023
1 parent 7d9d72c commit bb737dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ add_library(imgui STATIC
imgui/imgui_demo.cpp
imgui/imgui_draw.cpp
imgui/imgui_tables.cpp
imgui/imgui_widgets.cpp)
imgui/imgui_widgets.cpp

imgui/misc/cpp/imgui_stdlib.cpp)
target_include_directories(imgui PUBLIC "${PROJECT_SOURCE_DIR}/lib/imgui")
target_compile_definitions(imgui PUBLIC "-DIMGUI_USER_CONFIG=\"../tz_imgui_config.hpp\"")

Expand Down
13 changes: 7 additions & 6 deletions src/tz/dbgui/dbgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "tz/gl/imported_shaders.hpp"
#include "tz/lua/state.hpp"
#include "imgui.h"
#include "misc/cpp/imgui_stdlib.h"

#if TZ_VULKAN
#include "tz/gl/impl/vulkan/detail/tz_vulkan.hpp"
Expand Down Expand Up @@ -41,7 +42,7 @@ namespace tz::dbgui
float frame_period = 0.0f;
tz::duration fps_update_duration;

char lua_console_buf[512] = "";
std::string lua_console_buf = "";
std::string lua_console_history = "";
};

Expand Down Expand Up @@ -732,7 +733,7 @@ namespace tz::dbgui

void draw_tz_lua_console()
{
if(ImGui::Begin("Lua Console", &tab_tz.show_lua_console))
if(ImGui::Begin("Lua Console", &tab_tz.show_lua_console, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse))
{
ImGui::BeginChild("ScrollingRegion", ImVec2(0, -ImGui::GetTextLineHeightWithSpacing()), false, ImGuiWindowFlags_HorizontalScrollbar);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Adjust item spacing to make it look more like a console
Expand All @@ -743,17 +744,17 @@ namespace tz::dbgui
ImGui::Separator();

ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue;
if(ImGui::InputText("Input", global_platform_data->lua_console_buf, IM_ARRAYSIZE(global_platform_data->lua_console_buf), input_text_flags))
if(ImGui::InputText("Input", &global_platform_data->lua_console_buf, input_text_flags))
{
if(ImGui::IsItemDeactivatedAfterEdit()) // Check if Enter key caused deactivation of the input field
{
global_platform_data->lua_console_history += std::string("\n>") + global_platform_data->lua_console_buf;
bool success = tz::lua::get_state().execute(global_platform_data->lua_console_buf, false);
bool success = tz::lua::get_state().execute(global_platform_data->lua_console_buf.c_str(), false);
if (!success)
{
global_platform_data->lua_console_history += "\nLua Error\n";
global_platform_data->lua_console_history += "\nLua Error: \"" + tz::lua::get_state().get_last_error() + "\"\n";
}
global_platform_data->lua_console_buf[0] = '\0';
global_platform_data->lua_console_buf.clear();
ImGui::SetKeyboardFocusHere(-1); // Set focus back to the input text field
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/tz/lua/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ namespace tz::lua
{
auto* s = static_cast<lua_State*>(this->lstate);
bool ret = luaL_dofile(s, path) == false;
tz::assert(!assert_on_failure || ret, "Lua Error: %s", lua_tostring(s, -1));
const char* err = lua_tostring(s, -1);
if(err != nullptr)
{
this->last_error = err;
}
tz::assert(!assert_on_failure || ret, "Lua Error: %s", err);
return ret;
}

bool state::execute(const char* lua_src, bool assert_on_failure) const
{
auto* s = static_cast<lua_State*>(this->lstate);
bool ret = luaL_dostring(s, lua_src) == false;
tz::assert(!assert_on_failure || ret, "Lua Error: %s", lua_tostring(s, -1));
const char* err = lua_tostring(s, -1);
if(err != nullptr)
{
this->last_error = err;
}
tz::assert(!assert_on_failure || ret, "Lua Error: %s", err);
return ret;
}

Expand Down Expand Up @@ -224,6 +234,11 @@ namespace tz::lua
return ret + "=== end ===";
}

const std::string& state::get_last_error() const
{
return this->last_error;
}

bool state::impl_check_stack(std::size_t sz) const
{
auto* s = static_cast<lua_State*>(this->lstate);
Expand Down Expand Up @@ -254,6 +269,10 @@ namespace tz::lua
lua_Debug ar;
lua_getstack(state, 1, &ar);
lua_getinfo(state, "nSl", &ar);
if(!b && TZ_DEBUG)
{
tz::dbgui::add_to_lua_log("<<Lua Assert Failure Detected>>");
}
tz::assert(b, "Lua Assertion Failure: ```lua\n\n%s\n\n```\nOn line %d\nStack:\n%s", ar.source, ar.currentline, stack.c_str());
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions src/tz/lua/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ namespace tz::lua
std::optional<std::int64_t> get_int(const char* varname) const;
std::optional<std::uint64_t> get_uint(const char* varname) const;
std::string collect_stack() const;
const std::string& get_last_error() const;
private:
bool impl_check_stack(std::size_t sz) const;
mutable std::string last_error = "";
void* lstate = nullptr;
};

Expand Down

0 comments on commit bb737dc

Please sign in to comment.