InspectableColor.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Displays GUI for a serializable property containing a color. Color is displayed as a GUI color field that allows
  8. /// the user to manipulate the color using a color picker.
  9. /// </summary>
  10. public class InspectableColor : InspectableField
  11. {
  12. private GUIColorField guiField;
  13. private InspectableState state;
  14. /// <summary>
  15. /// Creates a new inspectable color GUI for the specified property.
  16. /// </summary>
  17. /// <param name="parent">Parent Inspector this field belongs to.</param>
  18. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  19. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  20. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  21. /// contain other fields, in which case you should increase this value by one.</param>
  22. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  23. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  24. public InspectableColor(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  25. SerializableProperty property)
  26. : base(parent, title, path, SerializableProperty.FieldType.Color, depth, layout, property)
  27. {
  28. }
  29. /// <inheritoc/>
  30. protected internal override void Initialize(int layoutIndex)
  31. {
  32. if (property != null)
  33. {
  34. guiField = new GUIColorField(new GUIContent(title));
  35. guiField.OnChanged += OnFieldValueChanged;
  36. layout.AddElement(layoutIndex, guiField);
  37. }
  38. }
  39. /// <inheritdoc/>
  40. public override InspectableState Refresh(int layoutIndex)
  41. {
  42. if (guiField != null)
  43. guiField.Value = property.GetValue<Color>();
  44. InspectableState oldState = state;
  45. if (state.HasFlag(InspectableState.Modified))
  46. state = InspectableState.NotModified;
  47. return oldState;
  48. }
  49. /// <summary>
  50. /// Triggered when the user selects a new color.
  51. /// </summary>
  52. /// <param name="newValue">New value of the color field.</param>
  53. private void OnFieldValueChanged(Color newValue)
  54. {
  55. property.SetValue(newValue);
  56. state = InspectableState.Modified;
  57. }
  58. }
  59. }