123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.EventSystems;
- namespace SC.XR.Unity.Module_Keyboard
- {
- public abstract class SCKeyboardMono : MonoBehaviour
- {
- #region Status
- public SCKeyboardType presentKeyboardType = SCKeyboardType.Default;
- public KeyboardState presentKeyboardState = KeyboardState.Cn;
- public Action<bool> OnKeyboardShifted { get; internal set; }
- public Action<KeyboardState> OnKeyboardStateChange;
- protected bool m_IsShifted = false;
- public bool IsShifted
- {
- get
- {
- return m_IsShifted;
- }
- }
- #endregion
- #region Actions
- public Action<string> OnTextChange;
- public Action<string> OnKeyClick;
- public Action<string> OnWebKeyClick;
- public Action OnDoneKeyClick;
- public Action OnDeleteKeyClick;
- public Action OnEnterKeyClick;
- public Action<SCKeyboard.Status> OnStatusChange;
- public Action<PointerEventData> OnPointerDown;
- #endregion
- #region String Builders
- private StringBuilder _stringBuilder;
- public virtual StringBuilder stringBuilder
- {
- get
- {
- if (_stringBuilder == null) _stringBuilder = new StringBuilder();
- return _stringBuilder;
- }
- }
- private StringBuilder _preInputStringBuilder;
- public virtual StringBuilder preInputStringBuilder
- {
- get
- {
- if (_preInputStringBuilder == null) _preInputStringBuilder = new StringBuilder();
- return _preInputStringBuilder;
- }
- }
- #endregion
- #region Life Cycles
- protected virtual void Awake()
- {
- //Initialize();
- }
- protected virtual void OnEnable()
- {
- Initialize(presentKeyboardType);
- }
- protected virtual void Start()
- {
- }
- protected virtual void LateUpdate()
- {
- if (SCKeyboard._status != SCKeyboard.Status.LostFocus &&
- EventSystem.current && EventSystem.current.currentSelectedGameObject != this.gameObject)
- {
- OnStatusChange?.Invoke(SCKeyboard.Status.LostFocus);
- HideKeyboardPrompt(false);
- }
- }
- #endregion
- #region Initialize
- public virtual void Initialize(SCKeyboardType keyboardType = SCKeyboardType.Default)
- {
- switch (keyboardType)
- {
- case SCKeyboardType.Default:
- DefaultKeyboardInitialize();
- break;
- case SCKeyboardType.ASCIICapable:
- ASCIICapableKeyboardInitialize();
- break;
- case SCKeyboardType.URL:
- URLKeyboardInitialize();
- break;
- case SCKeyboardType.Search:
- SearchKeyboardInitialize();
- break;
- }
- }
- protected virtual void DefaultKeyboardInitialize()
- {
-
- }
- protected virtual void ASCIICapableKeyboardInitialize()
- {
- }
- protected virtual void URLKeyboardInitialize()
- {
- }
- protected virtual void SearchKeyboardInitialize()
- {
- }
- #endregion
- #region Keyboards
- protected GameObject previousKeyboard;
- protected GameObject presentKeyboard;
- public SCKeyboardBasePrompt keyboard_prompt;
- public GameObject keyboard_abc;
- #endregion
- #region Keyboard Key Dics
- public Dictionary<SpecialKeyEnum, List<SCKeyboardBaseKey>> specialKeysDic = new Dictionary<SpecialKeyEnum, List<SCKeyboardBaseKey>>();
- public Dictionary<string, List<SCKeyboardBaseKey>> symbolKeysDic = new Dictionary<string, List<SCKeyboardBaseKey>>();
- protected Dictionary<int, RectTransform> promptRowRectTransformsDic = new Dictionary<int, RectTransform>();
- public Dictionary<int, Dictionary<int, Dictionary<int, SCKeyboardBaseKey>>> promptPagesDic = new Dictionary<int, Dictionary<int, Dictionary<int, SCKeyboardBaseKey>>>();
- public Dictionary<int, Dictionary<int, Dictionary<int, string>>> chinesePagesDic = new Dictionary<int, Dictionary<int, Dictionary<int, string>>>();
- public Dictionary<int, Dictionary<int, Dictionary<int, string>>> urlPagesDic = new Dictionary<int, Dictionary<int, Dictionary<int, string>>>();
- protected Dictionary<SpecialKeyEnum, Action> specialCallbackDic;
- protected Dictionary<UGUIKeyEnum, Action> uGUICallbackDic;
- protected Dictionary<LanguageKeyEnum, Action> languageCallBackDic;
- protected virtual void InitSpecialCallbackDic()
- {
- if (specialCallbackDic == null)
- {
- specialCallbackDic = new Dictionary<SpecialKeyEnum, Action>();
- }
- specialCallbackDic.Clear();
- specialCallbackDic.Add(SpecialKeyEnum.Enter, OnEnterClick);
- specialCallbackDic.Add(SpecialKeyEnum.Delete, OnDeleteClick);
- specialCallbackDic.Add(SpecialKeyEnum.Shift, OnShiftClick);
- specialCallbackDic.Add(SpecialKeyEnum.Space, OnSpaceClick);
- specialCallbackDic.Add(SpecialKeyEnum.CapsLock, OnCapsLockClick);
- specialCallbackDic.Add(SpecialKeyEnum.Tab, OnTabClick);
- specialCallbackDic.Add(SpecialKeyEnum.Ctrl, OnCtrlClick);
- specialCallbackDic.Add(SpecialKeyEnum.Alt, OnAltClick);
- specialCallbackDic.Add(SpecialKeyEnum.Done, OnDoneClick);
- specialCallbackDic.Add(SpecialKeyEnum.Clear, OnClearClick);
- specialCallbackDic.Add(SpecialKeyEnum.Symbol, OnSymbolClick);
- specialCallbackDic.Add(SpecialKeyEnum.Number, OnNumberClick);
- specialCallbackDic.Add(SpecialKeyEnum.Meme, OnMemeClick);
- specialCallbackDic.Add(SpecialKeyEnum.ABC, OnABCClick);
- specialCallbackDic.Add(SpecialKeyEnum.Participle, OnParticipleClick);
- specialCallbackDic.Add(SpecialKeyEnum.Com, OnComClick);
- specialCallbackDic.Add(SpecialKeyEnum.Microphone, OnMicrophoneClick);
- specialCallbackDic.Add(SpecialKeyEnum.Language, OnLanguageClick);
- specialCallbackDic.Add(SpecialKeyEnum.Search, OnSearchClick);
- specialCallbackDic.Add(SpecialKeyEnum.PageDown, OnPageDownClick);
- specialCallbackDic.Add(SpecialKeyEnum.PageUp, OnPageUpClick);
- specialCallbackDic.Add(SpecialKeyEnum.PromptPageDown, OnPromptPageDownClick);
- specialCallbackDic.Add(SpecialKeyEnum.PromptPageUp, OnPromptPageUpClick);
- specialCallbackDic.Add(SpecialKeyEnum.Background, OnBackgroundClick);
- specialCallbackDic.Add(SpecialKeyEnum.Cn, OnCnClick);
- specialCallbackDic.Add(SpecialKeyEnum.En, OnEnClick);
- }
- protected virtual void InitUGUICallbackDic()
- {
- if (uGUICallbackDic == null)
- {
- uGUICallbackDic = new Dictionary<UGUIKeyEnum, Action>();
- }
- uGUICallbackDic.Clear();
- uGUICallbackDic.Add(UGUIKeyEnum.Back, OnBackClick);
- uGUICallbackDic.Add(UGUIKeyEnum.OpenMore, OnOpenMoreClick);
- }
- protected virtual void InitLanguageCallbackDic()
- {
- if (languageCallBackDic == null)
- {
- languageCallBackDic = new Dictionary<LanguageKeyEnum, Action>();
- }
- languageCallBackDic.Clear();
- languageCallBackDic.Add(LanguageKeyEnum.Chinese, OnChineseClick);
- languageCallBackDic.Add(LanguageKeyEnum.English, OnEnglishClick);
- languageCallBackDic.Add(LanguageKeyEnum.More, OnMoreClick);
- }
- #endregion
- #region Key Clicks
- public virtual void OnNormalKeyClick(string value)
- {
- OnKeyClick?.Invoke(value);
- OnWebKeyClick?.Invoke(value);
- }
- public virtual void OnNumberKeyClick(string value)
- {
- OnNormalKeyClick(value);
- }
- public virtual void OnPinyinKeyClick(string value)
- {
- }
- public virtual void OnSpecialKeyClick(SpecialKeyEnum specialKeyEnum)
- {
- if (specialCallbackDic == null)
- InitSpecialCallbackDic();
- if (specialCallbackDic.ContainsKey(specialKeyEnum))
- specialCallbackDic[specialKeyEnum]?.Invoke();
- }
- public virtual void OnLanguageKeyClick(LanguageKeyEnum languageKeyEnum)
- {
- if (languageCallBackDic == null)
- InitLanguageCallbackDic();
- if (languageCallBackDic.ContainsKey(languageKeyEnum))
- {
- languageCallBackDic[languageKeyEnum]?.Invoke();
- }
- }
- public virtual void OnPromptKeyClick(string value)
- {
- OnKeyClick?.Invoke(value);
- for (int i = 0; i < value.Length; i++)
- {
- OnWebKeyClick?.Invoke(value[i].ToString());
- }
- }
-
- public virtual void OnUGUIKeyClick(UGUIKeyEnum uguiKeyEnum)
- {
- if (uGUICallbackDic == null)
- InitUGUICallbackDic();
- if (uGUICallbackDic.ContainsKey(uguiKeyEnum))
- {
- uGUICallbackDic[uguiKeyEnum]?.Invoke();
- }
- }
- #endregion
- #region Special Key Click Events
- public virtual void OnEnterClick() {
- OnWebKeyClick?.Invoke("Enter");
- switch (presentKeyboardState)
- {
- case KeyboardState.Cn:
- switch (presentKeyboardType)
- {
- case SCKeyboardType.URL:
- case SCKeyboardType.Search:
- SCKeyboard._status = SCKeyboard.Status.Done;
- break;
- default:
- break;
- }
- break;
- default:
- SCKeyboard._status = SCKeyboard.Status.Done;
- break;
- }
- HideKeyboardPrompt();
- }
- public virtual void OnDeleteClick() {
- OnKeyClick?.Invoke("Backspace");
- OnWebKeyClick?.Invoke("Backspace");
- }
- public virtual void OnShiftClick()
- {
- //OnKeyClick?.Invoke("Tab");
- //OnWebKeyClick?.Invoke("Tab");
- OnKeyboardShifted?.Invoke(!m_IsShifted);
- m_IsShifted = !m_IsShifted;
- if (m_IsShifted)
- {
- HideKeyboardPrompt();
- }
- }
- public virtual void OnSpaceClick()
- {
- OnKeyClick?.Invoke(" ");
- OnWebKeyClick?.Invoke(" ");
- }
- public virtual void OnCapsLockClick() { }
- public virtual void OnTabClick() { }
- public virtual void OnCtrlClick() { }
- public virtual void OnAltClick() { }
- public virtual void OnDoneClick()
- {
- OnStatusChange?.Invoke(SCKeyboard.Status.Done);
- HideKeyboardPrompt();
- }
- public virtual void OnClearClick() {
- OnKeyClick?.Invoke("Backspace");
- OnWebKeyClick?.Invoke("Backspace");
- }
- public virtual void OnSymbolClick() { ActiveKeyboard(KeyboardEnum.Symbol); }
- public virtual void OnNumberClick() { ActiveKeyboard(KeyboardEnum.Number); }
- public virtual void OnMemeClick() { /*ActiveKeyboard(KeyboardEnum.Meme);*/ }
- public virtual void OnABCClick() { /*ActiveKeyboard(KeyboardEnum.Meme);*/ }
- public virtual void OnParticipleClick() {
- OnKeyClick?.Invoke("/");
- OnWebKeyClick?.Invoke("/");
- }
- public virtual void OnComClick()
- {
- OnKeyClick?.Invoke(".com");
- OnWebKeyClick?.Invoke(".com");
- }
- public virtual void OnMicrophoneClick() { }
- public virtual void OnLanguageClick() { ActiveKeyboard(KeyboardEnum.Language); }
- public virtual void OnSearchClick() { }
- public virtual void OnPageDownClick() { }
- public virtual void OnPageUpClick() { }
- public virtual void OnPromptPageDownClick() { }
- public virtual void OnPromptPageUpClick() { }
- public virtual void OnBackgroundClick() { }
- public virtual void OnCnClick() { ActiveKeyboard(KeyboardEnum.ABC, KeyboardState.En); }
- public virtual void OnEnClick() { ActiveKeyboard(KeyboardEnum.ABC, KeyboardState.Cn); }
- #endregion
- #region UGUI Key Click Events
- protected virtual void OnBackClick()
- {
- previousKeyboard.SetActive(true);
- presentKeyboard.SetActive(false);
- presentKeyboard = previousKeyboard;
- }
- protected virtual void OnOpenMoreClick()
- {
- }
- #endregion
- #region Language Key Click Events
- public virtual void OnChineseClick()
- {
- ActiveKeyboard(KeyboardEnum.ABC,KeyboardState.Cn);
- }
- public virtual void OnEnglishClick()
- {
- ActiveKeyboard(KeyboardEnum.ABC,KeyboardState.En);
- }
- public virtual void OnMoreClick()
- {
- }
- #endregion
- #region Key Handlers
- protected virtual string keyPath { get { return "Prefabs/Key/2D/PromptKey"; } }
- protected virtual string rowPath { get { return "Prefabs/Key/2D/PromptRow"; } }
- public virtual void PinyinKeyHandler(string preInput)
- {
-
- }
- #endregion
- #region Active Keyboards
- protected virtual void ActiveKeyboard(KeyboardEnum keyboardEnum, KeyboardState keyboardState = KeyboardState.En)
- {
- if (m_IsShifted)
- {
- OnShiftClick();
- }
- }
- protected virtual void InitKeyboard(KeyboardEnum keyboardEnum)
- {
-
- }
- protected virtual void HideKeyboardPrompt(bool inputPreString = true)
- {
-
- }
- #endregion
- #region Methods
- public virtual void SetTextString()
- {
- }
- public virtual void SetKeyboardType(SCKeyboardType scKeyboardType)
- {
- presentKeyboardType = scKeyboardType;
- }
- #endregion
- }
- }
|