guiConvexShapeEditorCtrl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 _GUICONVEXSHAPEEDITORCTRL_H_
  23. #define _GUICONVEXSHAPEEDITORCTRL_H_
  24. #ifndef _EDITTSCTRL_H_
  25. #include "gui/worldEditor/editTSCtrl.h"
  26. #endif
  27. #ifndef _UNDO_H_
  28. #include "util/undo.h"
  29. #endif
  30. #ifndef _GIZMO_H_
  31. #include "gui/worldEditor/gizmo.h"
  32. #endif
  33. #ifndef _CONVEXSHAPE_H_
  34. #include "T3D/convexShape.h"
  35. #endif
  36. class GameBase;
  37. class GuiConvexEditorUndoAction;
  38. class ConvexEditorTool;
  39. class ConvexEditorCreateTool;
  40. class GuiConvexEditorCtrl : public EditTSCtrl
  41. {
  42. typedef EditTSCtrl Parent;
  43. friend class GuiConvexEditorUndoAction;
  44. public:
  45. GuiConvexEditorCtrl();
  46. virtual ~GuiConvexEditorCtrl();
  47. DECLARE_CONOBJECT( GuiConvexEditorCtrl );
  48. // SimObject
  49. bool onAdd() override;
  50. void onRemove() override;
  51. static void initPersistFields();
  52. // GuiControl
  53. bool onWake() override;
  54. void onSleep() override;
  55. void setVisible(bool value) override;
  56. // EditTSCtrl
  57. bool onKeyDown( const GuiEvent &event ) override;
  58. bool onKeyUp( const GuiEvent &event ) override;
  59. void get3DCursor( GuiCursor *&cursor, bool &visible, const Gui3DMouseEvent &event_ ) override;
  60. void on3DMouseDown( const Gui3DMouseEvent &event ) override;
  61. void on3DMouseUp( const Gui3DMouseEvent &event ) override;
  62. void on3DMouseMove( const Gui3DMouseEvent &event ) override;
  63. void on3DMouseDragged( const Gui3DMouseEvent &event ) override;
  64. void on3DMouseEnter( const Gui3DMouseEvent &event ) override;
  65. void on3DMouseLeave( const Gui3DMouseEvent &event ) override;
  66. void on3DRightMouseDown( const Gui3DMouseEvent &event ) override;
  67. void on3DRightMouseUp( const Gui3DMouseEvent &event ) override;
  68. void renderScene(const RectI & updateRect) override;
  69. void updateGizmo() override;
  70. void updateShape( ConvexShape *shape, S32 offsetFace = -1 );
  71. static void synchClientObject( const ConvexShape *serverConvex );
  72. void updateGizmoPos();
  73. bool setActiveTool( ConvexEditorTool *tool );
  74. void drawFacePlane( ConvexShape *shape, S32 faceId );
  75. void scaleFace( ConvexShape *shape, S32 faceId, Point3F scale );
  76. void translateFace( ConvexShape *shape, S32 faceId, const Point3F &displace );
  77. void updateModifiedFace( ConvexShape *shape, S32 faceId );
  78. bool isShapeValid( ConvexShape *shape );
  79. void setupShape( ConvexShape *shape );
  80. void setPivotPos( ConvexShape *shape, S32 faceId, const Gui3DMouseEvent &event );
  81. void cleanMatrix( MatrixF &mat );
  82. S32 getEdgeByPoints( ConvexShape *shape, S32 faceId, S32 pId0, S32 pId1 );
  83. bool getEdgesTouchingPoint( ConvexShape *shape, S32 faceId, S32 pId, Vector< U32 > &edgeIdxList, S32 excludeEdge = -1 );
  84. void hollowSelection();
  85. void hollowShape( ConvexShape *shape, F32 thickness );
  86. void recenterSelection();
  87. void recenterShape( ConvexShape *shape );
  88. void dropSelectionAtScreenCenter();
  89. void splitSelectedFace();
  90. SceneObject* createPolyhedralObject(const char* className, SceneObject* geometryProvider);
  91. ConvexShape* createConvexShapeFrom(SceneObject* polyObject);
  92. Point2F getSelectedFaceUVOffset();
  93. Point2F getSelectedFaceUVScale();
  94. const char* getSelectedFaceMaterial();
  95. bool getSelectedFaceHorzFlip();
  96. bool getSelectedFaceVertFlip();
  97. float getSelectedFaceZRot();
  98. void setSelectedFaceUVOffset(Point2F offset);
  99. void setSelectedFaceUVScale(Point2F offset);
  100. void setSelectedFaceMaterial(const char* materialName);
  101. void setSelectedFaceHorzFlip(bool flipped);
  102. void setSelectedFaceVertFlip(bool flipped);
  103. void setSelectedFaceZRot(float degrees);
  104. void toggleGridSnapping();
  105. bool getGridSnap() { return mGridSnap; };
  106. void setGridSnap(bool snap) { mGridSnap = snap; };
  107. void setGridSnapSize(float gridSize);
  108. void updateShape();
  109. float getGridSnapSize() { return mGridPlaneSize; }
  110. /// Interface with Tools.
  111. /// @{
  112. MatrixF getCameraMat() const { return mLastCameraQuery.cameraMatrix; }
  113. enum UndoType
  114. {
  115. ModifyShape = 0,
  116. CreateShape,
  117. DeleteShape,
  118. HollowShape
  119. };
  120. void submitUndo( UndoType type, ConvexShape *shape );
  121. void submitUndo( UndoType type, const Vector< ConvexShape* > &shape );
  122. /// @}
  123. bool hasSelection() const;
  124. void clearSelection();
  125. void setSelection( ConvexShape *shape, S32 faceId );
  126. void handleDeselect();
  127. bool handleEscape();
  128. bool handleDelete();
  129. bool handleTab();
  130. public:
  131. StringTableEntry mMaterialName;
  132. protected:
  133. void _prepRenderImage( SceneManager* sceneGraph, const SceneRenderState* sceneState );
  134. void _renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst );
  135. bool _cursorCast( const Gui3DMouseEvent &event, ConvexShape **hitShape, S32 *hitFace );
  136. static bool _cursorCastCallback( SceneObject* object );
  137. protected:
  138. bool mIsDirty;
  139. U32 mSavedGizmoFlags;
  140. Vector<SimObjectPtr<ConvexShape>> mSelectedBrushes;
  141. struct selectedFace
  142. {
  143. SimObjectPtr<ConvexShape> mOwnerBrush;
  144. U32 faceId;
  145. };
  146. Vector<selectedFace> mSelectedFaces;
  147. struct selectedVert
  148. {
  149. SimObjectPtr<ConvexShape> mOwnerBrush;
  150. U32 vertId;
  151. };
  152. Vector<selectedVert> mSelectedVerts;
  153. /// The selected ConvexShape.
  154. SimObjectPtr<ConvexShape> mConvexSEL;
  155. /// The highlighted ConvexShape ( mouse over ).
  156. SimObjectPtr<ConvexShape> mConvexHL;
  157. S32 mFaceSEL;
  158. S32 mFaceHL;
  159. MatrixF mFaceSavedXfm;
  160. ConvexShape::Geometry mSavedGeometry;
  161. Vector< MatrixF > mSavedSurfaces;
  162. Vector< MatrixF > mLastValidShape;
  163. Point3F mSavedPivotPos;
  164. bool mCtrlDown;
  165. bool mSavedUndo;
  166. bool mHasGeometry;
  167. bool mDragging;
  168. bool mMouseDown;
  169. bool mHasCopied;
  170. RayInfo mLastRayInfo;
  171. bool mGridSnap;
  172. Gui3DMouseEvent mMouseDownEvent;
  173. Point3F mGizmoMatOffset;
  174. Point3F mPivotPos;
  175. bool mUsingPivot;
  176. bool mSettingPivot;
  177. UndoAction *mLastUndo;
  178. UndoManager *mUndoManager;
  179. struct ConvexShapeProxy
  180. {
  181. ConvexShape* shapeProxy;
  182. SceneObject* targetObject;
  183. String targetObjectClass;
  184. bool dirty;
  185. };
  186. Vector<ConvexShapeProxy> mProxyObjects;
  187. ConvexEditorTool *mActiveTool;
  188. ConvexEditorCreateTool *mCreateTool;
  189. };
  190. class GuiConvexEditorUndoAction : public UndoAction
  191. {
  192. friend class GuiConvexEditorCtrl;
  193. public:
  194. GuiConvexEditorUndoAction( const UTF8* actionName ) : UndoAction( actionName ), mEditor(NULL), mObjId(0)
  195. {
  196. }
  197. GuiConvexEditorCtrl *mEditor;
  198. SimObjectId mObjId;
  199. Vector< MatrixF > mSavedSurfaces;
  200. MatrixF mSavedObjToWorld;
  201. Point3F mSavedScale;
  202. void undo() override;
  203. void redo() override { undo(); }
  204. };
  205. class ConvexEditorTool
  206. {
  207. public:
  208. enum EventResult
  209. {
  210. NotHandled = 0,
  211. Handled = 1,
  212. Done = 2,
  213. Failed = 3
  214. };
  215. ConvexEditorTool( GuiConvexEditorCtrl *editor )
  216. : mEditor( editor ), mDone( false ) {}
  217. virtual ~ConvexEditorTool() {}
  218. virtual void onActivated( ConvexEditorTool *prevTool ) {}
  219. virtual void onDeactivated( ConvexEditorTool *newTool ) {}
  220. virtual EventResult onKeyDown( const GuiEvent &event ) { return NotHandled; }
  221. virtual EventResult on3DMouseDown( const Gui3DMouseEvent &event ) { return NotHandled; }
  222. virtual EventResult on3DMouseUp( const Gui3DMouseEvent &event ) { return NotHandled; }
  223. virtual EventResult on3DMouseMove( const Gui3DMouseEvent &event ) { return NotHandled; }
  224. virtual EventResult on3DMouseDragged( const Gui3DMouseEvent &event ) { return NotHandled; }
  225. virtual EventResult on3DMouseEnter( const Gui3DMouseEvent &event ) { return NotHandled; }
  226. virtual EventResult on3DMouseLeave( const Gui3DMouseEvent &event ) { return NotHandled; }
  227. virtual EventResult on3DRightMouseDown( const Gui3DMouseEvent &event ) { return NotHandled; }
  228. virtual EventResult on3DRightMouseUp( const Gui3DMouseEvent &event ) { return NotHandled; }
  229. virtual void renderScene(const RectI & updateRect) {}
  230. virtual void render2D() {}
  231. bool isDone() { return mDone; }
  232. public:
  233. GuiConvexEditorCtrl *mEditor;
  234. protected:
  235. bool mDone;
  236. };
  237. class ConvexEditorCreateTool : public ConvexEditorTool
  238. {
  239. typedef ConvexEditorTool Parent;
  240. public:
  241. ConvexEditorCreateTool( GuiConvexEditorCtrl *editor );
  242. virtual ~ConvexEditorCreateTool() {}
  243. void onActivated( ConvexEditorTool *prevTool ) override;
  244. void onDeactivated( ConvexEditorTool *newTool ) override;
  245. EventResult on3DMouseDown( const Gui3DMouseEvent &event ) override;
  246. EventResult on3DMouseUp( const Gui3DMouseEvent &event ) override;
  247. EventResult on3DMouseMove( const Gui3DMouseEvent &event ) override;
  248. EventResult on3DMouseDragged( const Gui3DMouseEvent &event ) override;
  249. void renderScene(const RectI & updateRect) override;
  250. ConvexShape* extrudeShapeFromFace( ConvexShape *shape, S32 face );
  251. protected:
  252. S32 mStage;
  253. ConvexShape *mNewConvex;
  254. PlaneF mCreatePlane;
  255. MatrixF mTransform;
  256. Point3F mStart;
  257. Point3F mEnd;
  258. Point3F mPlaneSizes;
  259. };
  260. #endif // _GUICONVEXSHAPEEDITORCTRL_H_