mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 23:23:28 +00:00
changed api
This commit is contained in:
@@ -3,127 +3,171 @@
|
||||
//
|
||||
#pragma once
|
||||
#include "canvas_box.hpp"
|
||||
#include "entity_overlay_widgets.hpp"
|
||||
#include "hud_renderer_interface.hpp"
|
||||
#include "omath/linear_algebra/vector2.hpp"
|
||||
#include "omath/utility/color.hpp"
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
namespace omath::hud
|
||||
{
|
||||
class EntityOverlay final
|
||||
{
|
||||
public:
|
||||
EntityOverlay(const Vector2<float>& top, const Vector2<float>& bottom,
|
||||
const std::shared_ptr<HudRendererInterface>& renderer);
|
||||
const std::shared_ptr<HudRendererInterface>& renderer);
|
||||
|
||||
void add_2d_box(const Color& box_color, const Color& fill_color = Color{0.f, 0.f, 0.f, 0.f},
|
||||
float thickness = 1.f) const;
|
||||
// ── Boxes ────────────────────────────────────────────────────────
|
||||
EntityOverlay& add_2d_box(const Color& box_color, const Color& fill_color = Color{0.f, 0.f, 0.f, 0.f},
|
||||
float thickness = 1.f);
|
||||
|
||||
void add_cornered_2d_box(const Color& box_color, const Color& fill_color = Color{0.f, 0.f, 0.f, 0.f},
|
||||
float corner_ratio_len = 0.2f, float thickness = 1.f) const;
|
||||
EntityOverlay& add_cornered_2d_box(const Color& box_color, const Color& fill_color = Color{0.f, 0.f, 0.f, 0.f},
|
||||
float corner_ratio_len = 0.2f, float thickness = 1.f);
|
||||
|
||||
void add_right_bar(const Color& color, const Color& outline_color, const Color& bg_color, float width,
|
||||
float ratio, float offset = 5.f);
|
||||
EntityOverlay& add_dashed_box(const Color& color, float dash_len = 8.f, float gap_len = 5.f,
|
||||
float thickness = 1.f);
|
||||
|
||||
void add_left_bar(const Color& color, const Color& outline_color, const Color& bg_color, float width,
|
||||
float ratio, float offset = 5.f);
|
||||
// ── Bars ─────────────────────────────────────────────────────────
|
||||
EntityOverlay& add_right_bar(const Color& color, const Color& outline_color, const Color& bg_color, float width,
|
||||
float ratio, float offset = 5.f);
|
||||
|
||||
EntityOverlay& add_left_bar(const Color& color, const Color& outline_color, const Color& bg_color, float width,
|
||||
float ratio, float offset = 5.f);
|
||||
|
||||
EntityOverlay& add_top_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height,
|
||||
float ratio, float offset = 5.f);
|
||||
|
||||
EntityOverlay& add_bottom_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float height, float ratio, float offset = 5.f);
|
||||
|
||||
EntityOverlay& add_right_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float width, float ratio, float dash_len, float gap_len,
|
||||
float offset = 5.f);
|
||||
|
||||
EntityOverlay& add_left_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float width, float ratio, float dash_len, float gap_len, float offset = 5.f);
|
||||
|
||||
EntityOverlay& add_top_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float height, float ratio, float dash_len, float gap_len, float offset = 5.f);
|
||||
|
||||
EntityOverlay& add_bottom_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float height, float ratio, float dash_len, float gap_len,
|
||||
float offset = 5.f);
|
||||
|
||||
// ── Labels ───────────────────────────────────────────────────────
|
||||
EntityOverlay& add_right_label(const Color& color, float offset, bool outlined, const std::string_view& text);
|
||||
|
||||
EntityOverlay& add_left_label(const Color& color, float offset, bool outlined, const std::string_view& text);
|
||||
|
||||
EntityOverlay& add_top_label(const Color& color, float offset, bool outlined, std::string_view text);
|
||||
|
||||
EntityOverlay& add_bottom_label(const Color& color, float offset, bool outlined, std::string_view text);
|
||||
|
||||
EntityOverlay& add_centered_top_label(const Color& color, float offset, bool outlined,
|
||||
const std::string_view& text);
|
||||
|
||||
EntityOverlay& add_centered_bottom_label(const Color& color, float offset, bool outlined,
|
||||
const std::string_view& text);
|
||||
|
||||
template<typename... Args>
|
||||
void add_right_label(const Color& color, const float offset, const bool outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
EntityOverlay& add_right_label(const Color& color, float offset, bool outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
{
|
||||
const std::string label = std::vformat(fmt.get(), std::make_format_args(args...));
|
||||
|
||||
add_right_label(color, offset, outlined, std::string_view{label});
|
||||
return add_right_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
void add_right_label(const Color& color, float offset, bool outlined, const std::string_view& text);
|
||||
|
||||
template<typename... Args>
|
||||
void add_top_label(const Color& color, const float offset, const bool outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
EntityOverlay& add_left_label(const Color& color, float offset, bool outlined, std::format_string<Args...> fmt,
|
||||
Args&&... args)
|
||||
{
|
||||
const std::string label = std::vformat(fmt.get(), std::make_format_args(args...));
|
||||
|
||||
add_top_label(color, offset, outlined, std::string_view{label});
|
||||
return add_left_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
void add_top_label(const Color& color, float offset, bool outlined, std::string_view text);
|
||||
|
||||
void add_top_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height,
|
||||
float ratio, float offset = 5.f);
|
||||
|
||||
void add_snap_line(const Vector2<float>& 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_skeleton(const Color& color, float thickness = 1.f) const;
|
||||
|
||||
void add_right_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float width, float ratio, float dash_len, float gap_len, float offset = 5.f);
|
||||
|
||||
void add_left_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float width, float ratio, float dash_len, float gap_len, float offset = 5.f);
|
||||
|
||||
void add_top_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float height, float ratio, float dash_len, float gap_len, float offset = 5.f);
|
||||
|
||||
void add_bottom_dashed_bar(const Color& color, const Color& outline_color, const Color& bg_color,
|
||||
float height, float ratio, float dash_len, float gap_len, float offset = 5.f);
|
||||
|
||||
void add_bottom_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height,
|
||||
float ratio, float offset = 5.f);
|
||||
|
||||
template<typename... Args>
|
||||
void add_bottom_label(const Color& color, const float offset, const bool outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
EntityOverlay& add_top_label(const Color& color, float offset, bool outlined, std::format_string<Args...> 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});
|
||||
return add_top_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
void add_bottom_label(const Color& color, float offset, bool outlined, std::string_view text);
|
||||
|
||||
template<typename... Args>
|
||||
void add_left_label(const Color& color, const float offset, const bool outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
EntityOverlay& add_bottom_label(const Color& color, float offset, bool outlined,
|
||||
std::format_string<Args...> 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});
|
||||
return add_bottom_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
void add_left_label(const Color& color, float offset, bool outlined, const std::string_view& text);
|
||||
|
||||
template<typename... Args>
|
||||
void add_centered_bottom_label(const Color& color, const float offset, const bool outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
EntityOverlay& add_centered_top_label(const Color& color, float offset, bool outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
const std::string label = std::vformat(fmt.get(), std::make_format_args(args...));
|
||||
add_centered_bottom_label(color, offset, outlined, std::string_view{label});
|
||||
return add_centered_top_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
void add_centered_bottom_label(const Color& color, float offset, bool outlined, const std::string_view& text);
|
||||
|
||||
template<typename... Args>
|
||||
void add_centered_top_label(const Color& color, const float offset, const bool outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
EntityOverlay& add_centered_bottom_label(const Color& color, float offset, bool outlined,
|
||||
std::format_string<Args...> fmt, Args&&... args)
|
||||
{
|
||||
const std::string label = std::vformat(fmt.get(), std::make_format_args(args...));
|
||||
add_centered_top_label(color, offset, outlined, std::string_view{label});
|
||||
return add_centered_bottom_label(color, offset, outlined,
|
||||
std::string_view{std::vformat(fmt.get(), std::make_format_args(args...))});
|
||||
}
|
||||
|
||||
void add_centered_top_label(const Color& color, float offset, bool outlined, const std::string_view& text);
|
||||
// ── Misc ─────────────────────────────────────────────────────────
|
||||
EntityOverlay& add_snap_line(const Vector2<float>& start_pos, const Color& color, float width);
|
||||
|
||||
EntityOverlay& add_skeleton(const Color& color, float thickness = 1.f);
|
||||
|
||||
// ── Declarative interface ─────────────────────────────────────────
|
||||
/// Pass any combination of widget:: descriptor structs (and std::optional<W>
|
||||
/// from when()) to render them all in declaration order.
|
||||
template<typename... Widgets>
|
||||
EntityOverlay& contents(Widgets&&... widgets)
|
||||
{
|
||||
(dispatch(std::forward<Widgets>(widgets)), ...);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
void draw_outlined_text(const Vector2<float>& position, const Color& color,
|
||||
const std::string_view& text);
|
||||
void draw_dashed_line(const Vector2<float>& from, const Vector2<float>& to, const Color& color,
|
||||
float dash_len, float gap_len, float thickness) const;
|
||||
// optional<W> dispatch — enables when() conditional widgets
|
||||
template<typename W>
|
||||
void dispatch(const std::optional<W>& w)
|
||||
{
|
||||
if (w)
|
||||
dispatch(*w);
|
||||
}
|
||||
|
||||
void dispatch(const widget::Box& w);
|
||||
void dispatch(const widget::CorneredBox& w);
|
||||
void dispatch(const widget::DashedBox& w);
|
||||
void dispatch(const widget::RightBar& w);
|
||||
void dispatch(const widget::LeftBar& w);
|
||||
void dispatch(const widget::TopBar& w);
|
||||
void dispatch(const widget::BottomBar& w);
|
||||
void dispatch(const widget::RightDashedBar& w);
|
||||
void dispatch(const widget::LeftDashedBar& w);
|
||||
void dispatch(const widget::TopDashedBar& w);
|
||||
void dispatch(const widget::BottomDashedBar& w);
|
||||
void dispatch(const widget::RightLabel& w);
|
||||
void dispatch(const widget::LeftLabel& w);
|
||||
void dispatch(const widget::TopLabel& w);
|
||||
void dispatch(const widget::BottomLabel& w);
|
||||
void dispatch(const widget::CenteredTopLabel& w);
|
||||
void dispatch(const widget::CenteredBottomLabel& w);
|
||||
void dispatch(const widget::Skeleton& w);
|
||||
void dispatch(const widget::SnapLine& w);
|
||||
void draw_outlined_text(const Vector2<float>& position, const Color& color, const std::string_view& text);
|
||||
void draw_dashed_line(const Vector2<float>& from, const Vector2<float>& to, const Color& color, float dash_len,
|
||||
float gap_len, float thickness) const;
|
||||
void draw_dashed_fill(const Vector2<float>& origin, const Vector2<float>& step_dir,
|
||||
const Vector2<float>& perp_dir, float full_len, float filled_len,
|
||||
const Color& fill_color, const Color& split_color,
|
||||
float dash_len, float gap_len) const;
|
||||
const Vector2<float>& perp_dir, float full_len, float filled_len, const Color& fill_color,
|
||||
const Color& split_color, float dash_len, float gap_len) const;
|
||||
|
||||
CanvasBox m_canvas;
|
||||
Vector2<float> m_text_cursor_right;
|
||||
Vector2<float> m_text_cursor_top;
|
||||
@@ -131,4 +175,4 @@ namespace omath::hud
|
||||
Vector2<float> m_text_cursor_left;
|
||||
std::shared_ptr<HudRendererInterface> m_renderer;
|
||||
};
|
||||
} // namespace omath::hud
|
||||
} // namespace omath::hud
|
||||
|
||||
189
include/omath/hud/entity_overlay_widgets.hpp
Normal file
189
include/omath/hud/entity_overlay_widgets.hpp
Normal file
@@ -0,0 +1,189 @@
|
||||
//
|
||||
// Created by orange on 15.03.2026.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/linear_algebra/vector2.hpp"
|
||||
#include "omath/utility/color.hpp"
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace omath::hud::widget
|
||||
{
|
||||
// ── Boxes ─────────────────────────────────────────────────────────────────
|
||||
struct Box
|
||||
{
|
||||
Color color;
|
||||
Color fill{0.f, 0.f, 0.f, 0.f};
|
||||
float thickness = 1.f;
|
||||
};
|
||||
|
||||
struct CorneredBox
|
||||
{
|
||||
Color color;
|
||||
Color fill{0.f, 0.f, 0.f, 0.f};
|
||||
float corner_ratio = 0.2f;
|
||||
float thickness = 1.f;
|
||||
};
|
||||
|
||||
struct DashedBox
|
||||
{
|
||||
Color color;
|
||||
float dash_len = 8.f;
|
||||
float gap_len = 5.f;
|
||||
float thickness = 1.f;
|
||||
};
|
||||
|
||||
// ── Bars ──────────────────────────────────────────────────────────────────
|
||||
struct RightBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float width;
|
||||
float ratio;
|
||||
float offset = 5.f;
|
||||
};
|
||||
struct LeftBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float width;
|
||||
float ratio;
|
||||
float offset = 5.f;
|
||||
};
|
||||
struct TopBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float height;
|
||||
float ratio;
|
||||
float offset = 5.f;
|
||||
};
|
||||
struct BottomBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float height;
|
||||
float ratio;
|
||||
float offset = 5.f;
|
||||
};
|
||||
|
||||
struct RightDashedBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float width;
|
||||
float ratio;
|
||||
float dash_len;
|
||||
float gap_len;
|
||||
float offset = 5.f;
|
||||
};
|
||||
struct LeftDashedBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float width;
|
||||
float ratio;
|
||||
float dash_len;
|
||||
float gap_len;
|
||||
float offset = 5.f;
|
||||
};
|
||||
struct TopDashedBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float height;
|
||||
float ratio;
|
||||
float dash_len;
|
||||
float gap_len;
|
||||
float offset = 5.f;
|
||||
};
|
||||
struct BottomDashedBar
|
||||
{
|
||||
Color color;
|
||||
Color outline;
|
||||
Color bg;
|
||||
float height;
|
||||
float ratio;
|
||||
float dash_len;
|
||||
float gap_len;
|
||||
float offset = 5.f;
|
||||
};
|
||||
|
||||
// ── Labels ────────────────────────────────────────────────────────────────
|
||||
struct RightLabel
|
||||
{
|
||||
Color color;
|
||||
float offset;
|
||||
bool outlined;
|
||||
std::string text;
|
||||
};
|
||||
struct LeftLabel
|
||||
{
|
||||
Color color;
|
||||
float offset;
|
||||
bool outlined;
|
||||
std::string text;
|
||||
};
|
||||
struct TopLabel
|
||||
{
|
||||
Color color;
|
||||
float offset;
|
||||
bool outlined;
|
||||
std::string text;
|
||||
};
|
||||
struct BottomLabel
|
||||
{
|
||||
Color color;
|
||||
float offset;
|
||||
bool outlined;
|
||||
std::string text;
|
||||
};
|
||||
struct CenteredTopLabel
|
||||
{
|
||||
Color color;
|
||||
float offset;
|
||||
bool outlined;
|
||||
std::string text;
|
||||
};
|
||||
struct CenteredBottomLabel
|
||||
{
|
||||
Color color;
|
||||
float offset;
|
||||
bool outlined;
|
||||
std::string text;
|
||||
};
|
||||
|
||||
// ── Misc ──────────────────────────────────────────────────────────────────
|
||||
struct Skeleton
|
||||
{
|
||||
Color color;
|
||||
float thickness = 1.f;
|
||||
};
|
||||
struct SnapLine
|
||||
{
|
||||
Vector2<float> start;
|
||||
Color color;
|
||||
float width;
|
||||
};
|
||||
|
||||
} // namespace omath::hud::widget
|
||||
|
||||
namespace omath::hud
|
||||
{
|
||||
/// Returns an engaged optional<W> when condition is true, std::nullopt otherwise.
|
||||
/// Designed for use with EntityOverlay::contents() to conditionally include widgets.
|
||||
template<typename W>
|
||||
std::optional<W> when(bool condition, W widget)
|
||||
{
|
||||
if (condition)
|
||||
return std::move(widget);
|
||||
return std::nullopt;
|
||||
}
|
||||
} // namespace omath::hud
|
||||
Reference in New Issue
Block a user