SCKeyboard3DKey.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System.Collections.Generic;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace SC.XR.Unity.Module_Keyboard
  6. {
  7. public class SCKeyboard3DKey : SCKeyboardBaseKey
  8. {
  9. #region KeyboardKeyEnums
  10. public SCKeyboardKeyEnum m_SCKeyboardKeyEnum;
  11. public SpecialKeyEnum m_SpecialKeyEnum;
  12. public LanguageKeyEnum m_LanguageKeyEnum;
  13. #endregion
  14. #region KeyboardKey Init Events
  15. /// <summary>
  16. /// Init Normal Key
  17. /// </summary>
  18. public virtual void OnNormalKeyInit()
  19. {
  20. }
  21. /// <summary>
  22. /// Init Symbol Key
  23. /// </summary>
  24. public virtual void OnSymbolKeyInit()
  25. {
  26. }
  27. /// <summary>
  28. /// Init Special Key
  29. /// </summary>
  30. public virtual void OnSpecialKeyInit()
  31. {
  32. }
  33. /// <summary>
  34. /// Init Pinyin Key
  35. /// </summary>
  36. public virtual void OnPinyinKeyInit()
  37. {
  38. }
  39. /// <summary>
  40. /// Init Language Key
  41. /// </summary>
  42. public virtual void OnLanguageKeyInit()
  43. {
  44. }
  45. /// <summary>
  46. /// Init Prompt Key
  47. /// </summary>
  48. public virtual void OnPromptKeyInit()
  49. {
  50. }
  51. #endregion
  52. #region KeyboardKey Regist Events
  53. /// <summary>
  54. /// Regist Normal Key
  55. /// </summary>
  56. public virtual void OnNormalKeyRegist()
  57. {
  58. OnKeyClickEvent += OnNormalKeyClick;
  59. }
  60. /// <summary>
  61. /// Regist Symbol Key
  62. /// </summary>
  63. public virtual void OnSymbolKeyRegist()
  64. {
  65. OnKeyClickEvent += OnNormalKeyClick;
  66. TextMesh textMesh = this.GetComponentInChildren<TextMesh>();
  67. this.Value = textMesh.text;
  68. if (SCKeyboardMono.symbolKeysDic.ContainsKey(this.Value))
  69. {
  70. List<SCKeyboardBaseKey> scKeyboard2DKeys = SCKeyboardMono.symbolKeysDic[this.Value];
  71. scKeyboard2DKeys.Add(this);
  72. SCKeyboardMono.symbolKeysDic[this.Value] = scKeyboard2DKeys;
  73. }
  74. else
  75. {
  76. List<SCKeyboardBaseKey> scKeyboard2DKeys = new List<SCKeyboardBaseKey>();
  77. scKeyboard2DKeys.Add(this);
  78. SCKeyboardMono.symbolKeysDic.Add(this.Value, scKeyboard2DKeys);
  79. }
  80. }
  81. /// <summary>
  82. /// Regist Special Key
  83. /// </summary>
  84. public virtual void OnSpecialKeyRegist()
  85. {
  86. OnKeyClickEvent += OnSpecialKeyClick;
  87. }
  88. /// <summary>
  89. /// Regist Pinyin Key
  90. /// </summary>
  91. public virtual void OnPinyinKeyRegist()
  92. {
  93. OnKeyClickEvent += OnPinyinKeyClick;
  94. TextMesh textMesh = this.GetComponentInChildren<TextMesh>();
  95. this.Value = textMesh.text;
  96. SCKeyboardMono.OnKeyboardShifted += Shift;
  97. }
  98. /// <summary>
  99. /// Regist Language Key
  100. /// </summary>
  101. public virtual void OnLanguageKeyRegist()
  102. {
  103. OnKeyClickEvent += OnLanguageKeyClick;
  104. }
  105. /// <summary>
  106. /// Regist Prompt Key
  107. /// </summary>
  108. public virtual void OnPromptKeyRegist()
  109. {
  110. OnKeyClickEvent += OnPromptKeyClick;
  111. }
  112. #endregion
  113. #region KeyboardKey UnRegist Events
  114. /// <summary>
  115. /// UnRegist Normal Key
  116. /// </summary>
  117. public virtual void OnNormalKeyUnRegist()
  118. {
  119. OnKeyClickEvent -= OnNormalKeyClick;
  120. }
  121. /// <summary>
  122. /// UnRegist Symbol Key
  123. /// </summary>
  124. public virtual void OnSymbolKeyUnRegist()
  125. {
  126. OnKeyClickEvent -= OnNormalKeyClick;
  127. SCKeyboardMono.symbolKeysDic.Clear();
  128. }
  129. /// <summary>
  130. /// UnRegist Special Key
  131. /// </summary>
  132. public virtual void OnSpecialKeyUnRegist()
  133. {
  134. OnKeyClickEvent -= OnSpecialKeyClick;
  135. }
  136. /// <summary>
  137. /// UnRegist Pinyin Key
  138. /// </summary>
  139. public virtual void OnPinyinKeyUnRegist()
  140. {
  141. OnKeyClickEvent -= OnPinyinKeyClick;
  142. SCKeyboardMono.OnKeyboardShifted -= Shift;
  143. }
  144. /// <summary>
  145. /// UnRegist Language Key
  146. /// </summary>
  147. public virtual void OnLanguageKeyUnRegist()
  148. {
  149. OnKeyClickEvent -= OnLanguageKeyClick;
  150. }
  151. /// <summary>
  152. /// UnRegist Prompt Key
  153. /// </summary>
  154. public virtual void OnPromptKeyUnRegist()
  155. {
  156. OnKeyClickEvent -= OnPromptKeyClick;
  157. }
  158. #endregion
  159. #region KeyboardKey Click Events
  160. public void OnNormalKeyClick()
  161. {
  162. SCKeyboardMono.OnNormalKeyClick(this.Value);
  163. }
  164. public void OnSpecialKeyClick()
  165. {
  166. SCKeyboardMono.OnSpecialKeyClick(m_SpecialKeyEnum);
  167. switch (m_SpecialKeyEnum)
  168. {
  169. case SpecialKeyEnum.Cn:
  170. GetComponentInChildren<TextMesh>().text = "英";
  171. m_SpecialKeyEnum = SpecialKeyEnum.En;
  172. break;
  173. case SpecialKeyEnum.En:
  174. GetComponentInChildren<TextMesh>().text = "中";
  175. m_SpecialKeyEnum = SpecialKeyEnum.Cn;
  176. break;
  177. }
  178. }
  179. public void OnPinyinKeyClick()
  180. {
  181. switch (SCKeyboardMono.presentKeyboardState)
  182. {
  183. case KeyboardState.Cn:
  184. if (SCKeyboardMono.IsShifted)
  185. {
  186. SCKeyboardMono.OnNormalKeyClick(this.Value);
  187. return;
  188. }
  189. SCKeyboardMono.OnPinyinKeyClick(this.Value);
  190. break;
  191. case KeyboardState.En:
  192. SCKeyboardMono.OnNormalKeyClick(this.Value);
  193. break;
  194. }
  195. }
  196. public void OnLanguageKeyClick()
  197. {
  198. SCKeyboardMono.OnLanguageKeyClick(m_LanguageKeyEnum);
  199. }
  200. public void OnPromptKeyClick()
  201. {
  202. TextMesh textMesh = this.GetComponentInChildren<TextMesh>();
  203. SCKeyboardMono.OnPromptKeyClick(textMesh.text);
  204. SCKeyboardMono.OnKeyClick?.Invoke(textMesh.text);
  205. for (int i = 0; i < textMesh.text.Length; i++)
  206. {
  207. SCKeyboardMono.OnWebKeyClick?.Invoke(textMesh.text[i].ToString());
  208. }
  209. }
  210. #endregion
  211. #region KeyboardKey State Events
  212. protected virtual void Shift(bool isShifted)
  213. {
  214. TextMesh textMesh = this.GetComponentInChildren<TextMesh>();
  215. if (isShifted)
  216. {
  217. string textUpper = textMesh.text.ToUpper();
  218. textMesh.text = textUpper;
  219. this.Value = textUpper;
  220. }
  221. else
  222. {
  223. string textLower = textMesh.text.ToLower();
  224. textMesh.text = textLower;
  225. this.Value = textLower;
  226. }
  227. }
  228. #endregion
  229. protected InteractionPressableEntry interactionPressableEntry;
  230. protected InteractionTouchableEntry interactionTouchableEntry;
  231. public override void Init()
  232. {
  233. base.Init();
  234. switch (m_SCKeyboardKeyEnum)
  235. {
  236. case SCKeyboardKeyEnum.ABC:
  237. case SCKeyboardKeyEnum.Num:
  238. OnNormalKeyInit();
  239. break;
  240. case SCKeyboardKeyEnum.Symbol:
  241. OnSymbolKeyInit();
  242. break;
  243. case SCKeyboardKeyEnum.Special:
  244. OnSpecialKeyInit();
  245. break;
  246. case SCKeyboardKeyEnum.Pinyin:
  247. OnPinyinKeyInit();
  248. break;
  249. case SCKeyboardKeyEnum.Prompt:
  250. OnPromptKeyInit();
  251. break;
  252. case SCKeyboardKeyEnum.Meme:
  253. break;
  254. case SCKeyboardKeyEnum.Language:
  255. OnLanguageKeyInit();
  256. break;
  257. default:
  258. OnNormalKeyInit();
  259. break;
  260. }
  261. }
  262. protected override void RegistKey()
  263. {
  264. PressableButton pressableButton = this.GetComponent<PressableButton>();
  265. TouchableButton touchableButton = this.GetComponent<TouchableButton>();
  266. if (pressableButton && touchableButton)
  267. {
  268. InteractionEvent interActionEvent = new InteractionEvent();
  269. interActionEvent.AddListener(OnKeyClick);
  270. interactionPressableEntry = new InteractionPressableEntry() { eventID = InteractionPressableType.PointerClick, callback = interActionEvent };
  271. interactionTouchableEntry = new InteractionTouchableEntry() { eventID = InteractionTouchableType.PokePress, callback = interActionEvent };
  272. pressableButton.Triggers.Add(interactionPressableEntry);
  273. touchableButton.Triggers.Add(interactionTouchableEntry);
  274. }
  275. switch (m_SCKeyboardKeyEnum)
  276. {
  277. case SCKeyboardKeyEnum.ABC:
  278. case SCKeyboardKeyEnum.Num:
  279. OnNormalKeyRegist();
  280. break;
  281. case SCKeyboardKeyEnum.Symbol:
  282. OnSymbolKeyRegist();
  283. break;
  284. case SCKeyboardKeyEnum.Special:
  285. OnSpecialKeyRegist();
  286. break;
  287. case SCKeyboardKeyEnum.Pinyin:
  288. OnPinyinKeyRegist();
  289. break;
  290. case SCKeyboardKeyEnum.Prompt:
  291. OnPromptKeyRegist();
  292. break;
  293. case SCKeyboardKeyEnum.Meme:
  294. break;
  295. case SCKeyboardKeyEnum.Language:
  296. OnLanguageKeyRegist();
  297. break;
  298. default:
  299. OnNormalKeyRegist();
  300. break;
  301. }
  302. }
  303. protected override void UnRegistKey()
  304. {
  305. PressableButton pressableButton = this.GetComponent<PressableButton>();
  306. TouchableButton touchableButton = this.GetComponent<TouchableButton>();
  307. if (pressableButton && touchableButton)
  308. {
  309. pressableButton.Triggers.Remove(interactionPressableEntry);
  310. touchableButton.Triggers.Remove(interactionTouchableEntry);
  311. }
  312. switch (m_SCKeyboardKeyEnum)
  313. {
  314. case SCKeyboardKeyEnum.ABC:
  315. case SCKeyboardKeyEnum.Num:
  316. OnNormalKeyUnRegist();
  317. break;
  318. case SCKeyboardKeyEnum.Symbol:
  319. OnSymbolKeyUnRegist();
  320. break;
  321. case SCKeyboardKeyEnum.Special:
  322. OnSpecialKeyUnRegist();
  323. break;
  324. case SCKeyboardKeyEnum.Pinyin:
  325. OnPinyinKeyUnRegist();
  326. break;
  327. case SCKeyboardKeyEnum.Prompt:
  328. OnPromptKeyUnRegist();
  329. break;
  330. case SCKeyboardKeyEnum.Meme:
  331. break;
  332. case SCKeyboardKeyEnum.Language:
  333. OnLanguageKeyUnRegist();
  334. break;
  335. default:
  336. OnNormalKeyUnRegist();
  337. break;
  338. }
  339. }
  340. }
  341. }