CMapEditManager.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * CMapEditManager.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 "../GameConstants.h"
  12. #include "CMapOperation.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CGObjectInstance;
  15. class CTerrainViewPatternConfig;
  16. struct TerrainViewPattern;
  17. class CMap;
  18. /// The CMapUndoManager provides the functionality to save operations and undo/redo them.
  19. class DLL_LINKAGE CMapUndoManager : boost::noncopyable
  20. {
  21. public:
  22. CMapUndoManager();
  23. void undo();
  24. void redo();
  25. void clearAll();
  26. /// The undo redo limit is a number which says how many undo/redo items can be saved. The default
  27. /// value is 10. If the value is 0, no undo/redo history will be maintained.
  28. /// FIXME: unlimited undo please
  29. int getUndoRedoLimit() const;
  30. void setUndoRedoLimit(int value);
  31. const CMapOperation * peekRedo() const;
  32. const CMapOperation * peekUndo() const;
  33. void addOperation(std::unique_ptr<CMapOperation> && operation); /// Client code does not need to call this method.
  34. //poor man's signal
  35. void setUndoCallback(std::function<void(bool, bool)> functor);
  36. private:
  37. using TStack = std::list<std::unique_ptr<CMapOperation>>;
  38. void doOperation(TStack & fromStack, TStack & toStack, bool doUndo);
  39. const CMapOperation * peek(const TStack & stack) const;
  40. TStack undoStack;
  41. TStack redoStack;
  42. int undoRedoLimit;
  43. void onUndoRedo();
  44. std::function<void(bool allowUndo, bool allowRedo)> undoCallback;
  45. };
  46. /// The map edit manager provides functionality for drawing terrain and placing
  47. /// objects on the map.
  48. class DLL_LINKAGE CMapEditManager : boost::noncopyable
  49. {
  50. public:
  51. CMapEditManager(CMap * map);
  52. CMap * getMap();
  53. /// Clears the terrain. The free level is filled with water and the underground level with rock.
  54. void clearTerrain(CRandomGenerator * gen = nullptr);
  55. /// Draws terrain at the current terrain selection. The selection will be cleared automatically.
  56. void drawTerrain(TerrainId terType, CRandomGenerator * gen = nullptr);
  57. /// Draws roads at the current terrain selection. The selection will be cleared automatically.
  58. void drawRoad(RoadId roadType, CRandomGenerator * gen = nullptr);
  59. /// Draws rivers at the current terrain selection. The selection will be cleared automatically.
  60. void drawRiver(RiverId riverType, CRandomGenerator * gen = nullptr);
  61. void insertObject(CGObjectInstance * obj);
  62. void insertObjects(std::set<CGObjectInstance *> & objects);
  63. void moveObject(CGObjectInstance * obj, const int3 & pos);
  64. void removeObject(CGObjectInstance * obj);
  65. void removeObjects(std::set<CGObjectInstance *> & objects);
  66. CTerrainSelection & getTerrainSelection();
  67. CObjectSelection & getObjectSelection();
  68. CMapUndoManager & getUndoManager();
  69. private:
  70. void execute(std::unique_ptr<CMapOperation> && operation);
  71. CMap * map;
  72. CMapUndoManager undoManager;
  73. CRandomGenerator gen;
  74. CTerrainSelection terrainSel;
  75. CObjectSelection objectSel;
  76. };
  77. VCMI_LIB_NAMESPACE_END