use yog_formats::book::layout::{ICON_PX, LayoutPart, RecipeLayout};
use yog_gfx::image::ImageSource;
#[derive(Debug, Clone, PartialEq)]
pub struct ScreenElement {
pub kind: &'static str,
pub attrs: Vec<(&'static str, String)>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ScreenArea {
pub elements: Vec<ScreenElement>,
pub places: Vec<(String, usize, usize)>,
pub result: Option<usize>,
pub next: usize,
}
const SLOT_BORDER: f32 = 1.0;
const LABEL_COLOR: &str = "#FF404040";
pub fn screen_area(layout: &RecipeLayout, first: usize) -> ScreenArea {
let mut out = ScreenArea { next: first, ..ScreenArea::default() };
let xy = |p: [f32; 2], d: f32| [("x", (p[0] - d).to_string()), ("y", (p[1] - d).to_string())];
let icon = |source: String, at: [f32; 2]| {
let [x, y] = xy(at, 0.0);
ScreenElement { kind: "Image", attrs: vec![("source", source), ("w", ICON_PX.to_string()), ("h", ICON_PX.to_string()), x, y] }
};
for part in &layout.parts {
match part {
LayoutPart::Image { source, uv, at } => {
let [u, v, w, h] = *uv;
let [x, y] = xy(*at, 0.0);
let mut attrs = vec![("source", source.clone()), ("w", w.to_string()), ("h", h.to_string()), ("stretch", "Fill".into()), x, y];
if !matches!(ImageSource::parse(source), Some(ImageSource::Sprite(_))) {
attrs.push(("uv", format!("{u} {v} {w} {h}")));
}
out.elements.push(ScreenElement { kind: "Image", attrs });
}
LayoutPart::Slots { slot, at, .. } => {
for (i, p) in at.iter().enumerate() {
let [x, y] = xy(*p, SLOT_BORDER);
out.elements.push(ScreenElement { kind: "Slot", attrs: vec![("index", out.next.to_string()), x, y] });
out.places.push((slot.clone(), i, out.next));
out.next += 1;
}
}
LayoutPart::Result { at, .. } => {
let [x, y] = xy(*at, SLOT_BORDER);
out.elements.push(ScreenElement { kind: "Slot", attrs: vec![("index", out.next.to_string()), x, y] });
out.result = Some(out.next);
out.next += 1;
}
LayoutPart::Item { item, at } => out.elements.push(icon(ImageSource::Item(item.clone()).to_string(), *at)),
LayoutPart::Block { block, at, .. } => {
let id = block.split('[').next().unwrap_or(block);
out.elements.push(icon(ImageSource::Block(id.to_string()).to_string(), *at));
}
LayoutPart::Text { text, at } if !text.contains('{') => {
let [x, y] = xy(*at, 0.0);
out.elements.push(ScreenElement { kind: "Label", attrs: vec![("text", text.clone()), ("color", LABEL_COLOR.into()), ("shadow", "false".into()), x, y] });
}
LayoutPart::Text { .. } | LayoutPart::Count { .. } => {}
}
}
out
}