use crate::bm::Font;
use crate::MaterialRef;
use int_math::{URect, UVec2, Vec2, Vec3};
use monotonic_time_rs::Millis;
use std::rc::Rc;
pub trait Assets {
fn material_png_raw(&mut self, png: &[u8], label: &str) -> MaterialRef;
fn material_png(&mut self, name: &str) -> MaterialRef;
fn set_prefix(&mut self, prefix: &str);
fn frame_fixed_grid_material_png(&mut self, name: &str, grid_size: UVec2) -> FixedAtlas;
fn bm_font(&mut self, name: &str) -> FontAndMaterialRef;
fn now(&self) -> Millis;
}
pub trait Gfx {
fn set_origin(&mut self, position: Vec2);
fn sprite_atlas_frame(&mut self, position: Vec3, frame: u16, atlas: &impl FrameLookup);
fn sprite_atlas(&mut self, position: Vec3, atlas_rect: URect, material: &MaterialRef);
fn text_draw(&mut self, position: Vec3, text: &str, font_ref: &FontAndMaterialRef);
fn text_glyphs(&self, position: Vec2, text: &str, font_ref: &FontAndMaterialRef) -> Vec<Glyph>;
fn tilemap(&mut self, position: Vec3, tiles: &[u16], width: u16, atlas: &FixedAtlas);
fn tilemap_params(
&mut self,
position: Vec3,
tiles: &[u16],
width: u16,
atlas: &FixedAtlas,
scale: u8,
);
fn now(&self) -> Millis;
}
pub trait FrameLookup {
fn lookup(&self, frame: u16) -> (MaterialRef, URect);
}
pub type FontAndMaterialRef = Rc<FontAndMaterial>;
#[derive(Debug)]
pub struct FontAndMaterial {
pub font: Font,
pub material_ref: MaterialRef,
}
#[derive(Debug)]
pub struct Glyph {
pub relative_position: Vec2,
pub texture_rectangle: URect,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixedAtlas {
pub material: MaterialRef,
pub one_cell_size: UVec2,
pub cell_count_size: UVec2,
}
impl FixedAtlas {
pub fn new(one_cell_size: UVec2, material_ref: MaterialRef) -> Self {
let cell_count_size = UVec2::new(
material_ref.texture_size.x / one_cell_size.x,
material_ref.texture_size.y / one_cell_size.y,
);
Self {
material: material_ref,
one_cell_size,
cell_count_size,
}
}
}
impl FrameLookup for FixedAtlas {
fn lookup(&self, frame: u16) -> (MaterialRef, URect) {
let x = frame % self.cell_count_size.x;
let y = frame / self.cell_count_size.x;
(
self.material.clone(),
URect::new(
x * self.one_cell_size.x,
y * self.one_cell_size.y,
self.one_cell_size.x,
self.one_cell_size.y,
),
)
}
}