A simple map viewer

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use image;
  2. use linked_hash_map::{Entry, LinkedHashMap};
  3. use coord::TileCoord;
  4. use tile::Tile;
  5. use tile_loader::TileLoader;
  6. use tile_source::TileSource;
  7. pub struct TileCache {
  8. loader: TileLoader,
  9. map: LinkedHashMap<Tile, image::DynamicImage>,
  10. max_tiles: usize,
  11. }
  12. impl TileCache {
  13. pub fn new<F>(new_tile_func: F) -> Self
  14. where F: Fn(Tile) + Sync + Send + 'static,
  15. {
  16. TileCache {
  17. loader: TileLoader::new(move |tile| {
  18. new_tile_func(tile);
  19. }),
  20. map: LinkedHashMap::new(),
  21. max_tiles: 512, //TODO set a reasonable value
  22. }
  23. }
  24. pub fn get_sync(
  25. &mut self,
  26. tile_coord: TileCoord,
  27. source: &TileSource,
  28. write_to_file: bool,
  29. ) -> Option<&image::DynamicImage>
  30. {
  31. let tile = Tile::new(tile_coord, source.id());
  32. //TODO Return the value from get_refresh with borrowck agreeing that this is OK.
  33. self.map.get_refresh(&tile);
  34. // remove old cache entries
  35. while self.map.len() + 1 > self.max_tiles {
  36. self.map.pop_front();
  37. }
  38. match self.map.entry(tile) {
  39. Entry::Occupied(entry) => {
  40. Some(entry.into_mut())
  41. },
  42. Entry::Vacant(entry) => {
  43. self.loader.get_sync(tile_coord, source, write_to_file).map(|img| entry.insert(img) as &_)
  44. },
  45. }
  46. }
  47. pub fn get_async(
  48. &mut self,
  49. tile_coord: TileCoord,
  50. source: &TileSource,
  51. write_to_file: bool,
  52. ) -> Option<&image::DynamicImage>
  53. {
  54. while let Some((t, img)) = self.loader.async_result() {
  55. // remove old cache entries
  56. while self.map.len() + 1 > self.max_tiles {
  57. self.map.pop_front();
  58. }
  59. self.map.insert(t, img);
  60. println!("CACHE SIZE: {} tiles", self.map.len());
  61. }
  62. let tile = Tile::new(tile_coord, source.id());
  63. //TODO Return the value from get_refresh with borrowck agreeing that this is OK.
  64. self.map.get_refresh(&tile);
  65. match self.map.entry(tile) {
  66. Entry::Occupied(entry) => Some(entry.into_mut()),
  67. Entry::Vacant(_) => {
  68. self.loader.async_request(tile_coord, source, write_to_file);
  69. None
  70. }
  71. }
  72. }
  73. // Return a tile from the cache but do not use TileLoader.
  74. pub fn lookup(&mut self, tile: Tile) -> Option<&image::DynamicImage> {
  75. //TODO Return the value from get_refresh with borrowck agreeing that this is OK.
  76. self.map.get_refresh(&tile);
  77. self.map.get(&tile)
  78. }
  79. }
  80. impl ::std::fmt::Debug for TileCache {
  81. fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
  82. write!(
  83. f,
  84. "TileCache {{ tiles: {:?} }}",
  85. self.map.keys().collect::<Vec<_>>()
  86. )
  87. }
  88. }