InspectableString.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  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="context">Context shared by all inspectable fields created by the same parent.</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 field whose contents to display.</param>
  26. public InspectableString(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  27. SerializableProperty property)
  28. : base(context, 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 += () =>
  39. {
  40. OnFieldValueConfirm();
  41. StartUndo();
  42. };
  43. guiField.OnFocusLost += OnFieldValueConfirm;
  44. guiField.OnFocusGained += StartUndo;
  45. layout.AddElement(layoutIndex, guiField);
  46. }
  47. }
  48. /// <inheritdoc/>
  49. public override InspectableState Refresh(int layoutIndex)
  50. {
  51. if (guiField != null && !guiField.HasInputFocus)
  52. guiField.Value = property.GetValue<string>();
  53. InspectableState oldState = state;
  54. if (state.HasFlag(InspectableState.Modified))
  55. state = InspectableState.NotModified;
  56. return oldState;
  57. }
  58. /// <summary>
  59. /// Triggered when the user inputs a new string.
  60. /// </summary>
  61. /// <param name="newValue">New value of the text field.</param>
  62. private void OnFieldValueChanged(string newValue)
  63. {
  64. property.SetValue(newValue);
  65. state |= InspectableState.ModifyInProgress;
  66. }
  67. /// <summary>
  68. /// Triggered when the user confirms input in the text field.
  69. /// </summary>
  70. private void OnFieldValueConfirm()
  71. {
  72. if (state.HasFlag(InspectableState.ModifyInProgress))
  73. state |= InspectableState.Modified;
  74. EndUndo();
  75. }
  76. }
  77. /** @} */
  78. }