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