|
| 1 | +using UnityEditor; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEngine.UIElements; |
| 4 | + |
| 5 | +namespace GBG.AssetQuickAccess.Editor |
| 6 | +{ |
| 7 | + internal class IMGUITextField : VisualElement, INotifyValueChanged<string> |
| 8 | + { |
| 9 | + public string LabelText |
| 10 | + { |
| 11 | + get => LabelElement.text; |
| 12 | + set |
| 13 | + { |
| 14 | + LabelElement.text = value; |
| 15 | + RefreshLabelElementStyle(); |
| 16 | + } |
| 17 | + } |
| 18 | + public string HintText |
| 19 | + { |
| 20 | + get => HintElement.text; |
| 21 | + set => HintElement.text = value; |
| 22 | + } |
| 23 | + |
| 24 | + public string value |
| 25 | + { |
| 26 | + get => _value; |
| 27 | + set |
| 28 | + { |
| 29 | + if (_value == value) |
| 30 | + { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + string previousValue = _value; |
| 35 | + SetValueWithoutNotify(value); |
| 36 | + using (ChangeEvent<string> evt = ChangeEvent<string>.GetPooled(previousValue, _value)) |
| 37 | + { |
| 38 | + evt.target = this; |
| 39 | + SendEvent(evt); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public Label LabelElement { get; } |
| 45 | + public Label HintElement { get; } |
| 46 | + public IMGUIContainer InputFieldElement { get; } |
| 47 | + |
| 48 | + private string _value; |
| 49 | + |
| 50 | + |
| 51 | + public IMGUITextField(string labelText = null, string hintText = null) |
| 52 | + { |
| 53 | + style.flexDirection = FlexDirection.Row; |
| 54 | + style.height = 21; |
| 55 | + |
| 56 | + // label |
| 57 | + LabelElement = new Label |
| 58 | + { |
| 59 | + name = "imgui-text-field-label", |
| 60 | + pickingMode = PickingMode.Ignore, |
| 61 | + }; |
| 62 | + Add(LabelElement); |
| 63 | + |
| 64 | + // imgui input field |
| 65 | + InputFieldElement = new IMGUIContainer(DrawTextField) |
| 66 | + { |
| 67 | + name = "imgui-text-field-container", |
| 68 | + style = |
| 69 | + { |
| 70 | + flexGrow = 1, |
| 71 | + } |
| 72 | + }; |
| 73 | + Add(InputFieldElement); |
| 74 | + |
| 75 | + // hint label |
| 76 | + HintElement = new Label |
| 77 | + { |
| 78 | + name = "imgui-text-field-hint-label", |
| 79 | + pickingMode = PickingMode.Ignore, |
| 80 | + style = |
| 81 | + { |
| 82 | + flexGrow = 1, |
| 83 | + marginLeft = 2, |
| 84 | + marginRight = 2, |
| 85 | + marginTop = 1, |
| 86 | + marginBottom = 1, |
| 87 | + unityFontStyleAndWeight = FontStyle.Italic, |
| 88 | + } |
| 89 | + }; |
| 90 | + InputFieldElement.Add(HintElement); |
| 91 | + |
| 92 | + LabelText = labelText; |
| 93 | + HintText = hintText; |
| 94 | + } |
| 95 | + |
| 96 | + public void SetValueWithoutNotify(string newValue) |
| 97 | + { |
| 98 | + _value = newValue; |
| 99 | + RefreshHintDisplay(); |
| 100 | + } |
| 101 | + |
| 102 | + public void RefreshHintDisplay() |
| 103 | + { |
| 104 | + HintElement.style.display = string.IsNullOrEmpty(_value) |
| 105 | + ? DisplayStyle.Flex |
| 106 | + : DisplayStyle.None; |
| 107 | + } |
| 108 | + |
| 109 | + public void RefreshLabelElementStyle() |
| 110 | + { |
| 111 | + if (string.IsNullOrEmpty(value)) |
| 112 | + { |
| 113 | + LabelElement.style.paddingLeft = 0; |
| 114 | + LabelElement.style.paddingRight = 0; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + LabelElement.style.paddingLeft = 3; |
| 119 | + LabelElement.style.paddingRight = 3; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void DrawTextField() |
| 124 | + { |
| 125 | + string previousValue = _value; |
| 126 | + EditorGUI.BeginChangeCheck(); |
| 127 | + _value = EditorGUILayout.TextField(_value); |
| 128 | + if (EditorGUI.EndChangeCheck()) |
| 129 | + { |
| 130 | + RefreshHintDisplay(); |
| 131 | + using (ChangeEvent<string> evt = ChangeEvent<string>.GetPooled(previousValue, _value)) |
| 132 | + { |
| 133 | + evt.target = this; |
| 134 | + SendEvent(evt); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments