use std::{
error::Error,
fmt,
panic::{AssertUnwindSafe, catch_unwind, resume_unwind},
};
use crate::{Color, Interpolate, LogicalScreenPosition, LogicalScreenVector, Rect, Vec2};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScenePrimitive {
Circle,
Rect,
Line,
Polyline,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SceneError {
InvalidBackground,
NonFiniteGeometry(ScenePrimitive),
InvalidDimension(ScenePrimitive),
DegenerateGeometry(ScenePrimitive),
MissingStyle(ScenePrimitive),
InvalidFill(ScenePrimitive),
InvalidStroke(ScenePrimitive),
InvalidShadow(ScenePrimitive),
InvalidScreenClip,
NonFiniteDepth,
}
impl fmt::Display for SceneError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "scene command rejected: {self:?}")
}
}
impl Error for SceneError {}
#[derive(Debug, Clone)]
pub struct Scene {
background: Color,
commands: Vec<SceneCommand>,
next_order: u64,
current_screen_clip: Option<ScreenClipRect>,
current_depth: f32,
}
impl Scene {
pub fn new(background: Color) -> Result<Self, SceneError> {
if !background.is_normalized() {
return Err(SceneError::InvalidBackground);
}
Ok(Self {
background,
commands: Vec::new(),
next_order: 0,
current_screen_clip: None,
current_depth: 0.0,
})
}
pub fn background(&self) -> Color {
self.background
}
pub fn set_background(&mut self, background: Color) -> Result<(), SceneError> {
if !background.is_normalized() {
return Err(SceneError::InvalidBackground);
}
self.background = background;
Ok(())
}
pub fn clear(&mut self) {
self.commands.clear();
self.next_order = 0;
self.current_screen_clip = None;
self.current_depth = 0.0;
}
pub fn commands(&self) -> &[SceneCommand] {
&self.commands
}
pub fn command_count(&self) -> usize {
self.commands.len()
}
pub fn set_screen_clip(
&mut self,
screen_clip: Option<ScreenClipRect>,
) -> Result<Option<ScreenClipRect>, SceneError> {
if screen_clip.is_some_and(|clip| !clip.is_valid()) {
return Err(SceneError::InvalidScreenClip);
}
Ok(std::mem::replace(
&mut self.current_screen_clip,
screen_clip,
))
}
pub fn with_screen_clip<R>(
&mut self,
screen_clip: ScreenClipRect,
draw: impl FnOnce(&mut Self) -> R,
) -> Result<R, SceneError> {
if !screen_clip.is_valid() {
return Err(SceneError::InvalidScreenClip);
}
let previous = self.current_screen_clip;
let combined = match previous {
Some(outer) => outer.intersection(screen_clip)?,
None => screen_clip,
};
if !combined.is_valid() {
return Err(SceneError::InvalidScreenClip);
}
self.current_screen_clip = Some(combined);
let result = catch_unwind(AssertUnwindSafe(|| draw(self)));
self.current_screen_clip = previous;
match result {
Ok(result) => Ok(result),
Err(payload) => resume_unwind(payload),
}
}
pub fn set_depth(&mut self, depth: f32) -> bool {
self.try_set_depth(depth).is_ok()
}
pub fn try_set_depth(&mut self, depth: f32) -> Result<(), SceneError> {
if !depth.is_finite() {
return Err(SceneError::NonFiniteDepth);
}
self.current_depth = depth;
Ok(())
}
pub fn with_depth<R>(
&mut self,
depth: f32,
draw: impl FnOnce(&mut Self) -> R,
) -> Result<R, SceneError> {
if !depth.is_finite() {
return Err(SceneError::NonFiniteDepth);
}
let previous = self.current_depth;
self.current_depth = depth;
let result = catch_unwind(AssertUnwindSafe(|| draw(self)));
self.current_depth = previous;
match result {
Ok(result) => Ok(result),
Err(payload) => resume_unwind(payload),
}
}
pub fn push(&mut self, command: DrawCommand) -> bool {
self.try_push(command).is_ok()
}
pub fn try_push(&mut self, command: DrawCommand) -> Result<(), SceneError> {
self.try_push_to_layer(Layer::DEFAULT, command)
}
pub fn push_to_layer(&mut self, layer: Layer, command: DrawCommand) -> bool {
self.try_push_to_layer(layer, command).is_ok()
}
pub fn try_push_to_layer(
&mut self,
layer: Layer,
command: DrawCommand,
) -> Result<(), SceneError> {
command.validate()?;
if self
.current_screen_clip
.is_some_and(|screen_clip| !screen_clip.is_valid())
{
return Err(SceneError::InvalidScreenClip);
}
let order = self.next_order;
self.next_order = self.next_order.saturating_add(1);
let scene_command = SceneCommand {
layer,
order,
depth: self.current_depth,
screen_clip: self.current_screen_clip,
command,
};
let position = self.commands.partition_point(|existing| {
existing.layer < scene_command.layer
|| (existing.layer == scene_command.layer && existing.order <= scene_command.order)
});
self.commands.insert(position, scene_command);
Ok(())
}
pub fn circle(&mut self, center: Vec2, radius: f32, style: ShapeStyle) -> bool {
self.try_circle(center, radius, style).is_ok()
}
pub fn try_circle(
&mut self,
center: Vec2,
radius: f32,
style: ShapeStyle,
) -> Result<(), SceneError> {
self.try_circle_on_layer(Layer::DEFAULT, center, radius, style)
}
pub fn circle_on_layer(
&mut self,
layer: Layer,
center: Vec2,
radius: f32,
style: ShapeStyle,
) -> bool {
self.try_circle_on_layer(layer, center, radius, style)
.is_ok()
}
pub fn try_circle_on_layer(
&mut self,
layer: Layer,
center: Vec2,
radius: f32,
style: ShapeStyle,
) -> Result<(), SceneError> {
self.try_push_to_layer(
layer,
DrawCommand::Circle(Circle {
center,
radius,
style,
}),
)
}
pub fn rect(&mut self, rect: Rect, corner_radius: f32, style: ShapeStyle) -> bool {
self.try_rect(rect, corner_radius, style).is_ok()
}
pub fn try_rect(
&mut self,
rect: Rect,
corner_radius: f32,
style: ShapeStyle,
) -> Result<(), SceneError> {
self.try_rect_on_layer(Layer::DEFAULT, rect, corner_radius, style)
}
pub fn rect_on_layer(
&mut self,
layer: Layer,
rect: Rect,
corner_radius: f32,
style: ShapeStyle,
) -> bool {
self.try_rect_on_layer(layer, rect, corner_radius, style)
.is_ok()
}
pub fn try_rect_on_layer(
&mut self,
layer: Layer,
rect: Rect,
corner_radius: f32,
style: ShapeStyle,
) -> Result<(), SceneError> {
self.try_push_to_layer(
layer,
DrawCommand::Rect(RectShape {
rect,
corner_radius,
style,
}),
)
}
pub fn line(&mut self, from: Vec2, to: Vec2, width: f32, color: Color) -> bool {
self.try_line(from, to, width, color).is_ok()
}
pub fn try_line(
&mut self,
from: Vec2,
to: Vec2,
width: f32,
color: Color,
) -> Result<(), SceneError> {
self.try_line_on_layer(Layer::DEFAULT, from, to, width, color)
}
pub fn line_on_layer(
&mut self,
layer: Layer,
from: Vec2,
to: Vec2,
width: f32,
color: Color,
) -> bool {
self.try_line_on_layer(layer, from, to, width, color)
.is_ok()
}
pub fn try_line_on_layer(
&mut self,
layer: Layer,
from: Vec2,
to: Vec2,
width: f32,
color: Color,
) -> Result<(), SceneError> {
self.try_push_to_layer(
layer,
DrawCommand::Line(Line {
from,
to,
stroke: Stroke { width, color },
}),
)
}
pub fn polyline(&mut self, points: Vec<Vec2>, width: f32, color: Color) -> bool {
self.try_polyline(points, width, color).is_ok()
}
pub fn try_polyline(
&mut self,
points: Vec<Vec2>,
width: f32,
color: Color,
) -> Result<(), SceneError> {
self.try_polyline_on_layer(Layer::DEFAULT, points, width, color)
}
pub fn polyline_on_layer(
&mut self,
layer: Layer,
points: Vec<Vec2>,
width: f32,
color: Color,
) -> bool {
self.try_polyline_on_layer(layer, points, width, color)
.is_ok()
}
pub fn try_polyline_on_layer(
&mut self,
layer: Layer,
points: Vec<Vec2>,
width: f32,
color: Color,
) -> Result<(), SceneError> {
self.try_push_to_layer(
layer,
DrawCommand::Polyline(Polyline {
points,
stroke: Stroke { width, color },
}),
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Layer {
order: i32,
}
impl Layer {
pub const DEFAULT: Self = Self::new(0);
pub const BACKGROUND: Self = Self::new(-100);
pub const FOREGROUND: Self = Self::new(100);
pub const fn new(order: i32) -> Self {
Self { order }
}
pub const fn order(self) -> i32 {
self.order
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScreenClipRect {
rect: Rect,
}
impl ScreenClipRect {
pub fn new(min: LogicalScreenPosition, max: LogicalScreenPosition) -> Result<Self, SceneError> {
Self::from_rect(Rect::new(min.to_vec2(), max.to_vec2()))
}
pub fn from_min_size(
min: LogicalScreenPosition,
size: LogicalScreenVector,
) -> Result<Self, SceneError> {
Self::from_rect(Rect::from_min_size(min.to_vec2(), size.to_vec2()))
}
fn from_rect(rect: Rect) -> Result<Self, SceneError> {
let clip = Self { rect };
clip.is_valid()
.then_some(clip)
.ok_or(SceneError::InvalidScreenClip)
}
#[cfg(feature = "wgpu")]
pub(crate) fn rect(self) -> Rect {
self.rect
}
pub fn min(self) -> LogicalScreenPosition {
LogicalScreenPosition::from_vec2(self.rect.normalized().min)
}
pub fn max(self) -> LogicalScreenPosition {
LogicalScreenPosition::from_vec2(self.rect.normalized().max)
}
pub fn intersection(self, other: Self) -> Result<Self, SceneError> {
let first = self.rect.normalized();
let second = other.rect.normalized();
let min = Vec2::new(first.min.x.max(second.min.x), first.min.y.max(second.min.y));
let max = Vec2::new(first.max.x.min(second.max.x), first.max.y.min(second.max.y));
Self::new(
LogicalScreenPosition::from_vec2(min),
LogicalScreenPosition::from_vec2(Vec2::new(max.x.max(min.x), max.y.max(min.y))),
)
}
fn is_valid(self) -> bool {
if !self.rect.min.is_finite() || !self.rect.max.is_finite() {
return false;
}
let rect = self.rect.normalized();
rect.width() > 0.0 && rect.height() > 0.0
}
}
#[derive(Debug, Clone)]
pub struct SceneCommand {
layer: Layer,
order: u64,
depth: f32,
screen_clip: Option<ScreenClipRect>,
command: DrawCommand,
}
impl SceneCommand {
pub fn layer(&self) -> Layer {
self.layer
}
pub fn order(&self) -> u64 {
self.order
}
pub fn depth(&self) -> f32 {
self.depth
}
pub fn screen_clip(&self) -> Option<ScreenClipRect> {
self.screen_clip
}
pub fn command(&self) -> &DrawCommand {
&self.command
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum DrawCommand {
Circle(Circle),
Rect(RectShape),
Line(Line),
Polyline(Polyline),
}
impl DrawCommand {
fn validate(&self) -> Result<(), SceneError> {
match self {
Self::Circle(circle) => {
if !circle.center.is_finite() {
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Circle));
}
if !circle.radius.is_finite() || circle.radius <= 0.0 {
return Err(SceneError::InvalidDimension(ScenePrimitive::Circle));
}
if !(circle.center + Vec2::splat(circle.radius)).is_finite()
|| !(circle.center - Vec2::splat(circle.radius)).is_finite()
{
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Circle));
}
circle.style.validate(ScenePrimitive::Circle)
}
Self::Rect(rectangle) => {
let rect = rectangle.rect.normalized();
if !rect.min.is_finite() || !rect.max.is_finite() {
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Rect));
}
let width = rect.width();
let height = rect.height();
if !width.is_finite() || !height.is_finite() {
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Rect));
}
if width <= 0.0
|| height <= 0.0
|| !rectangle.corner_radius.is_finite()
|| rectangle.corner_radius < 0.0
{
return Err(SceneError::InvalidDimension(ScenePrimitive::Rect));
}
rectangle.style.validate(ScenePrimitive::Rect)
}
Self::Line(line) => {
if !line.from.is_finite() || !line.to.is_finite() {
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Line));
}
if !(line.to - line.from).is_finite() {
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Line));
}
if !drawable_segment(line.from, line.to) {
return Err(SceneError::DegenerateGeometry(ScenePrimitive::Line));
}
if !line.stroke.is_valid() {
return Err(SceneError::InvalidStroke(ScenePrimitive::Line));
}
Ok(())
}
Self::Polyline(polyline) => {
if !polyline.points.iter().all(|point| point.is_finite()) {
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Polyline));
}
if polyline
.points
.windows(2)
.any(|pair| !(pair[1] - pair[0]).is_finite())
{
return Err(SceneError::NonFiniteGeometry(ScenePrimitive::Polyline));
}
if polyline.points.len() < 2
|| !polyline
.points
.windows(2)
.any(|pair| drawable_segment(pair[0], pair[1]))
{
return Err(SceneError::DegenerateGeometry(ScenePrimitive::Polyline));
}
if !polyline.stroke.is_valid() {
return Err(SceneError::InvalidStroke(ScenePrimitive::Polyline));
}
Ok(())
}
}
}
}
fn drawable_segment(from: Vec2, to: Vec2) -> bool {
let delta = to - from;
delta.is_finite() && (delta.x != 0.0 || delta.y != 0.0)
}
#[derive(Debug, Clone)]
pub struct Circle {
center: Vec2,
radius: f32,
style: ShapeStyle,
}
impl Circle {
pub const fn center(&self) -> Vec2 {
self.center
}
pub const fn radius(&self) -> f32 {
self.radius
}
pub const fn style(&self) -> ShapeStyle {
self.style
}
}
#[derive(Debug, Clone)]
pub struct RectShape {
rect: Rect,
corner_radius: f32,
style: ShapeStyle,
}
impl RectShape {
pub const fn rect(&self) -> Rect {
self.rect
}
pub const fn corner_radius(&self) -> f32 {
self.corner_radius
}
pub const fn style(&self) -> ShapeStyle {
self.style
}
}
#[derive(Debug, Clone)]
pub struct Line {
from: Vec2,
to: Vec2,
stroke: Stroke,
}
impl Line {
pub const fn from(&self) -> Vec2 {
self.from
}
pub const fn to(&self) -> Vec2 {
self.to
}
pub const fn stroke(&self) -> Stroke {
self.stroke
}
}
#[derive(Debug, Clone)]
pub struct Polyline {
points: Vec<Vec2>,
stroke: Stroke,
}
impl Polyline {
pub fn points(&self) -> &[Vec2] {
&self.points
}
pub const fn stroke(&self) -> Stroke {
self.stroke
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ShapeStyle {
fill: Option<Fill>,
stroke: Option<Stroke>,
shadow: Option<Shadow>,
}
impl ShapeStyle {
pub const fn new(fill: Option<Fill>, stroke: Option<Stroke>, shadow: Option<Shadow>) -> Self {
Self {
fill,
stroke,
shadow,
}
}
pub const fn fill(self) -> Option<Fill> {
self.fill
}
pub const fn stroke(self) -> Option<Stroke> {
self.stroke
}
pub const fn shadow(self) -> Option<Shadow> {
self.shadow
}
pub fn filled(color: Color) -> Self {
Self::filled_with(Fill::Solid(color))
}
pub fn filled_with(fill: Fill) -> Self {
Self {
fill: Some(fill),
stroke: None,
shadow: None,
}
}
pub fn stroked(width: f32, color: Color) -> Self {
Self {
fill: None,
stroke: Some(Stroke { width, color }),
shadow: None,
}
}
pub fn fill_stroke(fill: Color, stroke_width: f32, stroke: Color) -> Self {
Self::fill_stroke_with(Fill::Solid(fill), stroke_width, stroke)
}
pub fn fill_stroke_with(fill: Fill, stroke_width: f32, stroke: Color) -> Self {
Self {
fill: Some(fill),
stroke: Some(Stroke {
width: stroke_width,
color: stroke,
}),
shadow: None,
}
}
pub fn with_shadow(mut self, shadow: Shadow) -> Self {
self.shadow = Some(shadow);
self
}
fn validate(self, primitive: ScenePrimitive) -> Result<(), SceneError> {
if self.fill.is_none() && self.stroke.is_none() && self.shadow.is_none() {
return Err(SceneError::MissingStyle(primitive));
}
if self.fill.is_some_and(|fill| !fill.is_valid()) {
return Err(SceneError::InvalidFill(primitive));
}
if self.stroke.is_some_and(|stroke| !stroke.is_valid()) {
return Err(SceneError::InvalidStroke(primitive));
}
if self.shadow.is_some_and(|shadow| !shadow.is_valid()) {
return Err(SceneError::InvalidShadow(primitive));
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Fill {
Solid(Color),
LinearGradient(LinearGradient),
RadialGradient(RadialGradient),
}
impl Fill {
pub fn color_at(self, world: Vec2) -> Color {
match self {
Self::Solid(color) => color,
Self::LinearGradient(gradient) => gradient.color_at(world),
Self::RadialGradient(gradient) => gradient.color_at(world),
}
}
fn is_valid(self) -> bool {
match self {
Self::Solid(color) => color.is_normalized(),
Self::LinearGradient(gradient) => gradient.is_valid(),
Self::RadialGradient(gradient) => gradient.is_valid(),
}
}
}
impl From<Color> for Fill {
fn from(color: Color) -> Self {
Self::Solid(color)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LinearGradient {
start: Vec2,
end: Vec2,
start_color: Color,
end_color: Color,
}
impl LinearGradient {
pub fn new(start: Vec2, end: Vec2, start_color: Color, end_color: Color) -> Self {
Self {
start,
end,
start_color,
end_color,
}
}
pub const fn start(self) -> Vec2 {
self.start
}
pub const fn end(self) -> Vec2 {
self.end
}
pub const fn start_color(self) -> Color {
self.start_color
}
pub const fn end_color(self) -> Color {
self.end_color
}
pub fn color_at(self, world: Vec2) -> Color {
if !self.is_valid() || !world.is_finite() {
return self.end_color.clamp();
}
let axis_x = f64::from(self.end.x) - f64::from(self.start.x);
let axis_y = f64::from(self.end.y) - f64::from(self.start.y);
let relative_x = f64::from(world.x) - f64::from(self.start.x);
let relative_y = f64::from(world.y) - f64::from(self.start.y);
let axis_length_squared = axis_x * axis_x + axis_y * axis_y;
let amount = ((relative_x * axis_x + relative_y * axis_y) / axis_length_squared)
.clamp(0.0, 1.0) as f32;
self.start_color.interpolate(self.end_color, amount)
}
fn is_valid(self) -> bool {
self.start.is_finite()
&& self.end.is_finite()
&& self.start_color.is_normalized()
&& self.end_color.is_normalized()
&& self.start != self.end
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RadialGradient {
center: Vec2,
inner_radius: f32,
outer_radius: f32,
inner_color: Color,
outer_color: Color,
}
impl RadialGradient {
pub fn new(
center: Vec2,
inner_radius: f32,
outer_radius: f32,
inner_color: Color,
outer_color: Color,
) -> Self {
if inner_radius.is_finite() && outer_radius.is_finite() && inner_radius > outer_radius {
Self {
center,
inner_radius: outer_radius,
outer_radius: inner_radius,
inner_color: outer_color,
outer_color: inner_color,
}
} else {
Self {
center,
inner_radius,
outer_radius,
inner_color,
outer_color,
}
}
}
pub const fn center(self) -> Vec2 {
self.center
}
pub const fn inner_radius(self) -> f32 {
self.inner_radius
}
pub const fn outer_radius(self) -> f32 {
self.outer_radius
}
pub const fn inner_color(self) -> Color {
self.inner_color
}
pub const fn outer_color(self) -> Color {
self.outer_color
}
pub fn color_at(self, world: Vec2) -> Color {
if !self.is_valid() || !world.is_finite() {
return self.outer_color.clamp();
}
let radius_range = self.outer_radius - self.inner_radius;
if radius_range.abs() <= f32::EPSILON {
return self.outer_color;
}
let horizontal = f64::from(world.x) - f64::from(self.center.x);
let vertical = f64::from(world.y) - f64::from(self.center.y);
let distance = horizontal.hypot(vertical);
let amount = ((distance - f64::from(self.inner_radius)) / f64::from(radius_range))
.clamp(0.0, 1.0) as f32;
self.inner_color.interpolate(self.outer_color, amount)
}
fn is_valid(self) -> bool {
self.center.is_finite()
&& self.inner_radius.is_finite()
&& self.outer_radius.is_finite()
&& self.inner_radius >= 0.0
&& self.outer_radius >= self.inner_radius
&& self.inner_color.is_normalized()
&& self.outer_color.is_normalized()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Stroke {
width: f32,
color: Color,
}
impl Stroke {
pub const fn new(width: f32, color: Color) -> Self {
Self { width, color }
}
pub const fn width(self) -> f32 {
self.width
}
pub const fn color(self) -> Color {
self.color
}
fn is_valid(self) -> bool {
self.width.is_finite() && self.width > 0.0 && self.color.is_normalized()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Shadow {
offset: LogicalScreenVector,
spread: f32,
color: Color,
}
impl Shadow {
pub fn new(offset: LogicalScreenVector, spread: f32, color: Color) -> Self {
Self {
offset,
spread,
color,
}
}
pub const fn offset(self) -> LogicalScreenVector {
self.offset
}
pub const fn spread(self) -> f32 {
self.spread
}
pub const fn color(self) -> Color {
self.color
}
fn is_valid(self) -> bool {
self.offset.is_finite()
&& self.spread.is_finite()
&& self.spread >= 0.0
&& (self.spread * 2.0).is_finite()
&& self.color.is_normalized()
}
}
#[cfg(test)]
mod tests {
use std::panic::{AssertUnwindSafe, catch_unwind};
use crate::{
Color, DrawCommand, Fill, Layer, LinearGradient, LogicalScreenPosition,
LogicalScreenVector, RadialGradient, Rect, Scene, SceneError, ScenePrimitive,
ScreenClipRect, ShapeStyle, Vec2,
};
#[test]
fn scene_collects_visual_commands_only() {
let mut scene = Scene::new(Color::BLACK).unwrap();
scene.circle(Vec2::ZERO, 10.0, ShapeStyle::filled(Color::WHITE));
assert_eq!(scene.commands.len(), 1);
}
#[test]
fn scene_keeps_commands_sorted_by_layer() {
let mut scene = Scene::new(Color::BLACK).unwrap();
scene.circle_on_layer(
Layer::FOREGROUND,
Vec2::ZERO,
10.0,
ShapeStyle::filled(Color::WHITE),
);
scene.line_on_layer(Layer::BACKGROUND, Vec2::ZERO, Vec2::X, 1.0, Color::WHITE);
assert!(matches!(scene.commands[0].command, DrawCommand::Line(_)));
assert!(matches!(scene.commands[1].command, DrawCommand::Circle(_)));
}
#[test]
fn linear_gradient_samples_along_axis() {
let gradient = LinearGradient::new(Vec2::ZERO, Vec2::X, Color::BLACK, Color::WHITE);
let sampled = Fill::LinearGradient(gradient).color_at(Vec2::new(0.5, 4.0));
assert!((sampled.red() - 0.5).abs() < 0.001);
assert!((sampled.green() - 0.5).abs() < 0.001);
assert!((sampled.blue() - 0.5).abs() < 0.001);
}
#[test]
fn extreme_gradients_sample_without_f32_overflow() {
let gradient = LinearGradient::new(
Vec2::new(-f32::MAX, 0.0),
Vec2::new(f32::MAX, 0.0),
Color::BLACK,
Color::WHITE,
);
let mut scene = Scene::new(Color::BLACK).unwrap();
assert!(
scene
.try_circle(
Vec2::ZERO,
1.0,
ShapeStyle::filled_with(Fill::LinearGradient(gradient)),
)
.is_ok()
);
assert_eq!(
gradient.color_at(Vec2::ZERO),
Color::rgba(0.5, 0.5, 0.5, 1.0)
);
let radial = RadialGradient::new(
Vec2::splat(f32::MAX),
0.0,
f32::MAX,
Color::BLACK,
Color::WHITE,
);
assert_eq!(radial.color_at(Vec2::splat(-f32::MAX)), Color::WHITE);
}
#[test]
fn overflowing_circle_and_shadow_are_rejected_at_scene_boundary() {
let mut scene = Scene::new(Color::BLACK).unwrap();
assert_eq!(
scene.try_circle(
Vec2::splat(f32::MAX),
f32::MAX,
ShapeStyle::filled(Color::WHITE),
),
Err(SceneError::NonFiniteGeometry(ScenePrimitive::Circle))
);
assert_eq!(
scene.try_circle(
Vec2::ZERO,
1.0,
ShapeStyle::filled(Color::WHITE).with_shadow(super::Shadow::new(
LogicalScreenVector::new(0.0, 0.0),
f32::MAX,
Color::WHITE,
)),
),
Err(SceneError::InvalidShadow(ScenePrimitive::Circle))
);
assert_eq!(
scene.try_line(
Vec2::new(-f32::MAX, 0.0),
Vec2::new(f32::MAX, 0.0),
1.0,
Color::WHITE,
),
Err(SceneError::NonFiniteGeometry(ScenePrimitive::Line))
);
assert_eq!(
scene.try_rect(
Rect::new(Vec2::new(-f32::MAX, 0.0), Vec2::new(f32::MAX, 1.0),),
0.0,
ShapeStyle::filled(Color::WHITE),
),
Err(SceneError::NonFiniteGeometry(ScenePrimitive::Rect))
);
}
#[test]
fn commands_capture_temporary_screen_clip() {
let mut scene = Scene::new(Color::BLACK).unwrap();
let outer = ScreenClipRect::from_min_size(
LogicalScreenPosition::new(10.0, 20.0),
LogicalScreenVector::new(100.0, 80.0),
)
.unwrap();
let inner = ScreenClipRect::from_min_size(
LogicalScreenPosition::new(50.0, 0.0),
LogicalScreenVector::new(100.0, 50.0),
)
.unwrap();
scene
.with_screen_clip(outer, |scene| {
scene
.with_screen_clip(inner, |scene| {
scene.circle(Vec2::ZERO, 10.0, ShapeStyle::filled(Color::WHITE));
})
.unwrap();
})
.unwrap();
scene.circle(Vec2::ZERO, 10.0, ShapeStyle::filled(Color::WHITE));
assert_eq!(
scene.commands[0].screen_clip,
Some(
ScreenClipRect::new(
LogicalScreenPosition::new(50.0, 20.0),
LogicalScreenPosition::new(110.0, 50.0),
)
.unwrap(),
)
);
assert_eq!(scene.commands[1].screen_clip, None);
}
#[test]
fn scene_rejects_invalid_primitives_before_ordering() {
let mut scene = Scene::new(Color::BLACK).unwrap();
assert!(!scene.circle(Vec2::ZERO, -1.0, ShapeStyle::filled(Color::WHITE)));
assert!(!scene.line(Vec2::ZERO, Vec2::X, f32::NAN, Color::WHITE));
assert!(!scene.polyline(vec![Vec2::ZERO], 1.0, Color::WHITE));
assert_eq!(scene.command_count(), 0);
assert!(scene.circle(Vec2::ZERO, 1.0, ShapeStyle::filled(Color::WHITE)));
assert_eq!(scene.commands()[0].order(), 0);
}
#[test]
fn background_and_clip_are_validated_when_created() {
assert!(matches!(
Scene::new(Color::rgba(f32::NAN, 0.0, 0.0, 1.0)),
Err(SceneError::InvalidBackground)
));
assert!(matches!(
Scene::new(Color::rgb(1.01, 0.0, 0.0)),
Err(SceneError::InvalidBackground)
));
assert_eq!(
ScreenClipRect::new(
LogicalScreenPosition::new(0.0, 0.0),
LogicalScreenPosition::new(0.0, 10.0),
),
Err(SceneError::InvalidScreenClip)
);
assert_eq!(
ScreenClipRect::new(
LogicalScreenPosition::new(f32::NAN, 0.0),
LogicalScreenPosition::new(10.0, 10.0),
),
Err(SceneError::InvalidScreenClip)
);
}
#[test]
fn render_bound_scene_colors_must_be_normalized() {
let mut scene = Scene::new(Color::BLACK).unwrap();
assert_eq!(
scene.try_circle(
Vec2::ZERO,
1.0,
ShapeStyle::filled(Color::rgb(2.0, 0.0, 0.0)),
),
Err(SceneError::InvalidFill(ScenePrimitive::Circle))
);
assert_eq!(
scene.try_line(Vec2::ZERO, Vec2::X, 1.0, Color::rgba(0.0, 0.0, 0.0, -0.01),),
Err(SceneError::InvalidStroke(ScenePrimitive::Line))
);
assert_eq!(scene.command_count(), 0);
}
#[test]
fn depth_does_not_reorder_commands_within_a_layer() {
let mut scene = Scene::new(Color::BLACK).unwrap();
scene
.with_depth(4.0, |scene| {
scene.circle(Vec2::X, 1.0, ShapeStyle::filled(Color::WHITE));
})
.unwrap();
scene.circle(Vec2::ZERO, 1.0, ShapeStyle::filled(Color::WHITE));
assert_eq!(scene.commands()[0].depth(), 4.0);
assert_eq!(scene.commands()[1].depth(), 0.0);
}
#[test]
fn structured_scene_errors_preserve_rejection_reason() {
let mut scene = Scene::new(Color::BLACK).unwrap();
assert_eq!(
scene.try_circle(Vec2::ZERO, -1.0, ShapeStyle::filled(Color::WHITE)),
Err(SceneError::InvalidDimension(ScenePrimitive::Circle))
);
assert_eq!(
scene.try_line(Vec2::ZERO, Vec2::ZERO, 1.0, Color::WHITE),
Err(SceneError::DegenerateGeometry(ScenePrimitive::Line))
);
assert_eq!(
scene.try_circle(
Vec2::ZERO,
1.0,
ShapeStyle {
fill: None,
stroke: None,
shadow: None,
},
),
Err(SceneError::MissingStyle(ScenePrimitive::Circle))
);
assert_eq!(scene.command_count(), 0);
}
#[test]
fn commands_capture_depth_and_restore_it_after_unwind() {
let mut scene = Scene::new(Color::BLACK).unwrap();
let panic_result = catch_unwind(AssertUnwindSafe(|| {
let _ = scene.with_depth(4.5, |scene| {
scene.circle(Vec2::ZERO, 1.0, ShapeStyle::filled(Color::WHITE));
panic!("intentional depth unwind");
});
}));
assert!(panic_result.is_err());
scene.circle(Vec2::X, 1.0, ShapeStyle::filled(Color::WHITE));
assert_eq!(scene.commands()[0].depth(), 4.5);
assert_eq!(scene.commands()[1].depth(), 0.0);
assert_eq!(
scene.with_depth(f32::NAN, |_| {}),
Err(SceneError::NonFiniteDepth)
);
}
#[test]
fn gradients_sanitize_invalid_and_reversed_configuration() {
let invalid_linear = LinearGradient::new(
Vec2::new(f32::NAN, 0.0),
Vec2::X,
Color::BLACK,
Color::WHITE,
);
assert_eq!(invalid_linear.color_at(Vec2::ZERO), Color::WHITE);
let reversed = RadialGradient::new(Vec2::ZERO, 10.0, 5.0, Color::WHITE, Color::BLACK);
assert_eq!(reversed.inner_radius(), 5.0);
assert_eq!(reversed.outer_radius(), 10.0);
assert_eq!(reversed.color_at(Vec2::ZERO), Color::BLACK);
}
#[test]
fn temporary_screen_clip_restores_state_after_unwind() {
let mut scene = Scene::new(Color::BLACK).unwrap();
let clip = ScreenClipRect::from_min_size(
LogicalScreenPosition::new(0.0, 0.0),
LogicalScreenVector::new(100.0, 100.0),
)
.unwrap();
let panic_result = catch_unwind(AssertUnwindSafe(|| {
let _ = scene.with_screen_clip(clip, |_| panic!("intentional test panic"));
}));
assert!(panic_result.is_err());
assert!(scene.circle(Vec2::ZERO, 1.0, ShapeStyle::filled(Color::WHITE)));
assert_eq!(scene.commands()[0].screen_clip(), None);
}
}