/Controls/HelixToolkit.Wpf/Controls/CameraController/PanHandler.cs

https://bitbucket.org/kubrakms/thermalconductivity · C# · 166 lines · 83 code · 28 blank · 55 comment · 15 complexity · 957ec7b2ffa025ba8ef1da94fdad6496 MD5 · raw file

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PanHandler.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.Input;
  10. using System.Windows.Media.Media3D;
  11. /// <summary>
  12. /// Handles panning.
  13. /// </summary>
  14. internal class PanHandler : MouseGestureHandler
  15. {
  16. #region Constants and Fields
  17. /// <summary>
  18. /// The 3D pan origin.
  19. /// </summary>
  20. private Point3D panPoint3D;
  21. #endregion
  22. #region Constructors and Destructors
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="PanHandler"/> class.
  25. /// </summary>
  26. /// <param name="controller">
  27. /// The controller.
  28. /// </param>
  29. public PanHandler(CameraController controller)
  30. : base(controller)
  31. {
  32. }
  33. #endregion
  34. #region Public Methods
  35. /// <summary>
  36. /// Occurs when the position is changed during a manipulation.
  37. /// </summary>
  38. /// <param name="e">The <see cref="ManipulationEventArgs"/> instance containing the event data.</param>
  39. public override void Delta(ManipulationEventArgs e)
  40. {
  41. base.Delta(e);
  42. var thisPoint3D = this.UnProject(e.CurrentPosition, this.panPoint3D, this.Controller.CameraLookDirection);
  43. if (this.LastPoint3D == null || thisPoint3D == null)
  44. {
  45. return;
  46. }
  47. Vector3D delta3D = this.LastPoint3D.Value - thisPoint3D.Value;
  48. this.Pan(delta3D);
  49. this.LastPoint = e.CurrentPosition;
  50. this.LastPoint3D = this.UnProject(e.CurrentPosition, this.panPoint3D, this.Controller.CameraLookDirection);
  51. }
  52. /// <summary>
  53. /// Pans the camera by the specified 3D vector (world coordinates).
  54. /// </summary>
  55. /// <param name="delta">
  56. /// The panning vector.
  57. /// </param>
  58. public void Pan(Vector3D delta)
  59. {
  60. if (!this.Controller.IsPanEnabled)
  61. {
  62. return;
  63. }
  64. if (this.CameraMode == CameraMode.FixedPosition)
  65. {
  66. return;
  67. }
  68. this.CameraPosition += delta;
  69. }
  70. /// <summary>
  71. /// Pans the camera by the specified 2D vector (screen coordinates).
  72. /// </summary>
  73. /// <param name="delta">
  74. /// The delta.
  75. /// </param>
  76. public void Pan(Vector delta)
  77. {
  78. var mousePoint = this.LastPoint + delta;
  79. var thisPoint3D = this.UnProject(mousePoint, this.panPoint3D, this.Controller.CameraLookDirection);
  80. if (this.LastPoint3D == null || thisPoint3D == null)
  81. {
  82. return;
  83. }
  84. Vector3D delta3D = this.LastPoint3D.Value - thisPoint3D.Value;
  85. this.Pan(delta3D);
  86. this.LastPoint3D = this.UnProject(mousePoint, this.panPoint3D, this.Controller.CameraLookDirection);
  87. this.LastPoint = mousePoint;
  88. }
  89. /// <summary>
  90. /// Occurs when the manipulation is started.
  91. /// </summary>
  92. /// <param name="e">The <see cref="ManipulationEventArgs"/> instance containing the event data.</param>
  93. public override void Started(ManipulationEventArgs e)
  94. {
  95. base.Started(e);
  96. this.panPoint3D = this.Controller.CameraTarget;
  97. if (this.MouseDownNearestPoint3D != null)
  98. {
  99. this.panPoint3D = this.MouseDownNearestPoint3D.Value;
  100. }
  101. this.LastPoint3D = this.UnProject(this.MouseDownPoint, this.panPoint3D, this.Controller.CameraLookDirection);
  102. }
  103. #endregion
  104. #region Methods
  105. /// <summary>
  106. /// Occurs when the command associated with this handler initiates a check to determine whether the command can be executed on the command target.
  107. /// </summary>
  108. /// <returns>
  109. /// True if the execution can continue.
  110. /// </returns>
  111. protected override bool CanExecute()
  112. {
  113. return this.Controller.IsPanEnabled && this.Controller.CameraMode != CameraMode.FixedPosition;
  114. }
  115. /// <summary>
  116. /// Gets the cursor for the gesture.
  117. /// </summary>
  118. /// <returns>
  119. /// A cursor.
  120. /// </returns>
  121. protected override Cursor GetCursor()
  122. {
  123. return this.Controller.PanCursor;
  124. }
  125. /// <summary>
  126. /// Called when inertia is starting.
  127. /// </summary>
  128. /// <param name="elapsedTime">
  129. /// The elapsed time (milliseconds).
  130. /// </param>
  131. protected override void OnInertiaStarting(int elapsedTime)
  132. {
  133. var speed = (this.LastPoint - this.MouseDownPoint) * (40.0 / elapsedTime);
  134. this.Controller.AddPanForce(speed.X, speed.Y);
  135. }
  136. #endregion
  137. }
  138. }