use crate::data::BlendMode;
pub mod clipping;
pub mod renderer;
pub use clipping::SkeletonClipping;
pub use renderer::SkeletonRenderer;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TextureId(pub u32);
impl TextureId {
pub const MISSING: TextureId = TextureId(u32::MAX);
}
#[derive(Debug, Clone, PartialEq)]
pub struct RenderCommand {
pub positions: Vec<f32>,
pub uvs: Vec<f32>,
pub colors: Vec<u32>,
pub dark_colors: Vec<u32>,
pub indices: Vec<u16>,
pub slots: Vec<u16>,
pub tags: Vec<u32>,
pub blend_mode: BlendMode,
pub texture: TextureId,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RenderOptions {
pub vertex_ids: bool,
pub merge_colors: bool,
}
impl RenderCommand {
#[must_use]
#[inline]
pub fn num_vertices(&self) -> usize {
self.positions.len() / 2
}
#[must_use]
#[inline]
pub fn num_indices(&self) -> usize {
self.indices.len()
}
#[must_use]
pub fn position_bounds(&self) -> Option<(f32, f32, f32, f32)> {
let n = self.num_vertices();
if n == 0 {
return None;
}
let mut xmin = f32::INFINITY;
let mut xmax = f32::NEG_INFINITY;
let mut ymin = f32::INFINITY;
let mut ymax = f32::NEG_INFINITY;
for i in 0..n {
let x = self.positions[i * 2];
let y = self.positions[i * 2 + 1];
xmin = xmin.min(x);
xmax = xmax.max(x);
ymin = ymin.min(y);
ymax = ymax.max(y);
}
Some((xmin, xmax, ymin, ymax))
}
}
#[allow(dead_code)]
#[must_use]
#[inline]
pub(crate) fn pack_color(r: f32, g: f32, b: f32, a: f32) -> u32 {
let ri = (r * 255.0) as u32 & 0xff;
let gi = (g * 255.0) as u32 & 0xff;
let bi = (b * 255.0) as u32 & 0xff;
let ai = (a * 255.0) as u32 & 0xff;
(ai << 24) | (ri << 16) | (gi << 8) | bi
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pack_color_matches_spine_cpp_layout() {
assert_eq!(pack_color(1.0, 1.0, 1.0, 1.0), 0xffff_ffff);
assert_eq!(pack_color(0.0, 0.0, 0.0, 1.0), 0xff00_0000);
assert_eq!(pack_color(1.0, 1.0, 1.0, 0.0), 0x00ff_ffff);
assert_eq!(pack_color(1.0, 0.0, 0.0, 0.5), 0x7fff_0000);
}
#[test]
fn texture_id_missing_is_u32_max() {
assert_eq!(TextureId::MISSING, TextureId(u32::MAX));
}
}