SCKeyboard.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace SC.XR.Unity.Module_Keyboard
  6. {
  7. //
  8. // Summary:
  9. // 3DKeyboard in the scene.Support for Android,UnityEditor
  10. public abstract class SCKeyboard
  11. {
  12. public SCKeyboard(Vector3 position, Quaternion rotation, Vector3 localScale, SCKeyboardEnum scKeyboardEnum, SCKeyboardType scKeyboardType)
  13. {
  14. SCKeyboardMono keyboardPrefab = Resources.Load<SCKeyboardMono>(PrefabResourceName);
  15. if (keyboardPrefab == null)
  16. {
  17. Debug.LogError("This prefab resource do not exist");
  18. return;
  19. }
  20. keyboardPrefab.presentKeyboardType = scKeyboardType;
  21. SCKeyboardMono keyboardMono = SCKeyboardMono.Instantiate(keyboardPrefab);
  22. keyboardMonoDic[scKeyboardEnum] = keyboardMono;
  23. SetKeyboardTransform(scKeyboardEnum, position, rotation, localScale);
  24. Init();
  25. ResetKeyboard(scKeyboardType);
  26. }
  27. #region protected properties
  28. protected static Rect _area;
  29. protected static bool _visible = false;
  30. protected static bool _isInPlaceEditingAllowed = true;
  31. protected string _text = string.Empty;
  32. public static Status _status;
  33. #endregion
  34. #region public properties
  35. public static Rect area { get { return _area; } }
  36. public static bool visible { get { return _visible; } }
  37. public static bool isInPlaceEditingAllowed { get { return _isInPlaceEditingAllowed; } }
  38. public static bool isSupported
  39. {
  40. get
  41. {
  42. RuntimePlatform platform = Application.platform;
  43. switch (platform)
  44. {
  45. case RuntimePlatform.WindowsEditor:
  46. case RuntimePlatform.Android:
  47. return true;
  48. default:
  49. return false;
  50. }
  51. }
  52. }
  53. public static bool hideInput { get; set; }
  54. public int targetDisplay { get; set; }
  55. public TouchScreenKeyboardType type { get; }
  56. public RangeInt selection { get; set; }
  57. public virtual string text { get; set; }
  58. public bool canSetSelection { get; }
  59. public bool active
  60. {
  61. get { if (keyboardMonoDic.ContainsKey(keyboardEnum) && keyboardMonoDic[keyboardEnum] != null) return keyboardMonoDic[keyboardEnum].isActiveAndEnabled; else return false; }
  62. set { if (keyboardMonoDic.ContainsKey(keyboardEnum) && keyboardMonoDic[keyboardEnum] != null) keyboardMonoDic[keyboardEnum].gameObject.SetActive(value); OnActiveStateChanged?.Invoke(value);_visible = value;
  63. //if (value == false) { ClearOnKeyClickEvent(); ClearAllSpecialKeyClickEvent(); }
  64. }
  65. }
  66. public bool canGetSelection { get; set; }
  67. public Status status { get { return _status; } }
  68. public int characterLimit { get; set; }
  69. public static Action<bool> OnActiveStateChanged;
  70. private static KeyboardSystemLanguage m_CurrentSystemLanguage = KeyboardSystemLanguage.Chinese;
  71. public static KeyboardSystemLanguage currentSystemLanguage
  72. {
  73. get
  74. {
  75. if (useSystemLanguage)
  76. {
  77. switch (API_Module_DetectorSystem_Language.GetSystemLanguage())
  78. {
  79. case SCLanguageEnum.ChineseSimplified:
  80. case SCLanguageEnum.ChineseTraditional:
  81. m_CurrentSystemLanguage = KeyboardSystemLanguage.Chinese;
  82. break;
  83. case SCLanguageEnum.English:
  84. m_CurrentSystemLanguage = KeyboardSystemLanguage.English;
  85. break;
  86. }
  87. }
  88. return m_CurrentSystemLanguage;
  89. }
  90. set
  91. {
  92. m_CurrentSystemLanguage = value;
  93. }
  94. }
  95. public static bool useSystemLanguage = true;
  96. #endregion
  97. #region global actions
  98. private static Action<string> OnWebKeyClick;
  99. #endregion
  100. public virtual Transform transform { get { return keyboardMonoDic[keyboardEnum].transform; } }
  101. public virtual GameObject gameObject { get { return keyboardMonoDic[keyboardEnum].gameObject; } }
  102. protected virtual void Init()
  103. {
  104. SCKeyboardMono keyboardMono = keyboardMonoDic[keyboardEnum];
  105. if (keyboardMono == null)
  106. {
  107. Debug.LogError("This keyboard prefab do not have scripts inhert from SCKeyboardMono!");
  108. return;
  109. }
  110. //keyboardMono.Initialize();
  111. //Local Actions
  112. keyboardMono.OnDoneKeyClick += OnDoneKeyClickEvent;
  113. keyboardMono.OnTextChange += OnTextChangeEvent;
  114. keyboardMono.OnStatusChange += OnStatusChangeEvent;
  115. //Global Actions
  116. keyboardMono.OnWebKeyClick += OnWebKeyClickEvent;
  117. }
  118. protected abstract string PrefabResourceName { get; }
  119. protected virtual SCKeyboardEnum keyboardEnum { get; }
  120. protected static Dictionary<SCKeyboardEnum, SCKeyboardMono> keyboardMonoDic = new Dictionary<SCKeyboardEnum, SCKeyboardMono>();
  121. protected static Dictionary<SCKeyboardEnum, Type> keyboardTypeDic = new Dictionary<SCKeyboardEnum, Type>()
  122. {
  123. { SCKeyboardEnum.SCKeyboard2D, typeof(SCKeyboard2D) },
  124. { SCKeyboardEnum.SCKeyboard3D, typeof(SCKeyboard3D) }
  125. };
  126. protected static Dictionary<SCKeyboardEnum, SCKeyboard> keyboardCacheDic = new Dictionary<SCKeyboardEnum, SCKeyboard>();
  127. /// <summary>
  128. /// Open the keyboard and put the keyboard in the world space.Set the keyboard's position, rotate and scale int the world space.
  129. /// </summary>
  130. /// <param name="scKeyboardEnum"></param>
  131. /// <param name="text"></param>
  132. /// <param name="touchScreenKeyboardType"></param>
  133. /// <param name="position"></param>
  134. /// <param name="rotation"></param>
  135. /// <param name="localScale"></param>
  136. /// <returns></returns>
  137. public static SCKeyboard Open(SCKeyboardEnum scKeyboardEnum, string text, SCKeyboardType scKeyboardType, Vector3 position, Quaternion rotation, Vector3 localScale)
  138. {
  139. // TODO 设置系统语言
  140. // SetSystemLanguage();
  141. if (keyboardMonoDic != null && keyboardMonoDic.ContainsKey(scKeyboardEnum) && keyboardMonoDic[scKeyboardEnum] != null)
  142. {
  143. if (keyboardCacheDic != null && keyboardCacheDic.ContainsKey(scKeyboardEnum))
  144. {
  145. SCKeyboard keyboardCache = keyboardCacheDic[scKeyboardEnum];
  146. keyboardCache.Clear();
  147. keyboardCache.ResetKeyboard(scKeyboardType);
  148. keyboardCache.SetKeyboardTransform(scKeyboardEnum, position, rotation, localScale);
  149. return keyboardCache;
  150. }
  151. }
  152. if (!keyboardTypeDic.ContainsKey(scKeyboardEnum))
  153. {
  154. DebugMy.LogError("This SCKeyboardEnum do not exist", scKeyboardEnum);
  155. return null;
  156. }
  157. SCKeyboard keyboard = Activator.CreateInstance(keyboardTypeDic[scKeyboardEnum], position, rotation, localScale, scKeyboardEnum, scKeyboardType) as SCKeyboard;
  158. keyboardCacheDic[scKeyboardEnum] = keyboard;
  159. return keyboard;
  160. }
  161. /// <summary>
  162. /// Open the keyboard.
  163. /// </summary>
  164. /// <param name="scKeyboardEnum"></param>
  165. /// <param name="text"></param>
  166. /// <returns></returns>
  167. public static SCKeyboard Open(SCKeyboardEnum scKeyboardEnum, string text)
  168. {
  169. // TODO 设置系统语言
  170. // SetSystemLanguage();
  171. if (keyboardMonoDic != null && keyboardMonoDic.ContainsKey(scKeyboardEnum) && keyboardMonoDic[scKeyboardEnum] != null)
  172. {
  173. if (keyboardCacheDic != null && keyboardCacheDic.ContainsKey(scKeyboardEnum))
  174. {
  175. SCKeyboard keyboardCache = keyboardCacheDic[scKeyboardEnum];
  176. keyboardCache.Clear();
  177. keyboardCache.ResetKeyboard();
  178. return keyboardCache;
  179. }
  180. }
  181. if (!keyboardTypeDic.ContainsKey(scKeyboardEnum))
  182. {
  183. DebugMy.LogError("This SCKeyboardEnum do not exist", scKeyboardEnum);
  184. return null;
  185. }
  186. SCKeyboard keyboard = Activator.CreateInstance(keyboardTypeDic[scKeyboardEnum], Vector3.zero, Quaternion.identity, Vector3.one, scKeyboardEnum, SCKeyboardType.Default) as SCKeyboard;
  187. keyboardCacheDic[scKeyboardEnum] = keyboard;
  188. return keyboard;
  189. }
  190. /// <summary>
  191. /// Close the keyboard.
  192. /// </summary>
  193. /// <param name="scKeyboardEnum"></param>
  194. /// <returns></returns>
  195. public static SCKeyboard Close(SCKeyboardEnum scKeyboardEnum)
  196. {
  197. if (keyboardCacheDic != null && keyboardCacheDic.ContainsKey(scKeyboardEnum))
  198. {
  199. SCKeyboard keyboardCache = keyboardCacheDic[scKeyboardEnum];
  200. keyboardCache.CloseKeyboard();
  201. return keyboardCache;
  202. }
  203. if (!keyboardTypeDic.ContainsKey(scKeyboardEnum))
  204. {
  205. DebugMy.LogError("This SCKeyboardEnum do not exist", scKeyboardEnum);
  206. return null;
  207. }
  208. return null;
  209. }
  210. /// <summary>
  211. /// Set Keyboard Default Language.
  212. /// </summary>
  213. public static void SetSystemLanguage(KeyboardSystemLanguage language)
  214. {
  215. currentSystemLanguage = language;
  216. }
  217. #region Register
  218. public void Clear()
  219. {
  220. ClearAllSpecialKeyClickEvent();
  221. ClearOnKeyClickEvent();
  222. }
  223. /// <summary>
  224. /// Now support Enter,DeletemDone
  225. /// </summary>
  226. /// <param name="specialKeyEnum"></param>
  227. public void RegisterOnSpecialKeyClickEvent(SpecialKeyEnum specialKeyEnum, Action action)
  228. {
  229. switch (specialKeyEnum)
  230. {
  231. case SpecialKeyEnum.Enter:
  232. keyboardMonoDic[keyboardEnum].OnEnterKeyClick += action;
  233. break;
  234. case SpecialKeyEnum.Delete:
  235. keyboardMonoDic[keyboardEnum].OnDeleteKeyClick += action;
  236. break;
  237. case SpecialKeyEnum.Done:
  238. keyboardMonoDic[keyboardEnum].OnDoneKeyClick += action;
  239. break;
  240. }
  241. }
  242. /// <summary>
  243. /// Now support Enter,DeletemDone
  244. /// </summary>
  245. /// <param name="specialKeyEnum"></param>
  246. public void UnRegisterOnSpecialKeyClickEvent(SpecialKeyEnum specialKeyEnum, Action action)
  247. {
  248. switch (specialKeyEnum)
  249. {
  250. case SpecialKeyEnum.Enter:
  251. keyboardMonoDic[keyboardEnum].OnEnterKeyClick -= action;
  252. break;
  253. case SpecialKeyEnum.Delete:
  254. keyboardMonoDic[keyboardEnum].OnDeleteKeyClick -= action;
  255. break;
  256. case SpecialKeyEnum.Done:
  257. keyboardMonoDic[keyboardEnum].OnDoneKeyClick -= action;
  258. break;
  259. }
  260. }
  261. /// <summary>
  262. /// Now support Enter,DeletemDone
  263. /// </summary>
  264. /// <param name="specialKeyEnum"></param>
  265. public void ClearOnSpecialKeyClickEvent(SpecialKeyEnum specialKeyEnum)
  266. {
  267. switch (specialKeyEnum)
  268. {
  269. case SpecialKeyEnum.Enter:
  270. keyboardMonoDic[keyboardEnum].OnEnterKeyClick = null;
  271. break;
  272. case SpecialKeyEnum.Delete:
  273. keyboardMonoDic[keyboardEnum].OnDeleteKeyClick = null;
  274. break;
  275. case SpecialKeyEnum.Done:
  276. keyboardMonoDic[keyboardEnum].OnDoneKeyClick = null;
  277. break;
  278. }
  279. }
  280. /// <summary>
  281. /// Now support Enter,DeletemDone
  282. /// </summary>
  283. public void ClearAllSpecialKeyClickEvent()
  284. {
  285. keyboardMonoDic[keyboardEnum].OnEnterKeyClick = null;
  286. keyboardMonoDic[keyboardEnum].OnDeleteKeyClick = null;
  287. keyboardMonoDic[keyboardEnum].OnDoneKeyClick = null;
  288. }
  289. public void RegisterOnKeyClickEvent(Action<string> action)
  290. {
  291. keyboardMonoDic[keyboardEnum].OnKeyClick += action;
  292. }
  293. public void UnRegisterOnKeyClickEvent(Action<string> action)
  294. {
  295. keyboardMonoDic[keyboardEnum].OnKeyClick -= action;
  296. }
  297. public void ClearOnKeyClickEvent()
  298. {
  299. keyboardMonoDic[keyboardEnum].OnKeyClick = null;
  300. }
  301. public static void RegisterOnWebKeyClickEvent(Action<string> action)
  302. {
  303. OnWebKeyClick += action;
  304. }
  305. public static void UnRegisterOnWebKeyClickEvent(Action<string> action)
  306. {
  307. OnWebKeyClick -= action;
  308. }
  309. public static void ClearOnWebKeyClickEvent()
  310. {
  311. OnWebKeyClick = null;
  312. }
  313. public void RegisterOnPointerDownEvent(Action<PointerEventData> action)
  314. {
  315. keyboardMonoDic[keyboardEnum].OnPointerDown += action;
  316. }
  317. public void UnRegisterOnPointerDownEvent(Action<PointerEventData> action)
  318. {
  319. keyboardMonoDic[keyboardEnum].OnPointerDown -= action;
  320. }
  321. public void ClearOnPointerDownEvent()
  322. {
  323. keyboardMonoDic[keyboardEnum].OnPointerDown = null;
  324. }
  325. #endregion
  326. #region protected Events
  327. protected void OnTextChangeEvent(string text)
  328. {
  329. this.text = text;
  330. }
  331. protected virtual void OnPointerDownEvent(PointerEventData action)
  332. {
  333. EventSystem.current.SetSelectedGameObject(keyboardMonoDic[keyboardEnum].gameObject);
  334. Debug.Log("EventSystem.current.SetSelectedGameObject : " + EventSystem.current.currentSelectedGameObject.name);
  335. _status = Status.Visible;
  336. }
  337. protected virtual void OnDoneKeyClickEvent()
  338. {
  339. _status = Status.Done;
  340. }
  341. protected virtual void ResetKeyboard()
  342. {
  343. _status = Status.LostFocus;
  344. this.active = true;
  345. ClearOnPointerDownEvent();
  346. RegisterOnPointerDownEvent(OnPointerDownEvent);
  347. }
  348. protected virtual void ResetKeyboard(SCKeyboardType scKeyboardType)
  349. {
  350. _status = Status.LostFocus;
  351. ResetKeyboardType(scKeyboardType);
  352. this.active = true;
  353. ClearOnPointerDownEvent();
  354. RegisterOnPointerDownEvent(OnPointerDownEvent);
  355. }
  356. protected virtual void ResetKeyboardType(SCKeyboardType scKeyboardType)
  357. {
  358. keyboardMonoDic[keyboardEnum].SetKeyboardType(scKeyboardType);
  359. }
  360. protected virtual void CloseKeyboard()
  361. {
  362. active = false;
  363. }
  364. protected virtual void SetKeyboardTransform(SCKeyboardEnum sckeyboardEnum, Vector3 positon, Quaternion rotation, Vector3 localScale)
  365. {
  366. if (!keyboardMonoDic.ContainsKey(sckeyboardEnum))
  367. {
  368. Debug.LogError("KeyboardMono is NULL");
  369. return;
  370. }
  371. SCKeyboardMono keyboardMono = keyboardMonoDic[sckeyboardEnum];
  372. keyboardMono.transform.SetParent(null);
  373. keyboardMono.transform.localPosition = positon;
  374. keyboardMono.transform.localRotation = rotation;
  375. keyboardMono.transform.localScale = localScale;
  376. }
  377. #endregion
  378. #region public Events
  379. public void OnStatusChangeEvent(Status status)
  380. {
  381. _status = status;
  382. }
  383. public void OnWebKeyClickEvent(string val)
  384. {
  385. OnWebKeyClick?.Invoke(val);
  386. }
  387. #endregion
  388. public enum Status
  389. {
  390. Visible = 0,
  391. Done = 1,
  392. Canceled = 2,
  393. LostFocus = 3
  394. }
  395. }
  396. }