SceneCamera.cs 4.6 KB

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