scenelayer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * scenelayer.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 "../lib/int3.h"
  12. class MapSceneBase;
  13. class MapScene;
  14. class MapController;
  15. class MapHandler;
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CMap;
  18. class CGObjectInstance;
  19. VCMI_LIB_NAMESPACE_END
  20. class AbstractLayer : public QObject
  21. {
  22. Q_OBJECT
  23. public:
  24. AbstractLayer(MapSceneBase * s);
  25. virtual void update() = 0;
  26. void show(bool show);
  27. void redraw();
  28. void initialize(MapController & controller);
  29. protected:
  30. MapSceneBase * scene;
  31. CMap * map = nullptr;
  32. MapHandler * handler = nullptr;
  33. bool isShown = false;
  34. std::unique_ptr<QPixmap> pixmap;
  35. QPixmap emptyPixmap;
  36. private:
  37. std::unique_ptr<QGraphicsPixmapItem> item;
  38. };
  39. class GridLayer: public AbstractLayer
  40. {
  41. Q_OBJECT
  42. public:
  43. GridLayer(MapSceneBase * s);
  44. void update() override;
  45. };
  46. class PassabilityLayer: public AbstractLayer
  47. {
  48. Q_OBJECT
  49. public:
  50. PassabilityLayer(MapSceneBase * s);
  51. void update() override;
  52. };
  53. class SelectionTerrainLayer: public AbstractLayer
  54. {
  55. Q_OBJECT
  56. public:
  57. SelectionTerrainLayer(MapSceneBase* s);
  58. void update() override;
  59. void draw();
  60. void select(const int3 & tile);
  61. void erase(const int3 & tile);
  62. void clear();
  63. const std::set<int3> & selection() const;
  64. signals:
  65. void selectionMade(bool anythingSelected);
  66. private:
  67. std::set<int3> area;
  68. std::set<int3> areaAdd;
  69. std::set<int3> areaErase;
  70. void onSelection();
  71. };
  72. class TerrainLayer: public AbstractLayer
  73. {
  74. Q_OBJECT
  75. public:
  76. TerrainLayer(MapSceneBase * s);
  77. void update() override;
  78. void draw(bool onlyDirty = true);
  79. void setDirty(const int3 & tile);
  80. private:
  81. std::set<int3> dirty;
  82. };
  83. class ObjectsLayer: public AbstractLayer
  84. {
  85. Q_OBJECT
  86. public:
  87. ObjectsLayer(MapSceneBase * s);
  88. void update() override;
  89. void draw(bool onlyDirty = true);
  90. void setDirty(int x, int y);
  91. void setDirty(const CGObjectInstance * object);
  92. void setLockObject(const CGObjectInstance * object, bool lock);
  93. void unlockAll();
  94. private:
  95. std::set<const CGObjectInstance *> objDirty;
  96. std::set<const CGObjectInstance *> lockedObjects;
  97. std::set<int3> dirty;
  98. };
  99. class ObjectPickerLayer: public AbstractLayer
  100. {
  101. Q_OBJECT
  102. public:
  103. ObjectPickerLayer(MapSceneBase * s);
  104. void update() override;
  105. bool isVisible() const;
  106. template<class T>
  107. void highlight()
  108. {
  109. highlight([](const CGObjectInstance * o){ return dynamic_cast<T*>(o); });
  110. }
  111. void highlight(std::function<bool(const CGObjectInstance *)> predicate);
  112. void clear();
  113. void select(const CGObjectInstance *);
  114. void discard();
  115. signals:
  116. void selectionMade(const CGObjectInstance *);
  117. private:
  118. bool isActive = false;
  119. std::set<const CGObjectInstance *> possibleObjects;
  120. };
  121. class SelectionObjectsLayer: public AbstractLayer
  122. {
  123. Q_OBJECT
  124. public:
  125. enum SelectionMode
  126. {
  127. NOTHING, SELECTION, MOVEMENT
  128. };
  129. SelectionObjectsLayer(MapSceneBase* s);
  130. void update() override;
  131. void draw();
  132. CGObjectInstance * selectObjectAt(int x, int y, const CGObjectInstance * ignore = nullptr) const;
  133. void selectObjects(int x1, int y1, int x2, int y2);
  134. void selectObject(CGObjectInstance *, bool inform = true);
  135. void deselectObject(CGObjectInstance *);
  136. bool isSelected(const CGObjectInstance *) const;
  137. std::set<CGObjectInstance*> getSelection() const;
  138. void clear();
  139. void setLockObject(const CGObjectInstance * object, bool lock);
  140. void unlockAll();
  141. QPoint shift;
  142. std::shared_ptr<CGObjectInstance> newObject;
  143. //FIXME: magic number
  144. SelectionMode selectionMode = SelectionMode::NOTHING;
  145. signals:
  146. void selectionMade(bool anythingSelected);
  147. private:
  148. std::set<CGObjectInstance *> selectedObjects;
  149. std::set<const CGObjectInstance *> lockedObjects;
  150. void onSelection();
  151. };
  152. class MinimapLayer: public AbstractLayer
  153. {
  154. public:
  155. MinimapLayer(MapSceneBase * s);
  156. void update() override;
  157. };
  158. class MinimapViewLayer: public AbstractLayer
  159. {
  160. public:
  161. MinimapViewLayer(MapSceneBase * s);
  162. void setViewport(int x, int y, int w, int h);
  163. void draw();
  164. void update() override;
  165. int viewportX() const {return x;}
  166. int viewportY() const {return y;}
  167. int viewportWidth() const {return w;}
  168. int viewportHeight() const {return h;}
  169. private:
  170. int x = 0;
  171. int y = 0;
  172. int w = 1;
  173. int h = 1;
  174. };