terrainActions.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. class ThermalErosionAction : public TerrainAction
  233. {
  234. public:
  235. ThermalErosionAction(TerrainEditor * editor)
  236. : TerrainAction(editor)
  237. {
  238. }
  239. StringTableEntry getName(){return("thermalErode");}
  240. void process(Selection * sel, const Gui3DMouseEvent & event, bool selChanged, Type type);
  241. };
  242. class HydraulicErosionAction : public TerrainAction
  243. {
  244. public:
  245. HydraulicErosionAction(TerrainEditor* editor)
  246. : TerrainAction(editor)
  247. {
  248. }
  249. StringTableEntry getName() { return("hydraulicErode"); }
  250. void process(Selection* sel, const Gui3DMouseEvent& event, bool selChanged, Type type);
  251. };
  252. class TerrainScratchPad
  253. {
  254. public:
  255. F32 mBottom, mTop;
  256. TerrainScratchPad(): mBottom(F32_MAX), mTop(F32_MIN_EX){};
  257. ~TerrainScratchPad() { mContents.clear(); };
  258. void clear();
  259. class gridStub
  260. {
  261. public:
  262. gridStub(F32 height, U8 material) : mHeight(height), mMaterial(material) {};
  263. F32 mHeight;
  264. U8 mMaterial;
  265. };
  266. void addTile(F32 height, U8 material);
  267. U32 size() { return(mContents.size()); };
  268. gridStub* operator [](U32 index) { return mContents[index]; };
  269. private:
  270. Vector<gridStub*> mContents;
  271. };
  272. class copyAction : public TerrainAction
  273. {
  274. public:
  275. copyAction(TerrainEditor* editor)
  276. : TerrainAction(editor)
  277. {
  278. }
  279. StringTableEntry getName() { return("copy"); }
  280. void process(Selection* sel, const Gui3DMouseEvent& event, bool selChanged, Type type);
  281. };
  282. class pasteAction : public TerrainAction
  283. {
  284. public:
  285. pasteAction(TerrainEditor* editor)
  286. : TerrainAction(editor)
  287. {
  288. }
  289. StringTableEntry getName() { return("paste"); }
  290. void process(Selection* sel, const Gui3DMouseEvent& event, bool selChanged, Type type);
  291. };
  292. class pasteUpAction : public TerrainAction
  293. {
  294. public:
  295. pasteUpAction(TerrainEditor* editor)
  296. : TerrainAction(editor)
  297. {
  298. }
  299. StringTableEntry getName() { return("pasteUp"); }
  300. void process(Selection* sel, const Gui3DMouseEvent& event, bool selChanged, Type type);
  301. };
  302. class pasteDownAction : public TerrainAction
  303. {
  304. public:
  305. pasteDownAction(TerrainEditor* editor)
  306. : TerrainAction(editor)
  307. {
  308. }
  309. StringTableEntry getName() { return("pasteDown"); }
  310. void process(Selection* sel, const Gui3DMouseEvent& event, bool selChanged, Type type);
  311. };
  312. /// An undo action used to perform terrain wide smoothing.
  313. class TerrainSmoothAction : public UndoAction
  314. {
  315. typedef UndoAction Parent;
  316. protected:
  317. SimObjectId mTerrainId;
  318. U32 mSteps;
  319. F32 mFactor;
  320. Vector<U16> mUnsmoothedHeights;
  321. public:
  322. TerrainSmoothAction();
  323. // SimObject
  324. DECLARE_CONOBJECT( TerrainSmoothAction );
  325. static void initPersistFields();
  326. // UndoAction
  327. void undo() override;
  328. void redo() override;
  329. /// Performs the initial smoothing and stores
  330. /// the heighfield state for later undo.
  331. void smooth( TerrainBlock *terrain, F32 factor, U32 steps );
  332. };
  333. #endif // _TERRAINACTIONS_H_