LightProbeVolumeGizmo.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Scene-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Render individual nodes of a light probe volume and allows the user to select and move them.
  13. /// </summary>
  14. [CustomHandle(typeof(LightProbeVolume))]
  15. internal class LightProbeVolumeNodeHandles : Handle
  16. {
  17. /// <summary>
  18. /// Returns the handle of the currently selected node in the light probe volume. Is set to uint.MaxValue if no
  19. /// node is selected.
  20. /// </summary>
  21. internal static uint SelectedNode
  22. {
  23. get { return selectedNode; }
  24. }
  25. private LightProbeVolume volume;
  26. private List<HandleSliderSphere> nodeColliders = new List<HandleSliderSphere>();
  27. private LightProbeInfo[] probeInfos;
  28. private MoveHandle moveHandle;
  29. private static uint selectedNode = uint.MaxValue;
  30. /// <summary>
  31. /// Creates a new instance of the object.
  32. /// </summary>
  33. public LightProbeVolumeNodeHandles(LightProbeVolume volume)
  34. {
  35. this.volume = volume;
  36. selectedNode = uint.MaxValue;
  37. }
  38. /// <inheritdoc/>
  39. protected internal override void PreInput()
  40. {
  41. Matrix4 transform = volume.SceneObject.WorldTransform;
  42. probeInfos = volume.GetProbes();
  43. for (int i = 0; i < probeInfos.Length; i++)
  44. {
  45. if (i == nodeColliders.Count)
  46. nodeColliders.Add(new HandleSliderSphere(this, 1.0f, false));
  47. Vector3 position = probeInfos[i].position;
  48. position = transform.MultiplyAffine(position);
  49. nodeColliders[i].Position = position;
  50. }
  51. while (nodeColliders.Count > probeInfos.Length)
  52. {
  53. nodeColliders[nodeColliders.Count - 1].Destroy();
  54. nodeColliders.RemoveAt(nodeColliders.Count - 1);
  55. }
  56. if (selectedNode != uint.MaxValue)
  57. {
  58. if (moveHandle == null)
  59. moveHandle = new MoveHandle();
  60. Vector3 position = Vector3.Zero;
  61. for (int i = 0; i < probeInfos.Length; i++)
  62. {
  63. if (probeInfos[i].handle == selectedNode)
  64. {
  65. position = transform.MultiplyAffine(probeInfos[i].position);
  66. break;
  67. }
  68. }
  69. moveHandle.Position = position;
  70. moveHandle.PreInput();
  71. }
  72. }
  73. /// <inheritdoc/>
  74. protected internal override void PostInput()
  75. {
  76. for (int i = 0; i < nodeColliders.Count; i++)
  77. {
  78. if (nodeColliders[i].State == HandleSlider.StateType.Active)
  79. {
  80. selectedNode = (uint) probeInfos[i].handle;
  81. break;
  82. }
  83. }
  84. if (selectedNode != uint.MaxValue && moveHandle != null)
  85. {
  86. moveHandle.PostInput();
  87. if (moveHandle.IsDragged())
  88. volume.SetProbePosition((int) selectedNode, moveHandle.Position + moveHandle.Delta);
  89. }
  90. }
  91. /// <inheritdoc/>
  92. protected internal override void Draw()
  93. {
  94. HandleDrawing.Transform = Matrix4.TRS(volume.SceneObject.Position, volume.SceneObject.Rotation, Vector3.One);
  95. HandleDrawing.Color = Color.White;
  96. foreach (var entry in probeInfos)
  97. HandleDrawing.DrawSphere(entry.position, 1.0f);
  98. }
  99. }
  100. /** @} */
  101. /** @addtogroup Gizmos
  102. * @{
  103. */
  104. /// <summary>
  105. /// Handles drawing of gizmos for <see cref="LightProbeVolume"/> component.
  106. /// </summary>
  107. internal class LightProbeVolumeGizmos
  108. {
  109. /// <summary>
  110. /// Draws light probe volume icon in scene view.
  111. /// </summary>
  112. /// <param name="volume">Light probe volume to draw the icon for.</param>
  113. [DrawGizmo(DrawGizmoFlags.NotSelected | DrawGizmoFlags.Pickable)]
  114. private static void DrawIcon(LightProbeVolume volume)
  115. {
  116. Gizmos.DrawIcon(volume.SceneObject.Position,
  117. EditorBuiltin.GetSceneViewIcon(SceneViewIcon.LightProbes), false);
  118. }
  119. }
  120. /** @} */
  121. }