InspectableColor.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 GUIColorField guiField;
  11. private InspectableState state;
  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, SerializableProperty.FieldType.Color, depth, layout, property)
  22. {
  23. }
  24. /// <inheritoc/>
  25. protected internal override void Initialize(int layoutIndex)
  26. {
  27. if (property != null)
  28. {
  29. guiField = new GUIColorField(new GUIContent(title));
  30. guiField.OnChanged += OnFieldValueChanged;
  31. layout.AddElement(layoutIndex, guiField);
  32. }
  33. }
  34. /// <inheritdoc/>
  35. public override InspectableState Refresh(int layoutIndex)
  36. {
  37. if (guiField != null)
  38. guiField.Value = property.GetValue<Color>();
  39. InspectableState oldState = state;
  40. if (state.HasFlag(InspectableState.Modified))
  41. state = InspectableState.NotModified;
  42. return oldState;
  43. }
  44. /// <summary>
  45. /// Triggered when the user selects a new color.
  46. /// </summary>
  47. /// <param name="newValue">New value of the color field.</param>
  48. private void OnFieldValueChanged(Color newValue)
  49. {
  50. property.SetValue(newValue);
  51. state = InspectableState.Modified;
  52. }
  53. }
  54. }