VPathEditor.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #ifndef _VT_VPATHEDITOR_H_
  24. #define _VT_VPATHEDITOR_H_
  25. #ifndef _EDITTSCTRL_H_
  26. #include "gui/worldEditor/editTSCtrl.h"
  27. #endif
  28. #ifndef _VT_VPATH_H_
  29. #include "VPath.h"
  30. #endif
  31. #ifndef _UNDO_H_
  32. #include "util/undo.h"
  33. #endif
  34. //-----------------------------------------------------------------------------
  35. class VPathEditor : public EditTSCtrl
  36. {
  37. typedef EditTSCtrl Parent;
  38. public:
  39. enum RenderType
  40. {
  41. k_RenderSegments,
  42. k_RenderNodes,
  43. };
  44. enum EditMode
  45. {
  46. k_Gizmo,
  47. k_AddNode,
  48. k_DeleteNode
  49. };
  50. struct Selection
  51. {
  52. Selection( void ) :
  53. Path( NULL ),
  54. Node( -1 )
  55. {
  56. TangentHandle[0].zero();
  57. TangentHandle[1].zero();
  58. };
  59. VPath *Path;
  60. S32 Node;
  61. Point3F TangentHandle[2];
  62. };
  63. struct PathEditAction
  64. {
  65. PathEditAction( void ) :
  66. Dirty( false ),
  67. Transform( true )
  68. {
  69. // Void.
  70. };
  71. bool Dirty;
  72. MatrixF Transform;
  73. };
  74. struct NodeEditAction
  75. {
  76. NodeEditAction( void ) :
  77. Dirty( false ),
  78. Position( 0.f, 0.f, 0.f ),
  79. Rotation( 0.f, 0.f, 0.f, 0.f ),
  80. Weight( 0.f )
  81. {
  82. // Void.
  83. };
  84. bool Dirty;
  85. Point3F Position;
  86. QuatF Rotation;
  87. F32 Weight;
  88. };
  89. bool mIsDirty;
  90. EditMode mEditMode;
  91. Selection mSelection;
  92. PathEditAction mPathEdit;
  93. NodeEditAction mNodeEdit;
  94. bool mEditWeight;
  95. S32 mEditWeightHandle;
  96. GFXStateBlockRef mStateBlock;
  97. public:
  98. VPathEditor( void );
  99. virtual bool onAdd( void );
  100. virtual bool onWake( void );
  101. static void initPersistFields( void );
  102. // Gui Events.
  103. virtual void on3DMouseDown( const Gui3DMouseEvent &pEvent );
  104. virtual void on3DMouseUp( const Gui3DMouseEvent &pEvent );
  105. virtual void on3DMouseMove( const Gui3DMouseEvent &pEvent );
  106. virtual void on3DMouseDragged( const Gui3DMouseEvent &pEvent );
  107. // Render Methods.
  108. virtual void setStateBlock( void );
  109. virtual void renderScene( const RectI &pUpdateRect );
  110. void renderPaths( const RenderType &pRenderType );
  111. void renderPath( const RenderType &pRenderType, VPath *pPath, const ColorI &pColor );
  112. void renderLinearPath( VPath *pPath, const ColorI &pColor );
  113. void renderBezierPath( VPath *pPath, const ColorI &pColor );
  114. DECLARE_CONOBJECT( VPathEditor );
  115. public:
  116. // Reference Methods.
  117. VPath *getClientPath( VPath *pPath );
  118. // Selection Methods.
  119. inline bool isValidSelection( void ) { return ( mSelection.Path != NULL && mSelection.Node != -1 ); };
  120. bool updateSelection( const Gui3DMouseEvent &pEvent );
  121. void updateSelection( VPath *pPathObject, const S32 &pNodeIndex );
  122. void updateSelection( void );
  123. // Weight Editing.
  124. bool isEditingWeight( const Gui3DMouseEvent &pEvent );
  125. inline bool isEditingWeight( void ) { return mEditWeight; };
  126. void updateWeight( const Gui3DMouseEvent &pEvent );
  127. // Path Editor.
  128. bool getPointOnPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform );
  129. bool getPointOnLinearPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform );
  130. bool getPointOnBezierPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform );
  131. void setPathPosition( const Point3F &pPosition );
  132. void setPathRotation( const QuatF &pRotation );
  133. void setPathTransform( const MatrixF &pTransform );
  134. void setPathScale( const VectorF &pScale );
  135. // Node Editing.
  136. void addNode( const Gui3DMouseEvent &pEvent );
  137. void deleteNode( const S32 &pNodeIndex );
  138. void setNodePosition( const S32 &pNodeIndex, const Point3F &pPosition );
  139. void setNodeRotation( const S32 &pNodeIndex, const QuatF &pRotation );
  140. void setNodeWeight( const S32 &pNodeIndex, const F32 &pWeight );
  141. void setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType );
  142. void setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType, const Point3F &pPoint );
  143. void pushPathEdit( void );
  144. void popPathEdit( void );
  145. void pushNodeEdit( void );
  146. void popNodeEdit( void );
  147. void setWorldEditorDirty( void );
  148. private:
  149. class VPathEditorEditPathAction : public UndoAction
  150. {
  151. public:
  152. VPathEditorEditPathAction( const UTF8 *pName = "" ) :
  153. UndoAction( pName ), mEditor(NULL), mPath(NULL)
  154. {
  155. // Void.
  156. };
  157. VPathEditor *mEditor;
  158. VPath *mPath;
  159. MatrixF mTransform;
  160. virtual void undo( void );
  161. virtual void redo( void );
  162. };
  163. class VPathEditorEditNodeAction : public UndoAction
  164. {
  165. public:
  166. VPathEditorEditNodeAction( const UTF8 *pName = "" ) :
  167. UndoAction( pName ), mEditor(NULL), mPath(NULL), mNodeIndex(0), mNodeWeight(0.0f)
  168. {
  169. // Void.
  170. mNodeOrientation.Type = VPathNode::k_OrientationFree;
  171. mNodeOrientation.Point = Point3F(0.0f, 0.0f, 0.0f);
  172. };
  173. VPathEditor *mEditor;
  174. VPath *mPath;
  175. S32 mNodeIndex;
  176. Point3F mNodePosition;
  177. QuatF mNodeRotation;
  178. F32 mNodeWeight;
  179. VPathNode::sOrientation mNodeOrientation;
  180. virtual void undo( void );
  181. virtual void redo( void );
  182. };
  183. class VPathEditorAddNodeAction : public VPathEditorEditNodeAction
  184. {
  185. public:
  186. VPathEditorAddNodeAction( const UTF8 *pName = "" ) :
  187. VPathEditorEditNodeAction( "Add Node" )
  188. {
  189. // Void.
  190. };
  191. virtual void undo( void );
  192. virtual void redo( void );
  193. };
  194. class VPathEditorDeleteNodeAction : public VPathEditorEditNodeAction
  195. {
  196. public:
  197. VPathEditorDeleteNodeAction( const UTF8 *pName = "" ) :
  198. VPathEditorEditNodeAction( "Delete Node" )
  199. {
  200. // Void.
  201. };
  202. virtual void undo( void );
  203. virtual void redo( void );
  204. };
  205. };
  206. //-----------------------------------------------------------------------------
  207. // Define Types.
  208. typedef VPathEditor::EditMode VPathEditorMode;
  209. // Declare Enum Types.
  210. DefineEnumType( VPathEditorMode );
  211. //-----------------------------------------------------------------------------
  212. namespace Utility
  213. {
  214. bool FindNearestDistanceBetweenLines( const Point3F &pA0, const Point3F &pA1, const Point3F &pB0, const Point3F &pB1, Point3F *pOutA, Point3F *pOutB, F32 *pDist );
  215. bool IntersectLineSegment( const Point3F &pA0, const Point3F &pA1, const Point3F &pB0, const Point3F &pB1, const bool pSnap, Point3F *pX );
  216. bool FindNearestPointOnLine( const Point3F &pSrcPosition, const Point3F &pA0, const Point3F &pA1, Point3F *pDstPosition );
  217. F32 GetPitch( const VectorF &pVec );
  218. F32 GetYaw( const VectorF &pVec );
  219. };
  220. //-----------------------------------------------------------------------------
  221. #endif // _VT_VPATHEDITOR_H_