SCKeyboardMono.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. namespace SC.XR.Unity.Module_Keyboard
  7. {
  8. public abstract class SCKeyboardMono : MonoBehaviour
  9. {
  10. #region Status
  11. public SCKeyboardType presentKeyboardType = SCKeyboardType.Default;
  12. public KeyboardState presentKeyboardState = KeyboardState.Cn;
  13. public Action<bool> OnKeyboardShifted { get; internal set; }
  14. public Action<KeyboardState> OnKeyboardStateChange;
  15. protected bool m_IsShifted = false;
  16. public bool IsShifted
  17. {
  18. get
  19. {
  20. return m_IsShifted;
  21. }
  22. }
  23. #endregion
  24. #region Actions
  25. public Action<string> OnTextChange;
  26. public Action<string> OnKeyClick;
  27. public Action<string> OnWebKeyClick;
  28. public Action OnDoneKeyClick;
  29. public Action OnDeleteKeyClick;
  30. public Action OnEnterKeyClick;
  31. public Action<SCKeyboard.Status> OnStatusChange;
  32. public Action<PointerEventData> OnPointerDown;
  33. #endregion
  34. #region String Builders
  35. private StringBuilder _stringBuilder;
  36. public virtual StringBuilder stringBuilder
  37. {
  38. get
  39. {
  40. if (_stringBuilder == null) _stringBuilder = new StringBuilder();
  41. return _stringBuilder;
  42. }
  43. }
  44. private StringBuilder _preInputStringBuilder;
  45. public virtual StringBuilder preInputStringBuilder
  46. {
  47. get
  48. {
  49. if (_preInputStringBuilder == null) _preInputStringBuilder = new StringBuilder();
  50. return _preInputStringBuilder;
  51. }
  52. }
  53. #endregion
  54. #region Life Cycles
  55. protected virtual void Awake()
  56. {
  57. //Initialize();
  58. }
  59. protected virtual void OnEnable()
  60. {
  61. Initialize(presentKeyboardType);
  62. }
  63. protected virtual void Start()
  64. {
  65. }
  66. protected virtual void LateUpdate()
  67. {
  68. if (SCKeyboard._status != SCKeyboard.Status.LostFocus &&
  69. EventSystem.current && EventSystem.current.currentSelectedGameObject != this.gameObject)
  70. {
  71. OnStatusChange?.Invoke(SCKeyboard.Status.LostFocus);
  72. HideKeyboardPrompt(false);
  73. }
  74. }
  75. #endregion
  76. #region Initialize
  77. public virtual void Initialize(SCKeyboardType keyboardType = SCKeyboardType.Default)
  78. {
  79. switch (keyboardType)
  80. {
  81. case SCKeyboardType.Default:
  82. DefaultKeyboardInitialize();
  83. break;
  84. case SCKeyboardType.ASCIICapable:
  85. ASCIICapableKeyboardInitialize();
  86. break;
  87. case SCKeyboardType.URL:
  88. URLKeyboardInitialize();
  89. break;
  90. case SCKeyboardType.Search:
  91. SearchKeyboardInitialize();
  92. break;
  93. }
  94. }
  95. protected virtual void DefaultKeyboardInitialize()
  96. {
  97. }
  98. protected virtual void ASCIICapableKeyboardInitialize()
  99. {
  100. }
  101. protected virtual void URLKeyboardInitialize()
  102. {
  103. }
  104. protected virtual void SearchKeyboardInitialize()
  105. {
  106. }
  107. #endregion
  108. #region Keyboards
  109. protected GameObject previousKeyboard;
  110. protected GameObject presentKeyboard;
  111. public SCKeyboardBasePrompt keyboard_prompt;
  112. public GameObject keyboard_abc;
  113. #endregion
  114. #region Keyboard Key Dics
  115. public Dictionary<SpecialKeyEnum, List<SCKeyboardBaseKey>> specialKeysDic = new Dictionary<SpecialKeyEnum, List<SCKeyboardBaseKey>>();
  116. public Dictionary<string, List<SCKeyboardBaseKey>> symbolKeysDic = new Dictionary<string, List<SCKeyboardBaseKey>>();
  117. protected Dictionary<int, RectTransform> promptRowRectTransformsDic = new Dictionary<int, RectTransform>();
  118. public Dictionary<int, Dictionary<int, Dictionary<int, SCKeyboardBaseKey>>> promptPagesDic = new Dictionary<int, Dictionary<int, Dictionary<int, SCKeyboardBaseKey>>>();
  119. public Dictionary<int, Dictionary<int, Dictionary<int, string>>> chinesePagesDic = new Dictionary<int, Dictionary<int, Dictionary<int, string>>>();
  120. public Dictionary<int, Dictionary<int, Dictionary<int, string>>> urlPagesDic = new Dictionary<int, Dictionary<int, Dictionary<int, string>>>();
  121. protected Dictionary<SpecialKeyEnum, Action> specialCallbackDic;
  122. protected Dictionary<UGUIKeyEnum, Action> uGUICallbackDic;
  123. protected Dictionary<LanguageKeyEnum, Action> languageCallBackDic;
  124. protected virtual void InitSpecialCallbackDic()
  125. {
  126. if (specialCallbackDic == null)
  127. {
  128. specialCallbackDic = new Dictionary<SpecialKeyEnum, Action>();
  129. }
  130. specialCallbackDic.Clear();
  131. specialCallbackDic.Add(SpecialKeyEnum.Enter, OnEnterClick);
  132. specialCallbackDic.Add(SpecialKeyEnum.Delete, OnDeleteClick);
  133. specialCallbackDic.Add(SpecialKeyEnum.Shift, OnShiftClick);
  134. specialCallbackDic.Add(SpecialKeyEnum.Space, OnSpaceClick);
  135. specialCallbackDic.Add(SpecialKeyEnum.CapsLock, OnCapsLockClick);
  136. specialCallbackDic.Add(SpecialKeyEnum.Tab, OnTabClick);
  137. specialCallbackDic.Add(SpecialKeyEnum.Ctrl, OnCtrlClick);
  138. specialCallbackDic.Add(SpecialKeyEnum.Alt, OnAltClick);
  139. specialCallbackDic.Add(SpecialKeyEnum.Done, OnDoneClick);
  140. specialCallbackDic.Add(SpecialKeyEnum.Clear, OnClearClick);
  141. specialCallbackDic.Add(SpecialKeyEnum.Symbol, OnSymbolClick);
  142. specialCallbackDic.Add(SpecialKeyEnum.Number, OnNumberClick);
  143. specialCallbackDic.Add(SpecialKeyEnum.Meme, OnMemeClick);
  144. specialCallbackDic.Add(SpecialKeyEnum.ABC, OnABCClick);
  145. specialCallbackDic.Add(SpecialKeyEnum.Participle, OnParticipleClick);
  146. specialCallbackDic.Add(SpecialKeyEnum.Com, OnComClick);
  147. specialCallbackDic.Add(SpecialKeyEnum.Microphone, OnMicrophoneClick);
  148. specialCallbackDic.Add(SpecialKeyEnum.Language, OnLanguageClick);
  149. specialCallbackDic.Add(SpecialKeyEnum.Search, OnSearchClick);
  150. specialCallbackDic.Add(SpecialKeyEnum.PageDown, OnPageDownClick);
  151. specialCallbackDic.Add(SpecialKeyEnum.PageUp, OnPageUpClick);
  152. specialCallbackDic.Add(SpecialKeyEnum.PromptPageDown, OnPromptPageDownClick);
  153. specialCallbackDic.Add(SpecialKeyEnum.PromptPageUp, OnPromptPageUpClick);
  154. specialCallbackDic.Add(SpecialKeyEnum.Background, OnBackgroundClick);
  155. specialCallbackDic.Add(SpecialKeyEnum.Cn, OnCnClick);
  156. specialCallbackDic.Add(SpecialKeyEnum.En, OnEnClick);
  157. }
  158. protected virtual void InitUGUICallbackDic()
  159. {
  160. if (uGUICallbackDic == null)
  161. {
  162. uGUICallbackDic = new Dictionary<UGUIKeyEnum, Action>();
  163. }
  164. uGUICallbackDic.Clear();
  165. uGUICallbackDic.Add(UGUIKeyEnum.Back, OnBackClick);
  166. uGUICallbackDic.Add(UGUIKeyEnum.OpenMore, OnOpenMoreClick);
  167. }
  168. protected virtual void InitLanguageCallbackDic()
  169. {
  170. if (languageCallBackDic == null)
  171. {
  172. languageCallBackDic = new Dictionary<LanguageKeyEnum, Action>();
  173. }
  174. languageCallBackDic.Clear();
  175. languageCallBackDic.Add(LanguageKeyEnum.Chinese, OnChineseClick);
  176. languageCallBackDic.Add(LanguageKeyEnum.English, OnEnglishClick);
  177. languageCallBackDic.Add(LanguageKeyEnum.More, OnMoreClick);
  178. }
  179. #endregion
  180. #region Key Clicks
  181. public virtual void OnNormalKeyClick(string value)
  182. {
  183. OnKeyClick?.Invoke(value);
  184. OnWebKeyClick?.Invoke(value);
  185. }
  186. public virtual void OnNumberKeyClick(string value)
  187. {
  188. OnNormalKeyClick(value);
  189. }
  190. public virtual void OnPinyinKeyClick(string value)
  191. {
  192. }
  193. public virtual void OnSpecialKeyClick(SpecialKeyEnum specialKeyEnum)
  194. {
  195. if (specialCallbackDic == null)
  196. InitSpecialCallbackDic();
  197. if (specialCallbackDic.ContainsKey(specialKeyEnum))
  198. specialCallbackDic[specialKeyEnum]?.Invoke();
  199. }
  200. public virtual void OnLanguageKeyClick(LanguageKeyEnum languageKeyEnum)
  201. {
  202. if (languageCallBackDic == null)
  203. InitLanguageCallbackDic();
  204. if (languageCallBackDic.ContainsKey(languageKeyEnum))
  205. {
  206. languageCallBackDic[languageKeyEnum]?.Invoke();
  207. }
  208. }
  209. public virtual void OnPromptKeyClick(string value)
  210. {
  211. OnKeyClick?.Invoke(value);
  212. for (int i = 0; i < value.Length; i++)
  213. {
  214. OnWebKeyClick?.Invoke(value[i].ToString());
  215. }
  216. }
  217. public virtual void OnUGUIKeyClick(UGUIKeyEnum uguiKeyEnum)
  218. {
  219. if (uGUICallbackDic == null)
  220. InitUGUICallbackDic();
  221. if (uGUICallbackDic.ContainsKey(uguiKeyEnum))
  222. {
  223. uGUICallbackDic[uguiKeyEnum]?.Invoke();
  224. }
  225. }
  226. #endregion
  227. #region Special Key Click Events
  228. public virtual void OnEnterClick() {
  229. OnWebKeyClick?.Invoke("Enter");
  230. switch (presentKeyboardState)
  231. {
  232. case KeyboardState.Cn:
  233. switch (presentKeyboardType)
  234. {
  235. case SCKeyboardType.URL:
  236. case SCKeyboardType.Search:
  237. SCKeyboard._status = SCKeyboard.Status.Done;
  238. break;
  239. default:
  240. break;
  241. }
  242. break;
  243. default:
  244. SCKeyboard._status = SCKeyboard.Status.Done;
  245. break;
  246. }
  247. HideKeyboardPrompt();
  248. }
  249. public virtual void OnDeleteClick() {
  250. OnKeyClick?.Invoke("Backspace");
  251. OnWebKeyClick?.Invoke("Backspace");
  252. }
  253. public virtual void OnShiftClick()
  254. {
  255. //OnKeyClick?.Invoke("Tab");
  256. //OnWebKeyClick?.Invoke("Tab");
  257. OnKeyboardShifted?.Invoke(!m_IsShifted);
  258. m_IsShifted = !m_IsShifted;
  259. if (m_IsShifted)
  260. {
  261. HideKeyboardPrompt();
  262. }
  263. }
  264. public virtual void OnSpaceClick()
  265. {
  266. OnKeyClick?.Invoke(" ");
  267. OnWebKeyClick?.Invoke(" ");
  268. }
  269. public virtual void OnCapsLockClick() { }
  270. public virtual void OnTabClick() { }
  271. public virtual void OnCtrlClick() { }
  272. public virtual void OnAltClick() { }
  273. public virtual void OnDoneClick()
  274. {
  275. OnStatusChange?.Invoke(SCKeyboard.Status.Done);
  276. HideKeyboardPrompt();
  277. }
  278. public virtual void OnClearClick() {
  279. OnKeyClick?.Invoke("Backspace");
  280. OnWebKeyClick?.Invoke("Backspace");
  281. }
  282. public virtual void OnSymbolClick() { ActiveKeyboard(KeyboardEnum.Symbol); }
  283. public virtual void OnNumberClick() { ActiveKeyboard(KeyboardEnum.Number); }
  284. public virtual void OnMemeClick() { /*ActiveKeyboard(KeyboardEnum.Meme);*/ }
  285. public virtual void OnABCClick() { /*ActiveKeyboard(KeyboardEnum.Meme);*/ }
  286. public virtual void OnParticipleClick() {
  287. OnKeyClick?.Invoke("/");
  288. OnWebKeyClick?.Invoke("/");
  289. }
  290. public virtual void OnComClick()
  291. {
  292. OnKeyClick?.Invoke(".com");
  293. OnWebKeyClick?.Invoke(".com");
  294. }
  295. public virtual void OnMicrophoneClick() { }
  296. public virtual void OnLanguageClick() { ActiveKeyboard(KeyboardEnum.Language); }
  297. public virtual void OnSearchClick() { }
  298. public virtual void OnPageDownClick() { }
  299. public virtual void OnPageUpClick() { }
  300. public virtual void OnPromptPageDownClick() { }
  301. public virtual void OnPromptPageUpClick() { }
  302. public virtual void OnBackgroundClick() { }
  303. public virtual void OnCnClick() { ActiveKeyboard(KeyboardEnum.ABC, KeyboardState.En); }
  304. public virtual void OnEnClick() { ActiveKeyboard(KeyboardEnum.ABC, KeyboardState.Cn); }
  305. #endregion
  306. #region UGUI Key Click Events
  307. protected virtual void OnBackClick()
  308. {
  309. previousKeyboard.SetActive(true);
  310. presentKeyboard.SetActive(false);
  311. presentKeyboard = previousKeyboard;
  312. }
  313. protected virtual void OnOpenMoreClick()
  314. {
  315. }
  316. #endregion
  317. #region Language Key Click Events
  318. public virtual void OnChineseClick()
  319. {
  320. ActiveKeyboard(KeyboardEnum.ABC,KeyboardState.Cn);
  321. }
  322. public virtual void OnEnglishClick()
  323. {
  324. ActiveKeyboard(KeyboardEnum.ABC,KeyboardState.En);
  325. }
  326. public virtual void OnMoreClick()
  327. {
  328. }
  329. #endregion
  330. #region Key Handlers
  331. protected virtual string keyPath { get { return "Prefabs/Key/2D/PromptKey"; } }
  332. protected virtual string rowPath { get { return "Prefabs/Key/2D/PromptRow"; } }
  333. public virtual void PinyinKeyHandler(string preInput)
  334. {
  335. }
  336. #endregion
  337. #region Active Keyboards
  338. protected virtual void ActiveKeyboard(KeyboardEnum keyboardEnum, KeyboardState keyboardState = KeyboardState.En)
  339. {
  340. if (m_IsShifted)
  341. {
  342. OnShiftClick();
  343. }
  344. }
  345. protected virtual void InitKeyboard(KeyboardEnum keyboardEnum)
  346. {
  347. }
  348. protected virtual void HideKeyboardPrompt(bool inputPreString = true)
  349. {
  350. }
  351. #endregion
  352. #region Methods
  353. public virtual void SetTextString()
  354. {
  355. }
  356. public virtual void SetKeyboardType(SCKeyboardType scKeyboardType)
  357. {
  358. presentKeyboardType = scKeyboardType;
  359. }
  360. #endregion
  361. }
  362. }