terrainActions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TERRAINACTIONS_H_
  23. #define _TERRAINACTIONS_H_
  24. #ifndef _TERRAINEDITOR_H_
  25. #include "gui/worldEditor/terrainEditor.h"
  26. #endif
  27. #ifndef _GUIFILTERCTRL_H_
  28. #include "gui/editor/guiFilterCtrl.h"
  29. #endif
  30. #ifndef _UNDO_H_
  31. #include "util/undo.h"
  32. #endif
  33. #ifndef _NOISE2D_H_
  34. #include "util/noise2d.h"
  35. #endif
  36. class TerrainAction
  37. {
  38. protected:
  39. TerrainEditor * mTerrainEditor;
  40. public:
  41. virtual ~TerrainAction(){};
  42. TerrainAction(TerrainEditor * editor) : mTerrainEditor(editor){}
  43. virtual StringTableEntry getName() = 0;
  44. enum Type {
  45. Begin = 0,
  46. Update,
  47. End,
  48. Process
  49. };
  50. bool isValid(GridInfo tile);
  51. //
  52. virtual void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) = 0;
  53. virtual bool useMouseBrush() { return(true); }
  54. };
  55. //------------------------------------------------------------------------------
  56. class SelectAction : public TerrainAction
  57. {
  58. public:
  59. SelectAction(TerrainEditor * editor) : TerrainAction(editor){};
  60. StringTableEntry getName() override{return("select");}
  61. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  62. };
  63. class DeselectAction : public TerrainAction
  64. {
  65. public:
  66. DeselectAction(TerrainEditor * editor) : TerrainAction(editor){};
  67. StringTableEntry getName() override{return("deselect");}
  68. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  69. };
  70. class ClearAction : public TerrainAction
  71. {
  72. public:
  73. ClearAction(TerrainEditor * editor) : TerrainAction(editor){};
  74. StringTableEntry getName() override{return("clear");}
  75. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override {};
  76. bool useMouseBrush() override { mTerrainEditor->getCurrentSel()->reset(); return true; }
  77. };
  78. class SoftSelectAction : public TerrainAction
  79. {
  80. public:
  81. SoftSelectAction(TerrainEditor * editor) : TerrainAction(editor){};
  82. StringTableEntry getName() override{return("softSelect");}
  83. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  84. Filter mFilter;
  85. };
  86. //------------------------------------------------------------------------------
  87. class OutlineSelectAction : public TerrainAction
  88. {
  89. public:
  90. OutlineSelectAction(TerrainEditor * editor) : TerrainAction(editor){};
  91. StringTableEntry getName() override{return("outlineSelect");}
  92. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  93. bool useMouseBrush() override { return(false); }
  94. private:
  95. Gui3DMouseEvent mLastEvent;
  96. };
  97. //------------------------------------------------------------------------------
  98. class PaintMaterialAction : public TerrainAction
  99. {
  100. public:
  101. PaintMaterialAction(TerrainEditor * editor) : TerrainAction(editor){}
  102. StringTableEntry getName() override{return("paintMaterial");}
  103. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  104. };
  105. //------------------------------------------------------------------------------
  106. class ClearMaterialsAction : public TerrainAction
  107. {
  108. public:
  109. ClearMaterialsAction(TerrainEditor * editor) : TerrainAction(editor){}
  110. StringTableEntry getName() override{return("clearMaterials");}
  111. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  112. };
  113. //------------------------------------------------------------------------------
  114. class RaiseHeightAction : public TerrainAction
  115. {
  116. public:
  117. RaiseHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  118. StringTableEntry getName() override{return("raiseHeight");}
  119. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  120. };
  121. //------------------------------------------------------------------------------
  122. class LowerHeightAction : public TerrainAction
  123. {
  124. public:
  125. LowerHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  126. StringTableEntry getName() override{return("lowerHeight");}
  127. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  128. };
  129. //------------------------------------------------------------------------------
  130. class SetHeightAction : public TerrainAction
  131. {
  132. public:
  133. SetHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  134. StringTableEntry getName() override{return("setHeight");}
  135. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  136. };
  137. //------------------------------------------------------------------------------
  138. class SetEmptyAction : public TerrainAction
  139. {
  140. public:
  141. SetEmptyAction(TerrainEditor * editor) : TerrainAction(editor){}
  142. StringTableEntry getName() override{return("setEmpty");}
  143. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  144. };
  145. //------------------------------------------------------------------------------
  146. class ClearEmptyAction : public TerrainAction
  147. {
  148. public:
  149. ClearEmptyAction(TerrainEditor * editor) : TerrainAction(editor){}
  150. StringTableEntry getName() override{return("clearEmpty");}
  151. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  152. };
  153. //------------------------------------------------------------------------------
  154. class ScaleHeightAction : public TerrainAction
  155. {
  156. public:
  157. ScaleHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  158. StringTableEntry getName() override{return("scaleHeight");}
  159. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  160. };
  161. //------------------------------------------------------------------------------
  162. class BrushAdjustHeightAction : public TerrainAction
  163. {
  164. public:
  165. BrushAdjustHeightAction(TerrainEditor* editor) : TerrainAction(editor) { mPreviousZ = 0.0f; }
  166. StringTableEntry getName() override{return("brushAdjustHeight");}
  167. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  168. private:
  169. PlaneF mIntersectionPlane;
  170. Point3F mTerrainUpVector;
  171. F32 mPreviousZ;
  172. };
  173. class AdjustHeightAction : public BrushAdjustHeightAction
  174. {
  175. public:
  176. AdjustHeightAction(TerrainEditor * editor);
  177. StringTableEntry getName() override{return("adjustHeight");}
  178. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  179. bool useMouseBrush() override { return(false); }
  180. private:
  181. //
  182. Point3F mHitPos;
  183. Point3F mLastPos;
  184. SimObjectPtr<GuiCursor> mCursor;
  185. };
  186. //------------------------------------------------------------------------------
  187. class FlattenHeightAction : public TerrainAction
  188. {
  189. public:
  190. FlattenHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  191. StringTableEntry getName() override{return("flattenHeight");}
  192. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  193. };
  194. class SmoothHeightAction : public TerrainAction
  195. {
  196. public:
  197. SmoothHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  198. StringTableEntry getName() override{return("smoothHeight");}
  199. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  200. };
  201. class SmoothSlopeAction : public TerrainAction
  202. {
  203. public:
  204. SmoothSlopeAction(TerrainEditor * editor) : TerrainAction(editor){}
  205. StringTableEntry getName() override{return("smoothSlope");}
  206. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) override;
  207. };
  208. class PaintNoiseAction : public TerrainAction
  209. {
  210. public:
  211. PaintNoiseAction( TerrainEditor *editor )
  212. : TerrainAction( editor ),
  213. mNoiseSize( 256 )
  214. {
  215. mNoise.setSeed( 5342219 );
  216. mNoiseData.setSize( mNoiseSize * mNoiseSize );
  217. mNoise.fBm( &mNoiseData, mNoiseSize, 12, 1.0f, 5.0f );
  218. //Vector<F32> scratch = mNoiseData;
  219. //mNoise.rigidMultiFractal( &mNoiseData, &scratch, TerrainBlock::BlockSize, 12, 1.0f, 5.0f );
  220. mNoise.getMinMax( &mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize );
  221. mScale = 1.5f / ( mMinMaxNoise.x - mMinMaxNoise.y);
  222. }
  223. StringTableEntry getName() override { return "paintNoise"; }
  224. void process( Selection *sel, const Gui3DMouseEvent &event, bool selChanged, Type type ) override;
  225. protected:
  226. const U32 mNoiseSize;
  227. Noise2D mNoise;
  228. Vector<F32> mNoiseData;
  229. Point2F mMinMaxNoise;
  230. F32 mScale;
  231. };
  232. /*
  233. class ThermalErosionAction : public TerrainAction
  234. {
  235. public:
  236. ThermalErosionAction(TerrainEditor * editor)
  237. : TerrainAction(editor)
  238. {
  239. mNoise.setSeed( 1 );//Sim::getCurrentTime() );
  240. mNoiseData.setSize( TerrainBlock::BlockSize * TerrainBlock::BlockSize );
  241. mTerrainHeights.setSize( TerrainBlock::BlockSize * TerrainBlock::BlockSize );
  242. }
  243. StringTableEntry getName(){return("thermalErode");}
  244. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  245. Noise2D mNoise;
  246. Vector<F32> mNoiseData;
  247. Vector<F32> mTerrainHeights;
  248. };
  249. */
  250. /// An undo action used to perform terrain wide smoothing.
  251. class TerrainSmoothAction : public UndoAction
  252. {
  253. typedef UndoAction Parent;
  254. protected:
  255. SimObjectId mTerrainId;
  256. U32 mSteps;
  257. F32 mFactor;
  258. Vector<U16> mUnsmoothedHeights;
  259. public:
  260. TerrainSmoothAction();
  261. // SimObject
  262. DECLARE_CONOBJECT( TerrainSmoothAction );
  263. static void initPersistFields();
  264. // UndoAction
  265. void undo() override;
  266. void redo() override;
  267. /// Performs the initial smoothing and stores
  268. /// the heighfield state for later undo.
  269. void smooth( TerrainBlock *terrain, F32 factor, U32 steps );
  270. };
  271. #endif // _TERRAINACTIONS_H_