InspectableColor.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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="parent">Parent Inspector this field belongs to.</param>
  16. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  17. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  18. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  19. /// contain other fields, in which case you should increase this value by one.</param>
  20. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  21. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  22. public InspectableColor(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  23. SerializableProperty property)
  24. : base(parent, title, path, SerializableProperty.FieldType.Color, depth, layout, property)
  25. {
  26. }
  27. /// <inheritoc/>
  28. protected internal override void Initialize(int layoutIndex)
  29. {
  30. if (property != null)
  31. {
  32. guiField = new GUIColorField(new GUIContent(title));
  33. guiField.OnChanged += OnFieldValueChanged;
  34. layout.AddElement(layoutIndex, guiField);
  35. }
  36. }
  37. /// <inheritdoc/>
  38. public override InspectableState Refresh(int layoutIndex)
  39. {
  40. if (guiField != null)
  41. guiField.Value = property.GetValue<Color>();
  42. InspectableState oldState = state;
  43. if (state.HasFlag(InspectableState.Modified))
  44. state = InspectableState.NotModified;
  45. return oldState;
  46. }
  47. /// <summary>
  48. /// Triggered when the user selects a new color.
  49. /// </summary>
  50. /// <param name="newValue">New value of the color field.</param>
  51. private void OnFieldValueChanged(Color newValue)
  52. {
  53. property.SetValue(newValue);
  54. state = InspectableState.Modified;
  55. }
  56. }
  57. }