Files
Renderive/Core/primitive/Label.h
T
2026-08-02 03:43:39 +08:00

100 lines
3.2 KiB
C++

#pragma once
#include <memory_resource>
#include <span>
#include <string>
#include <utility>
#include <vector>
#include "../architecture/Renderable.h"
#include "../Axis/Abs_Axis.h"
#include "../base/Geometry.h"
#include "../base/Memory.h"
#include "../base/Style.h"
#include "../base/Text.h"
namespace renderive {
struct Label_Private;
/// @brief 单标签构造属性。
struct Label_Prop {
std::weak_ptr<Abs_Axis> domain_axis{};
std::weak_ptr<Abs_Axis> value_axis{};
PointF position{};
PointF offset{};
std::string text;
Text_Align alignment = Text_Align::Left | Text_Align::Top;
Font font;
Pen text_pen = Pen{Color::white()};
Brush background_brush = Brush{};
Pen border_pen = Pen{.style = Line_Style::None};
};
/// @brief 批量标签项。
struct Label_Item {
PointF position{};
PointF offset{};
std::string text;
Text_Align alignment = Text_Align::Left | Text_Align::Top;
Font font;
Pen text_pen = Pen{Color::white()};
Brush background_brush = Brush{};
Pen border_pen = Pen{.style = Line_Style::None};
};
/// @brief 按坐标轴绘制文本标签的图元。
class LIB_DECL Label : public Renderable {
public:
/// @brief 绑定 X 轴。
PROP(std::shared_ptr<Abs_Axis>, domain_axis)
/// @brief 绑定 Y 轴。
PROP(std::shared_ptr<Abs_Axis>, value_axis)
/// @brief 单标签坐标位置。
PROP(PointF, position)
/// @brief 单标签像素偏移。
PROP(PointF, offset)
/// @brief 单标签文本。
PROP(std::string, text)
/// @brief 文本相对锚点对齐方式。
PROP(Text_Align, alignment)
/// @brief 文本字体。
PROP(Font, font)
/// @brief 文本画笔。
PROP(Pen, text_pen)
/// @brief 背景填充刷。
PROP(Brush, background_brush)
/// @brief 边框画笔。
PROP(Pen, border_pen)
/// @brief 写入批量标签项。
void update_items(std::span<const Label_Item> items);
/// @brief 移动写入批量标签项。
void update_items(std::pmr::vector<Label_Item>&& items);
/// @brief 写入容器中的批量标签项。
template <typename Items>
void update_items(const Items& items) {
update_items(std::span<const Label_Item>(items.data(), items.size()));
}
struct Builder;
/// @brief 创建标签图元。
Label();
private:
Label_Private* d();
Render_Object_Owner create_render_data() override;
Render_Object_Owner create_render_state() override;
Render_Object_Owner create_input_data() override;
};
/// @brief 标签构造器。
struct LIB_DECL Label::Builder : Label_Prop {
PROP_B(std::shared_ptr<Renderable>, parent)
PROP_B(std::shared_ptr<Abs_Axis>, domain_axis)
PROP_B(std::shared_ptr<Abs_Axis>, value_axis)
PROP_B(PointF, position)
PROP_B(PointF, offset)
PROP_B(std::string, text)
PROP_B(Text_Align, alignment)
PROP_B(Font, font)
PROP_B(Pen, text_pen)
PROP_B(Brush, background_brush)
PROP_B(Pen, border_pen)
/// @brief 创建标签构造器。
Builder(std::shared_ptr<Renderable> parent, std::shared_ptr<Abs_Axis> domain_axis, std::shared_ptr<Abs_Axis> value_axis);
/// @brief 构建并返回标签对象。
std::shared_ptr<Label> build();
std::shared_ptr<Renderable> parent{};
};
} // namespace renderive