diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp index 026d2b5..3d868d6 100644 --- a/include/omath/hud/entity_overlay.hpp +++ b/include/omath/hud/entity_overlay.hpp @@ -26,7 +26,7 @@ namespace omath::hud float ratio, float offset = 5.f); void add_left_bar(const Color& color, const Color& outline_color, const Color& bg_color, float width, - float ratio, float offset = 5.f) const; + float ratio, float offset = 5.f); template void add_right_label(const Color& color, const float offset, const bool outlined, @@ -55,12 +55,47 @@ namespace omath::hud void add_snap_line(const Vector2& start_pos, const Color& color, float width); + void add_bottom_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height, + float ratio, float offset = 5.f); + + template + void add_bottom_label(const Color& color, const float offset, const bool outlined, + std::format_string fmt, Args&&... args) + { + const std::string label = std::vformat(fmt.get(), std::make_format_args(args...)); + add_bottom_label(color, offset, outlined, std::string_view{label}); + } + + void add_bottom_label(const Color& color, float offset, bool outlined, std::string_view text); + + template + void add_left_label(const Color& color, const float offset, const bool outlined, + std::format_string fmt, Args&&... args) + { + const std::string label = std::vformat(fmt.get(), std::make_format_args(args...)); + add_left_label(color, offset, outlined, std::string_view{label}); + } + + void add_left_label(const Color& color, float offset, bool outlined, const std::string_view& text); + + template + void add_centered_label(const Color& color, const float offset, const bool outlined, + std::format_string fmt, Args&&... args) + { + const std::string label = std::vformat(fmt.get(), std::make_format_args(args...)); + add_centered_label(color, offset, outlined, std::string_view{label}); + } + + void add_centered_label(const Color& color, float offset, bool outlined, const std::string_view& text); + private: void draw_outlined_text(const Vector2& position, const Color& color, const std::string_view& text); CanvasBox m_canvas; Vector2 m_text_cursor_right; Vector2 m_text_cursor_top; + Vector2 m_text_cursor_bottom; + Vector2 m_text_cursor_left; std::shared_ptr m_renderer; }; } // namespace omath::hud \ No newline at end of file diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp index c560195..bb06b23 100644 --- a/source/hud/entity_overlay.cpp +++ b/source/hud/entity_overlay.cpp @@ -65,7 +65,7 @@ namespace omath::hud m_text_cursor_right.x += offset + width; } void EntityOverlay::add_left_bar(const Color& color, const Color& outline_color, const Color& bg_color, - const float width, float ratio, const float offset) const + const float width, float ratio, const float offset) { ratio = std::clamp(ratio, 0.f, 1.f); const auto max_bar_height = std::abs(m_canvas.top_left_corner.y - m_canvas.bottom_right_corner.y); @@ -76,6 +76,8 @@ namespace omath::hud m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(width, -max_bar_height * ratio), color); m_renderer->add_rectangle(bar_start - Vector2(1.f, 0.f), bar_start + Vector2(width, -max_bar_height), outline_color); + + m_text_cursor_left.x -= offset + width; } void EntityOverlay::add_right_label(const Color& color, const float offset, const bool outlined, const std::string_view& text) @@ -129,10 +131,68 @@ namespace omath::hud m_renderer->add_text(position + outline_offset, Color{0.f, 0.f, 0.f, 1.f}, text.data()); m_renderer->add_text(position, color, text.data()); } + void EntityOverlay::add_bottom_bar(const Color& color, const Color& outline_color, const Color& bg_color, + const float height, float ratio, const float offset) + { + ratio = std::clamp(ratio, 0.f, 1.f); + const auto max_bar_width = std::abs(m_canvas.bottom_right_corner.x - m_canvas.bottom_left_corner.x); + + const auto bar_start = m_canvas.bottom_left_corner + Vector2{0.f, offset}; + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(max_bar_width, height), bg_color); + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(max_bar_width * ratio, height), color); + m_renderer->add_rectangle(bar_start, bar_start + Vector2(max_bar_width, height), outline_color); + + m_text_cursor_bottom.y += offset + height; + } + + void EntityOverlay::add_bottom_label(const Color& color, const float offset, const bool outlined, + const std::string_view text) + { + const auto text_size = m_renderer->calc_text_size(text); + + if (outlined) + draw_outlined_text(m_text_cursor_bottom + Vector2{0.f, offset}, color, text); + else + m_renderer->add_text(m_text_cursor_bottom + Vector2{0.f, offset}, color, text); + + m_text_cursor_bottom.y += text_size.y; + } + + void EntityOverlay::add_left_label(const Color& color, const float offset, const bool outlined, + const std::string_view& text) + { + const auto text_size = m_renderer->calc_text_size(text); + const auto pos = m_text_cursor_left + Vector2{-(offset + text_size.x), 0.f}; + + if (outlined) + draw_outlined_text(pos, color, text); + else + m_renderer->add_text(pos, color, text); + + m_text_cursor_left.y += text_size.y; + } + + void EntityOverlay::add_centered_label(const Color& color, const float offset, const bool outlined, + const std::string_view& text) + { + const auto text_size = m_renderer->calc_text_size(text); + const auto box_center_x = m_canvas.bottom_left_corner.x + + (m_canvas.bottom_right_corner.x - m_canvas.bottom_left_corner.x) / 2.f; + const auto pos = Vector2{box_center_x - text_size.x / 2.f, m_text_cursor_bottom.y + offset}; + + if (outlined) + draw_outlined_text(pos, color, text); + else + m_renderer->add_text(pos, color, text); + + m_text_cursor_bottom.y += text_size.y; + } + EntityOverlay::EntityOverlay(const Vector2& top, const Vector2& bottom, const std::shared_ptr& renderer) : m_canvas(top, bottom), m_text_cursor_right(m_canvas.top_right_corner), - m_text_cursor_top(m_canvas.top_left_corner), m_renderer(renderer) + m_text_cursor_top(m_canvas.top_left_corner), m_text_cursor_bottom(m_canvas.bottom_left_corner), + m_text_cursor_left(m_canvas.top_left_corner), m_renderer(renderer) { } } // namespace omath::hud \ No newline at end of file diff --git a/source/hud/renderer_realizations/imgui_renderer.cpp b/source/hud/renderer_realizations/imgui_renderer.cpp index 8c66491..ac5cadd 100644 --- a/source/hud/renderer_realizations/imgui_renderer.cpp +++ b/source/hud/renderer_realizations/imgui_renderer.cpp @@ -22,7 +22,7 @@ namespace omath::hud { ImGui::GetBackgroundDrawList()->AddPolyline(reinterpret_cast(vertexes.data()), static_cast(vertexes.size()), color.to_im_color(), - ImDrawFlags_None, thickness); + ImDrawFlags_Closed, thickness); } void ImguiHudRenderer::add_filled_polyline(const std::span>& vertexes, const Color& color, @@ -50,7 +50,7 @@ namespace omath::hud text.data() + text.size()); } [[nodiscard]] - Vector2 calc_text_size(const std::string_view& text) + Vector2 ImguiHudRenderer::calc_text_size(const std::string_view& text) { return Vector2::from_im_vec2(ImGui::CalcTextSize(text.data())); }