SceneCamera.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /// <summary>
  10. /// Handles camera movement in the scene view.
  11. /// </summary>
  12. public class SceneCamera : Component
  13. {
  14. public const string MoveForwardBinding = "SceneForward";
  15. public const string MoveLeftBinding = "SceneLeft";
  16. public const string MoveRightBinding = "SceneRight";
  17. public const string MoveBackBinding = "SceneBackward";
  18. public const string FastMoveBinding = "SceneFastMove";
  19. public const string RotateBinding = "SceneRotate";
  20. public const string HorizontalAxisBinding = "SceneHorizontal";
  21. public const string VerticalAxisBinding = "SceneVertical";
  22. private const float StartSpeed = 4.0f;
  23. private const float TopSpeed = 12.0f;
  24. private const float Acceleration = 1.0f;
  25. private const float FastModeMultiplier = 2.0f;
  26. private const float RotationalSpeed = 360.0f; // Degrees/second
  27. private VirtualButton moveForwardBtn;
  28. private VirtualButton moveLeftBtn;
  29. private VirtualButton moveRightBtn;
  30. private VirtualButton moveBackwardBtn;
  31. private VirtualButton fastMoveBtn;
  32. private VirtualButton activeBtn;
  33. private VirtualAxis horizontalAxis;
  34. private VirtualAxis verticalAxis;
  35. private float currentSpeed;
  36. private Degree yaw;
  37. private Radian pitch;
  38. private bool lastButtonState;
  39. private void OnReset()
  40. {
  41. moveForwardBtn = new VirtualButton(MoveForwardBinding);
  42. moveLeftBtn = new VirtualButton(MoveLeftBinding);
  43. moveRightBtn = new VirtualButton(MoveRightBinding);
  44. moveBackwardBtn = new VirtualButton(MoveBackBinding);
  45. fastMoveBtn = new VirtualButton(FastMoveBinding);
  46. activeBtn = new VirtualButton(RotateBinding);
  47. horizontalAxis = new VirtualAxis(HorizontalAxisBinding);
  48. verticalAxis = new VirtualAxis(VerticalAxisBinding);
  49. }
  50. private void OnDisable()
  51. {
  52. if (VirtualInput.IsButtonHeld(activeBtn))
  53. Cursor.Show();
  54. }
  55. private void Update()
  56. {
  57. bool goingForward = VirtualInput.IsButtonHeld(moveForwardBtn);
  58. bool goingBack = VirtualInput.IsButtonHeld(moveBackwardBtn);
  59. bool goingLeft = VirtualInput.IsButtonHeld(moveLeftBtn);
  60. bool goingRight = VirtualInput.IsButtonHeld(moveRightBtn);
  61. bool fastMove = VirtualInput.IsButtonHeld(fastMoveBtn);
  62. bool camActive = VirtualInput.IsButtonHeld(activeBtn);
  63. if (camActive != lastButtonState)
  64. {
  65. if (camActive)
  66. Cursor.Hide();
  67. else
  68. Cursor.Show();
  69. lastButtonState = camActive;
  70. }
  71. float frameDelta = Time.FrameDelta;
  72. if (camActive)
  73. {
  74. float horzValue = VirtualInput.GetAxisValue(horizontalAxis);
  75. float vertValue = VirtualInput.GetAxisValue(verticalAxis);
  76. yaw += new Degree(horzValue * RotationalSpeed * frameDelta);
  77. pitch += new Degree(vertValue * RotationalSpeed * frameDelta);
  78. yaw = MathEx.WrapAngle(yaw);
  79. pitch = MathEx.WrapAngle(pitch);
  80. Quaternion yRot = Quaternion.FromAxisAngle(Vector3.YAxis, yaw);
  81. Quaternion xRot = Quaternion.FromAxisAngle(Vector3.XAxis, pitch);
  82. Quaternion camRot = yRot * xRot;
  83. camRot.Normalize();
  84. SceneObject.Rotation = camRot;
  85. Vector3 direction = Vector3.Zero;
  86. if (goingForward) direction += SceneObject.Forward;
  87. if (goingBack) direction -= SceneObject.Forward;
  88. if (goingRight) direction += SceneObject.Right;
  89. if (goingLeft) direction -= SceneObject.Right;
  90. if (direction.SqrdLength != 0)
  91. {
  92. direction.Normalize();
  93. float multiplier = 1.0f;
  94. if (fastMove)
  95. multiplier = FastModeMultiplier;
  96. currentSpeed = MathEx.Clamp(currentSpeed + Acceleration * frameDelta, StartSpeed, TopSpeed);
  97. currentSpeed *= multiplier;
  98. }
  99. else
  100. {
  101. currentSpeed = 0.0f;
  102. }
  103. const float tooSmall = 0.0001f;
  104. if (currentSpeed > tooSmall)
  105. {
  106. Vector3 velocity = direction * currentSpeed;
  107. SceneObject.Move(velocity * frameDelta);
  108. }
  109. }
  110. }
  111. }
  112. }