MapView.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * MapView.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "MapRendererContext.h"
  12. #include "../gui/CIntObject.h"
  13. #include "../lib/int3.h"
  14. class Canvas;
  15. class MapRenderer;
  16. class MapViewController;
  17. struct HeroAnimationState
  18. {
  19. ObjectInstanceID target;
  20. int3 tileFrom;
  21. int3 tileDest;
  22. double progress;
  23. };
  24. struct FadingAnimationState
  25. {
  26. ObjectInstanceID target;
  27. double progress;
  28. };
  29. class MapRendererContext : public IMapRendererContext
  30. {
  31. friend class MapViewController;
  32. boost::multi_array<MapObjectsList, 3> objects;
  33. Point tileSize = Point(32, 32);
  34. uint32_t animationTime = 0;
  35. boost::optional<HeroAnimationState> movementAnimation;
  36. boost::optional<HeroAnimationState> teleportAnimation;
  37. boost::optional<FadingAnimationState> fadeOutAnimation;
  38. boost::optional<FadingAnimationState> fadeInAnimation;
  39. public:
  40. MapRendererContext();
  41. void addObject(const CGObjectInstance * object);
  42. void addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest);
  43. void removeObject(const CGObjectInstance * object);
  44. int3 getMapSize() const final;
  45. bool isInMap(const int3 & coordinates) const final;
  46. bool isVisible(const int3 & coordinates) const override;
  47. const TerrainTile & getMapTile(const int3 & coordinates) const override;
  48. const MapObjectsList & getObjects(const int3 & coordinates) const override;
  49. const CGObjectInstance * getObject(ObjectInstanceID objectID) const override;
  50. const CGPath * currentPath() const override;
  51. size_t objectGroupIndex(ObjectInstanceID objectID) const override;
  52. Point objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const override;
  53. double objectTransparency(ObjectInstanceID objectID) const override;
  54. size_t objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const override;
  55. Point getTileSize() const override;
  56. bool showGrid() const override;
  57. };
  58. class MapViewModel
  59. {
  60. Point tileSize;
  61. Point viewCenter;
  62. Point viewDimensions;
  63. int mapLevel = 0;
  64. public:
  65. void setTileSize(const Point & newValue);
  66. void setViewCenter(const Point & newValue);
  67. void setViewDimensions(const Point & newValue);
  68. void setLevel(int newLevel);
  69. /// returns current size of map tile in pixels
  70. Point getSingleTileSize() const;
  71. /// returns center point of map view, in Map coordinates
  72. Point getMapViewCenter() const;
  73. /// returns total number of visible tiles
  74. Point getTilesVisibleDimensions() const;
  75. /// returns rect encompassing all visible tiles
  76. Rect getTilesTotalRect() const;
  77. /// returns required area in pixels of cache canvas
  78. Point getCacheDimensionsPixels() const;
  79. /// returns actual player-visible area
  80. Point getPixelsVisibleDimensions() const;
  81. /// returns area covered by specified tile in map cache
  82. Rect getCacheTileArea(const int3 & coordinates) const;
  83. /// returns area covered by specified tile in target view
  84. Rect getTargetTileArea(const int3 & coordinates) const;
  85. /// returns tile under specified position in target view
  86. int3 getTileAtPoint(const Point & position) const;
  87. /// returns currently visible map level
  88. int getLevel() const;
  89. };
  90. /// Class responsible for rendering of entire map view
  91. /// uses rendering parameters provided by owner class
  92. class MapViewCache
  93. {
  94. std::shared_ptr<MapViewModel> model;
  95. std::unique_ptr<Canvas> terrain;
  96. std::unique_ptr<MapRenderer> mapRenderer;
  97. Canvas getTile(const int3 & coordinates);
  98. void updateTile(const std::shared_ptr<MapRendererContext> & context, const int3 & coordinates);
  99. public:
  100. explicit MapViewCache(const std::shared_ptr<MapViewModel> & model);
  101. ~MapViewCache();
  102. /// updates internal terrain cache according to provided time delta
  103. void update(const std::shared_ptr<MapRendererContext> & context);
  104. /// renders updated terrain cache onto provided canvas
  105. void render(Canvas & target);
  106. };
  107. /// Class responsible for updating view state,
  108. /// such as its position and any animations
  109. class MapViewController : public IMapObjectObserver
  110. {
  111. std::shared_ptr<MapRendererContext> context;
  112. std::shared_ptr<MapViewModel> model;
  113. private:
  114. // IMapObjectObserver impl
  115. bool hasOngoingAnimations() override;
  116. void onObjectFadeIn(const CGObjectInstance * obj) override;
  117. void onObjectFadeOut(const CGObjectInstance * obj) override;
  118. void onObjectInstantAdd(const CGObjectInstance * obj) override;
  119. void onObjectInstantRemove(const CGObjectInstance * obj) override;
  120. void onHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest) override;
  121. void onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest) override;
  122. void onHeroRotated(const CGHeroInstance * obj, const int3 & from, const int3 & dest) override;
  123. public:
  124. MapViewController(std::shared_ptr<MapRendererContext> context, std::shared_ptr<MapViewModel> model);
  125. void setViewCenter(const int3 & position);
  126. void setViewCenter(const Point & position, int level);
  127. void setTileSize(const Point & tileSize);
  128. void update(uint32_t timeDelta);
  129. };
  130. /// Main map rendering class that mostly acts as container for component classes
  131. class MapView : public CIntObject
  132. {
  133. std::shared_ptr<MapViewModel> model;
  134. std::shared_ptr<MapRendererContext> context;
  135. std::unique_ptr<MapViewCache> tilesCache;
  136. std::shared_ptr<MapViewController> controller;
  137. std::shared_ptr<MapViewModel> createModel(const Point & dimensions) const;
  138. public:
  139. std::shared_ptr<const MapViewModel> getModel() const;
  140. std::shared_ptr<MapViewController> getController();
  141. MapView(const Point & offset, const Point & dimensions);
  142. void show(SDL_Surface * to) override;
  143. void showAll(SDL_Surface * to) override;
  144. };