use crate::{
text::{Fonts, Galley, TextStyle},
Color32, Mesh, Stroke,
};
use emath::*;
#[must_use = "Add a Shape to a Painter"]
#[derive(Clone, Debug)]
pub enum Shape {
Noop,
Vec(Vec<Shape>),
Circle {
center: Pos2,
radius: f32,
fill: Color32,
stroke: Stroke,
},
LineSegment {
points: [Pos2; 2],
stroke: Stroke,
},
Path {
points: Vec<Pos2>,
closed: bool,
fill: Color32,
stroke: Stroke,
},
Rect {
rect: Rect,
corner_radius: f32,
fill: Color32,
stroke: Stroke,
},
Text {
pos: Pos2,
galley: Galley,
text_style: TextStyle,
color: Color32,
fake_italics: bool,
},
Mesh(Mesh),
}
impl Shape {
pub fn line_segment(points: [Pos2; 2], stroke: impl Into<Stroke>) -> Self {
Self::LineSegment {
points,
stroke: stroke.into(),
}
}
pub fn line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
Self::Path {
points,
closed: false,
fill: Default::default(),
stroke: stroke.into(),
}
}
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
Self::Path {
points,
closed: true,
fill: Default::default(),
stroke: stroke.into(),
}
}
pub fn polygon(points: Vec<Pos2>, fill: impl Into<Color32>, stroke: impl Into<Stroke>) -> Self {
Self::Path {
points,
closed: true,
fill: fill.into(),
stroke: stroke.into(),
}
}
pub fn circle_filled(center: Pos2, radius: f32, fill_color: impl Into<Color32>) -> Self {
Self::Circle {
center,
radius,
fill: fill_color.into(),
stroke: Default::default(),
}
}
pub fn circle_stroke(center: Pos2, radius: f32, stroke: impl Into<Stroke>) -> Self {
Self::Circle {
center,
radius,
fill: Default::default(),
stroke: stroke.into(),
}
}
pub fn rect_filled(rect: Rect, corner_radius: f32, fill_color: impl Into<Color32>) -> Self {
Self::Rect {
rect,
corner_radius,
fill: fill_color.into(),
stroke: Default::default(),
}
}
pub fn rect_stroke(rect: Rect, corner_radius: f32, stroke: impl Into<Stroke>) -> Self {
Self::Rect {
rect,
corner_radius,
fill: Default::default(),
stroke: stroke.into(),
}
}
pub fn text(
fonts: &Fonts,
pos: Pos2,
anchor: Align2,
text: impl Into<String>,
text_style: TextStyle,
color: Color32,
) -> Self {
let font = &fonts[text_style];
let galley = font.layout_multiline(text.into(), f32::INFINITY);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size));
Self::Text {
pos: rect.min,
galley,
text_style,
color,
fake_italics: false,
}
}
}
impl Shape {
pub fn mesh(mesh: Mesh) -> Self {
debug_assert!(mesh.is_valid());
Self::Mesh(mesh)
}
#[deprecated = "Renamed `mesh`"]
pub fn triangles(mesh: Mesh) -> Self {
Self::mesh(mesh)
}
pub fn texture_id(&self) -> super::TextureId {
if let Shape::Mesh(mesh) = self {
mesh.texture_id
} else {
super::TextureId::Egui
}
}
pub fn translate(&mut self, delta: Vec2) {
match self {
Shape::Noop => {}
Shape::Vec(shapes) => {
for shape in shapes {
shape.translate(delta);
}
}
Shape::Circle { center, .. } => {
*center += delta;
}
Shape::LineSegment { points, .. } => {
for p in points {
*p += delta;
}
}
Shape::Path { points, .. } => {
for p in points {
*p += delta;
}
}
Shape::Rect { rect, .. } => {
*rect = rect.translate(delta);
}
Shape::Text { pos, .. } => {
*pos += delta;
}
Shape::Mesh(mesh) => {
mesh.translate(delta);
}
}
}
}