CMapEditManager.h 3.0 KB

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