forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmi.rs
More file actions
178 lines (153 loc) · 5.3 KB
/
dmi.rs
File metadata and controls
178 lines (153 loc) · 5.3 KB
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
//! Editor-environment specific DMI (texture) handling.
use std::io;
use std::path::{Path, PathBuf};
use std::collections::hash_map::HashMap;
use std::sync::{Arc, RwLock};
use lodepng::{self, RGBA, Decoder, ColorType};
use gfx::{self, Factory as FactoryTrait};
use crate::{Factory, Texture};
type Rect = (u32, u32, u32, u32);
pub use dm::dmi::*;
// ----------------------------------------------------------------------------
// Icon file and metadata handling
pub struct IconCache {
base_path: PathBuf,
lock: RwLock<IconCacheInner>,
}
#[derive(Default)]
pub struct TextureCache {
textures: Vec<Option<Texture>>,
}
#[derive(Default)]
struct IconCacheInner {
icons: Vec<Arc<IconFile>>,
paths: HashMap<PathBuf, Option<usize>>,
}
impl IconCache {
pub fn new(base_path: &Path) -> IconCache {
IconCache {
base_path: base_path.to_owned(),
lock: Default::default(),
}
}
pub fn get_index(&self, relative_file_path: &Path) -> Option<usize> {
let existing = self
.lock
.read()
.expect("IconCache poisoned")
.paths
.get(relative_file_path)
.cloned();
// shouldn't be inlined or the lifetime of the lock will be extended
match existing {
// inner None = failure to load, don't keep trying every time
Some(existing) => existing,
None => match load(&self.base_path.join(relative_file_path)) {
Some(icon) => {
let arc = Arc::new(icon);
let mut lock = self.lock.write().expect("IconCache poisoned");
let i = lock.icons.len();
lock.icons.push(arc);
lock.paths.insert(relative_file_path.to_owned(), Some(i));
Some(i)
}
None => {
let mut lock = self.lock.write().expect("IconCache poisoned");
lock.paths.insert(relative_file_path.to_owned(), None);
None
}
},
}
}
pub fn get_icon(&self, id: usize) -> Arc<IconFile> {
self.lock.read().expect("IconCache poisoned").icons[id].clone()
}
pub fn len(&self) -> usize {
self.lock.read().expect("IconCache poisoned").icons.len()
}
/*pub fn clear(&mut self) {
self.map.clear();
}*/
}
impl TextureCache {
pub fn retrieve(&mut self, factory: &mut Factory, icons: &IconCache, id: usize) -> &Texture {
use crate::Fulfill;
if id >= self.textures.len() {
self.textures.resize(id + 1, None);
}
self.textures[id].fulfill(|| load_texture(factory, &icons.get_icon(id).bitmap))
}
pub fn clear(&mut self) {
self.textures.clear();
}
}
fn load(path: &Path) -> Option<IconFile> {
match IconFile::from_file(path) {
Ok(loaded) => Some(loaded),
Err(err) => {
eprintln!("error loading icon: {}\n {}", path.display(), err);
None
}
}
}
// ----------------------------------------------------------------------------
// Icon file and metadata handling
pub struct IconFile {
pub metadata: Metadata,
pub width: u32,
pub height: u32,
bitmap: lodepng::Bitmap<RGBA>,
}
impl IconFile {
pub fn from_file(path: &Path) -> io::Result<IconFile> {
let (bitmap, metadata) = Metadata::from_file(path)?;
Ok(IconFile {
metadata,
width: bitmap.width as u32,
height: bitmap.height as u32,
bitmap,
})
}
pub fn uv_of(&self, icon_state: &str, dir: Dir) -> Option<[f32; 4]> {
self.rect_of(icon_state, dir).map(|(x1, y1, w, h)| [
x1 as f32 / self.width as f32,
y1 as f32 / self.height as f32,
(x1 + w) as f32 / self.width as f32,
(y1 + h) as f32 / self.height as f32,
])
}
#[inline]
pub fn rect_of(&self, icon_state: &str, dir: Dir) -> Option<Rect> {
self.metadata.rect_of(self.width, icon_state, dir, 0)
}
}
pub fn texture_from_bytes(factory: &mut Factory, bytes: &[u8]) -> io::Result<Texture> {
let mut decoder = Decoder::new();
decoder.info_raw_mut().colortype = ColorType::RGBA;
decoder.info_raw_mut().set_bitdepth(8);
decoder.remember_unknown_chunks(false);
let bitmap = match decoder.decode(bytes) {
Ok(::lodepng::Image::RGBA(bitmap)) => bitmap,
Ok(_) => return Err(io::Error::new(io::ErrorKind::InvalidData, "not RGBA")),
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e)),
};
Ok(load_texture(factory, &bitmap))
}
pub fn load_texture(factory: &mut Factory, bitmap: &lodepng::Bitmap<RGBA>) -> Texture {
let width = bitmap.width;
let height = bitmap.height;
let mut new_buffer = Vec::with_capacity(4 * width * height);
for pixel in &bitmap.buffer {
new_buffer.push(pixel.r);
new_buffer.push(pixel.g);
new_buffer.push(pixel.b);
new_buffer.push(pixel.a);
}
let kind = gfx::texture::Kind::D2(width as u16, height as u16, gfx::texture::AaMode::Single);
let (_, view) = factory.create_texture_immutable_u8::<crate::ColorFormat>(
kind,
gfx::texture::Mipmap::Provided,
&[&new_buffer[..]]
).expect("create_texture_immutable_u8");
view
}