A game about forced loneliness, made by TACStudios
1using UnityEngine.InputSystem.LowLevel;
2using UnityEngine.UI;
3
4namespace UnityEngine.InputSystem.Samples.IMEInput
5{
6 /// <summary>
7 /// An example IME Input Handler showing how IME input can be handled using
8 /// input system provided events.
9 /// </summary>
10 /// <seealso cref="Keyboard.OnIMECompositionChanged"/>
11 public class IMEInputHandler : MonoBehaviour
12 {
13 // Handles text passed via Keyboard.onTextInput event
14 private void OnTextInput(char character)
15 {
16 AssertReferencesAreValid();
17
18 // Assumes the current IME composition text has been submitted
19 if (m_ComposingViaIME)
20 {
21 m_CompositionField.text = string.Empty;
22 m_ComposingViaIME = false;
23 }
24
25 m_CurrentInput += character;
26 m_TextField.text = m_CurrentInput;
27 m_CombinedField.text = m_CurrentInput;
28 }
29
30 // Handles text passed via Keyboard.onIMECompositionChange event
31 private void OnIMECompositionChange(IMECompositionString text)
32 {
33 AssertReferencesAreValid();
34
35 // IME composition strings without length can also mean
36 // the composition has been submitted
37 if (text.Count == 0)
38 {
39 m_ComposingViaIME = false;
40 m_CompositionField.text = string.Empty;
41 return;
42 }
43
44 var compositionText = text.ToString();
45 m_ComposingViaIME = true;
46 m_CompositionField.text = compositionText;
47
48 // The combined text contains both the current input and the current status of the composition
49 m_CombinedField.text = string.Format("{0}{1}", m_CurrentInput, compositionText);
50 }
51
52 // Adds keyboard and input field listeners
53 private void OnEnable()
54 {
55 if (m_EventListenersAdded)
56 return;
57
58 var keyboard = InputSystem.GetDevice<Keyboard>();
59 if (keyboard is null)
60 return;
61
62 keyboard.onTextInput += OnTextInput;
63 keyboard.onIMECompositionChange += OnIMECompositionChange;
64 m_InputField.onValueChanged.AddListener(OnValueChanged);
65
66 m_EventListenersAdded = true;
67 }
68
69 // Removes keyboard and input field listeners
70 private void OnDisable()
71 {
72 if (!m_EventListenersAdded)
73 return;
74
75 var keyboard = InputSystem.GetDevice<Keyboard>();
76 if (keyboard is null)
77 return;
78
79 keyboard.onTextInput -= OnTextInput;
80 keyboard.onIMECompositionChange -= OnIMECompositionChange;
81 m_InputField.onValueChanged.RemoveListener(OnValueChanged);
82
83 Clear();
84
85 m_EventListenersAdded = false;
86 }
87
88 // Called when the input field's text is changed
89 private void OnValueChanged(string value)
90 {
91 AssertReferencesAreValid();
92
93 if (!string.IsNullOrEmpty(value))
94 return;
95
96 Clear();
97 }
98
99 // Clears the text from all of the fields
100 private void Clear()
101 {
102 m_CompositionField.text = string.Empty;
103 m_TextField.text = string.Empty;
104 m_CombinedField.text = string.Empty;
105
106 m_CurrentInput = string.Empty;
107 m_ComposingViaIME = false;
108 }
109
110 // Ensures all fields are correctly referenced
111 private void AssertReferencesAreValid()
112 {
113 Debug.Assert(m_CompositionField != null, "Composition field cannot be null");
114 Debug.Assert(m_TextField != null, "Text field field cannot be null");
115 Debug.Assert(m_CombinedField != null, "Combined field field cannot be null");
116 Debug.Assert(m_InputField != null, "Input field field cannot be null");
117 }
118
119 [Tooltip("Text field intended to display the string received via OnIMECompositionChanged")]
120 [SerializeField] private InputField m_CompositionField;
121
122 [Tooltip("Text field intended to display characters received via OnTextInput")]
123 [SerializeField] private InputField m_TextField;
124
125 [Tooltip("Text field intended to display a combination of characters received by "
126 + "both the OnIMECompositionChanged & OnTextInput events")]
127 [SerializeField] private InputField m_CombinedField;
128
129 [Tooltip("Text field intended for user input")]
130 [SerializeField] private InputField m_InputField;
131
132 private bool m_EventListenersAdded = false;
133 private bool m_ComposingViaIME = false;
134 private string m_CurrentInput;
135 }
136}