simple window and engine debug gui

This commit is contained in:
2019-02-19 11:22:00 +07:00
parent 0ad0803421
commit 0a2f4ca665
7 changed files with 163 additions and 43 deletions

View File

@@ -30,6 +30,9 @@ namespace e2d
dbgui(debug& d, input& i, render& r, window& w);
~dbgui() noexcept final;
bool visible() const noexcept;
void toggle_visible(bool yesno) noexcept;
void frame_tick();
void frame_render();
private:

View File

@@ -163,10 +163,15 @@ namespace
bool frame_tick() final {
const keyboard& k = the<input>().keyboard();
if ( the<window>().should_close() || k.is_key_just_released(keyboard_key::escape) ) {
return false;
}
if ( k.is_key_just_pressed(keyboard_key::f12) ) {
the<dbgui>().toggle_visible(!the<dbgui>().visible());
}
const auto framebuffer_size = the<window>().real_size().cast_to<f32>();
const auto projection = math::make_orthogonal_lh_matrix4(framebuffer_size, 0.f, 1.f);

View File

@@ -219,10 +219,15 @@ namespace
bool frame_tick() final {
const keyboard& k = the<input>().keyboard();
if ( the<window>().should_close() || k.is_key_just_released(keyboard_key::escape) ) {
return false;
}
if ( k.is_key_just_pressed(keyboard_key::f12) ) {
the<dbgui>().toggle_visible(!the<dbgui>().visible());
}
const auto framebuffer_size = the<window>().real_size().cast_to<f32>();
const auto projection = math::make_perspective_lh_matrix4(
make_deg(45.f),

View File

@@ -189,10 +189,15 @@ namespace
bool frame_tick() final {
const keyboard& k = the<input>().keyboard();
if ( the<window>().should_close() || k.is_key_just_released(keyboard_key::escape) ) {
return false;
}
if ( k.is_key_just_pressed(keyboard_key::f12) ) {
the<dbgui>().toggle_visible(!the<dbgui>().visible());
}
const auto framebuffer_size = the<window>().real_size().cast_to<f32>();
const auto projection = math::make_perspective_lh_matrix4(
make_deg(45.f),

View File

@@ -85,6 +85,14 @@ namespace e2d
return ImGui::GetIO();
}
bool visible() const noexcept {
return visible_;
}
void toggle_visible(bool yesno) noexcept {
visible_ = yesno;
}
void frame_tick() {
ImGuiIO& io = bind_context();
const mouse& m = input_.mouse();
@@ -352,6 +360,7 @@ namespace e2d
input& input_;
render& render_;
window& window_;
bool visible_{false};
ImGuiContext* context_{nullptr};
window::event_listener& listener_;
private:
@@ -371,8 +380,20 @@ namespace e2d
: state_(new internal_state(d, i, r, w)) {}
dbgui::~dbgui() noexcept = default;
bool dbgui::visible() const noexcept {
return state_->visible();
}
void dbgui::toggle_visible(bool yesno) noexcept {
state_->toggle_visible(yesno);
}
void dbgui::frame_tick() {
state_->frame_tick();
if ( visible() ) {
dbgui_widgets::show_main_menu();
}
}
void dbgui::frame_render() {

View File

@@ -25,6 +25,4 @@ namespace e2d { namespace dbgui_shaders
namespace e2d { namespace dbgui_widgets
{
void show_main_menu();
void show_engine_stats();
void show_window_stats();
}}

View File

@@ -6,63 +6,146 @@
#include "dbgui.hpp"
namespace e2d { namespace dbgui_widgets
namespace
{
void show_main_menu() {
using namespace e2d;
void show_debug_engine(bool* open) {
if ( !modules::is_initialized<engine>() ) {
if ( open ) {
*open = false;
}
return;
}
engine& e = the<engine>();
const char* window_title = "Debug Engine";
if ( !ImGui::Begin(window_title, open, ImGuiWindowFlags_NoResize) ) {
ImGui::End();
return;
}
try {
{
ImGui::Text("%s", strings::rformat("time: %0", e.time()).c_str());
ImGui::Text("%s", strings::rformat("delta time: %0", e.delta_time()).c_str());
}
ImGui::Separator();
{
ImGui::Text("%s", strings::rformat("frame rate: %0", e.frame_rate()).c_str());
ImGui::Text("%s", strings::rformat("frame count: %0", e.frame_count()).c_str());
ImGui::Text("%s", strings::rformat("realtime time: %0", e.realtime_time()).c_str());
}
ImGui::SetWindowSize(window_title, v2f::zero());
} catch (...) {
ImGui::End();
throw;
}
ImGui::End();
}
void show_debug_window(bool* open) {
if ( !modules::is_initialized<window>() ) {
if ( open ) {
*open = false;
}
return;
}
window& w = the<window>();
if ( ImGui::BeginMainMenuBar() ) {
if ( ImGui::BeginMenu("Window") ) {
if ( w.fullscreen() ) {
if ( ImGui::MenuItem("Exit Full Screen") ) {
w.toggle_fullscreen(false);
}
} else {
if ( ImGui::MenuItem("Enter Full Screen") ) {
w.toggle_fullscreen(true);
const char* window_title = "Debug Window";
if ( !ImGui::Begin(window_title, open, ImGuiWindowFlags_NoResize) ) {
ImGui::End();
return;
}
try {
{
bool visible = w.visible();
if ( ImGui::Checkbox("visible", &visible) ) {
if ( visible ) {
w.show();
} else {
w.hide();
}
}
}
{
bool focused = w.focused();
ImGui::Checkbox("focused", &focused);
}
{
bool minimized = w.minimized();
if ( ImGui::Checkbox("minimized", &minimized) ) {
if ( minimized ) {
w.minimize();
} else {
w.restore();
}
}
}
ImGui::Separator();
{
ImGui::Text("%s", strings::rformat("real size: %0", w.real_size()).c_str());
ImGui::Text("%s", strings::rformat("virtual size: %0", w.virtual_size()).c_str());
ImGui::Text("%s", strings::rformat("framebuffer size: %0", w.framebuffer_size()).c_str());
}
ImGui::Separator();
{
bool fullscreen = w.fullscreen();
if ( ImGui::Checkbox("fullscreen", &fullscreen) ) {
w.toggle_fullscreen(fullscreen);
}
}
{
bool cursor = !w.is_cursor_hidden();
if ( ImGui::Checkbox("system cursor", &cursor) ) {
if ( cursor ) {
w.show_cursor();
} else {
w.hide_cursor();
}
}
}
{
char title_buf[128] = {0};
strings::format(title_buf, E2D_COUNTOF(title_buf), w.title());
if ( ImGui::InputText("title", title_buf, E2D_COUNTOF(title_buf)) ) {
w.set_title(title_buf);
}
}
ImGui::SetWindowSize(window_title, v2f::zero());
} catch (...) {
ImGui::End();
throw;
}
ImGui::End();
}
}
namespace e2d { namespace dbgui_widgets
{
void show_main_menu() {
static bool show_engine = false;
static bool show_window = false;
if ( ImGui::BeginMainMenuBar() ) {
if ( ImGui::BeginMenu("Debug") ) {
ImGui::MenuItem("Engine...", nullptr, &show_engine);
ImGui::MenuItem("Window...", nullptr, &show_window);
ImGui::Separator();
if ( ImGui::MenuItem("Close", "Cmd+Q") ) {
w.set_should_close(true);
if ( ImGui::MenuItem("Quit") ) {
if ( modules::is_initialized<window>() ) {
the<window>().set_should_close(true);
}
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
}
void show_engine_stats() {
if ( !modules::is_initialized<engine>() ) {
return;
if ( show_engine ) {
show_debug_engine(&show_engine);
}
const engine& e = the<engine>();
if ( ImGui::Begin("Engine info") ) {
const f32 delta_time = e.delta_time();
const u32 frame_rate = e.frame_rate();
const u32 frame_count = e.frame_count();
ImGui::Text("delta time: %f", delta_time);
ImGui::Text("frame rate: %u", frame_rate);
ImGui::Text("frame count: %u", frame_count);
}
ImGui::End();
}
void show_window_stats() {
if ( !modules::is_initialized<window>() ) {
return;
if ( show_window ) {
show_debug_window(&show_window);
}
const window& w = the<window>();
if ( ImGui::Begin("Window Stats") ) {
v2u real_size = w.real_size();
v2u virtual_size = w.virtual_size();
v2u framebuffer_size = w.framebuffer_size();
ImGui::Text("real size: (%u,%u)", real_size.x, real_size.y);
ImGui::Text("virtual size: (%u,%u)", virtual_size.x, virtual_size.y);
ImGui::Text("framebuffer size: (%u,%u)", framebuffer_size.x, framebuffer_size.y);
}
ImGui::End();
}
}}