Handles.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Manages various global values relating to handles.
  7. /// </summary>
  8. public sealed class Handles
  9. {
  10. /// <summary>
  11. /// Determines if snapping for move handle is active. When active the move handle can only be moved in increments
  12. /// specified by <see cref="MoveSnapAmount"/>.
  13. /// </summary>
  14. public static bool MoveHandleSnapActive
  15. {
  16. get { return EditorSettings.MoveHandleSnapActive; }
  17. set { EditorSettings.MoveHandleSnapActive = value; }
  18. }
  19. /// <summary>
  20. /// Determines if snapping for rotate handle is active. When active the rotate handle can only be rotated in
  21. /// increments specified by <see cref="RotateSnapAmount"/>.
  22. /// </summary>
  23. public static bool RotateHandleSnapActive
  24. {
  25. get { return EditorSettings.RotateHandleSnapActive; }
  26. set { EditorSettings.RotateHandleSnapActive = value; }
  27. }
  28. /// <summary>
  29. /// Determines size of the increments the move handle can be moved when <see cref="MoveHandleSnapActive"/> is
  30. /// active.
  31. /// </summary>
  32. public static float MoveSnapAmount
  33. {
  34. get { return EditorSettings.MoveHandleSnapAmount; }
  35. set { EditorSettings.MoveHandleSnapAmount = value; }
  36. }
  37. /// <summary>
  38. /// Determines size of the increments the rotate handle can be moved when <see cref="RotateHandleSnapActive"/> is
  39. /// active.
  40. /// </summary>
  41. public static Degree RotateSnapAmount
  42. {
  43. get { return EditorSettings.RotateHandleSnapAmount; }
  44. set { EditorSettings.RotateHandleSnapAmount = value; }
  45. }
  46. /// <summary>
  47. /// Snaps a value to the specified increments.
  48. /// </summary>
  49. /// <param name="value">Value to snap.</param>
  50. /// <param name="snapAmount">Increment to which to snap the value to.</param>
  51. /// <returns>Value snapped to the provided increments.</returns>
  52. public static float SnapValue(float value, float snapAmount)
  53. {
  54. if (snapAmount > 0)
  55. return MathEx.RoundToInt(value / snapAmount) * snapAmount;
  56. return value;
  57. }
  58. /// <summary>
  59. /// Snaps an angle value to the specified increments.
  60. /// </summary>
  61. /// <param name="value">Value to snap.</param>
  62. /// <param name="snapAmount">Increment to which to snap the value to.</param>
  63. /// <returns>Value snapped to the provided increments.</returns>
  64. public static Degree SnapValue(Degree value, Degree snapAmount)
  65. {
  66. return SnapValue(value.Degrees, snapAmount.Degrees);
  67. }
  68. /// <summary>
  69. /// Returns a scale that can be applied to a handle in order to keep it at constant size regardless of distance
  70. /// from the provided camera.
  71. /// </summary>
  72. /// <param name="camera">Camera through which the handle is being viewed.</param>
  73. /// <param name="position">Center of the handle.</param>
  74. /// <returns>Uniform scale to apply to the handle.</returns>
  75. public static float GetHandleSize(Camera camera, Vector3 position)
  76. {
  77. Vector3 cameraPos = camera.SceneObject.Position;
  78. Vector3 diff = position - cameraPos;
  79. float distAlongViewDir = Math.Abs(Vector3.Dot(diff, camera.SceneObject.Rotation.Forward));
  80. return distAlongViewDir * EditorSettings.DefaultHandleSize;
  81. }
  82. }
  83. }