InspectableString.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /** @addtogroup Inspector
  7. * @{
  8. */
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing a string.
  11. /// </summary>
  12. public class InspectableString : InspectableField
  13. {
  14. private GUITextField guiField;
  15. private InspectableState state;
  16. /// <summary>
  17. /// Creates a new inspectable string GUI for the specified property.
  18. /// </summary>
  19. /// <param name="parent">Parent Inspector this field belongs to.</param>
  20. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  21. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</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 InspectableString(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  27. SerializableProperty property)
  28. : base(parent, title, path, SerializableProperty.FieldType.String, depth, layout, property)
  29. {
  30. }
  31. /// <inheritoc/>
  32. protected internal override void Initialize(int layoutIndex)
  33. {
  34. if (property.Type == SerializableProperty.FieldType.String)
  35. {
  36. guiField = new GUITextField(new GUIContent(title));
  37. guiField.OnChanged += OnFieldValueChanged;
  38. guiField.OnConfirmed += OnFieldValueConfirm;
  39. guiField.OnFocusLost += OnFieldValueConfirm;
  40. layout.AddElement(layoutIndex, guiField);
  41. }
  42. }
  43. /// <inheritdoc/>
  44. public override InspectableState Refresh(int layoutIndex)
  45. {
  46. if (guiField != null && !guiField.HasInputFocus)
  47. guiField.Value = property.GetValue<string>();
  48. InspectableState oldState = state;
  49. if (state.HasFlag(InspectableState.Modified))
  50. state = InspectableState.NotModified;
  51. return oldState;
  52. }
  53. /// <summary>
  54. /// Triggered when the user inputs a new string.
  55. /// </summary>
  56. /// <param name="newValue">New value of the text field.</param>
  57. private void OnFieldValueChanged(string newValue)
  58. {
  59. property.SetValue(newValue);
  60. state |= InspectableState.ModifyInProgress;
  61. }
  62. /// <summary>
  63. /// Triggered when the user confirms input in the text field.
  64. /// </summary>
  65. private void OnFieldValueConfirm()
  66. {
  67. if (state.HasFlag(InspectableState.ModifyInProgress))
  68. state |= InspectableState.Modified;
  69. }
  70. }
  71. /** @} */
  72. }