LightProbeVolumeInspector.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /** @addtogroup Inspectors
  8. * @{
  9. */
  10. /// <summary>
  11. /// Renders an inspector for the <see cref="LightProbeVolume"/> component.
  12. /// </summary>
  13. [CustomInspector(typeof(LightProbeVolume))]
  14. internal class LightProbeVolumeInspector : Inspector
  15. {
  16. private GUILabel manualLabel = new GUILabel(new LocEdString("Manual probe placement"));
  17. private GUIButton addProbeButton = new GUIButton(new LocEdString("Add probe"));
  18. private GUIButton removeProbeButton = new GUIButton(new LocEdString("Remove probe"));
  19. private GUILabel uniformLabel = new GUILabel(new LocEdString("Uniform grid probe placement"));
  20. private GUIVector3Field positionField = new GUIVector3Field(new LocEdString("Grid origin"));
  21. private GUIVector3Field sizeField = new GUIVector3Field(new LocEdString("Grid size"));
  22. private GUIVector3Field densityField = new GUIVector3Field(new LocEdString("Grid density"));
  23. private GUIButton resetToGridButton = new GUIButton(new LocEdString("Reset to grid"));
  24. private GUIButton clipOuterButton = new GUIButton(new LocEdString("Clip to volume"));
  25. private GUIButton renderButton = new GUIButton(new LocEdString("Render"));
  26. private InspectableState modifyState;
  27. /// <inheritdoc/>
  28. protected internal override void Initialize()
  29. {
  30. BuildGUI();
  31. }
  32. /// <inheritdoc/>
  33. protected internal override InspectableState Refresh()
  34. {
  35. LightProbeVolume lpv = InspectedObject as LightProbeVolume;
  36. if (lpv == null)
  37. return InspectableState.NotModified;
  38. InspectableState oldState = modifyState;
  39. if (modifyState.HasFlag(InspectableState.Modified))
  40. modifyState = InspectableState.NotModified;
  41. // Don't update fields while modification in progress
  42. if (modifyState == InspectableState.NotModified)
  43. {
  44. AABox gridVolume = lpv.GridVolume;
  45. Vector3 size = gridVolume.Maximum - gridVolume.Minimum;
  46. Vector3 position = gridVolume.Minimum + size * 0.5f;
  47. positionField.Value = position;
  48. sizeField.Value = size;
  49. Vector3I cellCount = lpv.CellCount;
  50. densityField.Value = new Vector3(cellCount.x, cellCount.y, cellCount.z);
  51. }
  52. return oldState;
  53. }
  54. /// <summary>
  55. /// Recreates all the GUI elements used by this inspector.
  56. /// </summary>
  57. private void BuildGUI()
  58. {
  59. Layout.Clear();
  60. LightProbeVolume lpv = InspectedObject as LightProbeVolume;
  61. if (lpv == null)
  62. return;
  63. // Set up callbacks
  64. addProbeButton.OnClick += () =>
  65. {
  66. lpv.AddProbe(Vector3.Zero);
  67. MarkAsModified();
  68. ConfirmModify();
  69. };
  70. removeProbeButton.OnClick += () =>
  71. {
  72. if (LightProbeVolumeNodeHandles.SelectedNode != uint.MaxValue)
  73. lpv.RemoveProbe(LightProbeVolumeNodeHandles.SelectedNode);
  74. MarkAsModified();
  75. ConfirmModify();
  76. };
  77. positionField.OnConfirmed += () =>
  78. {
  79. AABox gridVolume = lpv.GridVolume;
  80. Vector3 extents = (gridVolume.Maximum - gridVolume.Minimum) * 0.5f;
  81. Vector3 min = positionField.Value - extents;
  82. Vector3 max = positionField.Value + extents;
  83. Vector3I cellCount = lpv.CellCount;
  84. gridVolume = new AABox(min, max);
  85. lpv.Resize(gridVolume, cellCount);
  86. MarkAsModified();
  87. ConfirmModify();
  88. };
  89. sizeField.OnConfirmed += () =>
  90. {
  91. AABox gridVolume = lpv.GridVolume;
  92. Vector3 min = gridVolume.Minimum;
  93. Vector3 max = min + sizeField.Value;
  94. Vector3I cellCount = lpv.CellCount;
  95. gridVolume = new AABox(min, max);
  96. lpv.Resize(gridVolume, cellCount);
  97. MarkAsModified();
  98. ConfirmModify();
  99. };
  100. densityField.OnConfirmed += () =>
  101. {
  102. AABox gridVolume = lpv.GridVolume;
  103. Vector3 density = densityField.Value;
  104. Vector3I cellCount = new Vector3I((int)density.x, (int)density.y, (int)density.y);
  105. lpv.Resize(gridVolume, cellCount);
  106. MarkAsModified();
  107. ConfirmModify();
  108. };
  109. resetToGridButton.OnClick += () =>
  110. {
  111. lpv.Reset();
  112. MarkAsModified();
  113. ConfirmModify();
  114. };
  115. clipOuterButton.OnClick += () =>
  116. {
  117. lpv.Clip();
  118. MarkAsModified();
  119. ConfirmModify();
  120. };
  121. renderButton.OnClick += () =>
  122. {
  123. lpv.RenderProbes();
  124. MarkAsModified();
  125. ConfirmModify();
  126. };
  127. // Set up layout
  128. Layout.AddElement(manualLabel);
  129. GUILayout manualLayout = Layout.AddLayoutX();
  130. manualLayout.AddElement(addProbeButton);
  131. manualLayout.AddElement(removeProbeButton);
  132. Layout.AddSpace(10);
  133. Layout.AddElement(uniformLabel);
  134. Layout.AddElement(positionField);
  135. Layout.AddElement(sizeField);
  136. Layout.AddElement(densityField);
  137. Layout.AddSpace(5);
  138. GUILayout uniformLayout = Layout.AddLayoutX();
  139. uniformLayout.AddElement(resetToGridButton);
  140. uniformLayout.AddElement(clipOuterButton);
  141. Layout.AddSpace(10);
  142. Layout.AddElement(renderButton);
  143. }
  144. /// <summary>
  145. /// Marks the contents of the inspector as modified.
  146. /// </summary>
  147. protected void MarkAsModified()
  148. {
  149. modifyState |= InspectableState.ModifyInProgress;
  150. }
  151. /// <summary>
  152. /// Confirms any queued modifications.
  153. /// </summary>
  154. protected void ConfirmModify()
  155. {
  156. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  157. modifyState |= InspectableState.Modified;
  158. }
  159. }
  160. /** @} */
  161. }