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. //
  51. virtual void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) = 0;
  52. virtual bool useMouseBrush() { return(true); }
  53. };
  54. //------------------------------------------------------------------------------
  55. class SelectAction : public TerrainAction
  56. {
  57. public:
  58. SelectAction(TerrainEditor * editor) : TerrainAction(editor){};
  59. StringTableEntry getName(){return("select");}
  60. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  61. };
  62. class DeselectAction : public TerrainAction
  63. {
  64. public:
  65. DeselectAction(TerrainEditor * editor) : TerrainAction(editor){};
  66. StringTableEntry getName(){return("deselect");}
  67. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  68. };
  69. class ClearAction : public TerrainAction
  70. {
  71. public:
  72. ClearAction(TerrainEditor * editor) : TerrainAction(editor){};
  73. StringTableEntry getName(){return("clear");}
  74. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type) {};
  75. bool useMouseBrush() { mTerrainEditor->getCurrentSel()->reset(); return true; }
  76. };
  77. class SoftSelectAction : public TerrainAction
  78. {
  79. public:
  80. SoftSelectAction(TerrainEditor * editor) : TerrainAction(editor){};
  81. StringTableEntry getName(){return("softSelect");}
  82. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  83. Filter mFilter;
  84. };
  85. //------------------------------------------------------------------------------
  86. class OutlineSelectAction : public TerrainAction
  87. {
  88. public:
  89. OutlineSelectAction(TerrainEditor * editor) : TerrainAction(editor){};
  90. StringTableEntry getName(){return("outlineSelect");}
  91. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  92. bool useMouseBrush() { return(false); }
  93. private:
  94. Gui3DMouseEvent mLastEvent;
  95. };
  96. //------------------------------------------------------------------------------
  97. class PaintMaterialAction : public TerrainAction
  98. {
  99. public:
  100. PaintMaterialAction(TerrainEditor * editor) : TerrainAction(editor){}
  101. StringTableEntry getName(){return("paintMaterial");}
  102. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  103. };
  104. //------------------------------------------------------------------------------
  105. class ClearMaterialsAction : public TerrainAction
  106. {
  107. public:
  108. ClearMaterialsAction(TerrainEditor * editor) : TerrainAction(editor){}
  109. StringTableEntry getName(){return("clearMaterials");}
  110. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  111. };
  112. //------------------------------------------------------------------------------
  113. class RaiseHeightAction : public TerrainAction
  114. {
  115. public:
  116. RaiseHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  117. StringTableEntry getName(){return("raiseHeight");}
  118. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  119. };
  120. //------------------------------------------------------------------------------
  121. class LowerHeightAction : public TerrainAction
  122. {
  123. public:
  124. LowerHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  125. StringTableEntry getName(){return("lowerHeight");}
  126. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  127. };
  128. //------------------------------------------------------------------------------
  129. class SetHeightAction : public TerrainAction
  130. {
  131. public:
  132. SetHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  133. StringTableEntry getName(){return("setHeight");}
  134. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  135. };
  136. //------------------------------------------------------------------------------
  137. class SetEmptyAction : public TerrainAction
  138. {
  139. public:
  140. SetEmptyAction(TerrainEditor * editor) : TerrainAction(editor){}
  141. StringTableEntry getName(){return("setEmpty");}
  142. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  143. };
  144. //------------------------------------------------------------------------------
  145. class ClearEmptyAction : public TerrainAction
  146. {
  147. public:
  148. ClearEmptyAction(TerrainEditor * editor) : TerrainAction(editor){}
  149. StringTableEntry getName(){return("clearEmpty");}
  150. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  151. };
  152. //------------------------------------------------------------------------------
  153. class ScaleHeightAction : public TerrainAction
  154. {
  155. public:
  156. ScaleHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  157. StringTableEntry getName(){return("scaleHeight");}
  158. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  159. };
  160. //------------------------------------------------------------------------------
  161. class BrushAdjustHeightAction : public TerrainAction
  162. {
  163. public:
  164. BrushAdjustHeightAction(TerrainEditor* editor) : TerrainAction(editor) { mPreviousZ = 0.0f; }
  165. StringTableEntry getName(){return("brushAdjustHeight");}
  166. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  167. private:
  168. PlaneF mIntersectionPlane;
  169. Point3F mTerrainUpVector;
  170. F32 mPreviousZ;
  171. };
  172. class AdjustHeightAction : public BrushAdjustHeightAction
  173. {
  174. public:
  175. AdjustHeightAction(TerrainEditor * editor);
  176. StringTableEntry getName(){return("adjustHeight");}
  177. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  178. bool useMouseBrush() { return(false); }
  179. private:
  180. //
  181. Point3F mHitPos;
  182. Point3F mLastPos;
  183. SimObjectPtr<GuiCursor> mCursor;
  184. };
  185. //------------------------------------------------------------------------------
  186. class FlattenHeightAction : public TerrainAction
  187. {
  188. public:
  189. FlattenHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  190. StringTableEntry getName(){return("flattenHeight");}
  191. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  192. };
  193. class SmoothHeightAction : public TerrainAction
  194. {
  195. public:
  196. SmoothHeightAction(TerrainEditor * editor) : TerrainAction(editor){}
  197. StringTableEntry getName(){return("smoothHeight");}
  198. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  199. };
  200. class SmoothSlopeAction : public TerrainAction
  201. {
  202. public:
  203. SmoothSlopeAction(TerrainEditor * editor) : TerrainAction(editor){}
  204. StringTableEntry getName(){return("smoothSlope");}
  205. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  206. };
  207. class PaintNoiseAction : public TerrainAction
  208. {
  209. public:
  210. PaintNoiseAction( TerrainEditor *editor )
  211. : TerrainAction( editor ),
  212. mNoiseSize( 256 )
  213. {
  214. mNoise.setSeed( 5342219 );
  215. mNoiseData.setSize( mNoiseSize * mNoiseSize );
  216. mNoise.fBm( &mNoiseData, mNoiseSize, 12, 1.0f, 5.0f );
  217. //Vector<F32> scratch = mNoiseData;
  218. //mNoise.rigidMultiFractal( &mNoiseData, &scratch, TerrainBlock::BlockSize, 12, 1.0f, 5.0f );
  219. mNoise.getMinMax( &mNoiseData, &mMinMaxNoise.x, &mMinMaxNoise.y, mNoiseSize );
  220. mScale = 1.5f / ( mMinMaxNoise.x - mMinMaxNoise.y);
  221. }
  222. StringTableEntry getName() { return "paintNoise"; }
  223. void process( Selection *sel, const Gui3DMouseEvent &event, bool selChanged, Type type );
  224. protected:
  225. const U32 mNoiseSize;
  226. Noise2D mNoise;
  227. Vector<F32> mNoiseData;
  228. Point2F mMinMaxNoise;
  229. F32 mScale;
  230. };
  231. /*
  232. class ThermalErosionAction : public TerrainAction
  233. {
  234. public:
  235. ThermalErosionAction(TerrainEditor * editor)
  236. : TerrainAction(editor)
  237. {
  238. mNoise.setSeed( 1 );//Sim::getCurrentTime() );
  239. mNoiseData.setSize( TerrainBlock::BlockSize * TerrainBlock::BlockSize );
  240. mTerrainHeights.setSize( TerrainBlock::BlockSize * TerrainBlock::BlockSize );
  241. }
  242. StringTableEntry getName(){return("thermalErode");}
  243. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  244. Noise2D mNoise;
  245. Vector<F32> mNoiseData;
  246. Vector<F32> mTerrainHeights;
  247. };
  248. */
  249. /// An undo action used to perform terrain wide smoothing.
  250. class TerrainSmoothAction : public UndoAction
  251. {
  252. typedef UndoAction Parent;
  253. protected:
  254. SimObjectId mTerrainId;
  255. U32 mSteps;
  256. F32 mFactor;
  257. Vector<U16> mUnsmoothedHeights;
  258. public:
  259. TerrainSmoothAction();
  260. // SimObject
  261. DECLARE_CONOBJECT( TerrainSmoothAction );
  262. static void initPersistFields();
  263. // UndoAction
  264. virtual void undo();
  265. virtual void redo();
  266. /// Performs the initial smoothing and stores
  267. /// the heighfield state for later undo.
  268. void smooth( TerrainBlock *terrain, F32 factor, U32 steps );
  269. };
  270. #endif // _TERRAINACTIONS_H_