forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextField.cs
More file actions
148 lines (128 loc) · 4.45 KB
/
Copy pathTextField.cs
File metadata and controls
148 lines (128 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections.Generic;
namespace UnityEngine.Experimental.UIElements
{
public class TextField : TextInputFieldBase, INotifyValueChanged<string>
{
// TODO: Switch over to default style properties
// Multiline (lossy behaviour when deactivated)
bool m_Multiline;
public bool multiline
{
get { return m_Multiline; }
set
{
m_Multiline = value;
if (!value)
text = text.Replace("\n", "");
}
}
// Password field (indirectly lossy behaviour when activated via multiline)
public override bool isPasswordField
{
set
{
base.isPasswordField = value;
if (value)
multiline = false;
}
}
public TextField() : this(kMaxLengthNone, false, false, char.MinValue)
{
}
public TextField(int maxLength, bool multiline, bool isPasswordField, char maskChar) : base(maxLength, maskChar)
{
this.multiline = multiline;
this.isPasswordField = isPasswordField;
}
protected string m_Value;
public string value
{
get { return m_Value; }
set
{
m_Value = value;
text = m_Value;
}
}
public void SetValueAndNotify(string newValue)
{
if (!EqualityComparer<string>.Default.Equals(value, newValue))
{
using (ChangeEvent<string> evt = ChangeEvent<string>.GetPooled(value, newValue))
{
evt.target = this;
value = newValue;
UIElementsUtility.eventDispatcher.DispatchEvent(evt, panel);
}
}
}
public void OnValueChanged(EventCallback<ChangeEvent<string>> callback)
{
RegisterCallback(callback);
}
public override void OnPersistentDataReady()
{
base.OnPersistentDataReady();
string key = GetFullHierarchicalPersistenceKey();
OverwriteFromPersistedData(this, key);
}
internal override void SyncTextEngine()
{
editorEngine.multiline = multiline;
editorEngine.isPasswordField = isPasswordField;
base.SyncTextEngine();
}
internal override void DoRepaint(IStylePainter painter)
{
if (isPasswordField)
{
// if we use system keyboard we will have normal text returned (hiding symbols is done inside os)
// so before drawing make sure we hide them ourselves
string drawText = "".PadRight(text.Length, maskChar);
if (!hasFocus)
{
// We don't have the focus, don't draw the selection and cursor
painter.DrawBackground(this);
painter.DrawBorder(this);
if (!string.IsNullOrEmpty(drawText) && contentRect.width > 0.0f && contentRect.height > 0.0f)
{
var textParams = painter.GetDefaultTextParameters(this);
textParams.text = drawText;
painter.DrawText(textParams);
}
}
else
{
DrawWithTextSelectionAndCursor(painter, drawText);
}
}
else
{
base.DoRepaint(painter);
}
}
protected internal override void ExecuteDefaultActionAtTarget(EventBase evt)
{
base.ExecuteDefaultActionAtTarget(evt);
if (evt.GetEventTypeId() == KeyDownEvent.TypeId())
{
KeyDownEvent kde = evt as KeyDownEvent;
if (kde.character == '\n')
{
SetValueAndNotify(text);
}
}
}
protected internal override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
if (evt.GetEventTypeId() == BlurEvent.TypeId())
{
SetValueAndNotify(text);
}
}
}
}