diff --git a/examples/example_hud/gui/main_window.cpp b/examples/example_hud/gui/main_window.cpp index d306d7c..b54be5a 100644 --- a/examples/example_hud/gui/main_window.cpp +++ b/examples/example_hud/gui/main_window.cpp @@ -1,7 +1,6 @@ // // Created by Orange on 11/11/2024. // - #include "main_window.hpp" #include "omath/hud/renderer_realizations/imgui_renderer.hpp" #include @@ -30,7 +29,6 @@ namespace imgui_desktop::gui ImGui::CreateContext(); ImGui::StyleColorsDark(); - ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = {0.05f, 0.05f, 0.05f, 0.92f}; ImGui::GetStyle().AntiAliasedLines = false; ImGui::GetStyle().AntiAliasedFill = false; @@ -40,193 +38,162 @@ namespace imgui_desktop::gui void MainWindow::Run() { - // Entity position - float entity_x = 550.f, entity_top_y = 150.f, entity_bottom_y = 450.f; - float box_ratio = 4.f; - - // Box - omath::Color box_color = {1.f, 1.f, 1.f, 1.f}; - omath::Color box_fill = {0.f, 0.f, 0.f, 0.f}; - float box_thickness = 1.f; - float corner_ratio = 0.2f; - bool show_box = true, show_cornered_box = true; - - // Bars - omath::Color bar_color = {0.f, 1.f, 0.f, 1.f}; - omath::Color bar_bg_color = {0.f, 0.f, 0.f, 0.5f}; - omath::Color bar_outline_color = {0.f, 0.f, 0.f, 1.f}; - float bar_width = 4.f, bar_value = 0.75f, bar_offset = 5.f; - bool show_right_bar = true, show_left_bar = true; - bool show_top_bar = true, show_bottom_bar = true; - - // Labels - float label_offset = 3.f; - bool outlined = true; - bool show_right_labels = true, show_left_labels = true; - bool show_top_labels = true, show_bottom_labels = true; - bool show_centered_top = true, show_centered_bottom = true; - - // Dashed box - omath::Color dash_color = omath::Color::from_rgba(255, 200, 0, 255); - float dash_len = 8.f, dash_gap = 5.f, dash_thickness = 1.f; - bool show_dashed_box = false; - - // Skeleton - omath::Color skel_color = omath::Color::from_rgba(255, 255, 255, 200); - float skel_thickness = 1.f; - bool show_skeleton = false; - - // Snap line - omath::Color snap_color = omath::Color::from_rgba(255, 50, 50, 255); - float snap_width = 1.5f; - bool show_snap = true; - while (!glfwWindowShouldClose(m_window) && m_opened) { glfwPollEvents(); - ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - const auto* viewport = ImGui::GetMainViewport(); - ImGui::GetBackgroundDrawList()->AddRectFilled({}, viewport->Size, ImColor(30, 30, 30, 220)); + const auto* vp = ImGui::GetMainViewport(); + ImGui::GetBackgroundDrawList()->AddRectFilled({}, vp->Size, ImColor(30, 30, 30, 220)); - // ── Side panel ────────────────────────────────────────────── - constexpr float panel_w = 280.f; - ImGui::SetNextWindowPos({0.f, 0.f}); - ImGui::SetNextWindowSize({panel_w, viewport->Size.y}); - ImGui::Begin("Controls", &m_opened, - ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse); - - ImGui::PushItemWidth(160.f); - - if (ImGui::CollapsingHeader("Entity", ImGuiTreeNodeFlags_DefaultOpen)) - { - ImGui::SliderFloat("X##ent", &entity_x, 100.f, viewport->Size.x - 100.f); - ImGui::SliderFloat("Top Y", &entity_top_y, 20.f, entity_bottom_y - 20.f); - ImGui::SliderFloat("Bottom Y", &entity_bottom_y, entity_top_y + 20.f, viewport->Size.y - 20.f); - ImGui::SliderFloat("Aspect ratio", &box_ratio, 1.f, 10.f); - } - - if (ImGui::CollapsingHeader("Box", ImGuiTreeNodeFlags_DefaultOpen)) - { - ImGui::Checkbox("Show box", &show_box); - ImGui::Checkbox("Show cornered box", &show_cornered_box); - ImGui::Checkbox("Show dashed box", &show_dashed_box); - ImGui::ColorEdit4("Color##box", reinterpret_cast(&box_color), ImGuiColorEditFlags_NoInputs); - ImGui::ColorEdit4("Fill##box", reinterpret_cast(&box_fill), ImGuiColorEditFlags_NoInputs); - ImGui::SliderFloat("Thickness", &box_thickness, 0.5f, 5.f); - ImGui::SliderFloat("Corner ratio", &corner_ratio, 0.05f, 0.5f); - ImGui::ColorEdit4("Dash color", reinterpret_cast(&dash_color), ImGuiColorEditFlags_NoInputs); - ImGui::SliderFloat("Dash length", &dash_len, 2.f, 30.f); - ImGui::SliderFloat("Dash gap", &dash_gap, 1.f, 20.f); - ImGui::SliderFloat("Dash thickness", &dash_thickness, 0.5f, 5.f); - } - - if (ImGui::CollapsingHeader("Bars", ImGuiTreeNodeFlags_DefaultOpen)) - { - ImGui::ColorEdit4("Color##bar", reinterpret_cast(&bar_color), ImGuiColorEditFlags_NoInputs); - ImGui::ColorEdit4("BG##bar", reinterpret_cast(&bar_bg_color), ImGuiColorEditFlags_NoInputs); - ImGui::ColorEdit4("Outline##bar", reinterpret_cast(&bar_outline_color), ImGuiColorEditFlags_NoInputs); - ImGui::SliderFloat("Width##bar", &bar_width, 1.f, 20.f); - ImGui::SliderFloat("Value##bar", &bar_value, 0.f, 1.f); - ImGui::SliderFloat("Offset##bar", &bar_offset, 1.f, 20.f); - ImGui::Checkbox("Right bar", &show_right_bar); ImGui::SameLine(); - ImGui::Checkbox("Left bar", &show_left_bar); - ImGui::Checkbox("Top bar", &show_top_bar); ImGui::SameLine(); - ImGui::Checkbox("Bottom bar", &show_bottom_bar); - } - - if (ImGui::CollapsingHeader("Labels", ImGuiTreeNodeFlags_DefaultOpen)) - { - ImGui::Checkbox("Outlined", &outlined); - ImGui::SliderFloat("Offset##lbl", &label_offset, 0.f, 15.f); - ImGui::Checkbox("Right##lbl", &show_right_labels); ImGui::SameLine(); - ImGui::Checkbox("Left##lbl", &show_left_labels); - ImGui::Checkbox("Top##lbl", &show_top_labels); ImGui::SameLine(); - ImGui::Checkbox("Bottom##lbl", &show_bottom_labels); - ImGui::Checkbox("Centered top##lbl", &show_centered_top); ImGui::SameLine(); - ImGui::Checkbox("Centered bot##lbl", &show_centered_bottom); - } - - if (ImGui::CollapsingHeader("Skeleton")) - { - ImGui::Checkbox("Show##skel", &show_skeleton); - ImGui::ColorEdit4("Color##skel", reinterpret_cast(&skel_color), ImGuiColorEditFlags_NoInputs); - ImGui::SliderFloat("Thickness##skel", &skel_thickness, 0.5f, 5.f); - } - - if (ImGui::CollapsingHeader("Snap Line")) - { - ImGui::Checkbox("Show##snap", &show_snap); - ImGui::ColorEdit4("Color##snap", reinterpret_cast(&snap_color), ImGuiColorEditFlags_NoInputs); - ImGui::SliderFloat("Width##snap", &snap_width, 0.5f, 5.f); - } - - ImGui::PopItemWidth(); - ImGui::End(); - - // ── Entity overlay ─────────────────────────────────────────── - omath::hud::EntityOverlay ent( - {entity_x, entity_top_y}, {entity_x, entity_bottom_y}, - std::make_shared()); - - if (show_box) - ent.add_2d_box(box_color, box_fill, box_thickness); - if (show_cornered_box) - ent.add_cornered_2d_box(omath::Color::from_rgba(255, 0, 255, 255), box_fill, corner_ratio, box_thickness); - if (show_dashed_box) - ent.add_dashed_box(dash_color, dash_len, dash_gap, dash_thickness); - - if (show_right_bar) - ent.add_right_bar(bar_color, bar_outline_color, bar_bg_color, bar_width, bar_value, bar_offset); - if (show_left_bar) - ent.add_left_bar(bar_color, bar_outline_color, bar_bg_color, bar_width, bar_value, bar_offset); - if (show_top_bar) - ent.add_top_bar(bar_color, bar_outline_color, bar_bg_color, bar_width, bar_value, bar_offset); - if (show_bottom_bar) - ent.add_bottom_bar(bar_color, bar_outline_color, bar_bg_color, bar_width, bar_value, bar_offset); - - if (show_right_labels) - { - ent.add_right_label({0.f, 1.f, 0.f, 1.f}, label_offset, outlined, "Health: {}/100", 100); - ent.add_right_label({1.f, 0.f, 0.f, 1.f}, label_offset, outlined, "Shield: {}/125", 125); - ent.add_right_label({1.f, 0.f, 1.f, 1.f}, label_offset, outlined, "*LOCKED*"); - } - if (show_left_labels) - { - ent.add_left_label(omath::Color::from_rgba(255, 128, 0, 255), label_offset, outlined, "Armor: 75"); - ent.add_left_label(omath::Color::from_rgba(0, 200, 255, 255), label_offset, outlined, "Level: 42"); - } - if (show_top_labels) - { - ent.add_top_label(omath::Color::from_rgba(255, 255, 0, 255), label_offset, outlined, "*SCOPED*"); - ent.add_top_label(omath::Color::from_rgba(255, 0, 0, 255), label_offset, outlined, "*BLEEDING*"); - } - if (show_centered_top) - ent.add_centered_top_label(omath::Color::from_rgba(0, 255, 255, 255), label_offset, outlined, "*VISIBLE*"); - if (show_centered_bottom) - ent.add_centered_bottom_label(omath::Color::from_rgba(255, 255, 255, 255), label_offset, outlined, "PlayerName"); - if (show_bottom_labels) - ent.add_bottom_label(omath::Color::from_rgba(200, 200, 0, 255), label_offset, outlined, "42m"); - - if (show_skeleton) - ent.add_skeleton(skel_color, skel_thickness); - - if (show_snap) - ent.add_snap_line({viewport->Size.x / 2.f, viewport->Size.y}, snap_color, snap_width); + draw_controls(); + draw_overlay(); ImGui::Render(); - - int display_w, display_h; - glfwGetFramebufferSize(m_window, &display_w, &display_h); - glViewport(0, 0, display_w, display_h); - glClearColor(0.f, 0.f, 0.f, 0.f); - glClear(GL_COLOR_BUFFER_BIT); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - glfwSwapBuffers(m_window); + present(); } glfwDestroyWindow(m_window); } + + void MainWindow::draw_controls() + { + const auto* vp = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos({0.f, 0.f}); + ImGui::SetNextWindowSize({280.f, vp->Size.y}); + ImGui::Begin("Controls", &m_opened, + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse); + ImGui::PushItemWidth(160.f); + + if (ImGui::CollapsingHeader("Entity", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::SliderFloat("X##ent", &m_entity_x, 100.f, vp->Size.x - 100.f); + ImGui::SliderFloat("Top Y", &m_entity_top_y, 20.f, m_entity_bottom_y - 20.f); + ImGui::SliderFloat("Bottom Y", &m_entity_bottom_y, m_entity_top_y + 20.f, vp->Size.y - 20.f); + } + + if (ImGui::CollapsingHeader("Box", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::Checkbox("Box", &m_show_box); ImGui::SameLine(); + ImGui::Checkbox("Cornered", &m_show_cornered_box); ImGui::SameLine(); + ImGui::Checkbox("Dashed", &m_show_dashed_box); + ImGui::ColorEdit4("Color##box", reinterpret_cast(&m_box_color), ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit4("Fill##box", reinterpret_cast(&m_box_fill), ImGuiColorEditFlags_NoInputs); + ImGui::SliderFloat("Thickness", &m_box_thickness, 0.5f, 5.f); + ImGui::SliderFloat("Corner ratio", &m_corner_ratio, 0.05f, 0.5f); + ImGui::Separator(); + ImGui::ColorEdit4("Dash color", reinterpret_cast(&m_dash_color), ImGuiColorEditFlags_NoInputs); + ImGui::SliderFloat("Dash length", &m_dash_len, 2.f, 30.f); + ImGui::SliderFloat("Dash gap", &m_dash_gap, 1.f, 20.f); + ImGui::SliderFloat("Dash thick", &m_dash_thickness, 0.5f, 5.f); + } + + if (ImGui::CollapsingHeader("Bars", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::ColorEdit4("Color##bar", reinterpret_cast(&m_bar_color), ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit4("BG##bar", reinterpret_cast(&m_bar_bg_color), ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit4("Outline##bar", reinterpret_cast(&m_bar_outline_color), ImGuiColorEditFlags_NoInputs); + ImGui::SliderFloat("Width##bar", &m_bar_width, 1.f, 20.f); + ImGui::SliderFloat("Value##bar", &m_bar_value, 0.f, 1.f); + ImGui::SliderFloat("Offset##bar", &m_bar_offset, 1.f, 20.f); + ImGui::Checkbox("Right##bar", &m_show_right_bar); ImGui::SameLine(); + ImGui::Checkbox("Left##bar", &m_show_left_bar); + ImGui::Checkbox("Top##bar", &m_show_top_bar); ImGui::SameLine(); + ImGui::Checkbox("Bottom##bar", &m_show_bottom_bar); + } + + if (ImGui::CollapsingHeader("Labels", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::Checkbox("Outlined", &m_outlined); + ImGui::SliderFloat("Offset##lbl", &m_label_offset, 0.f, 15.f); + ImGui::Checkbox("Right##lbl", &m_show_right_labels); ImGui::SameLine(); + ImGui::Checkbox("Left##lbl", &m_show_left_labels); + ImGui::Checkbox("Top##lbl", &m_show_top_labels); ImGui::SameLine(); + ImGui::Checkbox("Bottom##lbl", &m_show_bottom_labels); + ImGui::Checkbox("Ctr top##lbl", &m_show_centered_top); ImGui::SameLine(); + ImGui::Checkbox("Ctr bot##lbl", &m_show_centered_bottom); + } + + if (ImGui::CollapsingHeader("Skeleton")) + { + ImGui::Checkbox("Show##skel", &m_show_skeleton); + ImGui::ColorEdit4("Color##skel", reinterpret_cast(&m_skel_color), ImGuiColorEditFlags_NoInputs); + ImGui::SliderFloat("Thick##skel", &m_skel_thickness, 0.5f, 5.f); + } + + if (ImGui::CollapsingHeader("Snap Line")) + { + ImGui::Checkbox("Show##snap", &m_show_snap); + ImGui::ColorEdit4("Color##snap", reinterpret_cast(&m_snap_color), ImGuiColorEditFlags_NoInputs); + ImGui::SliderFloat("Width##snap", &m_snap_width, 0.5f, 5.f); + } + + ImGui::PopItemWidth(); + ImGui::End(); + } + + void MainWindow::draw_overlay() + { + const auto* vp = ImGui::GetMainViewport(); + + omath::hud::EntityOverlay ent( + {m_entity_x, m_entity_top_y}, {m_entity_x, m_entity_bottom_y}, + std::make_shared()); + + if (m_show_box) + ent.add_2d_box(m_box_color, m_box_fill, m_box_thickness); + if (m_show_cornered_box) + ent.add_cornered_2d_box(omath::Color::from_rgba(255, 0, 255, 255), m_box_fill, m_corner_ratio, m_box_thickness); + if (m_show_dashed_box) + ent.add_dashed_box(m_dash_color, m_dash_len, m_dash_gap, m_dash_thickness); + + if (m_show_right_bar) + ent.add_right_bar(m_bar_color, m_bar_outline_color, m_bar_bg_color, m_bar_width, m_bar_value, m_bar_offset); + if (m_show_left_bar) + ent.add_left_bar(m_bar_color, m_bar_outline_color, m_bar_bg_color, m_bar_width, m_bar_value, m_bar_offset); + if (m_show_top_bar) + ent.add_top_bar(m_bar_color, m_bar_outline_color, m_bar_bg_color, m_bar_width, m_bar_value, m_bar_offset); + if (m_show_bottom_bar) + ent.add_bottom_bar(m_bar_color, m_bar_outline_color, m_bar_bg_color, m_bar_width, m_bar_value, m_bar_offset); + + if (m_show_right_labels) + { + ent.add_right_label({0.f, 1.f, 0.f, 1.f}, m_label_offset, m_outlined, "Health: {}/100", 100); + ent.add_right_label({1.f, 0.f, 0.f, 1.f}, m_label_offset, m_outlined, "Shield: {}/125", 125); + ent.add_right_label({1.f, 0.f, 1.f, 1.f}, m_label_offset, m_outlined, "*LOCKED*"); + } + if (m_show_left_labels) + { + ent.add_left_label(omath::Color::from_rgba(255, 128, 0, 255), m_label_offset, m_outlined, "Armor: 75"); + ent.add_left_label(omath::Color::from_rgba(0, 200, 255, 255), m_label_offset, m_outlined, "Level: 42"); + } + if (m_show_top_labels) + { + ent.add_top_label(omath::Color::from_rgba(255, 255, 0, 255), m_label_offset, m_outlined, "*SCOPED*"); + ent.add_top_label(omath::Color::from_rgba(255, 0, 0, 255), m_label_offset, m_outlined, "*BLEEDING*"); + } + if (m_show_centered_top) + ent.add_centered_top_label(omath::Color::from_rgba(0, 255, 255, 255), m_label_offset, m_outlined, "*VISIBLE*"); + if (m_show_centered_bottom) + ent.add_centered_bottom_label(omath::Color::from_rgba(255, 255, 255, 255), m_label_offset, m_outlined, "PlayerName"); + if (m_show_bottom_labels) + ent.add_bottom_label(omath::Color::from_rgba(200, 200, 0, 255), m_label_offset, m_outlined, "42m"); + + if (m_show_skeleton) + ent.add_skeleton(m_skel_color, m_skel_thickness); + if (m_show_snap) + ent.add_snap_line({vp->Size.x / 2.f, vp->Size.y}, m_snap_color, m_snap_width); + } + + void MainWindow::present() + { + int w, h; + glfwGetFramebufferSize(m_window, &w, &h); + glViewport(0, 0, w, h); + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + glfwSwapBuffers(m_window); + } } // namespace imgui_desktop::gui diff --git a/examples/example_hud/gui/main_window.hpp b/examples/example_hud/gui/main_window.hpp index f8eb072..d57590d 100644 --- a/examples/example_hud/gui/main_window.hpp +++ b/examples/example_hud/gui/main_window.hpp @@ -1,11 +1,8 @@ -// -// Created by Vlad on 6/17/2025. -// - // // Created by Orange on 11/11/2024. // #pragma once +#include #include struct GLFWwindow; @@ -15,14 +12,54 @@ namespace imgui_desktop::gui class MainWindow { public: - MainWindow(const std::string_view &caption, int width, int height); - + MainWindow(const std::string_view& caption, int width, int height); void Run(); private: - GLFWwindow* m_window; + void draw_controls(); + void draw_overlay(); + void present(); + + GLFWwindow* m_window = nullptr; static bool m_canMoveWindow; bool m_opened = true; + + // Entity + float m_entity_x = 550.f, m_entity_top_y = 150.f, m_entity_bottom_y = 450.f; + + // Box + omath::Color m_box_color{1.f, 1.f, 1.f, 1.f}; + omath::Color m_box_fill{0.f, 0.f, 0.f, 0.f}; + float m_box_thickness = 1.f, m_corner_ratio = 0.2f; + bool m_show_box = true, m_show_cornered_box = true, m_show_dashed_box = false; + + // Dashed box + omath::Color m_dash_color = omath::Color::from_rgba(255, 200, 0, 255); + float m_dash_len = 8.f, m_dash_gap = 5.f, m_dash_thickness = 1.f; + + // Bars + omath::Color m_bar_color{0.f, 1.f, 0.f, 1.f}; + omath::Color m_bar_bg_color{0.f, 0.f, 0.f, 0.5f}; + omath::Color m_bar_outline_color{0.f, 0.f, 0.f, 1.f}; + float m_bar_width = 4.f, m_bar_value = 0.75f, m_bar_offset = 5.f; + bool m_show_right_bar = true, m_show_left_bar = true; + bool m_show_top_bar = true, m_show_bottom_bar = true; + + // Labels + float m_label_offset = 3.f; + bool m_outlined = true; + bool m_show_right_labels = true, m_show_left_labels = true; + bool m_show_top_labels = true, m_show_bottom_labels = true; + bool m_show_centered_top = true, m_show_centered_bottom = true; + + // Skeleton + omath::Color m_skel_color = omath::Color::from_rgba(255, 255, 255, 200); + float m_skel_thickness = 1.f; + bool m_show_skeleton = false; + + // Snap line + omath::Color m_snap_color = omath::Color::from_rgba(255, 50, 50, 255); + float m_snap_width = 1.5f; + bool m_show_snap = true; }; -} // gui -// imgui_desktop \ No newline at end of file +} // namespace imgui_desktop::gui