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 OnKeyboardShifted { get; internal set; } public Action OnKeyboardStateChange; protected bool m_IsShifted = false; public bool IsShifted { get { return m_IsShifted; } } #endregion #region Actions public Action OnTextChange; public Action OnKeyClick; public Action OnWebKeyClick; public Action OnDoneKeyClick; public Action OnDeleteKeyClick; public Action OnEnterKeyClick; public Action OnStatusChange; public Action 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> specialKeysDic = new Dictionary>(); public Dictionary> symbolKeysDic = new Dictionary>(); protected Dictionary promptRowRectTransformsDic = new Dictionary(); public Dictionary>> promptPagesDic = new Dictionary>>(); public Dictionary>> chinesePagesDic = new Dictionary>>(); public Dictionary>> urlPagesDic = new Dictionary>>(); protected Dictionary specialCallbackDic; protected Dictionary uGUICallbackDic; protected Dictionary languageCallBackDic; protected virtual void InitSpecialCallbackDic() { if (specialCallbackDic == null) { specialCallbackDic = new Dictionary(); } 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(); } uGUICallbackDic.Clear(); uGUICallbackDic.Add(UGUIKeyEnum.Back, OnBackClick); uGUICallbackDic.Add(UGUIKeyEnum.OpenMore, OnOpenMoreClick); } protected virtual void InitLanguageCallbackDic() { if (languageCallBackDic == null) { languageCallBackDic = new Dictionary(); } 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 } }