terrainEditor.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 _TERRAINEDITOR_H_
  23. #define _TERRAINEDITOR_H_
  24. #ifndef _EDITTSCTRL_H_
  25. #include "gui/worldEditor/editTSCtrl.h"
  26. #endif
  27. #ifndef _TERRDATA_H_
  28. #include "terrain/terrData.h"
  29. #endif
  30. #ifndef _UNDO_H_
  31. #include "util/undo.h"
  32. #endif
  33. // Each 2D grid position must be associated with a terrainBlock
  34. struct GridPoint
  35. {
  36. Point2I gridPos;
  37. TerrainBlock* terrainBlock;
  38. GridPoint() { gridPos.set(0, 0); terrainBlock = NULL; };
  39. };
  40. class GridInfo
  41. {
  42. public:
  43. GridPoint mGridPoint;
  44. U8 mMaterial;
  45. F32 mHeight;
  46. F32 mWeight;
  47. F32 mStartHeight;
  48. bool mPrimarySelect;
  49. bool mMaterialChanged;
  50. // hash table
  51. S32 mNext;
  52. S32 mPrev;
  53. };
  54. class Selection : public Vector<GridInfo>
  55. {
  56. private:
  57. StringTableEntry mName;
  58. BitSet32 mUndoFlags;
  59. // hash table
  60. S32 lookup(const Point2I & pos);
  61. void insert(GridInfo info);
  62. U32 getHashIndex(const Point2I & pos);
  63. bool validate();
  64. Vector<S32> mHashLists;
  65. U32 mHashListSize;
  66. public:
  67. Selection();
  68. virtual ~Selection();
  69. void reset();
  70. /// add unique grid info into the selection - test uniqueness by grid position
  71. bool add(const GridInfo &info);
  72. bool getInfo(Point2I pos, GridInfo & info);
  73. bool setInfo(GridInfo & info);
  74. bool remove(const GridInfo &info);
  75. void setName(StringTableEntry name);
  76. StringTableEntry getName(){return(mName);}
  77. F32 getAvgHeight();
  78. F32 getMinHeight();
  79. F32 getMaxHeight();
  80. };
  81. class TerrainEditor;
  82. class Brush : public Selection
  83. {
  84. protected:
  85. TerrainEditor * mTerrainEditor;
  86. Point2I mSize;
  87. GridPoint mGridPoint;
  88. Vector<S32> mRenderList;
  89. public:
  90. enum { MaxBrushDim = 40 };
  91. Brush(TerrainEditor * editor);
  92. virtual ~Brush(){};
  93. virtual const char *getType() const = 0;
  94. // Brush appears to intentionally bypass Selection's hash table, so we
  95. // override validate() here.
  96. bool validate() { return true; }
  97. void setPosition(const Point3F & pos);
  98. void setPosition(const Point2I & pos);
  99. const Point2I & getPosition();
  100. const GridPoint & getGridPoint();
  101. void setTerrain(TerrainBlock* terrain) { mGridPoint.terrainBlock = terrain; };
  102. Point2I getSize() const {return(mSize);}
  103. virtual void setSize(const Point2I & size){mSize = size;}
  104. void update();
  105. void render();
  106. virtual void rebuild() = 0;
  107. virtual void _renderOutline() = 0;
  108. };
  109. class BoxBrush : public Brush
  110. {
  111. public:
  112. BoxBrush(TerrainEditor * editor) : Brush(editor){}
  113. const char *getType() const { return "box"; }
  114. void rebuild();
  115. protected:
  116. void _renderOutline();
  117. };
  118. class EllipseBrush : public Brush
  119. {
  120. public:
  121. EllipseBrush(TerrainEditor * editor) : Brush(editor){}
  122. const char *getType() const { return "ellipse"; }
  123. void rebuild();
  124. protected:
  125. void _renderOutline();
  126. };
  127. class SelectionBrush : public Brush
  128. {
  129. public:
  130. SelectionBrush(TerrainEditor * editor);
  131. const char *getType() const { return "selection"; }
  132. void rebuild();
  133. void render(Vector<GFXVertexPC> & vertexBuffer, S32 & verts, S32 & elems, S32 & prims, const ColorF & inColorFull, const ColorF & inColorNone, const ColorF & outColorFull, const ColorF & outColorNone) const;
  134. void setSize(const Point2I &){}
  135. protected:
  136. void _renderOutline() {}
  137. };
  138. class TerrainAction;
  139. class TerrainEditor : public EditTSCtrl
  140. {
  141. // XA: This methods where added to replace the friend consoleMethods.
  142. public:
  143. void attachTerrain(TerrainBlock *terrBlock);
  144. void detachTerrain(TerrainBlock *terrBlock);
  145. S32 getTerrainBlockCount() {return mTerrainBlocks.size();}
  146. TerrainBlock* getTerrainBlock(S32 index);
  147. void getTerrainBlocksMaterialList(Vector<StringTableEntry>& list); // Returns consolidated list of all materials used on all terrain blocks
  148. void setBrushType(const char* type);
  149. const char* getBrushType() const;
  150. void setBrushSize(S32 w, S32 h);
  151. const char* getBrushPos();
  152. void setBrushPos(Point2I pos);
  153. void setAction(const char* action);
  154. const char* getActionName(U32 index);
  155. const char* getCurrentAction() const;
  156. S32 getNumActions();
  157. void processAction(const char* sAction);
  158. void resetSelWeights(bool clear);
  159. void clearSelection();
  160. S32 getNumTextures();
  161. void markEmptySquares();
  162. void mirrorTerrain(S32 mirrorIndex);
  163. TerrainBlock* getActiveTerrain() { return mActiveTerrain; };
  164. void scheduleGridUpdate() { mNeedsGridUpdate = true; }
  165. void scheduleMaterialUpdate() { mNeedsMaterialUpdate = true; }
  166. void setGridUpdateMinMax()
  167. {
  168. mGridUpdateMax.set( S32_MAX, S32_MAX );
  169. mGridUpdateMin.set( 0, 0 );
  170. }
  171. void submitMaterialUndo( String actionName );
  172. void onMaterialUndo( TerrainBlock *terr );
  173. private:
  174. typedef EditTSCtrl Parent;
  175. TerrainBlock* mActiveTerrain;
  176. // A list of all of the TerrainBlocks this editor can edit
  177. VectorPtr<TerrainBlock*> mTerrainBlocks;
  178. Point2I mGridUpdateMin;
  179. Point2I mGridUpdateMax;
  180. U32 mMouseDownSeq;
  181. /// If one of these flags when the terrainEditor goes to render
  182. /// an appropriate update method will be called on the terrain.
  183. /// This prevents unnecessary work from happening from directly
  184. /// within an editor event's process method.
  185. bool mNeedsGridUpdate;
  186. bool mNeedsMaterialUpdate;
  187. bool mMouseDown;
  188. PlaneF mMousePlane;
  189. Point3F mMousePos;
  190. Brush * mMouseBrush;
  191. bool mBrushChanged;
  192. bool mRenderBrush;
  193. F32 mBrushPressure;
  194. Point2I mBrushSize;
  195. F32 mBrushSoftness;
  196. Vector<TerrainAction *> mActions;
  197. TerrainAction * mCurrentAction;
  198. bool mInAction;
  199. Selection mDefaultSel;
  200. bool mSelectionLocked;
  201. S32 mPaintIndex;
  202. Selection * mCurrentSel;
  203. class TerrainEditorUndoAction : public UndoAction
  204. {
  205. public:
  206. TerrainEditorUndoAction( const UTF8* actionName )
  207. : UndoAction( actionName ),
  208. mTerrainEditor( NULL ),
  209. mSel( NULL )
  210. {
  211. }
  212. virtual ~TerrainEditorUndoAction()
  213. {
  214. delete mSel;
  215. }
  216. TerrainEditor *mTerrainEditor;
  217. Selection *mSel;
  218. virtual void undo();
  219. virtual void redo() { undo(); }
  220. };
  221. void submitUndo( Selection *sel );
  222. Selection *mUndoSel;
  223. class TerrainMaterialUndoAction : public UndoAction
  224. {
  225. public:
  226. TerrainMaterialUndoAction( const UTF8 *actionName )
  227. : UndoAction( actionName ),
  228. mEditor( NULL ),
  229. mTerrain( NULL )
  230. {
  231. }
  232. TerrainEditor *mEditor;
  233. TerrainBlock *mTerrain;
  234. Vector<U8> mLayerMap;
  235. Vector<TerrainMaterial*> mMaterials;
  236. virtual void undo();
  237. virtual void redo();
  238. };
  239. bool mIsDirty; // dirty flag for writing terrain.
  240. bool mIsMissionDirty; // dirty flag for writing mission.
  241. GFXStateBlockRef mStateBlock;
  242. public:
  243. TerrainEditor();
  244. ~TerrainEditor();
  245. // conversion functions
  246. // Returns true if the grid position is on the main tile
  247. bool isMainTile(const GridPoint & gPoint) const;
  248. // Takes a world point and find the "highest" terrain underneath it
  249. // Returns true if the returned GridPoint includes a valid terrain and grid position
  250. TerrainBlock* getTerrainUnderWorldPoint(const Point3F & wPos);
  251. // Converts a GridPoint to a world position
  252. bool gridToWorld(const GridPoint & gPoint, Point3F & wPos);
  253. bool gridToWorld(const Point2I & gPos, Point3F & wPos, TerrainBlock* terrain);
  254. // Converts a world position to a grid point
  255. // If the grid point does not have a TerrainBlock already it will find the nearest
  256. // terrian under the world position
  257. bool worldToGrid(const Point3F & wPos, GridPoint & gPoint);
  258. bool worldToGrid(const Point3F & wPos, Point2I & gPos, TerrainBlock* terrain = NULL);
  259. // Converts any point that is off of the main tile to its equivalent on the main tile
  260. // Returns true if the point was already on the main tile
  261. bool gridToCenter(const Point2I & gPos, Point2I & cPos) const;
  262. //bool getGridInfo(const Point3F & wPos, GridInfo & info);
  263. // Gets the grid info for a point on a TerrainBlock's grid
  264. bool getGridInfo(const GridPoint & gPoint, GridInfo & info);
  265. bool getGridInfo(const Point2I & gPos, GridInfo & info, TerrainBlock* terrain);
  266. // Returns a list of infos for all points on the terrain that are at that point in space
  267. void getGridInfos(const GridPoint & gPoint, Vector<GridInfo>& infos);
  268. void setGridInfo(const GridInfo & info, bool checkActive = false);
  269. void setGridInfoHeight(const GridInfo & info);
  270. void gridUpdateComplete( bool materialChanged = false );
  271. void materialUpdateComplete();
  272. void processActionTick(U32 sequence);
  273. TerrainBlock* collide(const Gui3DMouseEvent & event, Point3F & pos);
  274. void lockSelection(bool lock) { mSelectionLocked = lock; };
  275. Selection * getUndoSel(){return(mUndoSel);}
  276. Selection * getCurrentSel(){return(mCurrentSel);}
  277. void setCurrentSel(Selection * sel) { mCurrentSel = sel; }
  278. void resetCurrentSel() {mCurrentSel = &mDefaultSel; }
  279. S32 getPaintMaterialIndex() const { return mPaintIndex; }
  280. void setBrushPressure( F32 pressure );
  281. F32 getBrushPressure() const { return mBrushPressure; }
  282. void setBrushSoftness( F32 softness );
  283. F32 getBrushSoftness() const { return mBrushSoftness; }
  284. Point2I getBrushSize() { return(mBrushSize); }
  285. TerrainBlock* getTerrainBlock() const { return mActiveTerrain; }
  286. TerrainBlock* getClientTerrain( TerrainBlock *serverTerrain = NULL ) const;
  287. bool terrainBlockValid() { return(mActiveTerrain ? true : false); }
  288. void setDirty() { mIsDirty = true; }
  289. void setMissionDirty() { mIsMissionDirty = true; }
  290. TerrainAction * lookupAction(const char * name);
  291. private:
  292. // terrain interface functions
  293. // Returns the height at a grid point
  294. F32 getGridHeight(const GridPoint & gPoint);
  295. // Sets a height at a grid point
  296. void setGridHeight(const GridPoint & gPoint, const F32 height);
  297. ///
  298. U8 getGridMaterial( const GridPoint &gPoint ) const;
  299. ///
  300. void setGridMaterial( const GridPoint & gPoint, U8 index );
  301. // Gets the material group of a specific spot on a TerrainBlock's grid
  302. U8 getGridMaterialGroup(const GridPoint & gPoint);
  303. // Sets a material group for a spot on a TerrainBlock's grid
  304. void setGridMaterialGroup(const GridPoint & gPoint, U8 group);
  305. //
  306. void updateBrush(Brush & brush, const Point2I & gPos);
  307. //
  308. void renderSelection(const Selection & sel, const ColorF & inColorFull, const ColorF & inColorNone, const ColorF & outColorFull, const ColorF & outColorNone, bool renderFill, bool renderFrame);
  309. void renderBrush(const Brush & brush, const ColorF & inColorFull, const ColorF & inColorNone, const ColorF & outColorFull, const ColorF & outColorNone, bool renderFill, bool renderFrame);
  310. void renderBorder();
  311. public:
  312. // persist field data - these are dynamic
  313. bool mRenderBorder;
  314. F32 mBorderHeight;
  315. ColorI mBorderFillColor;
  316. ColorI mBorderFrameColor;
  317. bool mBorderLineMode;
  318. bool mSelectionHidden;
  319. bool mRenderVertexSelection;
  320. bool mRenderSolidBrush;
  321. bool mProcessUsesBrush;
  322. //
  323. F32 mAdjustHeightVal;
  324. F32 mSetHeightVal;
  325. F32 mScaleVal;
  326. F32 mSmoothFactor;
  327. F32 mNoiseFactor;
  328. S32 mMaterialGroup;
  329. F32 mSoftSelectRadius;
  330. StringTableEntry mSoftSelectFilter;
  331. StringTableEntry mSoftSelectDefaultFilter;
  332. F32 mAdjustHeightMouseScale;
  333. Point2I mMaxBrushSize;
  334. F32 mSlopeMinAngle;
  335. F32 mSlopeMaxAngle;
  336. public:
  337. // SimObject
  338. bool onAdd();
  339. void onDeleteNotify(SimObject * object);
  340. static void initPersistFields();
  341. // GuiControl
  342. bool onWake();
  343. void onSleep();
  344. // EditTSCtrl
  345. bool onInputEvent( const InputEventInfo & evt );
  346. void on3DMouseUp( const Gui3DMouseEvent & evt );
  347. void on3DMouseDown( const Gui3DMouseEvent & evt );
  348. void on3DMouseMove( const Gui3DMouseEvent & evt );
  349. void on3DMouseDragged( const Gui3DMouseEvent & evt );
  350. bool onMouseWheelUp( const GuiEvent & evt );
  351. bool onMouseWheelDown( const GuiEvent & evt );
  352. void get3DCursor( GuiCursor *&cursor, bool &visible, const Gui3DMouseEvent &evt );
  353. void onPreRender();
  354. void renderScene(const RectI & updateRect);
  355. void renderGui( Point2I offset, const RectI &updateRect );
  356. void updateGuiInfo();
  357. // Determine if the given grid point is valid within a non-wrap
  358. // around terrain.
  359. bool isPointInTerrain( const GridPoint & gPoint);
  360. /// Reorder material at the given index to the new position, changing the order in which it is rendered / blended
  361. void reorderMaterial( S32 index, S32 orderPos );
  362. //
  363. Point3F getMousePos(){return(mMousePos);};
  364. void renderPoints( const Vector<GFXVertexPCT> &pointList );
  365. DECLARE_CONOBJECT(TerrainEditor);
  366. };
  367. inline void TerrainEditor::setGridInfoHeight(const GridInfo & info)
  368. {
  369. setGridHeight(info.mGridPoint, info.mHeight);
  370. }
  371. #endif