From adad66599a9271e4ff18ffa0fb83d9c20278eedc Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 15 Mar 2026 04:49:01 +0300 Subject: [PATCH] adde dash box --- examples/example_hud/gui/main_window.cpp | 16 +++++++++++++-- include/omath/hud/entity_overlay.hpp | 5 +++++ source/hud/entity_overlay.cpp | 25 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/examples/example_hud/gui/main_window.cpp b/examples/example_hud/gui/main_window.cpp index 5624c3e..dd066df 100644 --- a/examples/example_hud/gui/main_window.cpp +++ b/examples/example_hud/gui/main_window.cpp @@ -66,6 +66,11 @@ namespace imgui_desktop::gui 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; + // Snap line omath::Color snap_color = omath::Color::from_rgba(255, 50, 50, 255); float snap_width = 1.5f; @@ -101,12 +106,17 @@ namespace imgui_desktop::gui if (ImGui::CollapsingHeader("Box", ImGuiTreeNodeFlags_DefaultOpen)) { - ImGui::Checkbox("Show box", &show_box); + 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::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)) @@ -154,6 +164,8 @@ namespace imgui_desktop::gui 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); diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp index b06fd62..fccad10 100644 --- a/include/omath/hud/entity_overlay.hpp +++ b/include/omath/hud/entity_overlay.hpp @@ -55,6 +55,9 @@ namespace omath::hud void add_snap_line(const Vector2& start_pos, const Color& color, float width); + void add_dashed_box(const Color& color, float dash_len = 8.f, float gap_len = 5.f, + float thickness = 1.f) const; + void add_bottom_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height, float ratio, float offset = 5.f); @@ -101,6 +104,8 @@ namespace omath::hud private: void draw_outlined_text(const Vector2& position, const Color& color, const std::string_view& text); + void draw_dashed_line(const Vector2& from, const Vector2& to, const Color& color, + float dash_len, float gap_len, float thickness) const; CanvasBox m_canvas; Vector2 m_text_cursor_right; Vector2 m_text_cursor_top; diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp index 073fffd..fd08483 100644 --- a/source/hud/entity_overlay.cpp +++ b/source/hud/entity_overlay.cpp @@ -122,6 +122,31 @@ namespace omath::hud + Vector2{m_canvas.bottom_right_corner.x - m_canvas.bottom_left_corner.x, 0.f} / 2; m_renderer->add_line(start_pos, line_end, color, width); } + void EntityOverlay::draw_dashed_line(const Vector2& from, const Vector2& to, const Color& color, + const float dash_len, const float gap_len, const float thickness) const + { + const auto edge = to - from; + const auto total = edge.length(); + const auto dir = edge.normalized(); + const float step = dash_len + gap_len; + + for (float pos = 0.f; pos < total; pos += step) + { + const auto dash_start = from + dir * pos; + const auto dash_end = from + dir * std::min(pos + dash_len, total); + m_renderer->add_line(dash_start, dash_end, color, thickness); + } + } + + void EntityOverlay::add_dashed_box(const Color& color, const float dash_len, const float gap_len, + const float thickness) const + { + draw_dashed_line(m_canvas.top_left_corner, m_canvas.top_right_corner, color, dash_len, gap_len, thickness); + draw_dashed_line(m_canvas.top_right_corner, m_canvas.bottom_right_corner, color, dash_len, gap_len, thickness); + draw_dashed_line(m_canvas.bottom_right_corner,m_canvas.bottom_left_corner, color, dash_len, gap_len, thickness); + draw_dashed_line(m_canvas.bottom_left_corner, m_canvas.top_left_corner, color, dash_len, gap_len, thickness); + } + void EntityOverlay::draw_outlined_text(const Vector2& position, const Color& color, const std::string_view& text) {