guiConvexShapeEditorCtrl.h 8.4 KB

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