123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- 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<SCKeyboardMono>(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<bool> 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<string> 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<SCKeyboardEnum, SCKeyboardMono> keyboardMonoDic = new Dictionary<SCKeyboardEnum, SCKeyboardMono>();
- protected static Dictionary<SCKeyboardEnum, Type> keyboardTypeDic = new Dictionary<SCKeyboardEnum, Type>()
- {
- { SCKeyboardEnum.SCKeyboard2D, typeof(SCKeyboard2D) },
- { SCKeyboardEnum.SCKeyboard3D, typeof(SCKeyboard3D) }
- };
- protected static Dictionary<SCKeyboardEnum, SCKeyboard> keyboardCacheDic = new Dictionary<SCKeyboardEnum, SCKeyboard>();
- /// <summary>
- /// Open the keyboard and put the keyboard in the world space.Set the keyboard's position, rotate and scale int the world space.
- /// </summary>
- /// <param name="scKeyboardEnum"></param>
- /// <param name="text"></param>
- /// <param name="touchScreenKeyboardType"></param>
- /// <param name="position"></param>
- /// <param name="rotation"></param>
- /// <param name="localScale"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Open the keyboard.
- /// </summary>
- /// <param name="scKeyboardEnum"></param>
- /// <param name="text"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Close the keyboard.
- /// </summary>
- /// <param name="scKeyboardEnum"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Set Keyboard Default Language.
- /// </summary>
- public static void SetSystemLanguage(KeyboardSystemLanguage language)
- {
- currentSystemLanguage = language;
- }
- #region Register
- public void Clear()
- {
- ClearAllSpecialKeyClickEvent();
- ClearOnKeyClickEvent();
- }
- /// <summary>
- /// Now support Enter,DeletemDone
- /// </summary>
- /// <param name="specialKeyEnum"></param>
- 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;
-
- }
- }
- /// <summary>
- /// Now support Enter,DeletemDone
- /// </summary>
- /// <param name="specialKeyEnum"></param>
- 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;
- }
- }
- /// <summary>
- /// Now support Enter,DeletemDone
- /// </summary>
- /// <param name="specialKeyEnum"></param>
- 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;
- }
- }
- /// <summary>
- /// Now support Enter,DeletemDone
- /// </summary>
- public void ClearAllSpecialKeyClickEvent()
- {
- keyboardMonoDic[keyboardEnum].OnEnterKeyClick = null;
- keyboardMonoDic[keyboardEnum].OnDeleteKeyClick = null;
- keyboardMonoDic[keyboardEnum].OnDoneKeyClick = null;
- }
- public void RegisterOnKeyClickEvent(Action<string> action)
- {
- keyboardMonoDic[keyboardEnum].OnKeyClick += action;
- }
- public void UnRegisterOnKeyClickEvent(Action<string> action)
- {
- keyboardMonoDic[keyboardEnum].OnKeyClick -= action;
- }
- public void ClearOnKeyClickEvent()
- {
- keyboardMonoDic[keyboardEnum].OnKeyClick = null;
- }
- public static void RegisterOnWebKeyClickEvent(Action<string> action)
- {
- OnWebKeyClick += action;
- }
- public static void UnRegisterOnWebKeyClickEvent(Action<string> action)
- {
- OnWebKeyClick -= action;
- }
- public static void ClearOnWebKeyClickEvent()
- {
- OnWebKeyClick = null;
- }
- public void RegisterOnPointerDownEvent(Action<PointerEventData> action)
- {
- keyboardMonoDic[keyboardEnum].OnPointerDown += action;
- }
- public void UnRegisterOnPointerDownEvent(Action<PointerEventData> 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
- }
- }
- }
|