Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

468 lines
15 KiB

  1. //====== Copyright � 1996-2008, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Implementation of CDmeTransformControl class, a helper for
  4. // modifying a transform. The CDmeTransformControl class implementation
  5. // contains functions for setting and retrieving parameters used in transform
  6. // manipulations, these parameters are allocated when the first value is set.
  7. // If the manipulation parameters have not been allocated a default value will
  8. // be returned. Additionally the CDmeTransformControl provides access to the
  9. // rotation channel associated with the transform, even if the transform
  10. // control is not directly attached to the rotation channel.
  11. //
  12. //=============================================================================
  13. #include "movieobjects/dmeanimationset.h"
  14. #include "movieobjects/dmetransformcontrol.h"
  15. #include "movieobjects/dmechannel.h"
  16. #include "movieobjects/dmetransform.h"
  17. #include "movieobjects/dmerigconstraintoperators.h"
  18. #include "datamodel/dmelementfactoryhelper.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. //-----------------------------------------------------------------------------
  22. // Expose this class to the scene database
  23. //-----------------------------------------------------------------------------
  24. IMPLEMENT_ELEMENT_FACTORY( DmeTransformControl, CDmeTransformControl );
  25. //-----------------------------------------------------------------------------
  26. // Purpose: Provide post construction processing.
  27. //-----------------------------------------------------------------------------
  28. void CDmeTransformControl::OnConstruction()
  29. {
  30. m_PivotOffset = vec3_origin;
  31. m_pManipulationParams = NULL;
  32. m_PositionValue.InitAndSet( this, "valuePosition", vec3_origin );
  33. m_OrientationValue.InitAndSet( this, "valueOrientation", quat_identity );
  34. m_PositionChannel.Init( this, "positionChannel" );
  35. m_OrientationChannel.Init( this, "orientationChannel" );
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Purpose: Provide processing and cleanup before shutdown
  39. //-----------------------------------------------------------------------------
  40. void CDmeTransformControl::OnDestruction()
  41. {
  42. // Destroy the manipulation parameters if they have been created.
  43. delete m_pManipulationParams;
  44. m_pManipulationParams = NULL;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Allocate and initialize the manipulation parameters
  48. //
  49. // Output : Returns true if the manipulation parameters were successfully
  50. // initialized, false if the initialization failed.
  51. //-----------------------------------------------------------------------------
  52. bool CDmeTransformControl::InitManipulationParams()
  53. {
  54. if ( m_pManipulationParams == NULL )
  55. {
  56. m_pManipulationParams = new ManipulationParams_t;
  57. if ( m_pManipulationParams )
  58. {
  59. m_pManipulationParams->Pivot = vec3_origin;
  60. m_pManipulationParams->RotationLocal = quat_identity;
  61. m_pManipulationParams->RotationParent = quat_identity;
  62. SetIdentityMatrix( m_pManipulationParams->Transform );
  63. }
  64. }
  65. return ( m_pManipulationParams != NULL );
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose: Get the manipulation transform matrix
  69. //
  70. // Output : transform - The transform that will be applied by the current
  71. // manipulation.
  72. //-----------------------------------------------------------------------------
  73. void CDmeTransformControl::GetManipulationTransform( matrix3x4_t &transform ) const
  74. {
  75. if ( m_pManipulationParams )
  76. {
  77. transform = m_pManipulationParams->Transform;
  78. }
  79. else
  80. {
  81. SetIdentityMatrix( transform );
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Set the manipulation transform matrix
  86. //
  87. // Input : transform - Matrix specifying the transform to be performed by the
  88. // manipulation.
  89. //-----------------------------------------------------------------------------
  90. void CDmeTransformControl::SetManipulationTransform( const matrix3x4_t &transform )
  91. {
  92. if ( InitManipulationParams() )
  93. {
  94. m_pManipulationParams->Transform = transform;
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Purpose: Get the manipulation rotation amount in the transform local space
  99. //
  100. // Output : deltaRotation - Current rotation that will be applied in the
  101. // transform manipulation
  102. //-----------------------------------------------------------------------------
  103. void CDmeTransformControl::GetManipulationRotationLocal( Quaternion &rotation ) const
  104. {
  105. if ( m_pManipulationParams )
  106. {
  107. rotation = m_pManipulationParams->RotationLocal;
  108. }
  109. else
  110. {
  111. rotation = quat_identity;
  112. }
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Purpose: Set the manipulation rotation amount in the transform local space
  116. //
  117. // Input : deltaRotation - Rotation to be applied in the transform manipulation
  118. //-----------------------------------------------------------------------------
  119. void CDmeTransformControl::SetManipulationRotationLocal( const Quaternion &rotation )
  120. {
  121. if ( InitManipulationParams() )
  122. {
  123. m_pManipulationParams->RotationLocal = rotation;
  124. }
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Get the manipulation rotation amount in the parent space of the transform
  128. //
  129. // Output : localRotation - Current local rotation that will be applied in the
  130. // transform manipulation
  131. //-----------------------------------------------------------------------------
  132. void CDmeTransformControl::GetManipulationRotationParent( Quaternion &rotation ) const
  133. {
  134. if ( m_pManipulationParams )
  135. {
  136. rotation = m_pManipulationParams->RotationParent;
  137. }
  138. else
  139. {
  140. rotation = quat_identity;
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Set the manipulation rotation amount in the parent space of the transform
  145. //
  146. // Input : localRotation - Local rotation to be applied in the transform
  147. // manipulation.
  148. //-----------------------------------------------------------------------------
  149. void CDmeTransformControl::SetManipulationRotationParent( const Quaternion &rotation )
  150. {
  151. if ( InitManipulationParams() )
  152. {
  153. m_pManipulationParams->RotationParent = rotation;
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose: Get the manipulation pivot position, this may differ from the pivot
  158. // offset
  159. //
  160. // Output : pivotPosition - Pivot position being used for the current transform
  161. // manipulation
  162. //-----------------------------------------------------------------------------
  163. void CDmeTransformControl::GetManipulationPivot( Vector &pivotPosition ) const
  164. {
  165. if ( m_pManipulationParams )
  166. {
  167. pivotPosition = m_pManipulationParams->Pivot;
  168. }
  169. else
  170. {
  171. pivotPosition = vec3_origin;
  172. }
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose: Set the manipulation pivot position
  176. //
  177. // Input : Position to be used as the pivot location for manipulations
  178. //-----------------------------------------------------------------------------
  179. void CDmeTransformControl::SetManipulationPivot( const Vector &pivotPosition )
  180. {
  181. if ( InitManipulationParams() )
  182. {
  183. m_pManipulationParams->Pivot = pivotPosition;
  184. }
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose: Get the current local space pivot offset
  188. //
  189. // Output : Return the current local space pivot offset
  190. //-----------------------------------------------------------------------------
  191. const Vector &CDmeTransformControl::GetPivotOffset() const
  192. {
  193. return m_PivotOffset;
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Purpose: Set the current local space pivot offset
  197. //
  198. // Input : localOffset - The new local offset that is to be assigned to the
  199. // pivot.
  200. //-----------------------------------------------------------------------------
  201. void CDmeTransformControl::SetPivotOffset( const Vector &localOffset )
  202. {
  203. m_PivotOffset = localOffset;
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Purpose: Get the transform associated with the transform control
  207. //
  208. // Output : Returns the pointer to the transform element
  209. //-----------------------------------------------------------------------------
  210. CDmeTransform *CDmeTransformControl::GetTransform() const
  211. {
  212. CDmeTransform *pTransform = NULL;
  213. CDmeChannel *pChannel = GetPositionChannel();
  214. if ( pChannel == NULL )
  215. {
  216. pChannel = GetOrientationChannel();
  217. }
  218. if ( pChannel )
  219. {
  220. CDmElement *pToElement = pChannel->GetToElement();
  221. pTransform = CastElement< CDmeTransform>( pToElement );
  222. // If the element targeted by the channel is not a transform,
  223. // see if it is a constraint slave, and get the transform from that.
  224. if ( pTransform == NULL )
  225. {
  226. CDmeConstraintSlave *pConstraintSlave = CastElement< CDmeConstraintSlave >( pToElement );
  227. if ( pConstraintSlave )
  228. {
  229. CDmeDag *pDag = pConstraintSlave->GetDag();
  230. if ( pDag )
  231. {
  232. pTransform = pDag->GetTransform();
  233. }
  234. }
  235. }
  236. }
  237. return pTransform;
  238. }
  239. //-----------------------------------------------------------------------------
  240. // Get the dag node associated with the transform control
  241. //
  242. // Output: Returns the pointer to the dag node targeted by the transform
  243. // control.
  244. //-----------------------------------------------------------------------------
  245. CDmeDag *CDmeTransformControl::GetDag() const
  246. {
  247. CDmeDag *pDagNode = NULL;
  248. CDmeTransform *pTransform = GetTransform();
  249. if ( pTransform )
  250. {
  251. pDagNode = pTransform->GetDag();
  252. }
  253. return pDagNode;
  254. }
  255. //-----------------------------------------------------------------------------
  256. // Get the position attribute of the control
  257. //-----------------------------------------------------------------------------
  258. CDmAttribute *CDmeTransformControl::GetPositionAttr()
  259. {
  260. return m_PositionValue.GetAttribute();
  261. }
  262. //-----------------------------------------------------------------------------
  263. // Get the orientation attribute of the control
  264. //-----------------------------------------------------------------------------
  265. CDmAttribute *CDmeTransformControl::GetOrientationAttr()
  266. {
  267. return m_OrientationValue.GetAttribute();
  268. }
  269. //-----------------------------------------------------------------------------
  270. // Get the position value of the control
  271. //-----------------------------------------------------------------------------
  272. const Vector &CDmeTransformControl::GetPosition() const
  273. {
  274. return m_PositionValue;
  275. }
  276. //-----------------------------------------------------------------------------
  277. // Get the orientation value of the control
  278. //-----------------------------------------------------------------------------
  279. const Quaternion &CDmeTransformControl::GetOrientation() const
  280. {
  281. return m_OrientationValue;
  282. }
  283. //-----------------------------------------------------------------------------
  284. // Determine if the control currently has a default position set
  285. //-----------------------------------------------------------------------------
  286. bool CDmeTransformControl::HasDefaultPosition() const
  287. {
  288. return HasAttribute( DEFAULT_POSITION_ATTR, AT_VECTOR3 );
  289. }
  290. //-----------------------------------------------------------------------------
  291. // Determine if the control currently has a default orientation set
  292. //-----------------------------------------------------------------------------
  293. bool CDmeTransformControl::HasDefaultOrientation() const
  294. {
  295. return HasAttribute( DEFAULT_ORIENTATION_ATTR, AT_QUATERNION );
  296. }
  297. //-----------------------------------------------------------------------------
  298. // Get the default position of the control
  299. //-----------------------------------------------------------------------------
  300. const Vector &CDmeTransformControl::GetDefaultPosition() const
  301. {
  302. return GetValue< Vector >( DEFAULT_POSITION_ATTR, vec3_origin );
  303. }
  304. //-----------------------------------------------------------------------------
  305. // Get the default orientation of the control
  306. //-----------------------------------------------------------------------------
  307. const Quaternion &CDmeTransformControl::GetDefaultOrientation() const
  308. {
  309. return GetValue< Quaternion >( DEFAULT_ORIENTATION_ATTR, quat_identity );
  310. }
  311. //-----------------------------------------------------------------------------
  312. // Get the default position attribute
  313. //-----------------------------------------------------------------------------
  314. const CDmAttribute *CDmeTransformControl::GetDefaultPositionAttr() const
  315. {
  316. return GetAttribute( DEFAULT_POSITION_ATTR, AT_VECTOR3 );
  317. }
  318. //-----------------------------------------------------------------------------
  319. // Get the default position attribute
  320. //-----------------------------------------------------------------------------
  321. const CDmAttribute *CDmeTransformControl::GetDefaultOrientationAttr() const
  322. {
  323. return GetAttribute( DEFAULT_ORIENTATION_ATTR, AT_QUATERNION );
  324. }
  325. //-----------------------------------------------------------------------------
  326. // Get the position channel targeting the control
  327. //-----------------------------------------------------------------------------
  328. CDmeChannel *CDmeTransformControl::GetPositionChannel() const
  329. {
  330. return m_PositionChannel;
  331. }
  332. //-----------------------------------------------------------------------------
  333. // Get the orientation channel targeting the control
  334. //-----------------------------------------------------------------------------
  335. CDmeChannel *CDmeTransformControl::GetOrientationChannel() const
  336. {
  337. return m_OrientationChannel;
  338. }
  339. //-----------------------------------------------------------------------------
  340. // Set the position value of the control
  341. //-----------------------------------------------------------------------------
  342. void CDmeTransformControl::SetPosition( const Vector &position )
  343. {
  344. m_PositionValue = position;
  345. }
  346. //-----------------------------------------------------------------------------
  347. // Set the orientation value of the control
  348. //-----------------------------------------------------------------------------
  349. void CDmeTransformControl::SetOrientation( const Quaternion &orientation )
  350. {
  351. m_OrientationValue = orientation;
  352. }
  353. //-----------------------------------------------------------------------------
  354. // Set the default position of the control
  355. //-----------------------------------------------------------------------------
  356. void CDmeTransformControl::SetDefaultPosition( const Vector &position )
  357. {
  358. CDmAttribute *pAttr = SetValue< Vector >( DEFAULT_POSITION_ATTR, position );
  359. if ( pAttr )
  360. {
  361. pAttr->AddFlag( FATTRIB_DONTSAVE );
  362. }
  363. }
  364. //-----------------------------------------------------------------------------
  365. // Set the default orientation of the control
  366. //-----------------------------------------------------------------------------
  367. void CDmeTransformControl::SetDefaultOrientation( const Quaternion &orientation )
  368. {
  369. CDmAttribute *pAttr = SetValue< Quaternion >( DEFAULT_ORIENTATION_ATTR, orientation );
  370. if ( pAttr )
  371. {
  372. pAttr->AddFlag( FATTRIB_DONTSAVE );
  373. }
  374. }
  375. //-----------------------------------------------------------------------------
  376. // Set the position channel that is targeting the control
  377. //-----------------------------------------------------------------------------
  378. void CDmeTransformControl::SetPositionChannel( CDmeChannel *pChannel )
  379. {
  380. m_PositionChannel = pChannel;
  381. }
  382. //-----------------------------------------------------------------------------
  383. // Get the orientation channel that is targeting the control
  384. //-----------------------------------------------------------------------------
  385. void CDmeTransformControl::SetOrientationChannel( CDmeChannel *pChannel )
  386. {
  387. m_OrientationChannel = pChannel;
  388. }