VPathEditor.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #include "torqueConfig.h"
  26. #ifdef TORQUE_TOOLS
  27. #ifndef _EDITTSCTRL_H_
  28. #include "gui/worldEditor/editTSCtrl.h"
  29. #endif
  30. #ifndef _VT_VPATH_H_
  31. #include "VPath.h"
  32. #endif
  33. #ifndef _UNDO_H_
  34. #include "util/undo.h"
  35. #endif
  36. //-----------------------------------------------------------------------------
  37. class VPathEditor : public EditTSCtrl
  38. {
  39. typedef EditTSCtrl Parent;
  40. public:
  41. enum RenderType
  42. {
  43. k_RenderSegments,
  44. k_RenderNodes,
  45. };
  46. enum EditMode
  47. {
  48. k_Gizmo,
  49. k_AddNode,
  50. k_DeleteNode
  51. };
  52. struct Selection
  53. {
  54. Selection( void ) :
  55. Path( NULL ),
  56. Node( -1 )
  57. {
  58. TangentHandle[0].zero();
  59. TangentHandle[1].zero();
  60. };
  61. VPath *Path;
  62. S32 Node;
  63. Point3F TangentHandle[2];
  64. };
  65. struct PathEditAction
  66. {
  67. PathEditAction( void ) :
  68. Dirty( false ),
  69. Transform( true )
  70. {
  71. // Void.
  72. };
  73. bool Dirty;
  74. MatrixF Transform;
  75. };
  76. struct NodeEditAction
  77. {
  78. NodeEditAction( void ) :
  79. Dirty( false ),
  80. Position( 0.f, 0.f, 0.f ),
  81. Rotation( 0.f, 0.f, 0.f, 0.f ),
  82. Weight( 0.f )
  83. {
  84. // Void.
  85. };
  86. bool Dirty;
  87. Point3F Position;
  88. QuatF Rotation;
  89. F32 Weight;
  90. };
  91. bool mIsDirty;
  92. EditMode mEditMode;
  93. Selection mSelection;
  94. PathEditAction mPathEdit;
  95. NodeEditAction mNodeEdit;
  96. bool mEditWeight;
  97. S32 mEditWeightHandle;
  98. GFXStateBlockRef mStateBlock;
  99. public:
  100. VPathEditor( void );
  101. virtual bool onAdd( void );
  102. virtual bool onWake( void );
  103. static void initPersistFields();
  104. // Gui Events.
  105. virtual void on3DMouseDown( const Gui3DMouseEvent &pEvent );
  106. virtual void on3DMouseUp( const Gui3DMouseEvent &pEvent );
  107. virtual void on3DMouseMove( const Gui3DMouseEvent &pEvent );
  108. virtual void on3DMouseDragged( const Gui3DMouseEvent &pEvent );
  109. // Render Methods.
  110. virtual void setStateBlock( void );
  111. virtual void renderScene( const RectI &pUpdateRect );
  112. void renderPaths( const RenderType &pRenderType );
  113. void renderPath( const RenderType &pRenderType, VPath *pPath, const ColorI &pColor );
  114. void renderLinearPath( VPath *pPath, const ColorI &pColor );
  115. void renderBezierPath( VPath *pPath, const ColorI &pColor );
  116. DECLARE_CONOBJECT( VPathEditor );
  117. public:
  118. // Reference Methods.
  119. VPath *getClientPath( VPath *pPath );
  120. // Selection Methods.
  121. inline bool isValidSelection( void ) { return ( mSelection.Path != NULL && mSelection.Node != -1 ); };
  122. bool updateSelection( const Gui3DMouseEvent &pEvent );
  123. void updateSelection( VPath *pPathObject, const S32 &pNodeIndex );
  124. void updateSelection( void );
  125. // Weight Editing.
  126. bool isEditingWeight( const Gui3DMouseEvent &pEvent );
  127. inline bool isEditingWeight( void ) { return mEditWeight; };
  128. void updateWeight( const Gui3DMouseEvent &pEvent );
  129. // Path Editor.
  130. bool getPointOnPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform );
  131. bool getPointOnLinearPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform );
  132. bool getPointOnBezierPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform );
  133. void setPathPosition( const Point3F &pPosition );
  134. void setPathRotation( const QuatF &pRotation );
  135. void setPathTransform( const MatrixF &pTransform );
  136. void setPathScale( const VectorF &pScale );
  137. // Node Editing.
  138. void addNode( const Gui3DMouseEvent &pEvent );
  139. void deleteNode( const S32 &pNodeIndex );
  140. void setNodePosition( const S32 &pNodeIndex, const Point3F &pPosition );
  141. void setNodeRotation( const S32 &pNodeIndex, const QuatF &pRotation );
  142. void setNodeWeight( const S32 &pNodeIndex, const F32 &pWeight );
  143. void setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType );
  144. void setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType, const Point3F &pPoint );
  145. void pushPathEdit( void );
  146. void popPathEdit( void );
  147. void pushNodeEdit( void );
  148. void popNodeEdit( void );
  149. void setWorldEditorDirty( void );
  150. private:
  151. class VPathEditorEditPathAction : public UndoAction
  152. {
  153. public:
  154. VPathEditorEditPathAction( const UTF8 *pName = "" ) :
  155. UndoAction( pName ), mEditor(NULL), mPath(NULL)
  156. {
  157. // Void.
  158. };
  159. VPathEditor *mEditor;
  160. VPath *mPath;
  161. MatrixF mTransform;
  162. virtual void undo( void );
  163. virtual void redo( void );
  164. };
  165. class VPathEditorEditNodeAction : public UndoAction
  166. {
  167. public:
  168. VPathEditorEditNodeAction( const UTF8 *pName = "" ) :
  169. UndoAction( pName ), mEditor(NULL), mPath(NULL), mNodeIndex(0), mNodeWeight(0.0f)
  170. {
  171. // Void.
  172. mNodeOrientation.Type = VPathNode::k_OrientationFree;
  173. mNodeOrientation.Point = Point3F(0.0f, 0.0f, 0.0f);
  174. };
  175. VPathEditor *mEditor;
  176. VPath *mPath;
  177. S32 mNodeIndex;
  178. Point3F mNodePosition;
  179. QuatF mNodeRotation;
  180. F32 mNodeWeight;
  181. VPathNode::sOrientation mNodeOrientation;
  182. virtual void undo( void );
  183. virtual void redo( void );
  184. };
  185. class VPathEditorAddNodeAction : public VPathEditorEditNodeAction
  186. {
  187. public:
  188. VPathEditorAddNodeAction( const UTF8 *pName = "" ) :
  189. VPathEditorEditNodeAction( "Add Node" )
  190. {
  191. // Void.
  192. };
  193. virtual void undo( void );
  194. virtual void redo( void );
  195. };
  196. class VPathEditorDeleteNodeAction : public VPathEditorEditNodeAction
  197. {
  198. public:
  199. VPathEditorDeleteNodeAction( const UTF8 *pName = "" ) :
  200. VPathEditorEditNodeAction( "Delete Node" )
  201. {
  202. // Void.
  203. };
  204. virtual void undo( void );
  205. virtual void redo( void );
  206. };
  207. };
  208. //-----------------------------------------------------------------------------
  209. // Define Types.
  210. typedef VPathEditor::EditMode VPathEditorMode;
  211. // Declare Enum Types.
  212. DefineEnumType( VPathEditorMode );
  213. //-----------------------------------------------------------------------------
  214. namespace Utility
  215. {
  216. bool FindNearestDistanceBetweenLines( const Point3F &pA0, const Point3F &pA1, const Point3F &pB0, const Point3F &pB1, Point3F *pOutA, Point3F *pOutB, F32 *pDist );
  217. bool IntersectLineSegment( const Point3F &pA0, const Point3F &pA1, const Point3F &pB0, const Point3F &pB1, const bool pSnap, Point3F *pX );
  218. bool FindNearestPointOnLine( const Point3F &pSrcPosition, const Point3F &pA0, const Point3F &pA1, Point3F *pDstPosition );
  219. F32 GetPitch( const VectorF &pVec );
  220. F32 GetYaw( const VectorF &pVec );
  221. };
  222. //-----------------------------------------------------------------------------
  223. #endif
  224. #endif // _VT_VPATHEDITOR_H_