Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IdentifierField.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. // This is to ensure backwards compatibility since TextValueField, TextValueFieldTraits and DeltaSpeed were moved from UnityEditor.UIElements to UnityEngine.UIElements.
  5. using UnityEditor.UIElements;
  6. namespace UnityEditor.ShaderGraph.Drawing
  7. {
  8. /*
  9. Field that allows entering a valid HLSL identifier.
  10. (variable name, function name, ...) this means
  11. no spaces, no funny characters, never starts with a number, ...
  12. */
  13. [UxmlElement]
  14. public partial class IdentifierField : TextValueField<string>
  15. {
  16. IdentifierInput tsInput => (IdentifierInput)textInputBase;
  17. [Obsolete("UxmlFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  18. public new class UxmlFactory : UxmlFactory<IdentifierField, UxmlTraits> { }
  19. [Obsolete("UxmlTraits is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  20. public new class UxmlTraits : TextValueFieldTraits<string, UxmlStringAttributeDescription> { }
  21. protected override string ValueToString(string v)
  22. {
  23. return v;
  24. }
  25. protected override string StringToValue(string str)
  26. {
  27. // Make sure this is a valid hlsl identifier. Allowed characters already ensures the characters are valid
  28. // but identifiers can't start with a number so fix this here.
  29. if (string.IsNullOrEmpty(str))
  30. {
  31. return "_0";
  32. }
  33. else if (Char.IsDigit(str[0]))
  34. {
  35. return "_" + str;
  36. }
  37. else
  38. {
  39. return str;
  40. }
  41. }
  42. public new static readonly string ussClassName = "unity-identifierfield-field";
  43. public new static readonly string labelUssClassName = ussClassName + "__label";
  44. public new static readonly string inputUssClassName = ussClassName + "__input";
  45. public IdentifierField() : this((string)null) { }
  46. public IdentifierField(string label) : base(label, -1, new IdentifierInput())
  47. {
  48. AddToClassList(ussClassName);
  49. labelElement.AddToClassList(labelUssClassName);
  50. tsInput.AddToClassList(inputUssClassName);
  51. }
  52. public override void ApplyInputDeviceDelta(Vector3 delta, DeltaSpeed speed, string startValue)
  53. {
  54. tsInput.ApplyInputDeviceDelta(delta, speed, startValue);
  55. }
  56. class IdentifierInput : TextValueInput
  57. {
  58. IdentifierField parentField => (IdentifierField)parent;
  59. internal IdentifierInput()
  60. {
  61. formatString = null;
  62. }
  63. protected override string allowedCharacters
  64. {
  65. get { return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; }
  66. }
  67. public override void ApplyInputDeviceDelta(Vector3 delta, DeltaSpeed speed, string startValue)
  68. {
  69. }
  70. protected override string ValueToString(string v)
  71. {
  72. return v;
  73. }
  74. protected override string StringToValue(string str)
  75. {
  76. // Make sure this is a valid hlsl identifier. Allowed characters already ensures the characters are valid
  77. // but identifiers can't start with a number so fix this here.
  78. if (string.IsNullOrEmpty(str))
  79. {
  80. return "_0";
  81. }
  82. else if (Char.IsDigit(str[0]))
  83. {
  84. return "_" + str;
  85. }
  86. else
  87. {
  88. return str;
  89. }
  90. }
  91. }
  92. }
  93. }