KeyboardPrompt.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class KeyboardPrompt : MonoBehaviour
  7. {
  8. private const int PROMPT_PER_PAGE = 5;
  9. private int totalPageCount;
  10. private int currentPageCount;
  11. private int wordCount;
  12. public PromptTurnBtn turnLeftBtn;
  13. public PromptTurnBtn turnRightBtn;
  14. public PromptItem[] promptItems;
  15. public RectTransform enteredRect;
  16. public Text alreadyInput;
  17. private Action<string> onPromptItemClick;
  18. public void Init(Action<string> onPromptItemClick)
  19. {
  20. for (int i = 0; i < promptItems.Length; i++)
  21. {
  22. PromptItem promptItem = promptItems[i];
  23. promptItem.onPromptItemClick = OnPromptItemClickCallback;
  24. }
  25. enteredRect.GetComponent<KeyboardKeydownBtn>().onClick.RemoveAllListeners();
  26. enteredRect.GetComponent<KeyboardKeydownBtn>().onClick.AddListener(OnEnteredPromptItemClickCallback);
  27. turnLeftBtn.onClick.RemoveAllListeners();
  28. turnLeftBtn.onClick.AddListener(TurnLeft);
  29. turnRightBtn.onClick.RemoveAllListeners();
  30. turnRightBtn.onClick.AddListener(TurnRight);
  31. this.onPromptItemClick = onPromptItemClick;
  32. }
  33. public int GetChinese(string englishWords)
  34. {
  35. wordCount = ChineseIMEManager.Instance.GetWordCount(englishWords);
  36. currentPageCount = 0;
  37. totalPageCount = wordCount / PROMPT_PER_PAGE;
  38. RefreshPromptItem();
  39. SetTrunBtnVisiable();
  40. return wordCount;
  41. }
  42. public void TurnLeft()
  43. {
  44. currentPageCount--;
  45. RefreshPromptItem();
  46. SetTrunBtnVisiable();
  47. }
  48. // Update is called once per frame
  49. public void TurnRight()
  50. {
  51. currentPageCount++;
  52. RefreshPromptItem();
  53. SetTrunBtnVisiable();
  54. }
  55. public string GetFirstChineseWord()
  56. {
  57. PromptItem firstPromptItem = promptItems[0];
  58. return firstPromptItem.chineseWord;
  59. }
  60. public void SetEnteredText(string textStr)
  61. {
  62. alreadyInput.text = textStr;
  63. }
  64. public void SetChineseFirstEnteredText()
  65. {
  66. string textStr = GetFirstChineseWord();
  67. textStr = string.IsNullOrEmpty(textStr) ? string.Empty : textStr;
  68. Text chineseAlreadyInput = enteredRect.GetComponentInChildren<Text>();
  69. float sizeY = enteredRect.sizeDelta.y;
  70. float sizeX = KeyboardUtils.CaculateTextLength(textStr , enteredRect.GetComponentInChildren<Text>());
  71. enteredRect.sizeDelta = new Vector2(sizeX + 160, sizeY);
  72. LayoutRebuilder.ForceRebuildLayoutImmediate(enteredRect);
  73. chineseAlreadyInput.text = textStr;
  74. }
  75. private void RefreshPromptItem()
  76. {
  77. int startIndex = currentPageCount * PROMPT_PER_PAGE;
  78. int endIndex = totalPageCount != currentPageCount ? startIndex + PROMPT_PER_PAGE - 1 : startIndex + wordCount % PROMPT_PER_PAGE - 1;
  79. int loopWordCount = endIndex - startIndex;
  80. for (int i = 0; i < promptItems.Length; i++)
  81. {
  82. int wordIndex = startIndex + i;
  83. PromptItem promptItem = promptItems[i];
  84. if (i <= loopWordCount)
  85. {
  86. string chineseWord = ChineseIMEManager.Instance.GetWord(wordIndex);
  87. promptItem.SetChineseWord(chineseWord, wordIndex);
  88. }
  89. else
  90. {
  91. promptItem.SetChineseWord(null, -1);
  92. }
  93. }
  94. SetChineseFirstEnteredText();
  95. LayoutRebuilder.ForceRebuildLayoutImmediate(this.GetComponent<RectTransform>());
  96. }
  97. private void SetTrunBtnVisiable()
  98. {
  99. if (currentPageCount == 0)
  100. {
  101. turnLeftBtn.gameObject.SetActive(false);
  102. }
  103. else
  104. {
  105. turnLeftBtn.gameObject.SetActive(true);
  106. }
  107. if (currentPageCount == totalPageCount)
  108. {
  109. turnRightBtn.gameObject.SetActive(false);
  110. }
  111. else
  112. {
  113. turnRightBtn.gameObject.SetActive(true);
  114. }
  115. }
  116. private void OnEnteredPromptItemClickCallback()
  117. {
  118. string firstChineseWord = GetFirstChineseWord();
  119. onPromptItemClick?.Invoke(firstChineseWord);
  120. }
  121. private void OnPromptItemClickCallback(string chineseWord)
  122. {
  123. onPromptItemClick?.Invoke(chineseWord);
  124. }
  125. }