2019-04-28 18:18:08 +00:00
|
|
|
use super::Graphic;
|
|
|
|
use dot_vox::DotVoxData;
|
|
|
|
use image::DynamicImage;
|
|
|
|
use common::assets::{Error, load};
|
|
|
|
|
|
|
|
pub struct BlankGraphic;
|
|
|
|
pub struct ImageGraphic;
|
|
|
|
pub struct VoxelGraphic;
|
|
|
|
|
|
|
|
pub trait GraphicCreator<'a> {
|
|
|
|
type Specifier;
|
|
|
|
fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error>;
|
|
|
|
}
|
|
|
|
impl<'a> GraphicCreator<'a> for BlankGraphic {
|
|
|
|
type Specifier = ();
|
|
|
|
fn new_graphic(_: ()) -> Result<Graphic, Error> {
|
|
|
|
Ok(Graphic::Blank)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'a> GraphicCreator<'a> for ImageGraphic {
|
|
|
|
type Specifier = &'a str;
|
|
|
|
fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error> {
|
|
|
|
Ok(Graphic::Image(load::<DynamicImage>(specifier)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'a> GraphicCreator<'a> for VoxelGraphic {
|
|
|
|
type Specifier = &'a str;
|
|
|
|
fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error> {
|
|
|
|
Ok(Graphic::Voxel(load::<DotVoxData>(specifier)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 03:30:46 +00:00
|
|
|
/// This macro will automatically load all specified assets, get the corresponding ImgIds and
|
|
|
|
/// create a struct with all of them
|
|
|
|
///
|
|
|
|
/// Example usage:
|
|
|
|
/// ```
|
|
|
|
/// image_ids! {
|
2019-04-28 16:44:44 +00:00
|
|
|
/// pub struct Imgs {
|
2019-04-28 18:18:08 +00:00
|
|
|
/// <VoxelGraphic>
|
2019-04-26 03:30:46 +00:00
|
|
|
/// button1: "filename1.vox",
|
|
|
|
/// button2: "filename2.vox",
|
2019-04-28 16:44:44 +00:00
|
|
|
///
|
2019-04-28 18:18:08 +00:00
|
|
|
/// <ImageGraphic>
|
2019-04-26 03:30:46 +00:00
|
|
|
/// background: "background.png",
|
2019-04-28 18:18:08 +00:00
|
|
|
///
|
|
|
|
/// <BlankGraphic>
|
|
|
|
/// blank: (),
|
2019-04-26 03:30:46 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
// TODO: will this work with shorter name paths? eg not rate::ui::Graphic::
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! image_ids {
|
2019-04-28 16:44:44 +00:00
|
|
|
($($v:vis struct $Ids:ident { $( <$T:ty> $( $name:ident: $specifier:expr ),* $(,)? )* })*) => {
|
2019-04-26 03:30:46 +00:00
|
|
|
$(
|
|
|
|
$v struct $Ids {
|
2019-04-28 16:44:44 +00:00
|
|
|
$($( $v $name: conrod_core::image::Id, )*)*
|
2019-04-26 03:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl $Ids {
|
2019-04-28 04:44:13 +00:00
|
|
|
pub fn load(ui: &mut crate::ui::Ui) -> Result<Self, common::assets::Error> {
|
2019-04-28 18:18:08 +00:00
|
|
|
use crate::ui::GraphicCreator;
|
2019-04-26 03:30:46 +00:00
|
|
|
Ok(Self {
|
2019-04-28 18:18:08 +00:00
|
|
|
$($( $name: ui.add_graphic(<$T>::new_graphic($specifier)?), )*)*
|
2019-04-26 03:30:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|