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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#![allow(unused_variables)]

use crate::prelude::*;
use std::{cell::RefCell, fmt};

#[derive(Clone, Debug)]
pub struct FinalizedClosure {
    pub uri: String,
    pub cache: TextureCache,
}

// Convention: posX with a value of -1 indicates whole texture
#[derive(Clone, Debug)]
pub struct TextureCacheItem {
    pub filename: String,
    pub width: i32,
    pub height: i32,
    pub pos_x: i32,
    pub pos_y: i32,
    pub ptr: dx::Handle,
    // pub meta: GHashTable,
}

#[derive(Clone, Debug)]
pub struct TextureCacheMetaEntry {
    // pub ident: gpointer,
    pub texture: dx::Handle,
    // pub destroy_func: GDestroyNotify,
}

#[derive(Clone, Debug)]
pub struct TextureCacheProps {
    // pub parent: GObject,
// pub cache: GHashTable,
// pub is_uri: GRegex,
}
#[derive(Clone, Debug)]
pub struct TextureCache {
    props: RefCell<TextureCacheProps>,
}

impl TextureCache {
    /// get_default:
    ///
    /// Returns the default texture cache. This is owned by Mx and should not be
    /// unreferenced or freed.
    ///
    /// Returns: (transfer none): a TextureCache
    ///
    pub fn get_default() -> Option<TextureCache> {
        // assert_initialized_main_thread!();
        // unsafe { from_glib_none(ffi::texture_cache_get_default()) }
        unimplemented!()
    }
}

impl Object for TextureCache {}
impl Is<TextureCache> for TextureCache {}

impl AsRef<TextureCache> for TextureCache {
    fn as_ref(&self) -> &TextureCache {
        self
    }
}

pub trait TextureCacheExt: 'static {
    /// contains:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    ///
    /// Checks whether the given URI/path is contained within the texture
    /// cache.
    ///
    /// Returns: %true if the image exists, %false otherwise
    ///
    fn contains(&self, uri: &str) -> bool;

    /// contains_meta:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    /// @ident: A unique identifier
    ///
    /// Checks whether there are any textures associated with the given URI by
    /// the given identifier.
    ///
    /// Returns: %true if the data exists, %false otherwise
    ///
    //fn contains_meta(&self, uri: &str, ident: Option<Fundamental: Pointer>) -> bool;

    /// get_cogl_texture:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    ///
    /// Create a #CoglHandle representing a texture of the specified image. Adds
    /// the image to the cache if the image had not been previously loaded.
    /// Subsequent calls with the same image URI/path will return the #CoglHandle of
    /// the previously loaded image with an increased reference count.
    ///
    /// Returns: (transfer none): a #CoglHandle to the cached texture
    ///
    fn get_cogl_texture(&self, uri: &str) -> Option<dx::Handle>;

    /// get_meta_cogl_texture:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    /// @ident: A unique identifier
    ///
    /// Retrieves the #CoglHandle of the previously added image associated
    /// with the given unique identifier.
    ///
    /// See insert_meta()
    ///
    /// Returns: (transfer full): A #CoglHandle to a texture, with an added
    ///   reference. %None if no image was found.
    ///
    // fn get_meta_cogl_texture(&self, uri: &str, ident: Option<Fundamental: Pointer>) -> Option<dx::Handle>;

    /// get_size:
    /// @self: A #TextureCache
    ///
    /// Returns the number of items in the texture cache
    ///
    /// Returns: the current size of the cache
    ///
    fn get_size(&self) -> usize;

    /// insert:
    /// @self: A #TextureCache
    /// @uri: A URI or local file path
    /// @texture: A #CoglHandle to a texture
    ///
    /// Inserts a texture into the texture cache. This can be useful if you
    /// want to cache a texture from a custom or unhandled URI type, or you
    /// want to override a particular texture.
    ///
    /// If the image is already in the cache, this texture will replace it. A
    /// reference will be taken on the given texture.
    ///
    fn insert(&self, uri: &str, texture: dx::Handle);

    /// insert_meta:
    /// @self: A #TextureCache
    /// @uri: A URI or local file path
    /// @ident: A unique identifier
    /// @texture: A #CoglHandle to a texture
    /// @destroy_func: An optional destruction function for @ident
    ///
    /// Inserts a texture that's associated with a URI into the cache.
    /// If the metadata already exists for this URI, it will be replaced.
    ///
    /// This is useful if you have a widely used modification of an image,
    /// for example, an image with a border composited around it.
    ///
    // fn insert_meta(&self, uri: &str, ident: Option<Fundamental: Pointer>, texture: dx::Handle);

    fn load_cache(&self, filename: &str);
}

impl<O: Is<TextureCache>> TextureCacheExt for O {
    /// contains:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    ///
    /// Checks whether the given URI/path is contained within the texture
    /// cache.
    ///
    /// Returns: %true if the image exists, %false otherwise
    ///
    fn contains(&self, uri: &str) -> bool {
        let cache = self.as_ref();
        // cache.get_item (uri, false) ? true : false;
        unimplemented!()
    }

