InspectableColor.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. /// <summary>
  5. /// Displays GUI for a serializable property containing a color. Color is displayed as a GUI color field that allows
  6. /// the user to manipulate the color using a color picker.
  7. /// </summary>
  8. public class InspectableColor : InspectableField
  9. {
  10. private Color propertyValue;
  11. private GUIColorField guiField;
  12. /// <summary>
  13. /// Creates a new inspectable color GUI for the specified property.
  14. /// </summary>
  15. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  16. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  17. /// contain other fields, in which case you should increase this value by one.</param>
  18. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  19. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  20. public InspectableColor(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  21. : base(title, depth, layout, property)
  22. {
  23. }
  24. /// <inheritoc/>
  25. protected internal override void BuildGUI(int layoutIndex)
  26. {
  27. if (property.Type == SerializableProperty.FieldType.Color)
  28. {
  29. guiField = new GUIColorField(new GUIContent(title));
  30. guiField.OnChanged += OnFieldValueChanged;
  31. layout.AddElement(layoutIndex, guiField);
  32. }
  33. }
  34. /// <inheritdoc/>
  35. public override bool IsModified()
  36. {
  37. Color newPropertyValue = property.GetValue<Color>();
  38. if (propertyValue != newPropertyValue)
  39. return true;
  40. return base.IsModified();
  41. }
  42. /// <inheritdoc/>
  43. protected internal override void Update(int layoutIndex)
  44. {
  45. // TODO - Skip update if it currently has input focus so user can modify the value in peace
  46. propertyValue = property.GetValue<Color>();
  47. if (guiField != null)
  48. guiField.Value = propertyValue;
  49. }
  50. /// <summary>
  51. /// Triggered when the user selects a new color.
  52. /// </summary>
  53. /// <param name="newValue">New value of the color field.</param>
  54. private void OnFieldValueChanged(Color newValue)
  55. {
  56. property.SetValue(newValue);
  57. }
  58. }
  59. }