Handles.cs 4.1 KB

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