LightInspector.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Renders an inspector for the <see cref="Light"/> component.
  9. /// </summary>
  10. [CustomInspector(typeof(Light))]
  11. public class LightInspector : Inspector
  12. {
  13. private GUIEnumField lightTypeField = new GUIEnumField(typeof(LightType), new LocEdString("Light type"));
  14. private GUIColorField colorField = new GUIColorField(new LocEdString("Color"));
  15. private GUIFloatField rangeField = new GUIFloatField(new LocEdString("Range"));
  16. private GUIFloatField intensityField = new GUIFloatField(new LocEdString("Intensity"));
  17. private GUISliderField spotAngleField = new GUISliderField(1, 180, new LocEdString("Spot angle"));
  18. private GUISliderField spotFalloffAngleField = new GUISliderField(1, 180, new LocEdString("Spot falloff angle"));
  19. private GUIToggleField physBasedAttenField = new GUIToggleField(new LocEdString("Physically based attenuation"));
  20. private GUIToggleField castShadowField = new GUIToggleField(new LocEdString("Cast shadow"));
  21. private InspectableState modifyState;
  22. /// <inheritdoc/>
  23. protected internal override void Initialize()
  24. {
  25. if (InspectedObject != null)
  26. {
  27. Light light = (Light)InspectedObject;
  28. lightTypeField.OnSelectionChanged += x =>
  29. {
  30. light.Type = (LightType)x;
  31. ToggleTypeSpecificFields((LightType) x, light.PhysicallyBasedAttenuation);
  32. };
  33. colorField.OnChanged += x =>
  34. {
  35. light.Color = x;
  36. MarkAsModified();
  37. ConfirmModify();
  38. };
  39. rangeField.OnChanged += x => { light.Range = x; MarkAsModified(); };
  40. rangeField.OnConfirmed += ConfirmModify;
  41. rangeField.OnFocusLost += ConfirmModify;
  42. intensityField.OnChanged += x => { light.Intensity = x; MarkAsModified(); };
  43. intensityField.OnConfirmed += ConfirmModify;
  44. intensityField.OnFocusLost += ConfirmModify;
  45. spotAngleField.OnChanged += x => { light.SpotAngle = (Degree)x; MarkAsModified(); };
  46. spotAngleField.OnFocusLost += ConfirmModify;
  47. spotFalloffAngleField.OnChanged += x => { light.SpotFalloffAngle = (Degree)x; MarkAsModified(); };
  48. spotFalloffAngleField.OnFocusLost += ConfirmModify;
  49. castShadowField.OnChanged += x =>
  50. {
  51. light.CastsShadow = x;
  52. MarkAsModified();
  53. ConfirmModify();
  54. };
  55. physBasedAttenField.OnChanged += x =>
  56. {
  57. light.PhysicallyBasedAttenuation = x;
  58. ToggleTypeSpecificFields(light.Type, x);
  59. MarkAsModified();
  60. ConfirmModify();
  61. };
  62. Layout.AddElement(lightTypeField);
  63. Layout.AddElement(colorField);
  64. Layout.AddElement(intensityField);
  65. Layout.AddElement(rangeField);
  66. Layout.AddElement(spotAngleField);
  67. Layout.AddElement(spotFalloffAngleField);
  68. Layout.AddElement(physBasedAttenField);
  69. Layout.AddElement(castShadowField);
  70. ToggleTypeSpecificFields(light.Type, light.PhysicallyBasedAttenuation);
  71. }
  72. }
  73. /// <inheritdoc/>
  74. protected internal override InspectableState Refresh()
  75. {
  76. Light light = InspectedObject as Light;
  77. if (light == null)
  78. return InspectableState.NotModified;
  79. LightType lightType = light.Type;
  80. if (lightTypeField.Value != (ulong)lightType || physBasedAttenField.Value != light.PhysicallyBasedAttenuation)
  81. ToggleTypeSpecificFields(lightType, light.PhysicallyBasedAttenuation);
  82. lightTypeField.Value = (ulong)lightType;
  83. colorField.Value = light.Color;
  84. intensityField.Value = light.Intensity;
  85. rangeField.Value = light.Range;
  86. spotAngleField.Value = light.SpotAngle.Degrees;
  87. spotFalloffAngleField.Value = light.SpotFalloffAngle.Degrees;
  88. physBasedAttenField.Value = light.PhysicallyBasedAttenuation;
  89. castShadowField.Value = light.CastsShadow;
  90. InspectableState oldState = modifyState;
  91. if (modifyState.HasFlag(InspectableState.Modified))
  92. modifyState = InspectableState.NotModified;
  93. return oldState;
  94. }
  95. /// <summary>
  96. /// Enables or disables different GUI elements depending on the light type.
  97. /// </summary>
  98. /// <param name="type">Light type to show GUI elements for.</param>
  99. /// <param name="physBasedAttenuation">Determines is physically based attenuation enabled.</param>
  100. private void ToggleTypeSpecificFields(LightType type, bool physBasedAttenuation)
  101. {
  102. if (type == LightType.Directional)
  103. {
  104. rangeField.Active = false;
  105. spotAngleField.Active = false;
  106. spotFalloffAngleField.Active = false;
  107. physBasedAttenField.Active = false;
  108. }
  109. else if (type == LightType.Point)
  110. {
  111. rangeField.Active = !physBasedAttenuation;
  112. spotAngleField.Active = false;
  113. spotFalloffAngleField.Active = false;
  114. physBasedAttenField.Active = true;
  115. }
  116. else
  117. {
  118. rangeField.Active = !physBasedAttenuation;
  119. spotAngleField.Active = true;
  120. spotFalloffAngleField.Active = true;
  121. physBasedAttenField.Active = true;
  122. }
  123. }
  124. /// <summary>
  125. /// Marks the contents of the inspector as modified.
  126. /// </summary>
  127. protected void MarkAsModified()
  128. {
  129. modifyState |= InspectableState.ModifyInProgress;
  130. }
  131. /// <summary>
  132. /// Confirms any queued modifications.
  133. /// </summary>
  134. protected void ConfirmModify()
  135. {
  136. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  137. modifyState |= InspectableState.Modified;
  138. }
  139. }
  140. }