added icon

This commit is contained in:
2026-03-16 13:13:41 +03:00
parent b6b0d4db13
commit 1117eb37f1
6 changed files with 89 additions and 1 deletions

View File

@@ -138,6 +138,16 @@ namespace omath::hud
EntityOverlay& add_bottom_progress_ring(const Color& color, const Color& bg, float radius, float ratio, EntityOverlay& add_bottom_progress_ring(const Color& color, const Color& bg, float radius, float ratio,
float thickness = 2.f, float offset = 5.f, int segments = 0); float thickness = 2.f, float offset = 5.f, int segments = 0);
// ── Icons ────────────────────────────────────────────────────────
EntityOverlay& add_right_icon(const std::any& texture_id, float width, float height,
const Color& tint = Color{1.f, 1.f, 1.f, 1.f}, float offset = 5.f);
EntityOverlay& add_left_icon(const std::any& texture_id, float width, float height,
const Color& tint = Color{1.f, 1.f, 1.f, 1.f}, float offset = 5.f);
EntityOverlay& add_top_icon(const std::any& texture_id, float width, float height,
const Color& tint = Color{1.f, 1.f, 1.f, 1.f}, float offset = 5.f);
EntityOverlay& add_bottom_icon(const std::any& texture_id, float width, float height,
const Color& tint = Color{1.f, 1.f, 1.f, 1.f}, float offset = 5.f);
// ── Misc ───────────────────────────────────────────────────────── // ── Misc ─────────────────────────────────────────────────────────
EntityOverlay& add_snap_line(const Vector2<float>& start_pos, const Color& color, float width); EntityOverlay& add_snap_line(const Vector2<float>& start_pos, const Color& color, float width);

View File

@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/linear_algebra/vector2.hpp" #include "omath/linear_algebra/vector2.hpp"
#include "omath/utility/color.hpp" #include "omath/utility/color.hpp"
#include <any>
#include <initializer_list> #include <initializer_list>
#include <optional> #include <optional>
#include <string_view> #include <string_view>
@@ -137,11 +138,21 @@ namespace omath::hud::widget
int segments = 32; int segments = 32;
}; };
struct Icon
{
std::any texture_id;
float width;
float height;
Color tint{1.f, 1.f, 1.f, 1.f};
float offset = 5.f;
};
// ── Side widget variant ─────────────────────────────────────────────────── // ── Side widget variant ───────────────────────────────────────────────────
struct None struct None
{ {
}; ///< No-op placeholder — used by widget::when for disabled elements. }; ///< No-op placeholder — used by widget::when for disabled elements.
using SideWidget = std::variant<None, Bar, DashedBar, Label, Centered<Label>, SpaceVertical, SpaceHorizontal, ProgressRing>; using SideWidget =
std::variant<None, Bar, DashedBar, Label, Centered<Label>, SpaceVertical, SpaceHorizontal, ProgressRing, Icon>;
// ── Side containers ─────────────────────────────────────────────────────── // ── Side containers ───────────────────────────────────────────────────────
// Storing std::initializer_list<SideWidget> is safe here: the backing array // Storing std::initializer_list<SideWidget> is safe here: the backing array

View File

@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/linear_algebra/vector2.hpp" #include "omath/linear_algebra/vector2.hpp"
#include "omath/utility/color.hpp" #include "omath/utility/color.hpp"
#include <any>
#include <span> #include <span>
namespace omath::hud namespace omath::hud
@@ -34,6 +35,10 @@ namespace omath::hud
virtual void add_arc(const Vector2<float>& center, float radius, float a_min, float a_max, const Color& color, virtual void add_arc(const Vector2<float>& center, float radius, float a_min, float a_max, const Color& color,
float thickness, int segments = 0) = 0; float thickness, int segments = 0) = 0;
/// Draw a textured quad. texture_id is renderer-specific (e.g. ImTextureID for ImGui).
virtual void add_image(const std::any& texture_id, const Vector2<float>& min, const Vector2<float>& max,
const Color& tint = Color{1.f, 1.f, 1.f, 1.f}) = 0;
virtual void add_text(const Vector2<float>& position, const Color& color, const std::string_view& text) = 0; virtual void add_text(const Vector2<float>& position, const Color& color, const std::string_view& text) = 0;
[[nodiscard]] [[nodiscard]]

View File

@@ -23,6 +23,8 @@ namespace omath::hud
int segments = 0) override; int segments = 0) override;
void add_arc(const Vector2<float>& center, float radius, float a_min, float a_max, const Color& color, void add_arc(const Vector2<float>& center, float radius, float a_min, float a_max, const Color& color,
float thickness, int segments = 0) override; float thickness, int segments = 0) override;
void add_image(const std::any& texture_id, const Vector2<float>& min, const Vector2<float>& max,
const Color& tint = Color{1.f, 1.f, 1.f, 1.f}) override;
void add_text(const Vector2<float>& position, const Color& color, const std::string_view& text) override; void add_text(const Vector2<float>& position, const Color& color, const std::string_view& text) override;
[[nodiscard]] [[nodiscard]]
virtual Vector2<float> calc_text_size(const std::string_view& text) override; virtual Vector2<float> calc_text_size(const std::string_view& text) override;

