guiConvexShapeEditorCtrl.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. 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. void setGridSnapSize(float gridSize);
  106. void updateShape();
  107. float getGridSnapSize() { return mGridPlaneSize; }
  108. /// Interface with Tools.
  109. /// @{
  110. MatrixF getCameraMat() const { return mLastCameraQuery.cameraMatrix; }
  111. enum UndoType
  112. {
  113. ModifyShape = 0,
  114. CreateShape,
  115. DeleteShape,
  116. HollowShape
  117. };
  118. void submitUndo( UndoType type, ConvexShape *shape );
  119. void submitUndo( UndoType type, const Vector< ConvexShape* > &shape );
  120. /// @}
  121. bool hasSelection() const;
  122. void clearSelection();
  123. void setSelection( ConvexShape *shape, S32 faceId );
  124. void handleDeselect();
  125. bool handleEscape();
  126. bool handleDelete();
  127. bool handleTab();
  128. public:
  129. StringTableEntry mMaterialName;
  130. protected:
  131. void _prepRenderImage( SceneManager* sceneGraph, const SceneRenderState* sceneState );
  132. void _renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst );
  133. bool _cursorCast( const Gui3DMouseEvent &event, ConvexShape **hitShape, S32 *hitFace );
  134. static bool _cursorCastCallback( RayInfo* ri );
  135. protected:
  136. bool mIsDirty;
  137. U32 mSavedGizmoFlags;
  138. Vector<SimObjectPtr<ConvexShape>> mSelectedBrushes;
  139. struct selectedFace
  140. {
  141. SimObjectPtr<ConvexShape> mOwnerBrush;
  142. U32 faceId;
  143. };
  144. Vector<selectedFace> mSelectedFaces;
  145. struct selectedVert
  146. {
  147. SimObjectPtr<ConvexShape> mOwnerBrush;
  148. U32 vertId;
  149. };
  150. Vector<selectedVert> mSelectedVerts;
  151. /// The selected ConvexShape.
  152. SimObjectPtr<ConvexShape> mConvexSEL;
  153. /// The highlighted ConvexShape ( mouse over ).
  154. SimObjectPtr<ConvexShape> mConvexHL;
  155. S32 mFaceSEL;
  156. S32 mFaceHL;
  157. MatrixF mFaceSavedXfm;
  158. ConvexShape::Geometry mSavedGeometry;
  159. Vector< MatrixF > mSavedSurfaces;
  160. Vector< MatrixF > mLastValidShape;
  161. Point3F mSavedPivotPos;
  162. bool mCtrlDown;
  163. bool mSavedUndo;
  164. bool mHasGeometry;
  165. bool mDragging;
  166. bool mMouseDown;
  167. bool mHasCopied;
  168. RayInfo mLastRayInfo;
  169. bool mGridSnap;
  170. Gui3DMouseEvent mMouseDownEvent;
  171. Point3F mGizmoMatOffset;
  172. Point3F mPivotPos;
  173. bool mUsingPivot;
  174. bool mSettingPivot;
  175. UndoAction *mLastUndo;
  176. UndoManager *mUndoManager;
  177. struct ConvexShapeProxy
  178. {
  179. ConvexShape* shapeProxy;
  180. SceneObject* targetObject;
  181. String targetObjectClass;
  182. bool dirty;
  183. };
  184. Vector<ConvexShapeProxy> mProxyObjects;
  185. ConvexEditorTool *mActiveTool;
  186. ConvexEditorCreateTool *mCreateTool;
  187. };
  188. class GuiConvexEditorUndoAction : public UndoAction
  189. {
  190. friend class GuiConvexEditorCtrl;
  191. public:
  192. GuiConvexEditorUndoAction( const UTF8* actionName ) : UndoAction( actionName ), mEditor(NULL), mObjId(0)
  193. {
  194. }
  195. GuiConvexEditorCtrl *mEditor;
  196. SimObjectId mObjId;
  197. Vector< MatrixF > mSavedSurfaces;
  198. MatrixF mSavedObjToWorld;
  199. Point3F mSavedScale;
  200. virtual void undo();
  201. virtual void redo() { undo(); }
  202. };
  203. class ConvexEditorTool
  204. {
  205. public:
  206. enum EventResult
  207. {
  208. NotHandled = 0,
  209. Handled = 1,
  210. Done = 2,
  211. Failed = 3
  212. };
  213. ConvexEditorTool( GuiConvexEditorCtrl *editor )
  214. : mEditor( editor ), mDone( false ) {}
  215. virtual ~ConvexEditorTool() {}
  216. virtual void onActivated( ConvexEditorTool *prevTool ) {}
  217. virtual void onDeactivated( ConvexEditorTool *newTool ) {}
  218. virtual EventResult onKeyDown( const GuiEvent &event ) { return NotHandled; }
  219. virtual EventResult on3DMouseDown( const Gui3DMouseEvent &event ) { return NotHandled; }
  220. virtual EventResult on3DMouseUp( const Gui3DMouseEvent &event ) { return NotHandled; }
  221. virtual EventResult on3DMouseMove( const Gui3DMouseEvent &event ) { return NotHandled; }
  222. virtual EventResult on3DMouseDragged( const Gui3DMouseEvent &event ) { return NotHandled; }
  223. virtual EventResult on3DMouseEnter( const Gui3DMouseEvent &event ) { return NotHandled; }
  224. virtual EventResult on3DMouseLeave( const Gui3DMouseEvent &event ) { return NotHandled; }
  225. virtual EventResult on3DRightMouseDown( const Gui3DMouseEvent &event ) { return NotHandled; }
  226. virtual EventResult on3DRightMouseUp( const Gui3DMouseEvent &event ) { return NotHandled; }
  227. virtual void renderScene(const RectI & updateRect) {}
  228. virtual void render2D() {}
  229. bool isDone() { return mDone; }
  230. public:
  231. GuiConvexEditorCtrl *mEditor;
  232. protected:
  233. bool mDone;
  234. };
  235. class ConvexEditorCreateTool : public ConvexEditorTool
  236. {
  237. typedef ConvexEditorTool Parent;
  238. public:
  239. ConvexEditorCreateTool( GuiConvexEditorCtrl *editor );
  240. virtual ~ConvexEditorCreateTool() {}
  241. virtual void onActivated( ConvexEditorTool *prevTool );
  242. virtual void onDeactivated( ConvexEditorTool *newTool );
  243. virtual EventResult on3DMouseDown( const Gui3DMouseEvent &event );
  244. virtual EventResult on3DMouseUp( const Gui3DMouseEvent &event );
  245. virtual EventResult on3DMouseMove( const Gui3DMouseEvent &event );
  246. virtual EventResult on3DMouseDragged( const Gui3DMouseEvent &event );
  247. virtual void renderScene(const RectI & updateRect);
  248. ConvexShape* extrudeShapeFromFace( ConvexShape *shape, S32 face );
  249. protected:
  250. S32 mStage;
  251. ConvexShape *mNewConvex;
  252. PlaneF mCreatePlane;
  253. MatrixF mTransform;
  254. Point3F mStart;
  255. Point3F mEnd;
  256. Point3F mPlaneSizes;
  257. };
  258. #endif // _GUICONVEXSHAPEEDITORCTRL_H_