using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
namespace SC.XR.Unity.Module_Keyboard
{
public class SCKeyboard3DKey : SCKeyboardBaseKey
{
#region KeyboardKeyEnums
public SCKeyboardKeyEnum m_SCKeyboardKeyEnum;
public SpecialKeyEnum m_SpecialKeyEnum;
public LanguageKeyEnum m_LanguageKeyEnum;
#endregion
#region KeyboardKey Init Events
///
/// Init Normal Key
///
public virtual void OnNormalKeyInit()
{
}
///
/// Init Symbol Key
///
public virtual void OnSymbolKeyInit()
{
}
///
/// Init Special Key
///
public virtual void OnSpecialKeyInit()
{
}
///
/// Init Pinyin Key
///
public virtual void OnPinyinKeyInit()
{
}
///
/// Init Language Key
///
public virtual void OnLanguageKeyInit()
{
}
///
/// Init Prompt Key
///
public virtual void OnPromptKeyInit()
{
}
#endregion
#region KeyboardKey Regist Events
///
/// Regist Normal Key
///
public virtual void OnNormalKeyRegist()
{
OnKeyClickEvent += OnNormalKeyClick;
}
///
/// Regist Symbol Key
///
public virtual void OnSymbolKeyRegist()
{
OnKeyClickEvent += OnNormalKeyClick;
TextMesh textMesh = this.GetComponentInChildren();
this.Value = textMesh.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;
}
///
/// Regist Pinyin Key
///
public virtual void OnPinyinKeyRegist()
{
OnKeyClickEvent += OnPinyinKeyClick;
TextMesh textMesh = this.GetComponentInChildren();
this.Value = textMesh.text;
SCKeyboardMono.OnKeyboardShifted += Shift;
}
///
/// Regist Language Key
///
public virtual void OnLanguageKeyRegist()
{
OnKeyClickEvent += OnLanguageKeyClick;
}
///
/// Regist Prompt Key
///
public virtual void OnPromptKeyRegist()
{
OnKeyClickEvent += OnPromptKeyClick;
}
#endregion
#region KeyboardKey UnRegist Events
///
/// UnRegist Normal Key
///
public virtual void OnNormalKeyUnRegist()
{
OnKeyClickEvent -= OnNormalKeyClick;
}
///
/// UnRegist Symbol Key
///
public virtual void OnSymbolKeyUnRegist()
{
OnKeyClickEvent -= OnNormalKeyClick;
SCKeyboardMono.symbolKeysDic.Clear();
}
///
/// UnRegist Special Key
///
public virtual void OnSpecialKeyUnRegist()
{
OnKeyClickEvent -= OnSpecialKeyClick;
}
///
/// 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;
}
#endregion
#region KeyboardKey Click Events
public void OnNormalKeyClick()
{
SCKeyboardMono.OnNormalKeyClick(this.Value);
}
public void OnSpecialKeyClick()
{
SCKeyboardMono.OnSpecialKeyClick(m_SpecialKeyEnum);
switch (m_SpecialKeyEnum)
{
case SpecialKeyEnum.Cn:
GetComponentInChildren().text = "英";
m_SpecialKeyEnum = SpecialKeyEnum.En;
break;
case SpecialKeyEnum.En:
GetComponentInChildren().text = "中";
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()
{
TextMesh textMesh = this.GetComponentInChildren();
SCKeyboardMono.OnPromptKeyClick(textMesh.text);
SCKeyboardMono.OnKeyClick?.Invoke(textMesh.text);
for (int i = 0; i < textMesh.text.Length; i++)
{
SCKeyboardMono.OnWebKeyClick?.Invoke(textMesh.text[i].ToString());
}
}
#endregion
#region KeyboardKey State Events
protected virtual void Shift(bool isShifted)
{
TextMesh textMesh = this.GetComponentInChildren();
if (isShifted)
{
string textUpper = textMesh.text.ToUpper();
textMesh.text = textUpper;
this.Value = textUpper;
}
else
{
string textLower = textMesh.text.ToLower();
textMesh.text = textLower;
this.Value = textLower;
}
}
#endregion
protected InteractionPressableEntry interactionPressableEntry;
protected InteractionTouchableEntry interactionTouchableEntry;
public override void Init()
{
base.Init();
switch (m_SCKeyboardKeyEnum)
{
case SCKeyboardKeyEnum.ABC:
case SCKeyboardKeyEnum.Num:
OnNormalKeyInit();
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;
default:
OnNormalKeyInit();
break;
}
}
protected override void RegistKey()
{
PressableButton pressableButton = this.GetComponent();
TouchableButton touchableButton = this.GetComponent();
if (pressableButton && touchableButton)
{
InteractionEvent interActionEvent = new InteractionEvent();
interActionEvent.AddListener(OnKeyClick);
interactionPressableEntry = new InteractionPressableEntry() { eventID = InteractionPressableType.PointerClick, callback = interActionEvent };
interactionTouchableEntry = new InteractionTouchableEntry() { eventID = InteractionTouchableType.PokePress, callback = interActionEvent };
pressableButton.Triggers.Add(interactionPressableEntry);
touchableButton.Triggers.Add(interactionTouchableEntry);
}
switch (m_SCKeyboardKeyEnum)
{
case SCKeyboardKeyEnum.ABC:
case SCKeyboardKeyEnum.Num:
OnNormalKeyRegist();
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;
default:
OnNormalKeyRegist();
break;
}
}
protected override void UnRegistKey()
{
PressableButton pressableButton = this.GetComponent();
TouchableButton touchableButton = this.GetComponent();
if (pressableButton && touchableButton)
{
pressableButton.Triggers.Remove(interactionPressableEntry);
touchableButton.Triggers.Remove(interactionTouchableEntry);
}
switch (m_SCKeyboardKeyEnum)
{
case SCKeyboardKeyEnum.ABC:
case SCKeyboardKeyEnum.Num:
OnNormalKeyUnRegist();
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;
default:
OnNormalKeyUnRegist();
break;
}
}
}
}