    /// contains_meta:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    /// @ident: A unique identifier
    ///
    /// Checks whether there are any textures associated with the given URI by
    /// the given identifier.
    ///
    /// Returns: %true if the data exists, %false otherwise
    ///
    //fn contains_meta(&self, uri: &str, ident: Option<Fundamental: Pointer>) -> bool {
    //    unsafe { TODO: call ffi:texture_cache_contains_meta() }
    //}

    /// get_cogl_texture:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    ///
    /// Create a #CoglHandle representing a texture of the specified image. Adds
    /// the image to the cache if the image had not been previously loaded.
    /// Subsequent calls with the same image URI/path will return the #CoglHandle of
    /// the previously loaded image with an increased reference count.
    ///
    /// Returns: (transfer none): a #CoglHandle to the cached texture
    ///
    fn get_cogl_texture(&self, uri: &str) -> Option<dx::Handle> {
        // unsafe { TODO: call ffi:texture_cache_get_cogl_texture() }
        unimplemented!()
    }

    /// get_meta_cogl_texture:
    /// @self: A #TextureCache
    /// @uri: A URI or path to an image file
    /// @ident: A unique identifier
    ///
    /// Retrieves the #CoglHandle of the previously added image associated
    /// with the given unique identifier.
    ///
    /// See insert_meta()
    ///
    /// Returns: (transfer full): A #CoglHandle to a texture, with an added
    ///   reference. %None if no image was found.
    ///
    // fn get_meta_cogl_texture(&self, uri: &str, ident: Option<Fundamental: Pointer>) -> Option<dx::Handle> {
    // unsafe { TODO: call ffi:texture_cache_get_meta_cogl_texture() }
    // unimplemented!()
    // }

    /// get_size:
    /// @self: A #TextureCache
    ///
    /// Returns the number of items in the texture cache
    ///
    /// Returns: the current size of the cache
    ///
    fn get_size(&self) -> usize {
        let cache = self.as_ref();
        // cache.cache.len()
        unimplemented!()
    }

    /// insert:
    /// @self: A #TextureCache
    /// @uri: A URI or local file path
    /// @texture: A #CoglHandle to a texture
    ///
    /// Inserts a texture into the texture cache. This can be useful if you
    /// want to cache a texture from a custom or unhandled URI type, or you
    /// want to override a particular texture.
    ///
    /// If the image is already in the cache, this texture will replace it. A
    /// reference will be taken on the given texture.
    ///
    fn insert(&self, uri: &str, texture: dx::Handle) {
        // unsafe { TODO: call ffi:texture_cache_insert() }
    }

    /// insert_meta:
    /// @self: A #TextureCache
    /// @uri: A URI or local file path
    /// @ident: A unique identifier
    /// @texture: A #CoglHandle to a texture
    /// @destroy_func: An optional destruction function for @ident
    ///
    /// Inserts a texture that's associated with a URI into the cache.
    /// If the metadata already exists for this URI, it will be replaced.
    ///
    /// This is useful if you have a widely used modification of an image,
    /// for example, an image with a border composited around it.
    ///
    //fn insert_meta(&self, uri: &str, ident: Option<Fundamental: Pointer>, texture: dx::Handle) {
    //    unsafe { TODO: call ffi:texture_cache_insert_meta() }
    //}

    fn load_cache(&self, filename: &str) {
        let cache = self.as_ref();

        // FILE *file;
        // TextureCacheItem *element;
        // TextureCacheItem head;
        // CoglHandle full_texture;

        // let file = fopen(filename, "rm");
        // if !file {
        //     return;
        // }

        // let ret = fread(&head, sizeof(TextureCacheItem), 1, file);
        // if ret < 0 {
        //     fclose (file);
        //     return;
        // }

        // // check if we already if this texture in the cache
        // if g_hash_table_lookup(cache.cache, head.filename) {
        //     /* skip it, we're done */
        //     fclose (file);
        //     return;
        // }

        // let full_texture = texture_cache_get_cogl_texture(self, head.filename);

        // if full_texture == COGL_INVALID_HANDLE {
        //     g_critical (G_STRLOC ": Error opening cache image file");
        //     fclose (file);
        //     return;
        // }

        // while !feof(file) {
        //     gchar *uri;

        //     element = texture_cache_item_new();
        //     ret = fread(element, sizeof(TextureCacheItem), 1, file);

        //     if ret < 1 {
        //         // end of file
        //         texture_cache_item_free(element);
        //         break;
        //     }

        //     uri = texture_cache_filename_to_uri(element.filename);
        //     if (!uri) {
        //         // Couldn't resolve path
        //         texture_cache_item_free(element);
        //         continue;
        //     }

        //     if g_hash_table_lookup (cache.cache, uri) {
        //         // URI is already in the cache....
        //         texture_cache_item_free(element);
        //     } else {
        //         element.ptr = cogl_texture_new_from_sub_texture (full_texture,
        //                                                             element.posX,
        //                                                             element.posY,
        //                                                             element.width,
        //                                                             element.height);
        //         g_hash_table_insert(cache.cache, uri, element);
        //     }
        // }

        // fclose(file);
    }
}

impl fmt::Display for TextureCache {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TextureCache")
    }
}