LightInspector.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /** @addtogroup Inspectors
  8. * @{
  9. */
  10. /// <summary>
  11. /// Renders an inspector for the <see cref="Light"/> component.
  12. /// </summary>
  13. [CustomInspector(typeof(Light))]
  14. internal class LightInspector : Inspector
  15. {
  16. private GUIEnumField lightTypeField = new GUIEnumField(typeof(LightType), new LocEdString("Light type"));
  17. private GUIColorField colorField = new GUIColorField(new LocEdString("Color"));
  18. private GUIFloatField attRadiusField = new GUIFloatField(new LocEdString("Attenuation radius"));
  19. private GUIFloatField sourceRadiusField = new GUIFloatField(new LocEdString("Source radius"));
  20. private GUIFloatField intensityField = new GUIFloatField(new LocEdString("Intensity"));
  21. private GUISliderField spotAngleField = new GUISliderField(1, 180, new LocEdString("Spot angle"));
  22. private GUISliderField spotFalloffAngleField = new GUISliderField(1, 180, new LocEdString("Spot falloff angle"));
  23. private GUIToggleField autoAttenuationField = new GUIToggleField(new LocEdString("Use auto. attenuation"));
  24. private GUIToggleField castShadowField = new GUIToggleField(new LocEdString("Cast shadow"));
  25. private GUISliderField shadowBiasField = new GUISliderField(-1.0f, 1.0f, new LocEdString("Shadow bias"));
  26. private InspectableState modifyState;
  27. /// <inheritdoc/>
  28. protected internal override void Initialize()
  29. {
  30. if (InspectedObject != null)
  31. {
  32. Light light = (Light)InspectedObject;
  33. lightTypeField.OnSelectionChanged += x =>
  34. {
  35. light.Type = (LightType)x;
  36. ToggleTypeSpecificFields((LightType) x, light.UseAutoAttenuation, light.CastsShadow);
  37. };
  38. colorField.OnChanged += x =>
  39. {
  40. light.Color = x;
  41. MarkAsModified();
  42. ConfirmModify();
  43. };
  44. attRadiusField.OnChanged += x => { light.AttenuationRadius = x; MarkAsModified(); };
  45. attRadiusField.OnConfirmed += ConfirmModify;
  46. attRadiusField.OnFocusLost += ConfirmModify;
  47. sourceRadiusField.OnChanged += x => { light.SourceRadius = x; MarkAsModified(); };
  48. sourceRadiusField.OnConfirmed += ConfirmModify;
  49. sourceRadiusField.OnFocusLost += ConfirmModify;
  50. intensityField.OnChanged += x => { light.Intensity = x; MarkAsModified(); };
  51. intensityField.OnConfirmed += ConfirmModify;
  52. intensityField.OnFocusLost += ConfirmModify;
  53. spotAngleField.OnChanged += x => { light.SpotAngle = (Degree)x; MarkAsModified(); };
  54. spotAngleField.OnFocusLost += ConfirmModify;
  55. spotFalloffAngleField.OnChanged += x => { light.SpotAngleFalloff = (Degree)x; MarkAsModified(); };
  56. spotFalloffAngleField.OnFocusLost += ConfirmModify;
  57. castShadowField.OnChanged += x =>
  58. {
  59. light.CastsShadow = x;
  60. ToggleTypeSpecificFields(light.Type, light.UseAutoAttenuation, x);
  61. MarkAsModified();
  62. ConfirmModify();
  63. };
  64. autoAttenuationField.OnChanged += x =>
  65. {
  66. light.UseAutoAttenuation = x;
  67. ToggleTypeSpecificFields(light.Type, x, light.CastsShadow);
  68. MarkAsModified();
  69. ConfirmModify();
  70. };
  71. shadowBiasField.OnChanged += x => { light.ShadowBias = x; MarkAsModified(); };
  72. shadowBiasField.OnFocusLost += ConfirmModify;
  73. Layout.AddElement(lightTypeField);
  74. Layout.AddElement(colorField);
  75. Layout.AddElement(intensityField);
  76. Layout.AddElement(attRadiusField);
  77. Layout.AddElement(sourceRadiusField);
  78. Layout.AddElement(spotAngleField);
  79. Layout.AddElement(spotFalloffAngleField);
  80. Layout.AddElement(autoAttenuationField);
  81. Layout.AddElement(castShadowField);
  82. Layout.AddElement(shadowBiasField);
  83. ToggleTypeSpecificFields(light.Type, light.UseAutoAttenuation, light.CastsShadow);
  84. }
  85. }
  86. /// <inheritdoc/>
  87. protected internal override InspectableState Refresh()
  88. {
  89. Light light = InspectedObject as Light;
  90. if (light == null)
  91. return InspectableState.NotModified;
  92. LightType lightType = light.Type;
  93. if (lightTypeField.Value != (ulong)lightType || autoAttenuationField.Value != light.UseAutoAttenuation)
  94. ToggleTypeSpecificFields(lightType, light.UseAutoAttenuation, light.CastsShadow);
  95. lightTypeField.Value = (ulong)lightType;
  96. colorField.Value = light.Color;
  97. intensityField.Value = light.Intensity;
  98. attRadiusField.Value = light.AttenuationRadius;
  99. sourceRadiusField.Value = light.SourceRadius;
  100. spotAngleField.Value = light.SpotAngle.Degrees;
  101. spotFalloffAngleField.Value = light.SpotAngleFalloff.Degrees;
  102. autoAttenuationField.Value = light.UseAutoAttenuation;
  103. castShadowField.Value = light.CastsShadow;
  104. shadowBiasField.Value = light.ShadowBias;
  105. InspectableState oldState = modifyState;
  106. if (modifyState.HasFlag(InspectableState.Modified))
  107. modifyState = InspectableState.NotModified;
  108. return oldState;
  109. }
  110. /// <summary>
  111. /// Enables or disables different GUI elements depending on the light type.
  112. /// </summary>
  113. /// <param name="type">Light type to show GUI elements for.</param>
  114. /// <param name="physBasedAttenuation">Determines is physically based attenuation enabled.</param>
  115. /// <param name="castsShadows">Determines if shadow specific options should be shown.</param>
  116. private void ToggleTypeSpecificFields(LightType type, bool physBasedAttenuation, bool castsShadows)
  117. {
  118. if (type == LightType.Directional)
  119. {
  120. attRadiusField.Active = false;
  121. spotAngleField.Active = false;
  122. spotFalloffAngleField.Active = false;
  123. autoAttenuationField.Active = false;
  124. }
  125. else if (type == LightType.Radial)
  126. {
  127. attRadiusField.Active = !physBasedAttenuation;
  128. spotAngleField.Active = false;
  129. spotFalloffAngleField.Active = false;
  130. autoAttenuationField.Active = true;
  131. }
  132. else
  133. {
  134. attRadiusField.Active = !physBasedAttenuation;
  135. spotAngleField.Active = true;
  136. spotFalloffAngleField.Active = true;
  137. autoAttenuationField.Active = true;
  138. }
  139. shadowBiasField.Active = castsShadows;
  140. }
  141. /// <summary>
  142. /// Marks the contents of the inspector as modified.
  143. /// </summary>
  144. protected void MarkAsModified()
  145. {
  146. modifyState |= InspectableState.ModifyInProgress;
  147. }
  148. /// <summary>
  149. /// Confirms any queued modifications.
  150. /// </summary>
  151. protected void ConfirmModify()
  152. {
  153. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  154. modifyState |= InspectableState.Modified;
  155. }
  156. }
  157. /** @} */
  158. }