CMapEditManager.h 3.0 KB

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