use bevy::prelude::*;
use crate::{prelude::*, render::ShapePipelineType};
mod disc;
pub use disc::*;
mod line;
pub use line::*;
mod rectangle;
pub use rectangle::*;
mod regular_polygon;
pub use regular_polygon::*;
mod triangle;
pub use triangle::*;
#[derive(Component, Clone, Reflect, Debug)]
pub struct ShapeMaterial {
pub alpha_mode: ShapeAlphaMode,
pub disable_laa: bool,
pub pipeline: ShapePipelineType,
pub canvas: Option<Entity>,
pub texture: Option<Handle<Image>>,
}
impl Default for ShapeMaterial {
fn default() -> Self {
Self {
alpha_mode: ShapeAlphaMode::Blend,
disable_laa: false,
pipeline: ShapePipelineType::Shape2d,
texture: None,
canvas: None,
}
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash, Reflect)]
pub enum ShapeAlphaMode {
#[default]
Blend,
Add,
Multiply,
}
impl From<AlphaMode> for ShapeAlphaMode {
fn from(value: AlphaMode) -> Self {
match value {
AlphaMode::Add => ShapeAlphaMode::Add,
AlphaMode::Multiply => ShapeAlphaMode::Multiply,
_ => ShapeAlphaMode::Blend,
}
}
}
#[derive(Default, Clone, Copy, Reflect, Debug)]
pub enum FillType {
#[default]
Fill,
Stroke(f32, ThicknessType),
}
#[derive(Default, Component, Clone, Copy, Reflect, Debug)]
pub struct ShapeFill {
pub color: Color,
pub ty: FillType,
}
impl ShapeFill {
pub fn new(config: &ShapeConfig) -> Self {
Self {
color: config.color,
ty: if config.hollow {
FillType::Stroke(config.thickness, config.thickness_type)
} else {
FillType::Fill
},
}
}
}
#[derive(Component, Default, Reflect)]
pub struct Shape3d;
#[derive(Component, Reflect)]
pub struct ShapeOrigin(pub Vec3);
#[derive(Bundle)]
pub struct ShapeBundle<T: Component> {
pub visibility: Visibility,
pub transform: Transform,
pub shape: ShapeMaterial,
pub fill: ShapeFill,
pub shape_type: T,
}
impl<T: Component> ShapeBundle<T> {
pub fn new(config: &ShapeConfig, component: T) -> Self {
Self {
visibility: default(),
transform: config.transform,
shape: ShapeMaterial {
alpha_mode: config.alpha_mode,
disable_laa: config.disable_laa,
pipeline: config.pipeline,
canvas: config.canvas,
texture: config.texture.clone(),
},
fill: ShapeFill::new(config),
shape_type: component,
}
}
pub fn insert_3d(self) -> (Self, Shape3d) {
(self, Shape3d)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Reflect)]
pub enum ThicknessType {
#[default]
World,
Pixels,
Screen,
}
impl From<ThicknessType> for u32 {
fn from(value: ThicknessType) -> Self {
value as u32
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Reflect)]
pub enum Cap {
None,
Square,
#[default]
Round,
}
impl From<Cap> for u32 {
fn from(value: Cap) -> Self {
value as u32
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Reflect)]
pub enum Alignment {
#[default]
Flat,
Billboard,
}
impl From<Alignment> for u32 {
fn from(value: Alignment) -> Self {
value as u32
}
}