#[cfg(test)]
use crcbl_sprite::load::Loaded;
use crcbl_sprite::load::load_baked;
use crcbl_ui::image::{AtlasError, AtlasImage, ImageAtlas, NineSliceImage};
use crcbl_ui::menu::MenuSkin;
use crcbl_ui::{ButtonSkin, SkinInsets};
include!(concat!(env!("OUT_DIR"), "/menu_art.rs"));
const PANEL_FRAME: usize = 0;
const IDLE_FRAME: usize = 1;
const HOVERED_FRAME: usize = 2;
const PRESSED_FRAME: usize = 3;
const SCRIM_FRAME: usize = 4;
pub fn menu_skin(images: &mut ImageAtlas) -> Result<MenuSkin, AtlasError> {
let art = load_baked("menu", MENU_PNG, MENU_JSON, ART_TICK_HZ);
let nine = art
.sheet
.nine
.expect("menu.crpix declares a nine-slice over its frames");
let insets = SkinInsets::new(
nine.left as f32,
nine.right as f32,
nine.top as f32,
nine.bottom as f32,
);
let mut register = |index: usize| -> Result<AtlasImage, AtlasError> {
let rect = art
.sheet
.frames
.get(index)
.expect("menu.crpix has the five frames this module names")
.rect;
let pixels = art
.frame_pixels(index)
.expect("the frame was looked up above");
images.register(rect.w, rect.h, &pixels)
};
let panel = register(PANEL_FRAME)?;
let idle = register(IDLE_FRAME)?;
let hovered = register(HOVERED_FRAME)?;
let pressed = register(PRESSED_FRAME)?;
let scrim = register(SCRIM_FRAME)?;
let sliced = |image| NineSliceImage { image, insets };
Ok(MenuSkin {
panel: sliced(panel),
buttons: ButtonSkin {
idle: sliced(idle),
hovered: sliced(hovered),
pressed: sliced(pressed),
},
scrim,
})
}
#[cfg(test)]
fn baked(name: &str, png: &[u8], json: Option<&str>) -> Loaded {
load_baked(name, png, json, ART_TICK_HZ)
}
#[cfg(test)]
mod tests {
use super::*;
use crcbl_sprite::NineSlice;
fn sheet_of(loaded: &Loaded) -> &crcbl_sprite::Sheet {
&loaded.sheet
}
#[test]
fn the_shipped_art_bakes_to_the_sheet_it_declares() {
let art = baked("menu", MENU_PNG, MENU_JSON);
assert_eq!(
(art.image.width, art.image.height),
(16 * 5, 16),
"five 16x16 frames in one horizontal strip"
);
let names: Vec<&str> = sheet_of(&art)
.frames
.iter()
.map(|f| f.name.as_str())
.collect();
assert_eq!(names, ["panel", "idle", "hovered", "pressed", "scrim"]);
assert_eq!(sheet_of(&art).nine, Some(NineSlice::new(4, 4, 4, 4)));
for (index, frame) in sheet_of(&art).frames.iter().enumerate() {
assert_eq!(
frame.rect,
crcbl_sprite::Rect::new(index as u32 * 16, 0, 16, 16),
"frame {index} is not a 16x16 cell of the strip"
);
assert_eq!(
frame.hold, 1,
"the default hold in ticks did not survive the millisecond round \
trip, so bake and load disagree about milliseconds"
);
}
assert!(
sheet_of(&art).clips.is_empty(),
"the button frames are states, not a clip: a clip here would animate \
a button nobody is touching"
);
let clear = art
.image
.pixels
.as_chunks::<4>()
.0
.iter()
.filter(|p| p[3] == 0)
.count();
assert_eq!(clear, 0, "the menu art has holes in it");
for y in 0..16usize {
for x in 0..16usize {
let i = ((y * 80) + SCRIM_FRAME * 16 + x) * 4;
assert_eq!(
&art.image.pixels[i..i + 4],
[255, 255, 255, 255],
"the scrim is not white at ({x}, {y})"
);
}
}
}
#[test]
fn every_stretched_band_is_uniform_along_its_span() {
let art = baked("menu", MENU_PNG, MENU_JSON);
let texel = |frame: usize, x: usize, y: usize| -> [u8; 4] {
let i = ((y * 80) + frame * 16 + x) * 4;
art.image.pixels[i..i + 4]
.try_into()
.expect("four channels")
};
for frame in 0..5 {
for y in 0..16 {
let first = texel(frame, 4, y);
for x in 5..12 {
assert_eq!(
texel(frame, x, y),
first,
"frame {frame} row {y} is not uniform across its stretched \
columns, so a widened panel smears it"
);
}
}
for x in 0..16 {
let first = texel(frame, x, 4);
for y in 5..12 {
assert_eq!(
texel(frame, x, y),
first,
"frame {frame} column {x} is not uniform down its stretched \
rows"
);
}
}
}
}
#[test]
fn the_three_button_frames_are_actually_different_pictures() {
let art = baked("menu", MENU_PNG, MENU_JSON);
let frames: Vec<Vec<u8>> = [IDLE_FRAME, HOVERED_FRAME, PRESSED_FRAME]
.iter()
.map(|i| art.frame_pixels(*i).expect("the sheet has the frame"))
.collect();
for (index, frame) in frames.iter().enumerate() {
assert_eq!(frame.len(), 16 * 16 * 4, "frame {index} is not 16x16");
}
for a in 0..3 {
for b in (a + 1)..3 {
assert_ne!(
frames[a], frames[b],
"button frames {a} and {b} are the same picture, so pressing \
the button would change nothing on screen",
);
}
}
for (index, frame) in frames.iter().enumerate() {
let row = |r: usize| frame[r * 16 * 4..(r + 1) * 16 * 4].to_vec();
assert_ne!(row(1), row(14), "frame {index} is a flat rectangle");
}
let brightness = |frame: &[u8], r: usize| -> u32 {
frame[r * 16 * 4..(r + 1) * 16 * 4]
.as_chunks::<4>()
.0
.iter()
.map(|p| u32::from(p[0]) + u32::from(p[1]) + u32::from(p[2]))
.sum()
};
assert!(
brightness(&frames[0], 1) > brightness(&frames[0], 14),
"idle is not lit from the top",
);
assert!(
brightness(&frames[2], 1) < brightness(&frames[2], 14),
"pressed is idle darkened rather than idle inverted",
);
}
#[test]
fn the_shipped_art_has_the_insets_the_layout_assumes() {
let skin = menu_skin(&mut ImageAtlas::new()).expect("five frames fit an empty page");
assert_eq!(
skin.panel.clamped_insets(),
crcbl_ui::menu::PANEL_INSETS,
"menu.crpix and crcbl_ui::menu::PANEL_INSETS disagree",
);
assert_eq!(
skin.buttons.insets(),
crcbl_ui::menu::BUTTON_INSETS,
"menu.crpix and crcbl_ui::menu::BUTTON_INSETS disagree",
);
assert!(
skin.buttons.insets_agree(),
"the three button frames must share their insets",
);
}
#[test]
fn each_registered_image_is_its_own_frame_of_the_sheet() {
let art = baked("menu", MENU_PNG, MENU_JSON);
let mut images = ImageAtlas::new();
let skin = menu_skin(&mut images).expect("fits");
let page = crcbl_ui::image::PAGE_SIZE as usize;
for (index, image) in [
(PANEL_FRAME, skin.panel.image),
(IDLE_FRAME, skin.buttons.idle.image),
(HOVERED_FRAME, skin.buttons.hovered.image),
(PRESSED_FRAME, skin.buttons.pressed.image),
(SCRIM_FRAME, skin.scrim),
] {
assert_eq!((image.width, image.height), (16, 16), "frame {index}");
let expected = art.frame_pixels(index).expect("the sheet has the frame");
let mut actual = Vec::with_capacity(expected.len());
for row in 0..16 {
let start = ((image.y as usize + row) * page + image.x as usize) * 4;
actual.extend_from_slice(&images.pixels()[start..start + 16 * 4]);
}
assert_eq!(actual, expected, "frame {index} is not what was registered");
}
let ids = [
skin.panel.image.id,
skin.buttons.idle.image.id,
skin.buttons.hovered.image.id,
skin.buttons.pressed.image.id,
skin.scrim.id,
];
for a in 0..ids.len() {
for b in (a + 1)..ids.len() {
assert_ne!(ids[a], ids[b], "two frames share one image");
}
}
}
}