Handles.cs 4.1 KB

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