InspectableString.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /// <summary>
  17. /// Creates a new inspectable string GUI for the specified property.
  18. /// </summary>
  19. /// <param name="title">Name of the property, or some other value to set as the title.</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 InspectableString(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  25. : base(title, depth, layout, property)
  26. {
  27. }
  28. /// <inheritoc/>
  29. protected override void BuildGUI(int layoutIndex)
  30. {
  31. if (property.Type == SerializableProperty.FieldType.String)
  32. {
  33. guiField = new GUITextField(new GUIContent(title));
  34. guiField.OnChanged += OnFieldValueChanged;
  35. layout.AddElement(layoutIndex, guiField);
  36. }
  37. }
  38. /// <inheritdoc/>
  39. protected override bool IsModified(out bool rebuildGUI)
  40. {
  41. string newPropertyValue = property.GetValue<string>();
  42. if (propertyValue != newPropertyValue)
  43. {
  44. rebuildGUI = false;
  45. return true;
  46. }
  47. return base.IsModified(out rebuildGUI);
  48. }
  49. /// <inheritdoc/>
  50. protected override void Update(int layoutIndex, bool rebuildGUI)
  51. {
  52. base.Update(layoutIndex, rebuildGUI);
  53. propertyValue = property.GetValue<string>();
  54. if (guiField != null)
  55. {
  56. if (guiField.HasInputFocus())
  57. return;
  58. guiField.Value = propertyValue;
  59. }
  60. }
  61. /// <summary>
  62. /// Triggered when the user inputs a new string.
  63. /// </summary>
  64. /// <param name="newValue">New value of the string field.</param>
  65. private void OnFieldValueChanged(string newValue)
  66. {
  67. property.SetValue(newValue);
  68. }
  69. }
  70. }