use yog_formats::book::layout::{ICON_PX, LayoutPart, RecipeLayout};
use yog_gfx::Rect;
use yog_ids::yog::CRAFTING_SHEET;
pub const FRAME_UV: [f32; 4] = [83.0, 71.0, 24.0, 24.0];
pub const FRAME_INSET: f32 = 4.0;
#[derive(Debug, Clone, PartialEq)]
pub enum Piece<'a> {
Image { source: &'a str, uv: Rect, at: Rect },
Item { item: &'a str, at: Rect },
Block { block: &'a str, at: Rect },
}
#[must_use]
pub fn pieces(layout: &RecipeLayout) -> Vec<Piece<'_>> {
let [fu, fv, fw, fh] = FRAME_UV;
let frame = |[x, y]: [f32; 2]| Piece::Image {
source: CRAFTING_SHEET,
uv: Rect::new(fu, fv, fw, fh),
at: Rect::new(x - FRAME_INSET, y - FRAME_INSET, fw, fh),
};
let mut out = Vec::new();
for part in &layout.parts {
match part {
LayoutPart::Image { source, uv, at } => {
let [u, v, w, h] = *uv;
out.push(Piece::Image { source, uv: Rect::new(u, v, w, h), at: Rect::new(at[0], at[1], w, h) });
}
LayoutPart::Slots { at, frame: true, .. } => out.extend(at.iter().map(|a| frame(*a))),
LayoutPart::Result { at, frame: true } => out.push(frame(*at)),
LayoutPart::Item { item, at } => out.push(Piece::Item { item, at: Rect::new(at[0], at[1], ICON_PX, ICON_PX) }),
LayoutPart::Block { block, at, size } => out.push(Piece::Block { block, at: Rect::new(at[0], at[1], *size, *size) }),
LayoutPart::Slots { .. } | LayoutPart::Result { .. } | LayoutPart::Count { .. } | LayoutPart::Text { .. } => {}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn frames_sit_around_their_items() {
let l = yog_formats::book::layout::find(&[], "smelting").expect("a built-in layout");
let p = pieces(l);
assert!(p.iter().any(|p| matches!(p, Piece::Image { source, .. } if *source == CRAFTING_SHEET)));
}
}