LightInspector.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections.Generic;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Renders an inspector for the <see cref="Light"/> component.
  7. /// </summary>
  8. [CustomInspector(typeof(Light))]
  9. public class LightInspector : Inspector
  10. {
  11. private GUIEnumField lightTypeField = new GUIEnumField(typeof(LightType), new LocEdString("Light type"));
  12. private GUIColorField colorField = new GUIColorField(new LocEdString("Color"));
  13. private GUIFloatField rangeField = new GUIFloatField(new LocEdString("Range"));
  14. private GUIFloatField intensityField = new GUIFloatField(new LocEdString("Intensity"));
  15. private GUISliderField spotAngleField = new GUISliderField(1, 180, new LocEdString("Spot angle"));
  16. private GUISliderField spotFalloffAngleField = new GUISliderField(1, 180, new LocEdString("Spot falloff angle"));
  17. private GUIToggleField castShadowField = new GUIToggleField(new LocEdString("Cast shadow"));
  18. /// <inheritdoc/>
  19. protected internal override void Initialize()
  20. {
  21. if (referencedObject != null)
  22. {
  23. Light light = (Light)referencedObject;
  24. lightTypeField.OnSelectionChanged += x =>
  25. {
  26. light.Type = (LightType)x;
  27. ToggleTypeSpecificFields((LightType) x);
  28. };
  29. colorField.OnChanged += x => light.Color = x;
  30. rangeField.OnChanged += x => light.Range = x;
  31. intensityField.OnChanged += x => light.Intensity = x;
  32. spotAngleField.OnChanged += x => light.SpotAngle = x;
  33. spotFalloffAngleField.OnChanged += x => light.SpotFalloffAngle = x;
  34. castShadowField.OnChanged += x => light.CastsShadow = x;
  35. layout.AddElement(lightTypeField);
  36. layout.AddElement(colorField);
  37. layout.AddElement(intensityField);
  38. layout.AddElement(rangeField);
  39. layout.AddElement(spotAngleField);
  40. layout.AddElement(spotFalloffAngleField);
  41. layout.AddElement(castShadowField);
  42. ToggleTypeSpecificFields(light.Type);
  43. }
  44. }
  45. /// <inheritdoc/>
  46. protected internal override bool Refresh()
  47. {
  48. Light light = referencedObject as Light;
  49. if (light == null)
  50. return false;
  51. bool anythingModified = false;
  52. LightType lightType = light.Type;
  53. if (lightTypeField.Value != (ulong)lightType)
  54. {
  55. lightTypeField.Value = (ulong)lightType;
  56. ToggleTypeSpecificFields(lightType);
  57. anythingModified = true;
  58. }
  59. if (colorField.Value != light.Color)
  60. {
  61. colorField.Value = light.Color;
  62. anythingModified = true;
  63. }
  64. if (intensityField.Value != light.Intensity)
  65. {
  66. intensityField.Value = light.Intensity;
  67. anythingModified = true;
  68. }
  69. if (rangeField.Value != light.Range)
  70. {
  71. rangeField.Value = light.Range;
  72. anythingModified = true;
  73. }
  74. if (spotAngleField.Value != light.SpotAngle.Degrees)
  75. {
  76. spotAngleField.Value = light.SpotAngle.Degrees;
  77. anythingModified = true;
  78. }
  79. if (spotFalloffAngleField.Value != light.SpotFalloffAngle.Degrees)
  80. {
  81. spotFalloffAngleField.Value = light.SpotFalloffAngle.Degrees;
  82. anythingModified = true;
  83. }
  84. if (castShadowField.Value != light.CastsShadow)
  85. {
  86. castShadowField.Value = light.CastsShadow;
  87. anythingModified = true;
  88. }
  89. return anythingModified;
  90. }
  91. /// <summary>
  92. /// Enables or disables different GUI elements depending on the light type.
  93. /// </summary>
  94. /// <param name="type">Light type to show GUI elements for.</param>
  95. private void ToggleTypeSpecificFields(LightType type)
  96. {
  97. if (type == LightType.Directional)
  98. {
  99. rangeField.Enabled = false;
  100. spotAngleField.Enabled = false;
  101. spotFalloffAngleField.Enabled = false;
  102. }
  103. else if (type == LightType.Point)
  104. {
  105. rangeField.Enabled = true;
  106. spotAngleField.Enabled = false;
  107. spotFalloffAngleField.Enabled = false;
  108. }
  109. else
  110. {
  111. rangeField.Enabled = true;
  112. spotAngleField.Enabled = true;
  113. spotFalloffAngleField.Enabled = true;
  114. }
  115. }
  116. }
  117. }