using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; namespace SC.XR.Unity.Module_Keyboard { public class SCKeyboard2DKey : SCKeyboardBaseKey { public override string Value { get { return base.Value; } set { base.Value = value; Text.text = value; } } private Text m_Text; public virtual Text Text { get { if (m_Text == null) m_Text = GetComponentInChildren(); return m_Text; } set { m_Text = value; } } #region KeyboardKeyEnums public SCKeyboardKeyEnum m_SCKeyboardKeyEnum; public SpecialKeyEnum m_SpecialKeyEnum; public LanguageKeyEnum m_LanguageKeyEnum; public UGUIKeyEnum m_UGUIKeyEnum; #endregion #region KeyboardKey Init Events /// /// Init Normal Key /// public virtual void OnNormalKeyInit() { } public virtual void OnNumberKeyInit() { } /// /// Init Symbol Key /// public virtual void OnSymbolKeyInit() { } /// /// Init Special Key /// public virtual void OnSpecialKeyInit() { switch (m_SpecialKeyEnum) { case SpecialKeyEnum.Cn: case SpecialKeyEnum.En: switch (SCKeyboardMono.presentKeyboardType) { case SCKeyboardType.ASCIICapable: Value = "英"; m_SpecialKeyEnum = SpecialKeyEnum.En; break; default: switch (SCKeyboard.currentSystemLanguage) { case KeyboardSystemLanguage.Chinese: Value = "中"; m_SpecialKeyEnum = SpecialKeyEnum.Cn; break; default: Value = "英"; m_SpecialKeyEnum = SpecialKeyEnum.En; break; } break; } break; } KeyboardStateChange(SCKeyboardMono.presentKeyboardState); } /// /// Init Pinyin Key /// public virtual void OnPinyinKeyInit() { } /// /// Init Language Key /// public virtual void OnLanguageKeyInit() { } /// /// Init Prompt Key /// public virtual void OnPromptKeyInit() { } /// /// Init UGUI Key /// public virtual void OnUGUIKeyInit() { } #endregion #region KeyboardKey Regist Events /// /// Regist Normal Key /// public virtual void OnNormalKeyRegist() { OnKeyClickEvent += OnNormalKeyClick; } /// /// Regist Number Key /// public virtual void OnNumberKeyRegist() { OnKeyClickEvent += OnNumberKeyClick; Text text = this.GetComponentInChildren(); this.Value = text.text; } /// /// Regist Symbol Key /// public virtual void OnSymbolKeyRegist() { OnKeyClickEvent += OnNormalKeyClick; Text text = this.GetComponentInChildren(); this.Value = text.text; if (SCKeyboardMono.symbolKeysDic.ContainsKey(this.Value)) { List scKeyboard2DKeys = SCKeyboardMono.symbolKeysDic[this.Value]; scKeyboard2DKeys.Add(this); SCKeyboardMono.symbolKeysDic[this.Value] = scKeyboard2DKeys; } else { List scKeyboard2DKeys = new List(); scKeyboard2DKeys.Add(this); SCKeyboardMono.symbolKeysDic.Add(this.Value, scKeyboard2DKeys); } } /// /// Regist Special Key /// public virtual void OnSpecialKeyRegist() { OnKeyClickEvent += OnSpecialKeyClick; if (SCKeyboardMono.specialKeysDic.ContainsKey(this.m_SpecialKeyEnum)) { List scKeyboard2DKeys = SCKeyboardMono.specialKeysDic[this.m_SpecialKeyEnum]; scKeyboard2DKeys.Add(this); SCKeyboardMono.specialKeysDic[this.m_SpecialKeyEnum] = scKeyboard2DKeys; } else { List scKeyboard2DKeys = new List(); scKeyboard2DKeys.Add(this); SCKeyboardMono.specialKeysDic.Add(this.m_SpecialKeyEnum, scKeyboard2DKeys); } SCKeyboardMono.OnKeyboardStateChange += KeyboardStateChange; } /// /// Regist Pinyin Key /// public virtual void OnPinyinKeyRegist() { OnKeyClickEvent += OnPinyinKeyClick; Text text = this.GetComponentInChildren(); this.Value = text.text; SCKeyboardMono.OnKeyboardShifted += Shift; } /// /// Regist Language Key /// public virtual void OnLanguageKeyRegist() { OnKeyClickEvent += OnLanguageKeyClick; } /// /// Regist Prompt Key /// public virtual void OnPromptKeyRegist() { OnKeyClickEvent += OnPromptKeyClick; } /// /// Regist UGUI Key /// public virtual void OnUGUIKeyRegist() { OnKeyClickEvent += OnUGUIKeyClick; } #endregion #region KeyboardKey UnRegist Events /// /// UnRegist Normal Key /// public virtual void OnNormalKeyUnRegist() { OnKeyClickEvent -= OnNormalKeyClick; } /// /// UnRegist Number Key /// public virtual void OnNumberKeyUnRegist() { OnKeyClickEvent -= OnNumberKeyClick; } /// /// UnRegist Symbol Key /// public virtual void OnSymbolKeyUnRegist() { OnKeyClickEvent -= OnNormalKeyClick; SCKeyboardMono.symbolKeysDic.Clear(); } /// /// UnRegist Special Key /// public virtual void OnSpecialKeyUnRegist() { OnKeyClickEvent -= OnSpecialKeyClick; SCKeyboardMono.specialKeysDic.Clear(); SCKeyboardMono.OnKeyboardStateChange -= KeyboardStateChange; } /// /// UnRegist Pinyin Key /// public virtual void OnPinyinKeyUnRegist() { OnKeyClickEvent -= OnPinyinKeyClick; SCKeyboardMono.OnKeyboardShifted -= Shift; } /// /// UnRegist Language Key /// public virtual void OnLanguageKeyUnRegist() { OnKeyClickEvent -= OnLanguageKeyClick; } /// /// UnRegist Prompt Key /// public virtual void OnPromptKeyUnRegist() { OnKeyClickEvent -= OnPromptKeyClick; } /// /// UnRegist UGUI Key /// public virtual void OnUGUIKeyUnRegist() { OnKeyClickEvent -= OnUGUIKeyClick; } #endregion #region KeyboardKey Click Events public void OnNormalKeyClick() { SCKeyboardMono.OnNormalKeyClick(this.Value); } public void OnNumberKeyClick() { SCKeyboardMono.OnNumberKeyClick(this.Value); } public void OnSpecialKeyClick() { SCKeyboardMono.OnSpecialKeyClick(m_SpecialKeyEnum); switch (m_SpecialKeyEnum) { case SpecialKeyEnum.Cn: Value = "英"; m_SpecialKeyEnum = SpecialKeyEnum.En; break; case SpecialKeyEnum.En: Value = "中"; m_SpecialKeyEnum = SpecialKeyEnum.Cn; break; } } public void OnPinyinKeyClick() { switch (SCKeyboardMono.presentKeyboardState) { case KeyboardState.Cn: if (SCKeyboardMono.IsShifted) { SCKeyboardMono.OnNormalKeyClick(this.Value); return; } SCKeyboardMono.OnPinyinKeyClick(this.Value); break; case KeyboardState.En: SCKeyboardMono.OnNormalKeyClick(this.Value); break; } } public void OnLanguageKeyClick() { SCKeyboardMono.OnLanguageKeyClick(m_LanguageKeyEnum); } public void OnPromptKeyClick() { SCKeyboardMono.OnPromptKeyClick(this.Value); } public void OnUGUIKeyClick() { SCKeyboardMono.OnUGUIKeyClick(m_UGUIKeyEnum); } #endregion #region KeyboardKey State Events protected virtual void Shift(bool isShifted) { Text text = this.GetComponentInChildren(); if (isShifted) { this.Value = this.Value.ToUpper(); } else { this.Value = this.Value.ToLower(); } } protected virtual void KeyboardStateChange(KeyboardState state) { switch (state) { case KeyboardState.Cn: switch (m_SCKeyboardKeyEnum) { case SCKeyboardKeyEnum.Special: switch (m_SpecialKeyEnum) { case SpecialKeyEnum.Enter: switch (SCKeyboardMono.presentKeyboardType) { case SCKeyboardType.URL: case SCKeyboardType.Search: this.Value = "搜索"; break; default: this.Value = "确认"; break; } break; case SpecialKeyEnum.Space: this.Value = "空格"; break; case SpecialKeyEnum.Participle: this.Value = "分词"; break; } break; } break; case KeyboardState.En: switch (m_SCKeyboardKeyEnum) { case SCKeyboardKeyEnum.Special: switch (m_SpecialKeyEnum) { case SpecialKeyEnum.Enter: switch (SCKeyboardMono.presentKeyboardType) { case SCKeyboardType.URL: case SCKeyboardType.Search: this.Value = "Search"; break; default: this.Value = "Enter"; break; } break; case SpecialKeyEnum.Space: this.Value = "Space"; break; case SpecialKeyEnum.Participle: this.Value = "/"; break; } break; } break; } } #endregion public override void Init() { base.Init(); switch (m_SCKeyboardKeyEnum) { case SCKeyboardKeyEnum.ABC: OnNormalKeyInit(); break; case SCKeyboardKeyEnum.Num: OnNumberKeyInit(); break; case SCKeyboardKeyEnum.Symbol: OnSymbolKeyInit(); break; case SCKeyboardKeyEnum.Special: OnSpecialKeyInit(); break; case SCKeyboardKeyEnum.Pinyin: OnPinyinKeyInit(); break; case SCKeyboardKeyEnum.Prompt: OnPromptKeyInit(); break; case SCKeyboardKeyEnum.Meme: break; case SCKeyboardKeyEnum.Language: OnLanguageKeyInit(); break; case SCKeyboardKeyEnum.UGUI: OnUGUIKeyInit(); break; default: OnNormalKeyInit(); break; } } protected override void RegistKey() { base.RegistKey(); switch (m_SCKeyboardKeyEnum) { case SCKeyboardKeyEnum.ABC: OnNormalKeyRegist(); break; case SCKeyboardKeyEnum.Num: OnNumberKeyRegist(); break; case SCKeyboardKeyEnum.Symbol: OnSymbolKeyRegist(); break; case SCKeyboardKeyEnum.Special: OnSpecialKeyRegist(); break; case SCKeyboardKeyEnum.Pinyin: OnPinyinKeyRegist(); break; case SCKeyboardKeyEnum.Prompt: OnPromptKeyRegist(); break; case SCKeyboardKeyEnum.Meme: break; case SCKeyboardKeyEnum.Language: OnLanguageKeyRegist(); break; case SCKeyboardKeyEnum.UGUI: OnUGUIKeyRegist(); break; default: OnNormalKeyRegist(); break; } } protected override void UnRegistKey() { base.UnRegistKey(); switch (m_SCKeyboardKeyEnum) { case SCKeyboardKeyEnum.ABC: OnNormalKeyUnRegist(); break; case SCKeyboardKeyEnum.Num: OnNumberKeyUnRegist(); break; case SCKeyboardKeyEnum.Symbol: OnSymbolKeyUnRegist(); break; case SCKeyboardKeyEnum.Special: OnSpecialKeyUnRegist(); break; case SCKeyboardKeyEnum.Pinyin: OnPinyinKeyUnRegist(); break; case SCKeyboardKeyEnum.Prompt: OnPromptKeyUnRegist(); break; case SCKeyboardKeyEnum.Meme: break; case SCKeyboardKeyEnum.Language: OnLanguageKeyUnRegist(); break; case SCKeyboardKeyEnum.UGUI: OnUGUIKeyUnRegist(); break; default: OnNormalKeyUnRegist(); break; } } } }