using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; namespace SC.XR.Unity.Module_Keyboard { // // Summary: // 3DKeyboard in the scene.Support for Android,UnityEditor public abstract class SCKeyboard { public SCKeyboard(Vector3 position, Quaternion rotation, Vector3 localScale, SCKeyboardEnum scKeyboardEnum, SCKeyboardType scKeyboardType) { SCKeyboardMono keyboardPrefab = Resources.Load(PrefabResourceName); if (keyboardPrefab == null) { Debug.LogError("This prefab resource do not exist"); return; } keyboardPrefab.presentKeyboardType = scKeyboardType; SCKeyboardMono keyboardMono = SCKeyboardMono.Instantiate(keyboardPrefab); keyboardMonoDic[scKeyboardEnum] = keyboardMono; SetKeyboardTransform(scKeyboardEnum, position, rotation, localScale); Init(); ResetKeyboard(scKeyboardType); } #region protected properties protected static Rect _area; protected static bool _visible = false; protected static bool _isInPlaceEditingAllowed = true; protected string _text = string.Empty; public static Status _status; #endregion #region public properties public static Rect area { get { return _area; } } public static bool visible { get { return _visible; } } public static bool isInPlaceEditingAllowed { get { return _isInPlaceEditingAllowed; } } public static bool isSupported { get { RuntimePlatform platform = Application.platform; switch (platform) { case RuntimePlatform.WindowsEditor: case RuntimePlatform.Android: return true; default: return false; } } } public static bool hideInput { get; set; } public int targetDisplay { get; set; } public TouchScreenKeyboardType type { get; } public RangeInt selection { get; set; } public virtual string text { get; set; } public bool canSetSelection { get; } public bool active { get { if (keyboardMonoDic.ContainsKey(keyboardEnum) && keyboardMonoDic[keyboardEnum] != null) return keyboardMonoDic[keyboardEnum].isActiveAndEnabled; else return false; } set { if (keyboardMonoDic.ContainsKey(keyboardEnum) && keyboardMonoDic[keyboardEnum] != null) keyboardMonoDic[keyboardEnum].gameObject.SetActive(value); OnActiveStateChanged?.Invoke(value);_visible = value; //if (value == false) { ClearOnKeyClickEvent(); ClearAllSpecialKeyClickEvent(); } } } public bool canGetSelection { get; set; } public Status status { get { return _status; } } public int characterLimit { get; set; } public static Action OnActiveStateChanged; private static KeyboardSystemLanguage m_CurrentSystemLanguage = KeyboardSystemLanguage.Chinese; public static KeyboardSystemLanguage currentSystemLanguage { get { if (useSystemLanguage) { switch (API_Module_DetectorSystem_Language.GetSystemLanguage()) { case SCLanguageEnum.ChineseSimplified: case SCLanguageEnum.ChineseTraditional: m_CurrentSystemLanguage = KeyboardSystemLanguage.Chinese; break; case SCLanguageEnum.English: m_CurrentSystemLanguage = KeyboardSystemLanguage.English; break; } } return m_CurrentSystemLanguage; } set { m_CurrentSystemLanguage = value; } } public static bool useSystemLanguage = true; #endregion #region global actions private static Action OnWebKeyClick; #endregion public virtual Transform transform { get { return keyboardMonoDic[keyboardEnum].transform; } } public virtual GameObject gameObject { get { return keyboardMonoDic[keyboardEnum].gameObject; } } protected virtual void Init() { SCKeyboardMono keyboardMono = keyboardMonoDic[keyboardEnum]; if (keyboardMono == null) { Debug.LogError("This keyboard prefab do not have scripts inhert from SCKeyboardMono!"); return; } //keyboardMono.Initialize(); //Local Actions keyboardMono.OnDoneKeyClick += OnDoneKeyClickEvent; keyboardMono.OnTextChange += OnTextChangeEvent; keyboardMono.OnStatusChange += OnStatusChangeEvent; //Global Actions keyboardMono.OnWebKeyClick += OnWebKeyClickEvent; } protected abstract string PrefabResourceName { get; } protected virtual SCKeyboardEnum keyboardEnum { get; } protected static Dictionary keyboardMonoDic = new Dictionary(); protected static Dictionary keyboardTypeDic = new Dictionary() { { SCKeyboardEnum.SCKeyboard2D, typeof(SCKeyboard2D) }, { SCKeyboardEnum.SCKeyboard3D, typeof(SCKeyboard3D) } }; protected static Dictionary keyboardCacheDic = new Dictionary(); /// /// Open the keyboard and put the keyboard in the world space.Set the keyboard's position, rotate and scale int the world space. /// /// /// /// /// /// /// /// public static SCKeyboard Open(SCKeyboardEnum scKeyboardEnum, string text, SCKeyboardType scKeyboardType, Vector3 position, Quaternion rotation, Vector3 localScale) { // TODO 设置系统语言 // SetSystemLanguage(); if (keyboardMonoDic != null && keyboardMonoDic.ContainsKey(scKeyboardEnum) && keyboardMonoDic[scKeyboardEnum] != null) { if (keyboardCacheDic != null && keyboardCacheDic.ContainsKey(scKeyboardEnum)) { SCKeyboard keyboardCache = keyboardCacheDic[scKeyboardEnum]; keyboardCache.Clear(); keyboardCache.ResetKeyboard(scKeyboardType); keyboardCache.SetKeyboardTransform(scKeyboardEnum, position, rotation, localScale); return keyboardCache; } } if (!keyboardTypeDic.ContainsKey(scKeyboardEnum)) { DebugMy.LogError("This SCKeyboardEnum do not exist", scKeyboardEnum); return null; } SCKeyboard keyboard = Activator.CreateInstance(keyboardTypeDic[scKeyboardEnum], position, rotation, localScale, scKeyboardEnum, scKeyboardType) as SCKeyboard; keyboardCacheDic[scKeyboardEnum] = keyboard; return keyboard; } /// /// Open the keyboard. /// /// /// /// public static SCKeyboard Open(SCKeyboardEnum scKeyboardEnum, string text) { // TODO 设置系统语言 // SetSystemLanguage(); if (keyboardMonoDic != null && keyboardMonoDic.ContainsKey(scKeyboardEnum) && keyboardMonoDic[scKeyboardEnum] != null) { if (keyboardCacheDic != null && keyboardCacheDic.ContainsKey(scKeyboardEnum)) { SCKeyboard keyboardCache = keyboardCacheDic[scKeyboardEnum]; keyboardCache.Clear(); keyboardCache.ResetKeyboard(); return keyboardCache; } } if (!keyboardTypeDic.ContainsKey(scKeyboardEnum)) { DebugMy.LogError("This SCKeyboardEnum do not exist", scKeyboardEnum); return null; } SCKeyboard keyboard = Activator.CreateInstance(keyboardTypeDic[scKeyboardEnum], Vector3.zero, Quaternion.identity, Vector3.one, scKeyboardEnum, SCKeyboardType.Default) as SCKeyboard; keyboardCacheDic[scKeyboardEnum] = keyboard; return keyboard; } /// /// Close the keyboard. /// /// /// public static SCKeyboard Close(SCKeyboardEnum scKeyboardEnum) { if (keyboardCacheDic != null && keyboardCacheDic.ContainsKey(scKeyboardEnum)) { SCKeyboard keyboardCache = keyboardCacheDic[scKeyboardEnum]; keyboardCache.CloseKeyboard(); return keyboardCache; } if (!keyboardTypeDic.ContainsKey(scKeyboardEnum)) { DebugMy.LogError("This SCKeyboardEnum do not exist", scKeyboardEnum); return null; } return null; } /// /// Set Keyboard Default Language. /// public static void SetSystemLanguage(KeyboardSystemLanguage language) { currentSystemLanguage = language; } #region Register public void Clear() { ClearAllSpecialKeyClickEvent(); ClearOnKeyClickEvent(); } /// /// Now support Enter,DeletemDone /// /// public void RegisterOnSpecialKeyClickEvent(SpecialKeyEnum specialKeyEnum, Action action) { switch (specialKeyEnum) { case SpecialKeyEnum.Enter: keyboardMonoDic[keyboardEnum].OnEnterKeyClick += action; break; case SpecialKeyEnum.Delete: keyboardMonoDic[keyboardEnum].OnDeleteKeyClick += action; break; case SpecialKeyEnum.Done: keyboardMonoDic[keyboardEnum].OnDoneKeyClick += action; break; } } /// /// Now support Enter,DeletemDone /// /// public void UnRegisterOnSpecialKeyClickEvent(SpecialKeyEnum specialKeyEnum, Action action) { switch (specialKeyEnum) { case SpecialKeyEnum.Enter: keyboardMonoDic[keyboardEnum].OnEnterKeyClick -= action; break; case SpecialKeyEnum.Delete: keyboardMonoDic[keyboardEnum].OnDeleteKeyClick -= action; break; case SpecialKeyEnum.Done: keyboardMonoDic[keyboardEnum].OnDoneKeyClick -= action; break; } } /// /// Now support Enter,DeletemDone /// /// public void ClearOnSpecialKeyClickEvent(SpecialKeyEnum specialKeyEnum) { switch (specialKeyEnum) { case SpecialKeyEnum.Enter: keyboardMonoDic[keyboardEnum].OnEnterKeyClick = null; break; case SpecialKeyEnum.Delete: keyboardMonoDic[keyboardEnum].OnDeleteKeyClick = null; break; case SpecialKeyEnum.Done: keyboardMonoDic[keyboardEnum].OnDoneKeyClick = null; break; } } /// /// Now support Enter,DeletemDone /// public void ClearAllSpecialKeyClickEvent() { keyboardMonoDic[keyboardEnum].OnEnterKeyClick = null; keyboardMonoDic[keyboardEnum].OnDeleteKeyClick = null; keyboardMonoDic[keyboardEnum].OnDoneKeyClick = null; } public void RegisterOnKeyClickEvent(Action action) { keyboardMonoDic[keyboardEnum].OnKeyClick += action; } public void UnRegisterOnKeyClickEvent(Action action) { keyboardMonoDic[keyboardEnum].OnKeyClick -= action; } public void ClearOnKeyClickEvent() { keyboardMonoDic[keyboardEnum].OnKeyClick = null; } public static void RegisterOnWebKeyClickEvent(Action action) { OnWebKeyClick += action; } public static void UnRegisterOnWebKeyClickEvent(Action action) { OnWebKeyClick -= action; } public static void ClearOnWebKeyClickEvent() { OnWebKeyClick = null; } public void RegisterOnPointerDownEvent(Action action) { keyboardMonoDic[keyboardEnum].OnPointerDown += action; } public void UnRegisterOnPointerDownEvent(Action action) { keyboardMonoDic[keyboardEnum].OnPointerDown -= action; } public void ClearOnPointerDownEvent() { keyboardMonoDic[keyboardEnum].OnPointerDown = null; } #endregion #region protected Events protected void OnTextChangeEvent(string text) { this.text = text; } protected virtual void OnPointerDownEvent(PointerEventData action) { EventSystem.current.SetSelectedGameObject(keyboardMonoDic[keyboardEnum].gameObject); Debug.Log("EventSystem.current.SetSelectedGameObject : " + EventSystem.current.currentSelectedGameObject.name); _status = Status.Visible; } protected virtual void OnDoneKeyClickEvent() { _status = Status.Done; } protected virtual void ResetKeyboard() { _status = Status.LostFocus; this.active = true; ClearOnPointerDownEvent(); RegisterOnPointerDownEvent(OnPointerDownEvent); } protected virtual void ResetKeyboard(SCKeyboardType scKeyboardType) { _status = Status.LostFocus; ResetKeyboardType(scKeyboardType); this.active = true; ClearOnPointerDownEvent(); RegisterOnPointerDownEvent(OnPointerDownEvent); } protected virtual void ResetKeyboardType(SCKeyboardType scKeyboardType) { keyboardMonoDic[keyboardEnum].SetKeyboardType(scKeyboardType); } protected virtual void CloseKeyboard() { active = false; } protected virtual void SetKeyboardTransform(SCKeyboardEnum sckeyboardEnum, Vector3 positon, Quaternion rotation, Vector3 localScale) { if (!keyboardMonoDic.ContainsKey(sckeyboardEnum)) { Debug.LogError("KeyboardMono is NULL"); return; } SCKeyboardMono keyboardMono = keyboardMonoDic[sckeyboardEnum]; keyboardMono.transform.SetParent(null); keyboardMono.transform.localPosition = positon; keyboardMono.transform.localRotation = rotation; keyboardMono.transform.localScale = localScale; } #endregion #region public Events public void OnStatusChangeEvent(Status status) { _status = status; } public void OnWebKeyClickEvent(string val) { OnWebKeyClick?.Invoke(val); } #endregion public enum Status { Visible = 0, Done = 1, Canceled = 2, LostFocus = 3 } } }