InspectableString.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 string.
  11. /// </summary>
  12. public class InspectableString : InspectableField
  13. {
  14. private string propertyValue;
  15. private GUITextField guiField;
  16. private bool isInitialized;
  17. /// <summary>
  18. /// Creates a new inspectable string GUI for the specified property.
  19. /// </summary>
  20. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  21. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  22. /// contain other fields, in which case you should increase this value by one.</param>
  23. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  24. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  25. public InspectableString(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  26. : base(title, depth, layout, property)
  27. {
  28. }
  29. /// <summary>
  30. /// Initializes the GUI elements the first time <see cref="Update"/> gets called.
  31. /// </summary>
  32. /// <param name="layoutIndex">Index at which to insert the GUI elements.</param>
  33. private void Initialize(int layoutIndex)
  34. {
  35. if (property.Type == SerializableProperty.FieldType.String)
  36. {
  37. guiField = new GUITextField(new GUIContent(title));
  38. guiField.OnChanged += OnFieldValueChanged;
  39. layout.AddElement(layoutIndex, guiField);
  40. }
  41. isInitialized = true;
  42. }
  43. /// <inheritdoc/>
  44. protected override bool IsModified()
  45. {
  46. if (!isInitialized)
  47. return true;
  48. string newPropertyValue = property.GetValue<string>();
  49. if (propertyValue != newPropertyValue)
  50. return true;
  51. return base.IsModified();
  52. }
  53. /// <inheritdoc/>
  54. protected override void Update(int layoutIndex)
  55. {
  56. base.Update(layoutIndex);
  57. if (!isInitialized)
  58. Initialize(layoutIndex);
  59. propertyValue = property.GetValue<string>();
  60. if (guiField != null)
  61. {
  62. if (guiField.HasInputFocus())
  63. return;
  64. guiField.Value = propertyValue;
  65. }
  66. }
  67. /// <summary>
  68. /// Triggered when the user inputs a new string.
  69. /// </summary>
  70. /// <param name="newValue">New value of the string field.</param>
  71. private void OnFieldValueChanged(string newValue)
  72. {
  73. property.SetValue(newValue);
  74. }
  75. }
  76. }