SceneCamera.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. public class SceneCamera : Component
  10. {
  11. public const string MoveForwardBinding = "SceneForward";
  12. public const string MoveLeftBinding = "SceneLeft";
  13. public const string MoveRightBinding = "SceneRight";
  14. public const string MoveBackBinding = "SceneBackward";
  15. public const string FastMoveBinding = "SceneFastMove";
  16. public const string RotateBinding = "SceneRotate";
  17. public const string HorizontalAxisBinding = "SceneHorizontal";
  18. public const string VerticalAxisBinding = "SceneVertical";
  19. private const float StartSpeed = 4.0f;
  20. private const float TopSpeed = 12.0f;
  21. private const float Acceleration = 1.0f;
  22. private const float FastModeMultiplier = 2.0f;
  23. private const float RotationalSpeed = 360.0f; // Degrees/second
  24. private VirtualButton moveForwardBtn;
  25. private VirtualButton moveLeftBtn;
  26. private VirtualButton moveRightBtn;
  27. private VirtualButton moveBackwardBtn;
  28. private VirtualButton fastMoveBtn;
  29. private VirtualButton rotateBtn;
  30. private VirtualAxis horizontalAxis;
  31. private VirtualAxis verticalAxis;
  32. private float currentSpeed;
  33. private Degree yaw;
  34. private Radian pitch;
  35. private bool lastButtonState;
  36. private void OnReset()
  37. {
  38. moveForwardBtn = new VirtualButton(MoveForwardBinding);
  39. moveLeftBtn = new VirtualButton(MoveLeftBinding);
  40. moveRightBtn = new VirtualButton(MoveRightBinding);
  41. moveBackwardBtn = new VirtualButton(MoveBackBinding);
  42. fastMoveBtn = new VirtualButton(FastMoveBinding);
  43. rotateBtn = new VirtualButton(RotateBinding);
  44. horizontalAxis = new VirtualAxis(HorizontalAxisBinding);
  45. verticalAxis = new VirtualAxis(VerticalAxisBinding);
  46. }
  47. private void Update()
  48. {
  49. // TODO - Only move if scene view is focused
  50. bool goingForward = VirtualInput.IsButtonHeld(moveForwardBtn);
  51. bool goingBack = VirtualInput.IsButtonHeld(moveBackwardBtn);
  52. bool goingLeft = VirtualInput.IsButtonHeld(moveLeftBtn);
  53. bool goingRight = VirtualInput.IsButtonHeld(moveRightBtn);
  54. bool fastMove = VirtualInput.IsButtonHeld(fastMoveBtn);
  55. bool camRotating = VirtualInput.IsButtonHeld(rotateBtn);
  56. if (camRotating != lastButtonState)
  57. {
  58. if (camRotating)
  59. Cursor.Hide();
  60. else
  61. Cursor.Show();
  62. lastButtonState = camRotating;
  63. }
  64. float frameDelta = Time.frameDelta;
  65. if (camRotating)
  66. {
  67. float horzValue = VirtualInput.GetAxisValue(horizontalAxis);
  68. float vertValue = VirtualInput.GetAxisValue(verticalAxis);
  69. yaw += new Degree(horzValue * RotationalSpeed * frameDelta);
  70. pitch += new Degree(vertValue * RotationalSpeed * frameDelta);
  71. yaw = MathEx.WrapAngle(yaw);
  72. pitch = MathEx.WrapAngle(pitch);
  73. Quaternion yRot = Quaternion.FromAxisAngle(Vector3.yAxis, yaw);
  74. Quaternion xRot = Quaternion.FromAxisAngle(Vector3.xAxis, pitch);
  75. Quaternion camRot = yRot * xRot;
  76. camRot.Normalize();
  77. sceneObject.rotation = camRot;
  78. }
  79. Vector3 direction = Vector3.zero;
  80. if (goingForward) direction += sceneObject.forward;
  81. if (goingBack) direction -= sceneObject.forward;
  82. if (goingRight) direction += sceneObject.right;
  83. if (goingLeft) direction -= sceneObject.right;
  84. if (direction.sqrdMagnitude != 0)
  85. {
  86. direction.Normalize();
  87. float multiplier = 1.0f;
  88. if (fastMove)
  89. multiplier = FastModeMultiplier;
  90. currentSpeed = MathEx.Clamp(currentSpeed + Acceleration * frameDelta, StartSpeed, TopSpeed);
  91. currentSpeed *= multiplier;
  92. }
  93. else
  94. {
  95. currentSpeed = 0.0f;
  96. }
  97. const float tooSmall = 0.0001f;
  98. if (currentSpeed > tooSmall)
  99. {
  100. Vector3 velocity = direction * currentSpeed;
  101. sceneObject.Move(velocity * frameDelta);
  102. }
  103. }
  104. }
  105. }