use crate::{text::TextFont, Color, CornerRadius, FontWeight, Rect, Shape, Vec2};
use std::sync::Arc;
#[derive(Clone, Debug, PartialEq)]
pub struct MaterialImage {
pub handle: crate::ImageHandle,
pub texture: crate::TextureId,
pub crop: Rect,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Paint {
Visual {
paint: Vec<Paint>,
transform: crate::Transform,
opacity: f32,
},
Shape(Shape),
Image {
rect: Rect,
uv: Rect,
rounding: CornerRadius,
color: Color,
opacity: f32,
handle: crate::ImageHandle,
texture: crate::TextureId,
hidden: bool,
},
ScrollHint {
rect: Rect,
axis: usize,
color: Color,
},
Material {
rect: Rect,
rounding: CornerRadius,
color: Color,
opacity: f32,
image: Option<MaterialImage>,
},
Gradient {
rect: Rect,
rounding: CornerRadius,
colors: Vec<Color>,
columns: usize,
rows: usize,
},
Text {
text: String,
position: Vec2,
size: f32,
weight: FontWeight,
wrap_width: f32,
color: Color,
},
Paragraph {
text: String,
position: Vec2,
size: f32,
font: TextFont,
wrap_width: f32,
tab: u16,
color: Color,
},
Rich {
text: String,
runs: Arc<[crate::text::StyleRun]>,
position: Vec2,
size: f32,
font: TextFont,
wrap_width: f32,
tab: u16,
color: Color,
},
}
impl Paint {
pub(crate) fn text(
text: String,
position: Vec2,
size: f32,
font: TextFont,
wrap_width: f32,
tab: u16,
color: Color,
) -> Self {
if font.is_plain() && tab == crate::text::DEFAULT_TAB {
Self::Text {
text,
position,
size,
weight: font.weight,
wrap_width,
color,
}
} else {
Self::Paragraph {
text,
position,
size,
font,
wrap_width,
tab,
color,
}
}
}
pub(super) fn origin(&self) -> Vec2 {
match self {
Self::Visual {
paint, transform, ..
} => transform.point(paint.first().map_or(Vec2::ZERO, Paint::origin)),
Self::Text { position, .. }
| Self::Paragraph { position, .. }
| Self::Rich { position, .. } => *position,
Self::Image { rect, .. }
| Self::Material { rect, .. }
| Self::ScrollHint { rect, .. }
| Self::Gradient { rect, .. } => rect.min,
Self::Shape(shape) => match shape {
Shape::Rect { rect, .. }
| Shape::GradientBorder { rect, .. }
| Shape::Gradient { rect, .. }
| Shape::Shadow { rect, .. } => rect.min,
Shape::Circle { center, .. } => *center,
Shape::Line { start, .. } => *start,
},
}
}
pub(crate) fn translate(&mut self, delta: Vec2) {
match self {
Self::Visual {
paint, transform, ..
} => {
for primitive in paint {
primitive.translate(delta);
}
transform.translation += delta - transform.vector(delta);
}
Self::Text { position, .. }
| Self::Paragraph { position, .. }
| Self::Rich { position, .. } => *position += delta,
Self::Image { rect, .. }
| Self::Material { rect, .. }
| Self::ScrollHint { rect, .. }
| Self::Gradient { rect, .. } => *rect = rect.translate(delta),
Self::Shape(shape) => match shape {
Shape::Rect { rect, .. }
| Shape::GradientBorder { rect, .. }
| Shape::Gradient { rect, .. }
| Shape::Shadow { rect, .. } => *rect = rect.translate(delta),
Shape::Circle { center, .. } => *center += delta,
Shape::Line { start, end, .. } => {
*start += delta;
*end += delta;
}
},
}
}
}
pub(super) fn is_scroll_hint(paint: &[Paint]) -> bool {
matches!(paint, [Paint::ScrollHint { .. }])
}