LightInspector.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. private InspectableState modifyState;
  19. /// <inheritdoc/>
  20. protected internal override void Initialize()
  21. {
  22. if (InspectedObject != null)
  23. {
  24. Light light = (Light)InspectedObject;
  25. lightTypeField.OnSelectionChanged += x =>
  26. {
  27. light.Type = (LightType)x;
  28. ToggleTypeSpecificFields((LightType) x);
  29. };
  30. colorField.OnChanged += x =>
  31. {
  32. light.Color = x;
  33. MarkAsModified();
  34. ConfirmModify();
  35. };
  36. rangeField.OnChanged += x => { light.Range = x; MarkAsModified(); };
  37. rangeField.OnConfirmed += ConfirmModify;
  38. rangeField.OnFocusLost += ConfirmModify;
  39. intensityField.OnChanged += x => { light.Intensity = x; MarkAsModified(); };
  40. intensityField.OnConfirmed += ConfirmModify;
  41. intensityField.OnFocusLost += ConfirmModify;
  42. spotAngleField.OnChanged += x => { light.SpotAngle = x; MarkAsModified(); };
  43. spotAngleField.OnFocusLost += ConfirmModify;
  44. spotFalloffAngleField.OnChanged += x => { light.SpotFalloffAngle = x; MarkAsModified(); };
  45. spotFalloffAngleField.OnFocusLost += ConfirmModify;
  46. castShadowField.OnChanged += x =>
  47. {
  48. light.CastsShadow = x;
  49. MarkAsModified();
  50. ConfirmModify();
  51. };
  52. Layout.AddElement(lightTypeField);
  53. Layout.AddElement(colorField);
  54. Layout.AddElement(intensityField);
  55. Layout.AddElement(rangeField);
  56. Layout.AddElement(spotAngleField);
  57. Layout.AddElement(spotFalloffAngleField);
  58. Layout.AddElement(castShadowField);
  59. ToggleTypeSpecificFields(light.Type);
  60. }
  61. }
  62. /// <inheritdoc/>
  63. protected internal override InspectableState Refresh()
  64. {
  65. Light light = InspectedObject as Light;
  66. if (light == null)
  67. return InspectableState.NotModified;
  68. LightType lightType = light.Type;
  69. if (lightTypeField.Value != (ulong)lightType)
  70. {
  71. lightTypeField.Value = (ulong)lightType;
  72. ToggleTypeSpecificFields(lightType);
  73. }
  74. colorField.Value = light.Color;
  75. intensityField.Value = light.Intensity;
  76. rangeField.Value = light.Range;
  77. spotAngleField.Value = light.SpotAngle.Degrees;
  78. spotFalloffAngleField.Value = light.SpotFalloffAngle.Degrees;
  79. castShadowField.Value = light.CastsShadow;
  80. InspectableState oldState = modifyState;
  81. if (modifyState.HasFlag(InspectableState.Modified))
  82. modifyState = InspectableState.NotModified;
  83. return oldState;
  84. }
  85. /// <summary>
  86. /// Enables or disables different GUI elements depending on the light type.
  87. /// </summary>
  88. /// <param name="type">Light type to show GUI elements for.</param>
  89. private void ToggleTypeSpecificFields(LightType type)
  90. {
  91. if (type == LightType.Directional)
  92. {
  93. rangeField.Active = false;
  94. spotAngleField.Active = false;
  95. spotFalloffAngleField.Active = false;
  96. }
  97. else if (type == LightType.Point)
  98. {
  99. rangeField.Active = true;
  100. spotAngleField.Active = false;
  101. spotFalloffAngleField.Active = false;
  102. }
  103. else
  104. {
  105. rangeField.Active = true;
  106. spotAngleField.Active = true;
  107. spotFalloffAngleField.Active = true;
  108. }
  109. }
  110. /// <summary>
  111. /// Marks the contents of the inspector as modified.
  112. /// </summary>
  113. protected void MarkAsModified()
  114. {
  115. modifyState |= InspectableState.ModifyInProgress;
  116. }
  117. /// <summary>
  118. /// Confirms any queued modifications.
  119. /// </summary>
  120. protected void ConfirmModify()
  121. {
  122. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  123. modifyState |= InspectableState.Modified;
  124. }
  125. }
  126. }