SCKeyboardMono.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public abstract class SCKeyboardMono : MonoBehaviour
  9. {
  10. public GameObject keyboard_num;
  11. public GameObject keyboard_symbol;
  12. public GameObject keyboard_enUp;
  13. public GameObject keyboard_enLow;
  14. public GameObject keyboard_cn;
  15. public Action OnDoneButtonClick;
  16. public Action<string> OnTextChange;
  17. public StringBuilder stringBuilder;
  18. private StringBuilder preInputStringBuilder;
  19. public KeyboardPrompt keyboardPrompt;
  20. public void Initialize()
  21. {
  22. if (stringBuilder == null)
  23. {
  24. stringBuilder = new StringBuilder();
  25. }
  26. if (preInputStringBuilder == null)
  27. {
  28. preInputStringBuilder = new StringBuilder();
  29. }
  30. keyboardPrompt.Init(OnChineseCharacterClick);
  31. ShowNum();
  32. }
  33. public void OnChineseCharacterClick(string value)
  34. {
  35. preInputStringBuilder.Clear();
  36. stringBuilder.Append(value);
  37. HideChinesePrompt();
  38. SetTextString();
  39. }
  40. public void OnPinyinKeyClick(string value)
  41. {
  42. preInputStringBuilder.Append(value);
  43. int wordCount = GetChinese(preInputStringBuilder.ToString());
  44. if (wordCount != 0)
  45. {
  46. ShowChinesePrompt();
  47. }
  48. else
  49. {
  50. if (preInputStringBuilder.Length != 0)
  51. {
  52. stringBuilder.Append(preInputStringBuilder.ToString());
  53. preInputStringBuilder.Clear();
  54. }
  55. }
  56. SetTextString();
  57. }
  58. public void OnNormalKeyClick(string value)
  59. {
  60. if (preInputStringBuilder.Length != 0)
  61. {
  62. stringBuilder.Append(preInputStringBuilder.ToString());
  63. preInputStringBuilder.Clear();
  64. HideChinesePrompt();
  65. }
  66. stringBuilder.Append(value);
  67. SetTextString();
  68. }
  69. public void OnDoneClick()
  70. {
  71. stringBuilder.Append(preInputStringBuilder.ToString());
  72. preInputStringBuilder.Clear();
  73. HideChinesePrompt();
  74. OnDoneButtonClick?.Invoke();
  75. }
  76. public void OnClearKeyClick()
  77. {
  78. preInputStringBuilder.Clear();
  79. stringBuilder.Clear();
  80. HideChinesePrompt();
  81. SetTextString();
  82. }
  83. public void OnDeleteKeyClick()
  84. {
  85. if (preInputStringBuilder.Length > 0)
  86. {
  87. preInputStringBuilder.Remove(preInputStringBuilder.Length - 1, 1);
  88. if (preInputStringBuilder.Length != 0)
  89. {
  90. GetChinese(preInputStringBuilder.ToString());
  91. }
  92. else
  93. {
  94. HideChinesePrompt();
  95. }
  96. SetTextString();
  97. return;
  98. }
  99. HideChinesePrompt();
  100. if (stringBuilder.Length > 0)
  101. {
  102. stringBuilder.Remove(stringBuilder.Length - 1, 1);
  103. SetTextString();
  104. }
  105. }
  106. public void OnSpaceClick()
  107. {
  108. if (preInputStringBuilder.Length > 0)
  109. {
  110. preInputStringBuilder.Clear();
  111. string firstChineseWord = GetFirstChineseWord();
  112. if (!string.IsNullOrEmpty(firstChineseWord))
  113. {
  114. stringBuilder.Append(GetFirstChineseWord());
  115. }
  116. HideChinesePrompt();
  117. SetTextString();
  118. return;
  119. }
  120. stringBuilder.Append(" ");
  121. SetTextString();
  122. }
  123. //for CN IME
  124. public void OnShiftClick()
  125. {
  126. if (preInputStringBuilder.Length != 0)
  127. {
  128. stringBuilder.Append(preInputStringBuilder.ToString());
  129. preInputStringBuilder.Clear();
  130. HideChinesePrompt();
  131. SetTextString();
  132. }
  133. }
  134. public void ShowNum()
  135. {
  136. if (preInputStringBuilder.Length != 0)
  137. {
  138. stringBuilder.Append(preInputStringBuilder.ToString());
  139. preInputStringBuilder.Clear();
  140. HideChinesePrompt();
  141. SetTextString();
  142. }
  143. keyboard_num.SetActive(true);
  144. keyboard_symbol.SetActive(false);
  145. keyboard_enUp.SetActive(false);
  146. keyboard_enLow.SetActive(false);
  147. keyboard_cn.SetActive(false);
  148. }
  149. public void ShowSymbol()
  150. {
  151. keyboard_num.SetActive(false);
  152. keyboard_symbol.SetActive(true);
  153. keyboard_enUp.SetActive(false);
  154. keyboard_enLow.SetActive(false);
  155. keyboard_cn.SetActive(false);
  156. }
  157. //Shift
  158. public void ShowEnUp()
  159. {
  160. keyboard_num.SetActive(false);
  161. keyboard_symbol.SetActive(false);
  162. keyboard_enUp.SetActive(true);
  163. keyboard_enLow.SetActive(false);
  164. keyboard_cn.SetActive(false);
  165. }
  166. public void ShowEnLow()
  167. {
  168. if (preInputStringBuilder.Length != 0)
  169. {
  170. stringBuilder.Append(preInputStringBuilder.ToString());
  171. preInputStringBuilder.Clear();
  172. HideChinesePrompt();
  173. SetTextString();
  174. }
  175. keyboard_num.SetActive(false);
  176. keyboard_symbol.SetActive(false);
  177. keyboard_enUp.SetActive(false);
  178. keyboard_enLow.SetActive(true);
  179. keyboard_cn.SetActive(false);
  180. }
  181. public void ShowCN()
  182. {
  183. keyboard_num.SetActive(false);
  184. keyboard_symbol.SetActive(false);
  185. keyboard_enUp.SetActive(false);
  186. keyboard_enLow.SetActive(false);
  187. keyboard_cn.SetActive(true);
  188. }
  189. public virtual void SetTextString()
  190. {
  191. string result = stringBuilder.ToString() + preInputStringBuilder.ToString();
  192. OnTextChange?.Invoke(result);
  193. }
  194. protected virtual int GetChinese(string englishWord)
  195. {
  196. return keyboardPrompt.GetChinese(englishWord);
  197. }
  198. protected void ShowChinesePrompt()
  199. {
  200. keyboardPrompt.gameObject.SetActive(true);
  201. }
  202. protected void HideChinesePrompt()
  203. {
  204. keyboardPrompt.gameObject.SetActive(false);
  205. }
  206. protected string GetFirstChineseWord()
  207. {
  208. return keyboardPrompt.GetFirstChineseWord();
  209. }
  210. }