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
use crate::ui::{Graphic, SampleStrat, Transform, Ui};
use common::{
    assets::{self, AssetCombined, AssetExt, AssetHandle, Concatenate, DotVoxAsset, ReloadWatcher},
    comp::item::item_key::ItemKey,
    figure::Segment,
};
use conrod_core::image::Id;
use hashbrown::HashMap;
use image::DynamicImage;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::{error, warn};
use vek::*;

pub fn animate_by_pulse(ids: &[Id], pulse: f32) -> Id {
    let animation_frame = (pulse * 3.0) as usize;
    ids[animation_frame % ids.len()]
}

#[derive(Debug, Serialize, Deserialize)]
pub enum ImageSpec {
    Png(String),
    Vox(String, #[serde(default)] u32),
    // (specifier, offset, (axis, 2 * angle / pi), zoom, model_index)
    VoxTrans(String, [f32; 3], [f32; 3], f32, #[serde(default)] u32),
}
impl ImageSpec {
    pub fn create_graphic(&self) -> Graphic {
        match self {
            ImageSpec::Png(specifier) => Graphic::Image(graceful_load_img(specifier), None),
            ImageSpec::Vox(specifier, model_index) => Graphic::Voxel(
                graceful_load_segment_no_skin(specifier, *model_index),
                Transform {
                    stretch: false,
                    ..Default::default()
                },
                SampleStrat::None,
            ),
            ImageSpec::VoxTrans(specifier, offset, [rot_x, rot_y, rot_z], zoom, model_index) => {
                Graphic::Voxel(
                    graceful_load_segment_no_skin(specifier, *model_index),
                    Transform {
                        ori: Quaternion::rotation_x(rot_x * std::f32::consts::PI / 180.0)
                            .rotated_y(rot_y * std::f32::consts::PI / 180.0)
                            .rotated_z(rot_z * std::f32::consts::PI / 180.0),
                        offset: Vec3::from(*offset),
                        zoom: *zoom,
                        orth: true, // TODO: Is this what we want here? @Pfau
                        stretch: false,
                    },
                    SampleStrat::None,
                )
            },
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct ItemImagesSpec(pub HashMap<ItemKey, ImageSpec>);
impl assets::Asset for ItemImagesSpec {
    type Loader = assets::RonLoader;

    const EXTENSION: &'static str = "ron";
}
impl Concatenate for ItemImagesSpec {
    fn concatenate(self, b: Self) -> Self { ItemImagesSpec(self.0.concatenate(b.0)) }
}

// TODO: when there are more images don't load them all into memory
pub struct ItemImgs {
    map: HashMap<ItemKey, Id>,
    manifest: AssetHandle<ItemImagesSpec>,
    watcher: ReloadWatcher,
    not_found: Id,
}

impl ItemImgs {
    pub fn new(ui: &mut Ui, not_found: Id) -> Self {
        let manifest = ItemImagesSpec::load_expect_combined_static("voxygen.item_image_manifest");
        let map = manifest
            .read()
            .0
            .iter()
            // TODO: what if multiple kinds map to the same image, it would be nice to use the same
            // image id for both, although this does interfere with the current hot-reloading
            // strategy
            .map(|(kind, spec)| (kind.clone(), ui.add_graphic(spec.create_graphic())))
            .collect();

        Self {
            map,
            manifest,
            watcher: manifest.reload_watcher(),
            not_found,
        }
    }

    /// Checks if the manifest has been changed and reloads the images if so
    /// Reuses img ids
    pub fn reload_if_changed(&mut self, ui: &mut Ui) {
        if self.watcher.reloaded() {
            for (kind, spec) in self.manifest.read().0.iter() {
                // Load new graphic
                let graphic = spec.create_graphic();
                // See if we already have an id we can use
                match self.map.get(kind) {
                    Some(id) => ui.replace_graphic(*id, graphic),
                    // Otherwise, generate new id and insert it into our Id -> ItemKey map
                    None => {
                        self.map.insert(kind.clone(), ui.add_graphic(graphic));
                    },
                }
            }
        }
    }

    pub fn img_ids(&self, item_key: ItemKey) -> Vec<Id> {
        if let ItemKey::TagExamples(keys, _) = item_key {
            return keys
                .iter()
                .filter_map(|k| self.map.get(k))
                .cloned()
                .collect();
        };
        match self.map.get(&item_key) {
            Some(id) => vec![*id],
            // There was no specification in the ron
            None => {
                warn!(
                    ?item_key,
                    "missing specified image file (note: hot-reloading won't work here)",
                );
                Vec::new()
            },
        }
    }

    pub fn img_ids_or_not_found_img(&self, item_key: ItemKey) -> Vec<Id> {
        let mut ids = self.img_ids(item_key);
        if ids.is_empty() {
            ids.push(self.not_found)
        }
        ids
    }
}

// Copied from figure/load.rs
// TODO: remove code dup?
fn graceful_load_vox(specifier: &str) -> AssetHandle<DotVoxAsset> {
    let full_specifier: String = ["voxygen.", specifier].concat();
    match DotVoxAsset::load(full_specifier.as_str()) {
        Ok(dot_vox) => dot_vox,
        Err(_) => {
            error!(?full_specifier, "Could not load vox file for item images",);
            DotVoxAsset::load_expect("voxygen.voxel.not_found")
        },
    }
}
fn graceful_load_img(specifier: &str) -> Arc<DynamicImage> {
    let full_specifier: String = ["voxygen.", specifier].concat();
    let handle = match assets::Image::load(&full_specifier) {
        Ok(img) => img,
        Err(_) => {
            error!(?full_specifier, "Could not load image file for item images");
            assets::Image::load_expect("voxygen.element.not_found")
        },
    };
    handle.read().to_image()
}

fn graceful_load_segment_no_skin(specifier: &str, model_index: u32) -> Arc<Segment> {
    use common::figure::{mat_cell::MatCell, MatSegment};
    let mat_seg = MatSegment::from_vox_model_index(
        &graceful_load_vox(specifier).read().0,
        model_index as usize,
    );
    let seg = mat_seg
        .map(|mat_cell| match mat_cell {
            MatCell::None => None,
            MatCell::Mat(_) => Some(MatCell::None),
            MatCell::Normal(data) => data.is_hollow().then_some(MatCell::None),
        })
        .to_segment(|_| Default::default());
    Arc::new(seg)
}