SceneCamera.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 activeBtn;
  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. activeBtn = new VirtualButton(RotateBinding);
  44. horizontalAxis = new VirtualAxis(HorizontalAxisBinding);
  45. verticalAxis = new VirtualAxis(VerticalAxisBinding);
  46. }
  47. private void OnDisable()
  48. {
  49. if (VirtualInput.IsButtonHeld(activeBtn))
  50. Cursor.Show();
  51. }
  52. private void Update()
  53. {
  54. bool goingForward = VirtualInput.IsButtonHeld(moveForwardBtn);
  55. bool goingBack = VirtualInput.IsButtonHeld(moveBackwardBtn);
  56. bool goingLeft = VirtualInput.IsButtonHeld(moveLeftBtn);
  57. bool goingRight = VirtualInput.IsButtonHeld(moveRightBtn);
  58. bool fastMove = VirtualInput.IsButtonHeld(fastMoveBtn);
  59. bool camActive = VirtualInput.IsButtonHeld(activeBtn);
  60. if (camActive != lastButtonState)
  61. {
  62. if (camActive)
  63. Cursor.Hide();
  64. else
  65. Cursor.Show();
  66. lastButtonState = camActive;
  67. }
  68. float frameDelta = Time.FrameDelta;
  69. if (camActive)
  70. {
  71. float horzValue = VirtualInput.GetAxisValue(horizontalAxis);
  72. float vertValue = VirtualInput.GetAxisValue(verticalAxis);
  73. yaw += new Degree(horzValue * RotationalSpeed * frameDelta);
  74. pitch += new Degree(vertValue * RotationalSpeed * frameDelta);
  75. yaw = MathEx.WrapAngle(yaw);
  76. pitch = MathEx.WrapAngle(pitch);
  77. Quaternion yRot = Quaternion.FromAxisAngle(Vector3.yAxis, yaw);
  78. Quaternion xRot = Quaternion.FromAxisAngle(Vector3.xAxis, pitch);
  79. Quaternion camRot = yRot * xRot;
  80. camRot.Normalize();
  81. SceneObject.Rotation = camRot;
  82. Vector3 direction = Vector3.zero;
  83. if (goingForward) direction += SceneObject.Forward;
  84. if (goingBack) direction -= SceneObject.Forward;
  85. if (goingRight) direction += SceneObject.Right;
  86. if (goingLeft) direction -= SceneObject.Right;
  87. if (direction.SqrdMagnitude != 0)
  88. {
  89. direction.Normalize();
  90. float multiplier = 1.0f;
  91. if (fastMove)
  92. multiplier = FastModeMultiplier;
  93. currentSpeed = MathEx.Clamp(currentSpeed + Acceleration * frameDelta, StartSpeed, TopSpeed);
  94. currentSpeed *= multiplier;
  95. }
  96. else
  97. {
  98. currentSpeed = 0.0f;
  99. }
  100. const float tooSmall = 0.0001f;
  101. if (currentSpeed > tooSmall)
  102. {
  103. Vector3 velocity = direction * currentSpeed;
  104. SceneObject.Move(velocity * frameDelta);
  105. }
  106. }
  107. }
  108. }
  109. }