-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTextEntry.cs
180 lines (155 loc) · 6.42 KB
/
SimpleTextEntry.cs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SimpleTextEntry : MonoBehaviour
{
// Core UI Components
private TMP_InputField inputField;
private GameObject entryPanel;
// Event to notify when text changes
public delegate void OnTextChangedDelegate(string newText);
public event OnTextChangedDelegate OnTextChanged;
private void Start()
{
CreateUIElements();
// Ensure panel starts hidden
if (entryPanel != null)
{
entryPanel.SetActive(false);
}
}
private void CreateUIElements()
{
try
{
// Create the entry panel
entryPanel = new GameObject("EntryPanel");
entryPanel.transform.SetParent(transform, false);
// Add Canvas
Canvas canvas = entryPanel.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvas.sortingOrder = 100; // Ensure it's above other UI
// Add CanvasScaler for proper scaling across different resolutions
CanvasScaler scaler = entryPanel.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(1920, 1080);
entryPanel.AddComponent<GraphicRaycaster>();
// Create background panel
GameObject panel = new GameObject("Panel");
panel.transform.SetParent(entryPanel.transform, false);
Image panelImage = panel.AddComponent<Image>();
panelImage.color = new Color(0, 0, 0, 0.9f);
RectTransform panelRect = panel.GetComponent<RectTransform>();
panelRect.anchorMin = new Vector2(0.5f, 0.5f);
panelRect.anchorMax = new Vector2(0.5f, 0.5f);
panelRect.pivot = new Vector2(0.5f, 0.5f);
panelRect.sizeDelta = new Vector2(400, 100);
panelRect.anchoredPosition = Vector2.zero;
// Create input field
GameObject inputObj = new GameObject("InputField");
inputObj.transform.SetParent(panel.transform, false);
RectTransform inputRect = inputObj.AddComponent<RectTransform>();
inputRect.anchorMin = new Vector2(0, 0);
inputRect.anchorMax = new Vector2(1, 1);
inputRect.offsetMin = new Vector2(20, 20);
inputRect.offsetMax = new Vector2(-20, -20);
inputField = inputObj.AddComponent<TMP_InputField>();
// Create text component for input field
GameObject textObj = new GameObject("Text");
textObj.transform.SetParent(inputObj.transform, false);
TextMeshProUGUI inputText = textObj.AddComponent<TextMeshProUGUI>();
inputText.fontSize = 32;
inputText.color = Color.white;
inputText.alignment = TextAlignmentOptions.Center;
inputText.fontStyle = FontStyles.Bold;
RectTransform textRect = textObj.GetComponent<RectTransform>();
textRect.anchorMin = Vector2.zero;
textRect.anchorMax = Vector2.one;
textRect.offsetMin = Vector2.zero;
textRect.offsetMax = Vector2.zero;
// Create placeholder
GameObject placeholderObj = new GameObject("Placeholder");
placeholderObj.transform.SetParent(inputObj.transform, false);
TextMeshProUGUI placeholder = placeholderObj.AddComponent<TextMeshProUGUI>();
placeholder.text = "Enter page number...";
placeholder.fontSize = 32;
placeholder.color = new Color(1, 1, 1, 0.5f);
placeholder.alignment = TextAlignmentOptions.Center;
RectTransform placeholderRect = placeholderObj.GetComponent<RectTransform>();
placeholderRect.anchorMin = Vector2.zero;
placeholderRect.anchorMax = Vector2.one;
placeholderRect.offsetMin = Vector2.zero;
placeholderRect.offsetMax = Vector2.zero;
// Configure input field
inputField.textViewport = inputRect;
inputField.textComponent = inputText;
inputField.placeholder = placeholder;
inputField.caretWidth = 2;
inputField.customCaretColor = true;
inputField.caretColor = Color.white;
inputField.selectionColor = new Color(1, 1, 1, 0.3f);
// Set input field settings
inputField.characterValidation = TMP_InputField.CharacterValidation.Integer;
inputField.contentType = TMP_InputField.ContentType.IntegerNumber;
inputField.keyboardType = TouchScreenKeyboardType.NumberPad;
inputField.characterLimit = 4;
// Set up event listeners
inputField.onValueChanged.AddListener(HandleTextChange);
inputField.onEndEdit.AddListener(HandleEndEdit);
}
catch (Exception e)
{
Debug.LogError($"Error creating UI elements: {e.Message}\n{e.StackTrace}");
}
}
private void OnDestroy()
{
if (inputField != null)
{
inputField.onValueChanged.RemoveListener(HandleTextChange);
inputField.onEndEdit.RemoveListener(HandleEndEdit);
}
}
private Coroutine debounceCoroutine;
private const float DEBOUNCE_TIME = 0.3f; // 300ms delay
private void HandleTextChange(string newText)
{
if (debounceCoroutine != null)
{
StopCoroutine(debounceCoroutine);
}
debounceCoroutine = StartCoroutine(DebounceTextChange(newText));
}
private IEnumerator DebounceTextChange(string text)
{
yield return new WaitForSeconds(DEBOUNCE_TIME);
OnTextChanged?.Invoke(text);
}
private void HandleEndEdit(string finalText)
{
CloseEntryPanel();
}
public void ShowEntryPanel()
{
if (entryPanel != null && inputField != null)
{
entryPanel.SetActive(true);
inputField.text = "";
inputField.ActivateInputField();
inputField.Select();
}
else
{
Debug.LogWarning("Cannot show panel - some elements are missing");
}
}
public void CloseEntryPanel()
{
if (entryPanel != null)
{
entryPanel.SetActive(false);
}
}
}