PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Source/HelixToolkit.Wpf/Visual3Ds/Manipulators/CombinedManipulator.cs

#
C# | 464 lines | 266 code | 58 blank | 140 comment | 6 complexity | 2f1db784e7adc1868e790cc8f3dd573c MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="CombinedManipulator.cs" company="Helix 3D Toolkit">
  3. // http://helixtoolkit.codeplex.com, license: Ms-PL
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace HelixToolkit.Wpf
  7. {
  8. using System.Windows;
  9. using System.Windows.Data;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Media3D;
  12. /// <summary>
  13. /// A visual element that shows translation and rotation manipulators.
  14. /// </summary>
  15. public class CombinedManipulator : ModelVisual3D
  16. {
  17. #region Constants and Fields
  18. /// <summary>
  19. /// The can rotate x property.
  20. /// </summary>
  21. public static readonly DependencyProperty CanRotateXProperty = DependencyProperty.Register(
  22. "CanRotateX", typeof(bool), typeof(CombinedManipulator), new UIPropertyMetadata(true, ChildrenChanged));
  23. /// <summary>
  24. /// The can rotate y property.
  25. /// </summary>
  26. public static readonly DependencyProperty CanRotateYProperty = DependencyProperty.Register(
  27. "CanRotateY", typeof(bool), typeof(CombinedManipulator), new UIPropertyMetadata(true, ChildrenChanged));
  28. /// <summary>
  29. /// The can rotate z property.
  30. /// </summary>
  31. public static readonly DependencyProperty CanRotateZProperty = DependencyProperty.Register(
  32. "CanRotateZ", typeof(bool), typeof(CombinedManipulator), new UIPropertyMetadata(true, ChildrenChanged));
  33. /// <summary>
  34. /// The can translate x property.
  35. /// </summary>
  36. public static readonly DependencyProperty CanTranslateXProperty = DependencyProperty.Register(
  37. "CanTranslateX", typeof(bool), typeof(CombinedManipulator), new UIPropertyMetadata(true, ChildrenChanged));
  38. /// <summary>
  39. /// The can translate y property.
  40. /// </summary>
  41. public static readonly DependencyProperty CanTranslateYProperty = DependencyProperty.Register(
  42. "CanTranslateY", typeof(bool), typeof(CombinedManipulator), new UIPropertyMetadata(true, ChildrenChanged));
  43. /// <summary>
  44. /// The can translate z property.
  45. /// </summary>
  46. public static readonly DependencyProperty CanTranslateZProperty = DependencyProperty.Register(
  47. "CanTranslateZ", typeof(bool), typeof(CombinedManipulator), new UIPropertyMetadata(true, ChildrenChanged));
  48. /// <summary>
  49. /// The diameter property.
  50. /// </summary>
  51. public static readonly DependencyProperty DiameterProperty = DependencyProperty.Register(
  52. "Diameter", typeof(double), typeof(CombinedManipulator), new UIPropertyMetadata(2.0, DiameterChanged));
  53. /// <summary>
  54. /// The target transform property.
  55. /// </summary>
  56. public static readonly DependencyProperty TargetTransformProperty =
  57. DependencyProperty.Register(
  58. "TargetTransform",
  59. typeof(Transform3D),
  60. typeof(CombinedManipulator),
  61. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
  62. /// <summary>
  63. /// The rotate x manipulator.
  64. /// </summary>
  65. private readonly RotateManipulator RotateXManipulator;
  66. /// <summary>
  67. /// The rotate y manipulator.
  68. /// </summary>
  69. private readonly RotateManipulator RotateYManipulator;
  70. /// <summary>
  71. /// The rotate z manipulator.
  72. /// </summary>
  73. private readonly RotateManipulator RotateZManipulator;
  74. /// <summary>
  75. /// The translate x manipulator.
  76. /// </summary>
  77. private readonly TranslateManipulator TranslateXManipulator;
  78. /// <summary>
  79. /// The translate y manipulator.
  80. /// </summary>
  81. private readonly TranslateManipulator TranslateYManipulator;
  82. /// <summary>
  83. /// The translate z manipulator.
  84. /// </summary>
  85. private readonly TranslateManipulator TranslateZManipulator;
  86. #endregion
  87. #region Constructors and Destructors
  88. /// <summary>
  89. /// Initializes a new instance of the <see cref = "CombinedManipulator" /> class.
  90. /// </summary>
  91. public CombinedManipulator()
  92. {
  93. this.TranslateXManipulator = new TranslateManipulator { Direction = new Vector3D(1, 0, 0), Color = Colors.Red };
  94. this.TranslateYManipulator = new TranslateManipulator { Direction = new Vector3D(0, 1, 0), Color = Colors.Green };
  95. this.TranslateZManipulator = new TranslateManipulator { Direction = new Vector3D(0, 0, 1), Color = Colors.Blue };
  96. this.RotateXManipulator = new RotateManipulator { Axis = new Vector3D(1, 0, 0), Color = Colors.Red };
  97. this.RotateYManipulator = new RotateManipulator { Axis = new Vector3D(0, 1, 0), Color = Colors.Green };
  98. this.RotateZManipulator = new RotateManipulator { Axis = new Vector3D(0, 0, 1), Color = Colors.Blue };
  99. BindingOperations.SetBinding(
  100. this,
  101. TransformProperty,
  102. new Binding("TargetTransform") { Source = this });
  103. BindingOperations.SetBinding(
  104. this.TranslateXManipulator,
  105. Manipulator.TargetTransformProperty,
  106. new Binding("TargetTransform") { Source = this });
  107. BindingOperations.SetBinding(
  108. this.TranslateYManipulator,
  109. Manipulator.TargetTransformProperty,
  110. new Binding("TargetTransform") { Source = this });
  111. BindingOperations.SetBinding(
  112. this.TranslateZManipulator,
  113. Manipulator.TargetTransformProperty,
  114. new Binding("TargetTransform") { Source = this });
  115. BindingOperations.SetBinding(
  116. this.RotateXManipulator, RotateManipulator.DiameterProperty, new Binding("Diameter") { Source = this });
  117. BindingOperations.SetBinding(
  118. this.RotateYManipulator, RotateManipulator.DiameterProperty, new Binding("Diameter") { Source = this });
  119. BindingOperations.SetBinding(
  120. this.RotateZManipulator, RotateManipulator.DiameterProperty, new Binding("Diameter") { Source = this });
  121. BindingOperations.SetBinding(
  122. this.RotateXManipulator,
  123. Manipulator.TargetTransformProperty,
  124. new Binding("TargetTransform") { Source = this });
  125. BindingOperations.SetBinding(
  126. this.RotateYManipulator,
  127. Manipulator.TargetTransformProperty,
  128. new Binding("TargetTransform") { Source = this });
  129. BindingOperations.SetBinding(
  130. this.RotateZManipulator,
  131. Manipulator.TargetTransformProperty,
  132. new Binding("TargetTransform") { Source = this });
  133. this.OnChildrenChanged();
  134. }
  135. #endregion
  136. #region Public Properties
  137. /// <summary>
  138. /// Gets or sets a value indicating whether this instance can rotate X.
  139. /// </summary>
  140. /// <value>
  141. /// <c>true</c> if this instance can rotate X; otherwise, <c>false</c>.
  142. /// </value>
  143. public bool CanRotateX
  144. {
  145. get
  146. {
  147. return (bool)this.GetValue(CanRotateXProperty);
  148. }
  149. set
  150. {
  151. this.SetValue(CanRotateXProperty, value);
  152. }
  153. }
  154. /// <summary>
  155. /// Gets or sets a value indicating whether this instance can rotate Y.
  156. /// </summary>
  157. /// <value>
  158. /// <c>true</c> if this instance can rotate Y; otherwise, <c>false</c>.
  159. /// </value>
  160. public bool CanRotateY
  161. {
  162. get
  163. {
  164. return (bool)this.GetValue(CanRotateYProperty);
  165. }
  166. set
  167. {
  168. this.SetValue(CanRotateYProperty, value);
  169. }
  170. }
  171. /// <summary>
  172. /// Gets or sets a value indicating whether this instance can rotate Z.
  173. /// </summary>
  174. /// <value>
  175. /// <c>true</c> if this instance can rotate Z; otherwise, <c>false</c>.
  176. /// </value>
  177. public bool CanRotateZ
  178. {
  179. get
  180. {
  181. return (bool)this.GetValue(CanRotateZProperty);
  182. }
  183. set
  184. {
  185. this.SetValue(CanRotateZProperty, value);
  186. }
  187. }
  188. /// <summary>
  189. /// Gets or sets a value indicating whether this instance can translate X.
  190. /// </summary>
  191. /// <value>
  192. /// <c>true</c> if this instance can translate X; otherwise, <c>false</c>.
  193. /// </value>
  194. public bool CanTranslateX
  195. {
  196. get
  197. {
  198. return (bool)this.GetValue(CanTranslateXProperty);
  199. }
  200. set
  201. {
  202. this.SetValue(CanTranslateXProperty, value);
  203. }
  204. }
  205. /// <summary>
  206. /// Gets or sets a value indicating whether this instance can translate Y.
  207. /// </summary>
  208. /// <value>
  209. /// <c>true</c> if this instance can translate Y; otherwise, <c>false</c>.
  210. /// </value>
  211. public bool CanTranslateY
  212. {
  213. get
  214. {
  215. return (bool)this.GetValue(CanTranslateYProperty);
  216. }
  217. set
  218. {
  219. this.SetValue(CanTranslateYProperty, value);
  220. }
  221. }
  222. /// <summary>
  223. /// Gets or sets a value indicating whether this instance can translate Z.
  224. /// </summary>
  225. /// <value>
  226. /// <c>true</c> if this instance can translate Z; otherwise, <c>false</c>.
  227. /// </value>
  228. public bool CanTranslateZ
  229. {
  230. get
  231. {
  232. return (bool)this.GetValue(CanTranslateZProperty);
  233. }
  234. set
  235. {
  236. this.SetValue(CanTranslateZProperty, value);
  237. }
  238. }
  239. /// <summary>
  240. /// Gets or sets the diameter.
  241. /// </summary>
  242. /// <value>The diameter.</value>
  243. public double Diameter
  244. {
  245. get
  246. {
  247. return (double)this.GetValue(DiameterProperty);
  248. }
  249. set
  250. {
  251. this.SetValue(DiameterProperty, value);
  252. }
  253. }
  254. /// <summary>
  255. /// Gets or sets the target transform.
  256. /// </summary>
  257. /// <value>The target transform.</value>
  258. public Transform3D TargetTransform
  259. {
  260. get
  261. {
  262. return (Transform3D)this.GetValue(TargetTransformProperty);
  263. }
  264. set
  265. {
  266. this.SetValue(TargetTransformProperty, value);
  267. }
  268. }
  269. /// <summary>
  270. /// Gets or sets the offset of the visual (this vector is added to the Position point).
  271. /// </summary>
  272. /// <value>The offset.</value>
  273. public Vector3D Offset
  274. {
  275. get { return TranslateXManipulator.Offset; }
  276. set
  277. {
  278. TranslateXManipulator.Offset = value;
  279. TranslateYManipulator.Offset = value;
  280. TranslateZManipulator.Offset = value;
  281. RotateXManipulator.Offset = value;
  282. RotateYManipulator.Offset = value;
  283. RotateZManipulator.Offset = value;
  284. }
  285. }
  286. /// <summary>
  287. /// Gets or sets the position of the manipulator.
  288. /// </summary>
  289. /// <value>The position.</value>
  290. public Point3D Position
  291. {
  292. get { return TranslateXManipulator.Position; }
  293. set
  294. {
  295. TranslateXManipulator.Position = value;
  296. TranslateYManipulator.Position = value;
  297. TranslateZManipulator.Position = value;
  298. RotateXManipulator.Position = value;
  299. RotateYManipulator.Position = value;
  300. RotateZManipulator.Position = value;
  301. }
  302. }
  303. /// <summary>
  304. /// Gets or sets the pivot point of the manipulator.
  305. /// </summary>
  306. /// <value>The position.</value>
  307. public Point3D Pivot
  308. {
  309. get { return TranslateXManipulator.Pivot; }
  310. set
  311. {
  312. TranslateXManipulator.Pivot = value;
  313. TranslateYManipulator.Pivot = value;
  314. TranslateZManipulator.Pivot = value;
  315. RotateXManipulator.Pivot = value;
  316. RotateYManipulator.Pivot = value;
  317. RotateZManipulator.Pivot = value;
  318. }
  319. }
  320. #endregion
  321. #region Methods
  322. /// <summary>
  323. /// The on children changed.
  324. /// </summary>
  325. protected virtual void OnChildrenChanged()
  326. {
  327. this.Children.Clear();
  328. if (this.CanTranslateX)
  329. {
  330. this.Children.Add(this.TranslateXManipulator);
  331. }
  332. if (this.CanTranslateY)
  333. {
  334. this.Children.Add(this.TranslateYManipulator);
  335. }
  336. if (this.CanTranslateZ)
  337. {
  338. this.Children.Add(this.TranslateZManipulator);
  339. }
  340. if (this.CanRotateX)
  341. {
  342. this.Children.Add(this.RotateXManipulator);
  343. }
  344. if (this.CanRotateY)
  345. {
  346. this.Children.Add(this.RotateYManipulator);
  347. }
  348. if (this.CanRotateZ)
  349. {
  350. this.Children.Add(this.RotateZManipulator);
  351. }
  352. }
  353. /// <summary>
  354. /// The on diameter changed.
  355. /// </summary>
  356. protected virtual void OnDiameterChanged()
  357. {
  358. }
  359. /// <summary>
  360. /// The children changed.
  361. /// </summary>
  362. /// <param name="d">
  363. /// The d.
  364. /// </param>
  365. /// <param name="e">
  366. /// The event arguments.
  367. /// </param>
  368. private static void ChildrenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  369. {
  370. ((CombinedManipulator)d).OnChildrenChanged();
  371. }
  372. /// <summary>
  373. /// The diameter changed.
  374. /// </summary>
  375. /// <param name="d">
  376. /// The d.
  377. /// </param>
  378. /// <param name="e">
  379. /// The event arguments.
  380. /// </param>
  381. private static void DiameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  382. {
  383. ((CombinedManipulator)d).OnDiameterChanged();
  384. }
  385. #endregion
  386. #region Public Bindig Methods
  387. /// <summary>
  388. /// Binds this manipulator to a given Visual3D.
  389. /// </summary>
  390. /// <param name="source">Source Visual3D which receives the manipulator transforms.</param>
  391. public virtual void Bind(ModelVisual3D source)
  392. {
  393. BindingOperations.SetBinding(this, CombinedManipulator.TargetTransformProperty, new Binding("Transform") { Source = source });
  394. BindingOperations.SetBinding(this, CombinedManipulator.TransformProperty, new Binding("Transform") { Source = source });
  395. }
  396. /// <summary>
  397. /// Releases the binding of this manipulator.
  398. /// </summary>
  399. public virtual void UnBind()
  400. {
  401. BindingOperations.ClearBinding(this, CombinedManipulator.TargetTransformProperty);
  402. BindingOperations.ClearBinding(this, CombinedManipulator.TransformProperty);
  403. }
  404. #endregion
  405. }
  406. }