View File

@@ -551,6 +551,43 @@ namespace omath::hud
return *this; return *this;
} }
// ── Icons ────────────────────────────────────────────────────────────────────
EntityOverlay& EntityOverlay::add_right_icon(const std::any& texture_id, const float width, const float height,
const Color& tint, const float offset)
{
const auto pos = m_text_cursor_right + Vector2<float>{offset, 0.f};
m_renderer->add_image(texture_id, pos, pos + Vector2<float>{width, height}, tint);
m_text_cursor_right.y += height;
return *this;
}
EntityOverlay& EntityOverlay::add_left_icon(const std::any& texture_id, const float width, const float height,
const Color& tint, const float offset)
{
const auto pos = m_text_cursor_left + Vector2<float>{-(offset + width), 0.f};
m_renderer->add_image(texture_id, pos, pos + Vector2<float>{width, height}, tint);
m_text_cursor_left.y += height;
return *this;
}
EntityOverlay& EntityOverlay::add_top_icon(const std::any& texture_id, const float width, const float height,
const Color& tint, const float offset)
{
m_text_cursor_top.y -= height;
const auto pos = m_text_cursor_top + Vector2<float>{0.f, -offset};
m_renderer->add_image(texture_id, pos, pos + Vector2<float>{width, height}, tint);
return *this;
}
EntityOverlay& EntityOverlay::add_bottom_icon(const std::any& texture_id, const float width, const float height,
const Color& tint, const float offset)
{
const auto pos = m_text_cursor_bottom + Vector2<float>{0.f, offset};
m_renderer->add_image(texture_id, pos, pos + Vector2<float>{width, height}, tint);
m_text_cursor_bottom.y += height;
return *this;
}
// ── widget dispatch ─────────────────────────────────────────────────────── // ── widget dispatch ───────────────────────────────────────────────────────
void EntityOverlay::dispatch(const widget::Box& box) void EntityOverlay::dispatch(const widget::Box& box)
{ {
@@ -659,6 +696,10 @@ namespace omath::hud
add_right_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset, add_right_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset,
w.segments); w.segments);
}, },
[this](const widget::Icon& w)
{
add_right_icon(w.texture_id, w.width, w.height, w.tint, w.offset);
},
}, },
child); child);
} }
@@ -701,6 +742,10 @@ namespace omath::hud
add_left_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset, add_left_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset,
w.segments); w.segments);
}, },
[this](const widget::Icon& w)
{
add_left_icon(w.texture_id, w.width, w.height, w.tint, w.offset);
},
}, },
child); child);
} }
@@ -743,6 +788,10 @@ namespace omath::hud
add_top_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset, add_top_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset,
w.segments); w.segments);
}, },
[this](const widget::Icon& w)
{
add_top_icon(w.texture_id, w.width, w.height, w.tint, w.offset);
},
}, },
child); child);
} }
@@ -785,6 +834,10 @@ namespace omath::hud
add_bottom_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset, add_bottom_progress_ring(w.color, w.bg, w.radius, w.ratio, w.thickness, w.offset,
w.segments); w.segments);
}, },
[this](const widget::Icon& w)
{
add_bottom_icon(w.texture_id, w.width, w.height, w.tint, w.offset);
},
}, },
child); child);
} }

View File

@@ -61,6 +61,13 @@ namespace omath::hud
ImGui::GetBackgroundDrawList()->PathStroke(color.to_im_color(), ImDrawFlags_None, thickness); ImGui::GetBackgroundDrawList()->PathStroke(color.to_im_color(), ImDrawFlags_None, thickness);
} }
void ImguiHudRenderer::add_image(const std::any& texture_id, const Vector2<float>& min, const Vector2<float>& max,
const Color& tint)
{
ImGui::GetBackgroundDrawList()->AddImage(std::any_cast<ImTextureID>(texture_id), min.to_im_vec2(),
max.to_im_vec2(), {0, 0}, {1, 1}, tint.to_im_color());
}
void ImguiHudRenderer::add_text(const Vector2<float>& position, const Color& color, const std::string_view& text) void ImguiHudRenderer::add_text(const Vector2<float>& position, const Color& color, const std::string_view& text)
{ {
ImGui::GetBackgroundDrawList()->AddText(position.to_im_vec2(), color.to_im_color(), text.data(), ImGui::GetBackgroundDrawList()->AddText(position.to_im_vec2(), color.to_im_color(), text.data(),