2
0

InspectableColor.cs 3.1 KB

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