SCKeyboard2DKey.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. namespace SC.XR.Unity.Module_Keyboard
  8. {
  9. public class SCKeyboard2DKey : SCKeyboardBaseKey
  10. {
  11. public override string Value
  12. {
  13. get
  14. {
  15. return base.Value;
  16. }
  17. set
  18. {
  19. base.Value = value;
  20. Text.text = value;
  21. }
  22. }
  23. private Text m_Text;
  24. public virtual Text Text
  25. {
  26. get
  27. {
  28. if (m_Text == null)
  29. m_Text = GetComponentInChildren<Text>();
  30. return m_Text;
  31. }
  32. set
  33. {
  34. m_Text = value;
  35. }
  36. }
  37. #region KeyboardKeyEnums
  38. public SCKeyboardKeyEnum m_SCKeyboardKeyEnum;
  39. public SpecialKeyEnum m_SpecialKeyEnum;
  40. public LanguageKeyEnum m_LanguageKeyEnum;
  41. public UGUIKeyEnum m_UGUIKeyEnum;
  42. #endregion
  43. #region KeyboardKey Init Events
  44. /// <summary>
  45. /// Init Normal Key
  46. /// </summary>
  47. public virtual void OnNormalKeyInit()
  48. {
  49. }
  50. public virtual void OnNumberKeyInit()
  51. {
  52. }
  53. /// <summary>
  54. /// Init Symbol Key
  55. /// </summary>
  56. public virtual void OnSymbolKeyInit()
  57. {
  58. }
  59. /// <summary>
  60. /// Init Special Key
  61. /// </summary>
  62. public virtual void OnSpecialKeyInit()
  63. {
  64. switch (m_SpecialKeyEnum)
  65. {
  66. case SpecialKeyEnum.Cn:
  67. case SpecialKeyEnum.En:
  68. switch (SCKeyboardMono.presentKeyboardType)
  69. {
  70. case SCKeyboardType.ASCIICapable:
  71. Value = "英";
  72. m_SpecialKeyEnum = SpecialKeyEnum.En;
  73. break;
  74. default:
  75. switch (SCKeyboard.currentSystemLanguage)
  76. {
  77. case KeyboardSystemLanguage.Chinese:
  78. Value = "中";
  79. m_SpecialKeyEnum = SpecialKeyEnum.Cn;
  80. break;
  81. default:
  82. Value = "英";
  83. m_SpecialKeyEnum = SpecialKeyEnum.En;
  84. break;
  85. }
  86. break;
  87. }
  88. break;
  89. }
  90. KeyboardStateChange(SCKeyboardMono.presentKeyboardState);
  91. }
  92. /// <summary>
  93. /// Init Pinyin Key
  94. /// </summary>
  95. public virtual void OnPinyinKeyInit()
  96. {
  97. }
  98. /// <summary>
  99. /// Init Language Key
  100. /// </summary>
  101. public virtual void OnLanguageKeyInit()
  102. {
  103. }
  104. /// <summary>
  105. /// Init Prompt Key
  106. /// </summary>
  107. public virtual void OnPromptKeyInit()
  108. {
  109. }
  110. /// <summary>
  111. /// Init UGUI Key
  112. /// </summary>
  113. public virtual void OnUGUIKeyInit()
  114. {
  115. }
  116. #endregion
  117. #region KeyboardKey Regist Events
  118. /// <summary>
  119. /// Regist Normal Key
  120. /// </summary>
  121. public virtual void OnNormalKeyRegist()
  122. {
  123. OnKeyClickEvent += OnNormalKeyClick;
  124. }
  125. /// <summary>
  126. /// Regist Number Key
  127. /// </summary>
  128. public virtual void OnNumberKeyRegist()
  129. {
  130. OnKeyClickEvent += OnNumberKeyClick;
  131. Text text = this.GetComponentInChildren<Text>();
  132. this.Value = text.text;
  133. }
  134. /// <summary>
  135. /// Regist Symbol Key
  136. /// </summary>
  137. public virtual void OnSymbolKeyRegist()
  138. {
  139. OnKeyClickEvent += OnNormalKeyClick;
  140. Text text = this.GetComponentInChildren<Text>();
  141. this.Value = text.text;
  142. if (SCKeyboardMono.symbolKeysDic.ContainsKey(this.Value))
  143. {
  144. List<SCKeyboardBaseKey> scKeyboard2DKeys = SCKeyboardMono.symbolKeysDic[this.Value];
  145. scKeyboard2DKeys.Add(this);
  146. SCKeyboardMono.symbolKeysDic[this.Value] = scKeyboard2DKeys;
  147. }
  148. else
  149. {
  150. List<SCKeyboardBaseKey> scKeyboard2DKeys = new List<SCKeyboardBaseKey>();
  151. scKeyboard2DKeys.Add(this);
  152. SCKeyboardMono.symbolKeysDic.Add(this.Value, scKeyboard2DKeys);
  153. }
  154. }
  155. /// <summary>
  156. /// Regist Special Key
  157. /// </summary>
  158. public virtual void OnSpecialKeyRegist()
  159. {
  160. OnKeyClickEvent += OnSpecialKeyClick;
  161. if (SCKeyboardMono.specialKeysDic.ContainsKey(this.m_SpecialKeyEnum))
  162. {
  163. List<SCKeyboardBaseKey> scKeyboard2DKeys = SCKeyboardMono.specialKeysDic[this.m_SpecialKeyEnum];
  164. scKeyboard2DKeys.Add(this);
  165. SCKeyboardMono.specialKeysDic[this.m_SpecialKeyEnum] = scKeyboard2DKeys;
  166. }
  167. else
  168. {
  169. List<SCKeyboardBaseKey> scKeyboard2DKeys = new List<SCKeyboardBaseKey>();
  170. scKeyboard2DKeys.Add(this);
  171. SCKeyboardMono.specialKeysDic.Add(this.m_SpecialKeyEnum, scKeyboard2DKeys);
  172. }
  173. SCKeyboardMono.OnKeyboardStateChange += KeyboardStateChange;
  174. }
  175. /// <summary>
  176. /// Regist Pinyin Key
  177. /// </summary>
  178. public virtual void OnPinyinKeyRegist()
  179. {
  180. OnKeyClickEvent += OnPinyinKeyClick;
  181. Text text = this.GetComponentInChildren<Text>();
  182. this.Value = text.text;
  183. SCKeyboardMono.OnKeyboardShifted += Shift;
  184. }
  185. /// <summary>
  186. /// Regist Language Key
  187. /// </summary>
  188. public virtual void OnLanguageKeyRegist()
  189. {
  190. OnKeyClickEvent += OnLanguageKeyClick;
  191. }
  192. /// <summary>
  193. /// Regist Prompt Key
  194. /// </summary>
  195. public virtual void OnPromptKeyRegist()
  196. {
  197. OnKeyClickEvent += OnPromptKeyClick;
  198. }
  199. /// <summary>
  200. /// Regist UGUI Key
  201. /// </summary>
  202. public virtual void OnUGUIKeyRegist()
  203. {
  204. OnKeyClickEvent += OnUGUIKeyClick;
  205. }
  206. #endregion
  207. #region KeyboardKey UnRegist Events
  208. /// <summary>
  209. /// UnRegist Normal Key
  210. /// </summary>
  211. public virtual void OnNormalKeyUnRegist()
  212. {
  213. OnKeyClickEvent -= OnNormalKeyClick;
  214. }
  215. /// <summary>
  216. /// UnRegist Number Key
  217. /// </summary>
  218. public virtual void OnNumberKeyUnRegist()
  219. {
  220. OnKeyClickEvent -= OnNumberKeyClick;
  221. }
  222. /// <summary>
  223. /// UnRegist Symbol Key
  224. /// </summary>
  225. public virtual void OnSymbolKeyUnRegist()
  226. {
  227. OnKeyClickEvent -= OnNormalKeyClick;
  228. SCKeyboardMono.symbolKeysDic.Clear();
  229. }
  230. /// <summary>
  231. /// UnRegist Special Key
  232. /// </summary>
  233. public virtual void OnSpecialKeyUnRegist()
  234. {
  235. OnKeyClickEvent -= OnSpecialKeyClick;
  236. SCKeyboardMono.specialKeysDic.Clear();
  237. SCKeyboardMono.OnKeyboardStateChange -= KeyboardStateChange;
  238. }
  239. /// <summary>
  240. /// UnRegist Pinyin Key
  241. /// </summary>
  242. public virtual void OnPinyinKeyUnRegist()
  243. {
  244. OnKeyClickEvent -= OnPinyinKeyClick;
  245. SCKeyboardMono.OnKeyboardShifted -= Shift;
  246. }
  247. /// <summary>
  248. /// UnRegist Language Key
  249. /// </summary>
  250. public virtual void OnLanguageKeyUnRegist()
  251. {
  252. OnKeyClickEvent -= OnLanguageKeyClick;
  253. }
  254. /// <summary>
  255. /// UnRegist Prompt Key
  256. /// </summary>
  257. public virtual void OnPromptKeyUnRegist()
  258. {
  259. OnKeyClickEvent -= OnPromptKeyClick;
  260. }
  261. /// <summary>
  262. /// UnRegist UGUI Key
  263. /// </summary>
  264. public virtual void OnUGUIKeyUnRegist()
  265. {
  266. OnKeyClickEvent -= OnUGUIKeyClick;
  267. }
  268. #endregion
  269. #region KeyboardKey Click Events
  270. public void OnNormalKeyClick()
  271. {
  272. SCKeyboardMono.OnNormalKeyClick(this.Value);
  273. }
  274. public void OnNumberKeyClick()
  275. {
  276. SCKeyboardMono.OnNumberKeyClick(this.Value);
  277. }
  278. public void OnSpecialKeyClick()
  279. {
  280. SCKeyboardMono.OnSpecialKeyClick(m_SpecialKeyEnum);
  281. switch (m_SpecialKeyEnum)
  282. {
  283. case SpecialKeyEnum.Cn:
  284. Value = "英";
  285. m_SpecialKeyEnum = SpecialKeyEnum.En;
  286. break;
  287. case SpecialKeyEnum.En:
  288. Value = "中";
  289. m_SpecialKeyEnum = SpecialKeyEnum.Cn;
  290. break;
  291. }
  292. }
  293. public void OnPinyinKeyClick()
  294. {
  295. switch (SCKeyboardMono.presentKeyboardState)
  296. {
  297. case KeyboardState.Cn:
  298. if (SCKeyboardMono.IsShifted)
  299. {
  300. SCKeyboardMono.OnNormalKeyClick(this.Value);
  301. return;
  302. }
  303. SCKeyboardMono.OnPinyinKeyClick(this.Value);
  304. break;
  305. case KeyboardState.En:
  306. SCKeyboardMono.OnNormalKeyClick(this.Value);
  307. break;
  308. }
  309. }
  310. public void OnLanguageKeyClick()
  311. {
  312. SCKeyboardMono.OnLanguageKeyClick(m_LanguageKeyEnum);
  313. }
  314. public void OnPromptKeyClick()
  315. {
  316. SCKeyboardMono.OnPromptKeyClick(this.Value);
  317. }
  318. public void OnUGUIKeyClick()
  319. {
  320. SCKeyboardMono.OnUGUIKeyClick(m_UGUIKeyEnum);
  321. }
  322. #endregion
  323. #region KeyboardKey State Events
  324. protected virtual void Shift(bool isShifted)
  325. {
  326. Text text = this.GetComponentInChildren<Text>();
  327. if (isShifted)
  328. {
  329. this.Value = this.Value.ToUpper();
  330. }
  331. else
  332. {
  333. this.Value = this.Value.ToLower();
  334. }
  335. }
  336. protected virtual void KeyboardStateChange(KeyboardState state)
  337. {
  338. switch (state)
  339. {
  340. case KeyboardState.Cn:
  341. switch (m_SCKeyboardKeyEnum)
  342. {
  343. case SCKeyboardKeyEnum.Special:
  344. switch (m_SpecialKeyEnum)
  345. {
  346. case SpecialKeyEnum.Enter:
  347. switch (SCKeyboardMono.presentKeyboardType)
  348. {
  349. case SCKeyboardType.URL:
  350. case SCKeyboardType.Search:
  351. this.Value = "搜索";
  352. break;
  353. default:
  354. this.Value = "确认";
  355. break;
  356. }
  357. break;
  358. case SpecialKeyEnum.Space:
  359. this.Value = "空格";
  360. break;
  361. case SpecialKeyEnum.Participle:
  362. this.Value = "分词";
  363. break;
  364. }
  365. break;
  366. }
  367. break;
  368. case KeyboardState.En:
  369. switch (m_SCKeyboardKeyEnum)
  370. {
  371. case SCKeyboardKeyEnum.Special:
  372. switch (m_SpecialKeyEnum)
  373. {
  374. case SpecialKeyEnum.Enter:
  375. switch (SCKeyboardMono.presentKeyboardType)
  376. {
  377. case SCKeyboardType.URL:
  378. case SCKeyboardType.Search:
  379. this.Value = "Search";
  380. break;
  381. default:
  382. this.Value = "Enter";
  383. break;
  384. }
  385. break;
  386. case SpecialKeyEnum.Space:
  387. this.Value = "Space";
  388. break;
  389. case SpecialKeyEnum.Participle:
  390. this.Value = "/";
  391. break;
  392. }
  393. break;
  394. }
  395. break;
  396. }
  397. }
  398. #endregion
  399. public override void Init()
  400. {
  401. base.Init();
  402. switch (m_SCKeyboardKeyEnum)
  403. {
  404. case SCKeyboardKeyEnum.ABC:
  405. OnNormalKeyInit();
  406. break;
  407. case SCKeyboardKeyEnum.Num:
  408. OnNumberKeyInit();
  409. break;
  410. case SCKeyboardKeyEnum.Symbol:
  411. OnSymbolKeyInit();
  412. break;
  413. case SCKeyboardKeyEnum.Special:
  414. OnSpecialKeyInit();
  415. break;
  416. case SCKeyboardKeyEnum.Pinyin:
  417. OnPinyinKeyInit();
  418. break;
  419. case SCKeyboardKeyEnum.Prompt:
  420. OnPromptKeyInit();
  421. break;
  422. case SCKeyboardKeyEnum.Meme:
  423. break;
  424. case SCKeyboardKeyEnum.Language:
  425. OnLanguageKeyInit();
  426. break;
  427. case SCKeyboardKeyEnum.UGUI:
  428. OnUGUIKeyInit();
  429. break;
  430. default:
  431. OnNormalKeyInit();
  432. break;
  433. }
  434. }
  435. protected override void RegistKey()
  436. {
  437. base.RegistKey();
  438. switch (m_SCKeyboardKeyEnum)
  439. {
  440. case SCKeyboardKeyEnum.ABC:
  441. OnNormalKeyRegist();
  442. break;
  443. case SCKeyboardKeyEnum.Num:
  444. OnNumberKeyRegist();
  445. break;
  446. case SCKeyboardKeyEnum.Symbol:
  447. OnSymbolKeyRegist();
  448. break;
  449. case SCKeyboardKeyEnum.Special:
  450. OnSpecialKeyRegist();
  451. break;
  452. case SCKeyboardKeyEnum.Pinyin:
  453. OnPinyinKeyRegist();
  454. break;
  455. case SCKeyboardKeyEnum.Prompt:
  456. OnPromptKeyRegist();
  457. break;
  458. case SCKeyboardKeyEnum.Meme:
  459. break;
  460. case SCKeyboardKeyEnum.Language:
  461. OnLanguageKeyRegist();
  462. break;
  463. case SCKeyboardKeyEnum.UGUI:
  464. OnUGUIKeyRegist();
  465. break;
  466. default:
  467. OnNormalKeyRegist();
  468. break;
  469. }
  470. }
  471. protected override void UnRegistKey()
  472. {
  473. base.UnRegistKey();
  474. switch (m_SCKeyboardKeyEnum)
  475. {
  476. case SCKeyboardKeyEnum.ABC:
  477. OnNormalKeyUnRegist();
  478. break;
  479. case SCKeyboardKeyEnum.Num:
  480. OnNumberKeyUnRegist();
  481. break;
  482. case SCKeyboardKeyEnum.Symbol:
  483. OnSymbolKeyUnRegist();
  484. break;
  485. case SCKeyboardKeyEnum.Special:
  486. OnSpecialKeyUnRegist();
  487. break;
  488. case SCKeyboardKeyEnum.Pinyin:
  489. OnPinyinKeyUnRegist();
  490. break;
  491. case SCKeyboardKeyEnum.Prompt:
  492. OnPromptKeyUnRegist();
  493. break;
  494. case SCKeyboardKeyEnum.Meme:
  495. break;
  496. case SCKeyboardKeyEnum.Language:
  497. OnLanguageKeyUnRegist();
  498. break;
  499. case SCKeyboardKeyEnum.UGUI:
  500. OnUGUIKeyUnRegist();
  501. break;
  502. default:
  503. OnNormalKeyUnRegist();
  504. break;
  505. }
  506. }
  507. }
  508. }