1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use super::{Graphic, SampleStrat, Transform};
use common::{
    assets::{self, AssetExt, DotVoxAsset, Error},
    figure::Segment,
};
use std::sync::Arc;
use vek::*;

pub enum BlankGraphic {}
pub enum ImageGraphic {}

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> {
        let image = assets::Image::load(specifier)?.read().to_image();
        Ok(Graphic::Image(image, None))
    }
}

pub enum VoxelGraphic {}
// TODO: Are these unneeded now that we have PixArtGraphic?
pub enum VoxelSsGraphic {}
pub enum VoxelSs4Graphic {}
pub enum VoxelSs9Graphic {}

pub enum VoxelPixArtGraphic {}

fn load_segment(specifier: &str) -> Result<Arc<Segment>, Error> {
    let dot_vox = DotVoxAsset::load(specifier)?;
    let seg = Segment::from_vox_model_index(&dot_vox.read().0, 0);
    Ok(Arc::new(seg))
}

impl<'a> GraphicCreator<'a> for VoxelGraphic {
    type Specifier = &'a str;

    fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error> {
        Ok(Graphic::Voxel(
            load_segment(specifier)?,
            Transform {
                ori: Quaternion::rotation_x(-std::f32::consts::PI / 2.0),
                ..Default::default()
            },
            SampleStrat::None,
        ))
    }
}
impl<'a> GraphicCreator<'a> for VoxelSsGraphic {
    type Specifier = (&'a str, u8);

    fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error> {
        Ok(Graphic::Voxel(
            load_segment(specifier.0)?,
            Transform {
                ori: Quaternion::rotation_x(-std::f32::consts::PI / 2.0),
                ..Default::default()
            },
            SampleStrat::SuperSampling(specifier.1),
        ))
    }
}
impl<'a> GraphicCreator<'a> for VoxelSs4Graphic {
    type Specifier = &'a str;

    fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error> {
        Ok(Graphic::Voxel(
            load_segment(specifier)?,
            Transform {
                ori: Quaternion::rotation_x(-std::f32::consts::PI / 2.0),
                ..Default::default()
            },
            SampleStrat::SuperSampling(4),
        ))
    }
}
impl<'a> GraphicCreator<'a> for VoxelSs9Graphic {
    type Specifier = &'a str;

    fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error> {
        Ok(Graphic::Voxel(
            load_segment(specifier)?,
            Transform {
                ori: Quaternion::rotation_x(-std::f32::consts::PI / 2.0),
                ..Default::default()
            },
            SampleStrat::SuperSampling(9),
        ))
    }
}
impl<'a> GraphicCreator<'a> for VoxelPixArtGraphic {
    type Specifier = &'a str;

    fn new_graphic(specifier: Self::Specifier) -> Result<Graphic, Error> {
        Ok(Graphic::Voxel(
            load_segment(specifier)?,
            Transform {
                ori: Quaternion::rotation_x(-std::f32::consts::PI / 2.0),
                ..Default::default()
            },
            SampleStrat::PixelCoverage,
        ))
    }
}

pub struct Rotations {
    pub none: conrod_core::image::Id,
    pub cw90: conrod_core::image::Id,
    pub cw180: conrod_core::image::Id,
    pub cw270: conrod_core::image::Id,
    pub source_north: conrod_core::image::Id,
    pub target_north: conrod_core::image::Id,
}

/// This macro will automatically load all specified assets, get the
/// corresponding ImgIds and create a struct with all of them.
///
/// Example usage:
/// ```ignore
/// use veloren_voxygen::{
///     image_ids,
///     ui::img_ids::{BlankGraphic, ImageGraphic, VoxelGraphic},
/// };
///
/// image_ids! {
///     pub struct Imgs {
///         <VoxelGraphic>
///         button1: "specifier1",
///         button2: "specifier2",
///
///         <ImageGraphic>
///         background: "background",
///
///         <BlankGraphic>
///         blank: (),
///     }
/// }
/// ```
#[macro_export]
macro_rules! image_ids {
    ($($v:vis struct $Ids:ident { $( <$T:ty> $( $name:ident: $specifier:expr ),* $(,)? )* })*) => {
        $(
            $v struct $Ids {
                $($( $v $name: conrod_core::image::Id, )*)*
            }

            impl $Ids {
                pub fn load(ui: &mut $crate::ui::Ui) -> Result<Self, common::assets::Error> {
                    use $crate::ui::img_ids::GraphicCreator;
                    Ok(Self {
                        $($( $name: ui.add_graphic(<$T as GraphicCreator>::new_graphic($specifier)?), )*)*
                    })
                }
            }
        )*
    };
}
#[macro_export]
macro_rules! image_ids_ice {
    ($($v:vis struct $Ids:ident { $( <$T:ty> $( $(#[$($attribute:tt)*])? $name:ident: $specifier:expr ),* $(,)? )* })*) => {
        $(
            $v struct $Ids {
                $($( $(#[$($attribute)*])? $v $name: $crate::ui::GraphicId, )*)*
            }

            impl $Ids {
                pub fn load(ui: &mut $crate::ui::ice::IcedUi) -> Result<Self, common::assets::Error> {
                    use $crate::ui::img_ids::GraphicCreator;
                    Ok(Self {
                        $($( $(#[$($attribute)*])? $name: ui.add_graphic(<$T as GraphicCreator>::new_graphic($specifier)?), )*)*
                    })
                }
            }
        )*
    };
}

// TODO: combine with the img_ids macro above using a marker for specific fields
// that should be `Rotations` instead of `widget::Id`
#[macro_export]
macro_rules! rotation_image_ids {
    ($($v:vis struct $Ids:ident { $( <$T:ty> $( $name:ident: $specifier:expr ),* $(,)? )* })*) => {
        $(
            $v struct $Ids {
                $($( $v $name: $crate::ui::img_ids::Rotations, )*)*
            }

            impl $Ids {
                pub fn load(ui: &mut $crate::ui::Ui) -> Result<Self, common::assets::Error> {
                    use $crate::ui::img_ids::GraphicCreator;
                    Ok(Self {
                        $($( $name: ui.add_graphic_with_rotations(<$T as GraphicCreator>::new_graphic($specifier)?), )*)*
                    })
                }
            }
        )*
    };
}