A simple map viewer

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