guiConvexShapeEditorCtrl.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. virtual bool onAdd();
  50. virtual void onRemove();
  51. static void initPersistFields();
  52. // GuiControl
  53. virtual bool onWake();
  54. virtual void onSleep();
  55. virtual void setVisible(bool value);
  56. // EditTSCtrl
  57. bool onKeyDown( const GuiEvent &event );
  58. bool onKeyUp( const GuiEvent &event );
  59. void get3DCursor( GuiCursor *&cursor, bool &visible, const Gui3DMouseEvent &event_ );
  60. void on3DMouseDown( const Gui3DMouseEvent &event );
  61. void on3DMouseUp( const Gui3DMouseEvent &event );
  62. void on3DMouseMove( const Gui3DMouseEvent &event );
  63. void on3DMouseDragged( const Gui3DMouseEvent &event );
  64. void on3DMouseEnter( const Gui3DMouseEvent &event );
  65. void on3DMouseLeave( const Gui3DMouseEvent &event );
  66. void on3DRightMouseDown( const Gui3DMouseEvent &event );
  67. void on3DRightMouseUp( const Gui3DMouseEvent &event );
  68. void renderScene(const RectI & updateRect);
  69. void updateGizmo();
  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. /// Interface with Tools.
  93. /// @{
  94. MatrixF getCameraMat() const { return mLastCameraQuery.cameraMatrix; }
  95. enum UndoType
  96. {
  97. ModifyShape = 0,
  98. CreateShape,
  99. DeleteShape,
  100. HollowShape
  101. };
  102. void submitUndo( UndoType type, ConvexShape *shape );
  103. void submitUndo( UndoType type, const Vector< ConvexShape* > &shape );
  104. /// @}
  105. bool hasSelection() const;
  106. void clearSelection();
  107. void setSelection( ConvexShape *shape, S32 faceId );
  108. void handleDeselect();
  109. bool handleEscape();
  110. bool handleDelete();
  111. bool handleTab();
  112. public:
  113. StringTableEntry mMaterialName;
  114. protected:
  115. void _prepRenderImage( SceneManager* sceneGraph, const SceneRenderState* sceneState );
  116. void _renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst );
  117. bool _cursorCast( const Gui3DMouseEvent &event, ConvexShape **hitShape, S32 *hitFace );
  118. static bool _cursorCastCallback( RayInfo* ri );
  119. protected:
  120. bool mIsDirty;
  121. U32 mSavedGizmoFlags;
  122. /// The selected ConvexShape.
  123. SimObjectPtr<ConvexShape> mConvexSEL;
  124. /// The highlighted ConvexShape ( mouse over ).
  125. SimObjectPtr<ConvexShape> mConvexHL;
  126. S32 mFaceSEL;
  127. S32 mFaceHL;
  128. MatrixF mFaceSavedXfm;
  129. ConvexShape::Geometry mSavedGeometry;
  130. Vector< MatrixF > mSavedSurfaces;
  131. Vector< MatrixF > mLastValidShape;
  132. Point3F mSavedPivotPos;
  133. bool mCtrlDown;
  134. bool mSavedUndo;
  135. bool mHasGeometry;
  136. bool mDragging;
  137. bool mMouseDown;
  138. bool mHasCopied;
  139. RayInfo mLastRayInfo;
  140. Gui3DMouseEvent mMouseDownEvent;
  141. Point3F mGizmoMatOffset;
  142. Point3F mPivotPos;
  143. bool mUsingPivot;
  144. bool mSettingPivot;
  145. UndoAction *mLastUndo;
  146. UndoManager *mUndoManager;
  147. struct ConvexShapeProxy
  148. {
  149. ConvexShape* shapeProxy;
  150. SceneObject* targetObject;
  151. String targetObjectClass;
  152. bool dirty;
  153. };
  154. Vector<ConvexShapeProxy> mProxyObjects;
  155. ConvexEditorTool *mActiveTool;
  156. ConvexEditorCreateTool *mCreateTool;
  157. };
  158. class GuiConvexEditorUndoAction : public UndoAction
  159. {
  160. friend class GuiConvexEditorCtrl;
  161. public:
  162. GuiConvexEditorUndoAction( const UTF8* actionName ) : UndoAction( actionName )
  163. {
  164. }
  165. GuiConvexEditorCtrl *mEditor;
  166. SimObjectId mObjId;
  167. Vector< MatrixF > mSavedSurfaces;
  168. MatrixF mSavedObjToWorld;
  169. Point3F mSavedScale;
  170. virtual void undo();
  171. virtual void redo() { undo(); }
  172. };
  173. class ConvexEditorTool
  174. {
  175. public:
  176. enum EventResult
  177. {
  178. NotHandled = 0,
  179. Handled = 1,
  180. Done = 2,
  181. Failed = 3
  182. };
  183. ConvexEditorTool( GuiConvexEditorCtrl *editor )
  184. : mEditor( editor ), mDone( false ) {}
  185. virtual ~ConvexEditorTool() {}
  186. virtual void onActivated( ConvexEditorTool *prevTool ) {}
  187. virtual void onDeactivated( ConvexEditorTool *newTool ) {}
  188. virtual EventResult onKeyDown( const GuiEvent &event ) { return NotHandled; }
  189. virtual EventResult on3DMouseDown( const Gui3DMouseEvent &event ) { return NotHandled; }
  190. virtual EventResult on3DMouseUp( const Gui3DMouseEvent &event ) { return NotHandled; }
  191. virtual EventResult on3DMouseMove( const Gui3DMouseEvent &event ) { return NotHandled; }
  192. virtual EventResult on3DMouseDragged( const Gui3DMouseEvent &event ) { return NotHandled; }
  193. virtual EventResult on3DMouseEnter( const Gui3DMouseEvent &event ) { return NotHandled; }
  194. virtual EventResult on3DMouseLeave( const Gui3DMouseEvent &event ) { return NotHandled; }
  195. virtual EventResult on3DRightMouseDown( const Gui3DMouseEvent &event ) { return NotHandled; }
  196. virtual EventResult on3DRightMouseUp( const Gui3DMouseEvent &event ) { return NotHandled; }
  197. virtual void renderScene(const RectI & updateRect) {}
  198. virtual void render2D() {}
  199. bool isDone() { return mDone; }
  200. public:
  201. GuiConvexEditorCtrl *mEditor;
  202. protected:
  203. bool mDone;
  204. };
  205. class ConvexEditorCreateTool : public ConvexEditorTool
  206. {
  207. typedef ConvexEditorTool Parent;
  208. public:
  209. ConvexEditorCreateTool( GuiConvexEditorCtrl *editor );
  210. virtual ~ConvexEditorCreateTool() {}
  211. virtual void onActivated( ConvexEditorTool *prevTool );
  212. virtual void onDeactivated( ConvexEditorTool *newTool );
  213. virtual EventResult on3DMouseDown( const Gui3DMouseEvent &event );
  214. virtual EventResult on3DMouseUp( const Gui3DMouseEvent &event );
  215. virtual EventResult on3DMouseMove( const Gui3DMouseEvent &event );
  216. virtual EventResult on3DMouseDragged( const Gui3DMouseEvent &event );
  217. virtual void renderScene(const RectI & updateRect);
  218. ConvexShape* extrudeShapeFromFace( ConvexShape *shape, S32 face );
  219. protected:
  220. S32 mStage;
  221. ConvexShape *mNewConvex;
  222. PlaneF mCreatePlane;
  223. MatrixF mTransform;
  224. Point3F mStart;
  225. Point3F mEnd;
  226. Point3F mPlaneSizes;
  227. };
  228. #endif // _GUICONVEXSHAPEEDITORCTRL_H_