SCKeyboard3DMono.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. namespace SC.XR.Unity.Module_Keyboard
  8. {
  9. public class SCKeyboard3DMono : SCKeyboardMono
  10. {
  11. #region Initialize
  12. public override void Initialize(SCKeyboardType keyboardType = SCKeyboardType.Default)
  13. {
  14. base.Initialize(keyboardType);
  15. ActiveKeyboard(KeyboardEnum.ABC,KeyboardState.Cn);
  16. }
  17. #endregion
  18. #region Keyboards
  19. protected SCKeyboard3DPrompt Keyboard3DPrompt
  20. {
  21. get
  22. {
  23. return keyboard_prompt as SCKeyboard3DPrompt;
  24. }
  25. }
  26. public GameObject keyboard_number;
  27. #endregion
  28. #region Key Clicks
  29. public override void OnPinyinKeyClick(string value)
  30. {
  31. preInputStringBuilder.Append(value);
  32. ActiveKeyboard(KeyboardEnum.Prompt);
  33. Keyboard3DPrompt.SetEnteredText(preInputStringBuilder.ToString());
  34. PinyinKeyHandler(preInputStringBuilder.ToString());
  35. }
  36. public override void OnPromptKeyClick(string value)
  37. {
  38. base.OnPromptKeyClick(value);
  39. HideKeyboardPrompt(false);
  40. }
  41. #endregion
  42. #region SpecialKeyClickEvents
  43. public override void OnABCClick()
  44. {
  45. ActiveKeyboard(KeyboardEnum.ABC, presentKeyboardState);
  46. }
  47. public override void OnDeleteClick()
  48. {
  49. OnWebKeyClick?.Invoke("Backspace");
  50. if (keyboard_prompt.isActiveAndEnabled)
  51. {
  52. preInputStringBuilder.Remove(preInputStringBuilder.Length - 1, 1);
  53. if (preInputStringBuilder.Length == 0)
  54. {
  55. Keyboard3DPrompt.gameObject.SetActive(false);
  56. preInputStringBuilder.Clear();
  57. return;
  58. }
  59. Keyboard3DPrompt.SetEnteredText(preInputStringBuilder.ToString());
  60. PinyinKeyHandler(preInputStringBuilder.ToString());
  61. }
  62. else
  63. {
  64. OnDeleteKeyClick?.Invoke();
  65. }
  66. }
  67. public override void OnLanguageClick() { }
  68. private string[] cnSymbols = new string[40] { ",", "。", "?", "”" ,
  69. "!", "@", "#", "¥", "%", "&", "(", ")", "-", "_", "=", "+", "\\", ";", ":", "、", "*", "/",
  70. "“", "”", "...", "·", "μ", "Σ", "《", "》", "【", "】", "{", "}", "|", "¢", "η", "°", "~", "……"};
  71. private string[] enSymbols = new string[40] { ",", ".", "?", "'" ,
  72. "!","@","#","$","%","&","(",")","-","_","=","+","\\",";",":","\"","*","/",
  73. "·", "¢", "∈", "£", "μ", "Σ", "<", ">", "[", "]", "{", "}", "|", "", "η", "°", "~", "^"};
  74. public override void OnCnClick()
  75. {
  76. HideKeyboardPrompt();
  77. //presentKeyboardState = KeyboardState.En;
  78. for (int i = 0; i < 4; i++)
  79. {
  80. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  81. {
  82. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  83. {
  84. symbolKeysDic[cnSymbols[i]][j].Value = enSymbols[i];
  85. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = enSymbols[i];
  86. }
  87. }
  88. }
  89. for (int i = 4; i < 22; i++)
  90. {
  91. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  92. {
  93. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  94. {
  95. symbolKeysDic[cnSymbols[i]][j].Value = enSymbols[i];
  96. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = enSymbols[i];
  97. }
  98. }
  99. }
  100. }
  101. public override void OnEnClick()
  102. {
  103. //presentKeyboardState = KeyboardState.Cn;
  104. for (int i = 0; i < 4; i++)
  105. {
  106. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  107. {
  108. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  109. {
  110. symbolKeysDic[cnSymbols[i]][j].Value = cnSymbols[i];
  111. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = cnSymbols[i];
  112. }
  113. }
  114. }
  115. for (int i = 4; i < 22; i++)
  116. {
  117. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  118. {
  119. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  120. {
  121. symbolKeysDic[cnSymbols[i]][j].Value = cnSymbols[i];
  122. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = cnSymbols[i];
  123. }
  124. }
  125. }
  126. }
  127. public override void OnPageDownClick()
  128. {
  129. if (presentKeyboard == keyboard_number)
  130. {
  131. switch (presentKeyboardState)
  132. {
  133. case KeyboardState.Cn:
  134. for (int i = 4; i < 22; i++)
  135. {
  136. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  137. {
  138. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  139. {
  140. symbolKeysDic[cnSymbols[i]][j].Value = cnSymbols[i + 18];
  141. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = cnSymbols[i + 18];
  142. }
  143. }
  144. }
  145. break;
  146. case KeyboardState.En:
  147. for (int i = 4; i < 22; i++)
  148. {
  149. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  150. {
  151. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  152. {
  153. symbolKeysDic[cnSymbols[i]][j].Value = enSymbols[i];
  154. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = enSymbols[i + 18];
  155. }
  156. }
  157. }
  158. break;
  159. }
  160. }
  161. }
  162. public override void OnPageUpClick()
  163. {
  164. if (presentKeyboard == keyboard_number)
  165. {
  166. switch (presentKeyboardState)
  167. {
  168. case KeyboardState.Cn:
  169. for (int i = 4; i < 22; i++)
  170. {
  171. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  172. {
  173. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  174. {
  175. symbolKeysDic[cnSymbols[i]][j].Value = cnSymbols[i];
  176. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = cnSymbols[i];
  177. }
  178. }
  179. }
  180. break;
  181. case KeyboardState.En:
  182. for (int i = 4; i < 22; i++)
  183. {
  184. if (symbolKeysDic.ContainsKey(cnSymbols[i]))
  185. {
  186. for (int j = 0; j < symbolKeysDic[cnSymbols[i]].Count; j++)
  187. {
  188. symbolKeysDic[cnSymbols[i]][j].Value = enSymbols[i];
  189. symbolKeysDic[cnSymbols[i]][j].GetComponentInChildren<TextMesh>().text = enSymbols[i];
  190. }
  191. }
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. #endregion
  198. #region Language Key Click Events
  199. public override void OnChineseClick()
  200. {
  201. }
  202. public override void OnEnglishClick()
  203. {
  204. ActiveKeyboard(KeyboardEnum.ABC,KeyboardState.En);
  205. }
  206. public override void OnMoreClick()
  207. {
  208. }
  209. #endregion
  210. #region Key Handlers
  211. private Text m_font;
  212. private RectTransform m_PromptPage;
  213. private RectTransform m_MorePromptPage;
  214. public Text Font
  215. {
  216. get
  217. {
  218. if (m_font == null)
  219. {
  220. m_font = Resources.Load<SCKeyboard2DKey>(keyPath).GetComponentInChildren<Text>();
  221. }
  222. return m_font;
  223. }
  224. }
  225. public RectTransform PromptPage
  226. {
  227. get
  228. {
  229. if (m_PromptPage == null)
  230. {
  231. m_PromptPage = Keyboard3DPrompt.promptInput;
  232. }
  233. return m_PromptPage;
  234. }
  235. }
  236. private int maxChineseWordCount;
  237. private static int currentChineseWordIndex = 0;
  238. public virtual void GenerateChinesePages(int page)
  239. {
  240. if (chinesePagesDic.ContainsKey(page))
  241. {
  242. HideAllPromptPages();
  243. foreach (int row in chinesePagesDic[page].Keys)
  244. {
  245. foreach (int word in chinesePagesDic[page][row].Keys)
  246. {
  247. SCKeyboardBaseKey key = promptPagesDic[0][row][word];
  248. string text = chinesePagesDic[page][row][word];
  249. key.Value = text;
  250. key.gameObject.SetActive(true);
  251. }
  252. }
  253. StartCoroutine(RebuildLayout(PromptPage));
  254. }
  255. else
  256. {
  257. GenerateChineseKeys();
  258. }
  259. }
  260. private void GenerateChineseKeys()
  261. {
  262. HideAllPromptPages();
  263. maxChineseWordCount = Keyboard3DPrompt.GetChinese(preInputStringBuilder.ToString());
  264. currentChineseWordIndex = 0;
  265. GenerateChineseKeys(0, PromptPage, new List<string>());
  266. StartCoroutine(RebuildLayout(PromptPage));
  267. }
  268. private void GenerateChineseKeys(int keyIndex, RectTransform promptRow, List<string> messages)
  269. {
  270. if (currentChineseWordIndex > maxChineseWordCount - 1)
  271. {
  272. return;
  273. }
  274. if (!promptPagesDic.ContainsKey(0)) promptPagesDic[0] = new Dictionary<int, Dictionary<int, SCKeyboardBaseKey>>();
  275. if (!promptPagesDic[0].ContainsKey(0)) promptPagesDic[0][0] = new Dictionary<int, SCKeyboardBaseKey>();
  276. if (!chinesePagesDic.ContainsKey(0)) chinesePagesDic[0] = new Dictionary<int, Dictionary<int, string>>();
  277. if (!chinesePagesDic[0].ContainsKey(0)) chinesePagesDic[0][0] = new Dictionary<int, string>();
  278. SCKeyboard2DKey promptKey;
  279. string text = Keyboard3DPrompt.GetChinese(currentChineseWordIndex);
  280. if (promptPagesDic[0][0].ContainsKey(keyIndex))
  281. {
  282. promptKey = promptPagesDic[0][0][keyIndex] as SCKeyboard2DKey;
  283. promptKey.Value = text;
  284. messages.Add(promptKey.Value);
  285. if (!WithinTheLimits(messages, promptRow.GetComponent<HorizontalLayoutGroup>(), promptRow.sizeDelta.x))
  286. {
  287. promptKey.gameObject.SetActive(false);
  288. return;
  289. }
  290. promptKey.gameObject.SetActive(true);
  291. chinesePagesDic[0][0][keyIndex] = text;
  292. }
  293. else
  294. {
  295. promptKey = InstantiateKey(keyPath, promptRow, text);
  296. messages.Add(promptKey.Value);
  297. promptPagesDic[0][0][keyIndex] = promptKey;
  298. if (!WithinTheLimits(messages, promptRow.GetComponent<HorizontalLayoutGroup>(), promptRow.sizeDelta.x))
  299. {
  300. promptKey.gameObject.SetActive(false);
  301. return;
  302. }
  303. promptKey.gameObject.SetActive(true);
  304. chinesePagesDic[0][0][keyIndex] = text;
  305. }
  306. //Debug.Log("promptKey [ " + promptKey.Value + " ] :" + currentChineseWordIndex);
  307. currentChineseWordIndex++;
  308. GenerateChineseKeys(keyIndex + 1, promptRow, messages);
  309. }
  310. private void HidePromptPage(int page)
  311. {
  312. if (promptPagesDic.ContainsKey(page))
  313. {
  314. foreach (var rowIndex in promptPagesDic[page].Keys)
  315. {
  316. foreach (var keyIndex in promptPagesDic[page][rowIndex].Keys)
  317. {
  318. promptPagesDic[page][rowIndex][keyIndex].gameObject.SetActive(false);
  319. }
  320. }
  321. }
  322. }
  323. private void HideAllPromptPages()
  324. {
  325. HidePromptPage(0);
  326. }
  327. IEnumerator RebuildLayout(RectTransform rect)
  328. {
  329. yield return new WaitForEndOfFrame();
  330. LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
  331. }
  332. public override void PinyinKeyHandler(string preInput)
  333. {
  334. chinesePagesDic = new Dictionary<int, Dictionary<int, Dictionary<int, string>>>();
  335. GenerateChinesePages(0);
  336. }
  337. private SCKeyboard2DKey InstantiateKey(string path, Transform parent, string value)
  338. {
  339. SCKeyboard2DKey prefab = Resources.Load<SCKeyboard2DKey>(path);
  340. SCKeyboard2DKey promptKey = GameObject.Instantiate(prefab);
  341. promptKey.transform.SetParent(parent);
  342. promptKey.transform.localPosition = Vector3.zero;
  343. promptKey.transform.localRotation = Quaternion.identity;
  344. promptKey.transform.localScale = Vector3.one;
  345. promptKey.Value = value;
  346. return promptKey;
  347. }
  348. private bool WithinTheLimits(List<string> messages, HorizontalLayoutGroup group, float LimitWidth)
  349. {
  350. float rectWidth = LimitWidth;
  351. float totalWidth = 0;
  352. float left = group.padding.left;
  353. float right = group.padding.right;
  354. float space = group.spacing;
  355. float width = 0;
  356. int wordCount = messages.Count;
  357. string message = "";
  358. for (int i = 0; i < messages.Count; i++)
  359. {
  360. message += messages[i];
  361. width += 12;
  362. }
  363. width += KeyboardUtils.CaculateTextLength(message, Font) / 10;
  364. totalWidth = (wordCount - 1) * space + left + width;
  365. return totalWidth <= rectWidth;
  366. }
  367. #endregion
  368. #region Active Keyboards
  369. protected override void ActiveKeyboard(KeyboardEnum keyboardEnum,KeyboardState keyboardState = KeyboardState.En)
  370. {
  371. base.ActiveKeyboard(keyboardEnum, keyboardState);
  372. switch (keyboardEnum)
  373. {
  374. case KeyboardEnum.ABC:
  375. OnActiveABCKeyboard(keyboardState);
  376. break;
  377. case KeyboardEnum.Number:
  378. OnActiveNumberKeyboard();
  379. break;
  380. case KeyboardEnum.Prompt:
  381. OnActivePromptKeyboard();
  382. break;
  383. }
  384. }
  385. public void OnActiveABCKeyboard(KeyboardState keyboardState)
  386. {
  387. if (presentKeyboard != null)
  388. previousKeyboard = presentKeyboard;
  389. keyboard_abc.SetActive(true);
  390. HideKeyboardPrompt();
  391. presentKeyboard = keyboard_abc;
  392. if (previousKeyboard != null && previousKeyboard != presentKeyboard)
  393. previousKeyboard.SetActive(false);
  394. presentKeyboardState = keyboardState;
  395. }
  396. public void OnActiveNumberKeyboard()
  397. {
  398. if (presentKeyboard != null)
  399. previousKeyboard = presentKeyboard;
  400. keyboard_number.SetActive(true);
  401. HideKeyboardPrompt();
  402. presentKeyboard = keyboard_number;
  403. if (previousKeyboard != null && previousKeyboard != presentKeyboard)
  404. previousKeyboard.SetActive(false);
  405. }
  406. public void OnActivePromptKeyboard()
  407. {
  408. Keyboard3DPrompt.gameObject.SetActive(true);
  409. }
  410. protected override void HideKeyboardPrompt(bool inputPreString = true)
  411. {
  412. if (keyboard_prompt.isActiveAndEnabled)
  413. {
  414. if (inputPreString) OnNormalKeyClick(preInputStringBuilder.ToString());
  415. preInputStringBuilder.Clear();
  416. Keyboard3DPrompt.gameObject.SetActive(false);
  417. }
  418. }
  419. #endregion
  420. }
  421. }