guiShapeEdPreview.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 _GUISHAPEEDPREVIEW_H_
  23. #define _GUISHAPEEDPREVIEW_H_
  24. #include "gui/worldEditor/editTSCtrl.h"
  25. #include "ts/tsShapeInstance.h"
  26. class LightInfo;
  27. class GuiShapeEdPreview : public EditTSCtrl
  28. {
  29. struct Thread
  30. {
  31. TSThread* key; //!< TSThread key
  32. String seqName; //!< Name of the sequence for this thread
  33. S32 direction; //!< Playback direction: -1 (reverse), 0 (paused), 1 (forward)
  34. bool pingpong; //!< Pingpong flag (auto-reverse direction at start/end)
  35. Thread()
  36. {
  37. init();
  38. }
  39. void init()
  40. {
  41. key = NULL;
  42. direction = 1.0f;
  43. pingpong = false;
  44. }
  45. };
  46. struct MountedShape
  47. {
  48. enum eMountType
  49. {
  50. Object, //!< Mount origin of shape to target node
  51. Image, //!< Mount 'mountPoint' or origin of shape to target node
  52. Wheel, //!< Mount origin of shape to target node, ignore target node rotation
  53. };
  54. TSShapeInstance* mShape; //!< The mounted shape instance
  55. S32 mNode; //!< Index of the node this shape is mounted to
  56. MatrixF mTransform; //!< Mount offset transform
  57. eMountType mType; //!< Type of mount
  58. Thread mThread; //!< Animation thread for the mounted shape
  59. MountedShape() : mShape(0), mNode(-1), mTransform(true), mType(Object) { }
  60. ~MountedShape() { delete mShape; }
  61. };
  62. protected:
  63. typedef EditTSCtrl Parent;
  64. // View and node selection
  65. bool mUsingAxisGizmo;
  66. bool mEditingSun; //!< True if editing the sun direction, false otherwise
  67. S32 mGizmoDragID; //!< Used to track transform changes within a single gizmo drag action
  68. S32 mSelectedNode; //!< Index of the selected node
  69. S32 mHoverNode; //!< Index of the node the mouse is over
  70. Vector<Point3F> mProjectedNodes; //!< Projected screen positions of the model nodes
  71. S32 mSelectedObject; //!< Name of the selected object
  72. S32 mSelectedObjDetail; //!< Detail mesh index of the selected object
  73. // Camera
  74. EulerF mCameraRot;
  75. bool mRenderCameraAxes;
  76. Point3F mOrbitPos;
  77. F32 mOrbitDist;
  78. F32 mMoveSpeed;
  79. F32 mZoomSpeed;
  80. // Current Detail
  81. bool mFixedDetail; //!< If false, the detail level will be determined based on camera distance
  82. S32 mCurrentDL; //!< Current detail level
  83. S32 mDetailSize; //!< Size of current detail level
  84. S32 mDetailPolys; //!< Number of polys (triangles) in current detail level
  85. F32 mPixelSize; //!< Current pixel size
  86. S32 mNumMaterials; //!< Number of materials in the current detail level
  87. S32 mNumDrawCalls; //!< Number of draw calls in the current detail level
  88. S32 mNumBones; //!< Number of bones in the current detail level (skins only)
  89. S32 mNumWeights; //!< Number of vertex weights in the current detail level (skins only)
  90. S32 mColMeshes; //!< Number of collision meshes
  91. S32 mColPolys; //!< Number of collision polygons (all meshes)
  92. // Rendering
  93. Point2I mGridDimension; //!< Dimension of grid in perspective view (eg. 30x30)
  94. bool mRenderGhost;
  95. bool mRenderNodes;
  96. bool mRenderBounds;
  97. bool mRenderObjBox;
  98. bool mRenderColMeshes;
  99. bool mRenderMounts;
  100. TSShapeInstance* mModel;
  101. StringTableEntry mModelName;
  102. LightInfo* mFakeSun;
  103. EulerF mSunRot;
  104. ColorI mSunDiffuseColor;
  105. ColorI mSunAmbientColor;
  106. // Animation and playback control
  107. Vector<Thread> mThreads;
  108. F32 mTimeScale;
  109. S32 mActiveThread;
  110. S32 mLastRenderTime;
  111. // Mounted objects
  112. Vector<MountedShape*> mMounts;
  113. static bool setFieldCurrentDL( void *object, const char *index, const char *data );
  114. static bool setFieldSunDiffuse( void *object, const char *index, const char *data );
  115. static bool setFieldSunAmbient( void *object, const char *index, const char *data );
  116. static bool setFieldSunAngleX( void *object, const char *index, const char *data );
  117. static bool setFieldSunAngleZ( void *object, const char *index, const char *data );
  118. static bool setFieldThreadPos( void *object, const char *index, const char *data );
  119. static bool setFieldThreadIn( void *object, const char *index, const char *data );
  120. static bool setFieldThreadOut( void *object, const char *index, const char *data );
  121. static bool setFieldThreadDir( void *object, const char *index, const char *data );
  122. static bool setFieldThreadPingPong( void *object, const char *index, const char *data );
  123. static const char *getFieldThreadPos( void *object, const char *data );
  124. static const char *getFieldThreadIn( void *object, const char *data );
  125. static const char *getFieldThreadOut( void *object, const char *data );
  126. static const char *getFieldThreadDir( void *object, const char *data );
  127. static const char *getFieldThreadPingPong( void *object, const char *data );
  128. // Generic mouse event handlers
  129. void handleMouseDown(const GuiEvent& event, GizmoMode mode);
  130. void handleMouseUp(const GuiEvent& event, GizmoMode mode);
  131. void handleMouseMove(const GuiEvent& event, GizmoMode mode);
  132. void handleMouseDragged(const GuiEvent& event, GizmoMode mode);
  133. // Node picking
  134. S32 collideNode(const Gui3DMouseEvent& event) const;
  135. void updateProjectedNodePoints();
  136. void updateSun();
  137. void updateDetailLevel(const SceneRenderState* state);
  138. void updateThreads(F32 delta);
  139. // Rendering
  140. void renderGrid();
  141. void renderNodes() const;
  142. void renderNodeAxes(S32 index, const LinearColorF& nodeColor) const;
  143. void renderNodeName(S32 index, const LinearColorF& textColor) const;
  144. void renderSunDirection() const;
  145. void renderCollisionMeshes() const;
  146. public:
  147. bool onWake();
  148. void setDisplayType(S32 type);
  149. /// @name Mouse event handlers
  150. ///@{
  151. void onMouseDown(const GuiEvent& event) { handleMouseDown(event, NoneMode); }
  152. void onMouseUp(const GuiEvent& event) { handleMouseUp(event, NoneMode); }
  153. void onMouseMove(const GuiEvent& event) { handleMouseMove(event, NoneMode); }
  154. void onMouseDragged(const GuiEvent& event) { handleMouseDragged(event, NoneMode); }
  155. void onMiddleMouseDown(const GuiEvent& event) { handleMouseDown(event, MoveMode); }
  156. void onMiddleMouseUp(const GuiEvent& event) { handleMouseUp(event, MoveMode); }
  157. void onMiddleMouseDragged(const GuiEvent& event) { handleMouseDragged(event, MoveMode); }
  158. void onRightMouseDown(const GuiEvent& event) { handleMouseDown(event, RotateMode); }
  159. void onRightMouseUp(const GuiEvent& event) { handleMouseUp(event, RotateMode); }
  160. void onRightMouseDragged(const GuiEvent& event) { handleMouseDragged(event, RotateMode); }
  161. void on3DMouseWheelUp(const Gui3DMouseEvent& event);
  162. void on3DMouseWheelDown(const Gui3DMouseEvent& event);
  163. ///@}
  164. // Setters/Getters
  165. TSShapeInstance* getModel() { return mModel; }
  166. void setCurrentDetail(S32 dl);
  167. bool setObjectModel(const char * modelName);
  168. bool setObjectShapeAsset(const char* assetId);
  169. void _onResourceChanged(const Torque::Path& path);
  170. /// @name Threads
  171. ///@{
  172. void addThread();
  173. void removeThread(S32 slot);
  174. S32 getThreadCount() const { return mThreads.size(); }
  175. void setTimeScale(F32 scale);
  176. void setActiveThreadSequence(const char* seqName, F32 duration, F32 pos, bool play);
  177. void setThreadSequence(Thread& thread, TSShapeInstance* shape, const char * seqName, F32 duration=0.0f, F32 pos=0.0f, bool play=false);
  178. const char* getThreadSequence() const;
  179. void refreshThreadSequences();
  180. DECLARE_CALLBACK( void, onThreadPosChanged, ( F32 pos, bool inTransition ) );
  181. ///@}
  182. /// @name Mounting
  183. ///@{
  184. bool mountShape(const char* modelName, const char* nodeName, const char* mountType, S32 slot=-1);
  185. void setMountNode(S32 mountSlot, const char* nodeName);
  186. const char* getMountThreadSequence(S32 mountSlot) const;
  187. void setMountThreadSequence(S32 mountSlot, const char* seqName);
  188. F32 getMountThreadPos(S32 mountSlot) const;
  189. void setMountThreadPos(S32 mountSlot, F32 pos);
  190. F32 getMountThreadDir(S32 mountSlot) const;
  191. void setMountThreadDir(S32 mountSlot, F32 dir);
  192. void unmountShape(S32 mountSlot);
  193. void unmountAll();
  194. ///@}
  195. void refreshShape();
  196. void updateNodeTransforms();
  197. void get3DCursor(GuiCursor *& cursor, bool& visible, const Gui3DMouseEvent& event_);
  198. void fitToShape();
  199. void setOrbitPos( const Point3F& pos );
  200. void exportToCollada( const String& path );
  201. /// @name Rendering
  202. ///@{
  203. bool getCameraTransform(MatrixF* cameraMatrix);
  204. void computeSceneBounds(Box3F& bounds);
  205. bool getMeshHidden(const char* name) const;
  206. void setMeshHidden(const char* name, bool hidden);
  207. void setAllMeshesHidden(bool hidden);
  208. void renderWorld(const RectI& updateRect);
  209. void renderGui(Point2I offset, const RectI& updateRect);
  210. ///@}
  211. DECLARE_CONOBJECT(GuiShapeEdPreview);
  212. DECLARE_CATEGORY( "Gui Editor" );
  213. static void initPersistFields();
  214. GuiShapeEdPreview();
  215. ~GuiShapeEdPreview();
  216. };
  217. #endif