SceneCamera.cs 4.4 KB

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