VFXStringField.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using UnityEditor.UIElements;
  4. namespace UnityEditor.VFX.UI
  5. {
  6. class VFXStringField : ValueControl<string>
  7. {
  8. protected TextField m_TextField;
  9. public TextField textfield
  10. {
  11. get { return m_TextField; }
  12. }
  13. void CreateTextField()
  14. {
  15. m_TextField = new TextField(-1, false, false, '*');
  16. m_TextField.AddToClassList("textfield");
  17. m_TextField.RegisterCallback<ChangeEvent<string>>(OnTextChanged);
  18. m_TextField.value = "";
  19. }
  20. public VFXStringField(string label) : base(label)
  21. {
  22. CreateTextField();
  23. style.flexDirection = FlexDirection.Row;
  24. Add(m_TextField);
  25. }
  26. public VFXStringField(Label existingLabel) : base(existingLabel)
  27. {
  28. CreateTextField();
  29. Add(m_TextField);
  30. }
  31. void OnTextChanged(ChangeEvent<string> e)
  32. {
  33. if (m_Value != m_TextField.text)
  34. {
  35. m_Value = m_TextField.text;
  36. if (OnValueChanged != null)
  37. {
  38. OnValueChanged();
  39. }
  40. }
  41. }
  42. bool m_Indeterminate;
  43. public bool indeterminate
  44. {
  45. get
  46. {
  47. return m_Indeterminate;
  48. }
  49. set
  50. {
  51. m_Indeterminate = value;
  52. ValueToGUI(true);
  53. }
  54. }
  55. protected override void ValueToGUI(bool force)
  56. {
  57. if (indeterminate)
  58. {
  59. m_TextField.value = "_";
  60. m_TextField.SetEnabled(false);
  61. }
  62. m_TextField.value = m_Value != null ? m_Value : "";
  63. }
  64. }
  65. }