263 lines
8.1 KiB
C#
263 lines
8.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using ZGame;
|
|
|
|
namespace HK.Keyboard
|
|
{
|
|
public enum KeyboardType
|
|
{
|
|
All,
|
|
NumberOnly,
|
|
}
|
|
|
|
public class Keyboard : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
public TMP_InputField inputField;
|
|
public GameObject AlphaBoardUnshifted;
|
|
public GameObject AlphaBoardShifted;
|
|
public GameObject NumBoardUnshifted;
|
|
public GameObject NumBoardShifted;
|
|
public Button btnClickClose;
|
|
public KeyboardType keyboardType = KeyboardType.All;
|
|
|
|
public FontsChange fontChange;
|
|
public ColorsChange colorChange;
|
|
|
|
public Keyboard_Chin chin;
|
|
private List<KeyBind> keyBinds;
|
|
private List<KeyBind> keyBinds_NumberOnly = new List<KeyBind>();
|
|
private List<KeyBind> keyBinds_LetterOnly = new List<KeyBind>();
|
|
private List<KeyBind> keyBinds_PinyinOnly = new List<KeyBind>();
|
|
private List<KeyBind> keyBinds_Switch;
|
|
private Action OKAction;
|
|
public bool isHideOnAwake = false;
|
|
|
|
private bool isChin
|
|
{
|
|
get => chin.isChin;
|
|
set => chin.isChin = value;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
keyBinds = new List<KeyBind>();
|
|
keyBinds_Switch = new List<KeyBind>();
|
|
var list = transform.FindChildDeeps<KeyBind>();
|
|
foreach (var keyBind in list)
|
|
{
|
|
keyBinds.Add(keyBind);
|
|
keyBind.onClickAction += ClickKey;
|
|
if (keyBind.keyName == "Switch")
|
|
keyBinds_Switch.Add(keyBind);
|
|
}
|
|
|
|
isChin = false;
|
|
chin.selectedAction += ClickKey;
|
|
btnClickClose.onClick.AddListener(ClickClose);
|
|
|
|
ShowOnlyOne(AlphaBoardUnshifted);
|
|
if (isHideOnAwake)
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
EventManager.Instance.Subscribe(OpenKeyboardEventArgs.EventId, OpenKeyboardEvent);
|
|
}
|
|
|
|
private void ClickClose()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
btnClickClose.onClick.RemoveListener(ClickClose);
|
|
EventManager.Instance.Unsubscribe(OpenKeyboardEventArgs.EventId, OpenKeyboardEvent);
|
|
}
|
|
|
|
private void OpenKeyboardEvent(object sender, GameEventArgs e)
|
|
{
|
|
var args = e as OpenKeyboardEventArgs;
|
|
if (args.inputField != null)
|
|
{
|
|
inputField = args.inputField;
|
|
OKAction = args.OKAction;
|
|
}
|
|
|
|
keyboardType = args.keyboardType;
|
|
ChangeKeyboard();
|
|
colorChange.gameObject.SetActive(args.isChangeColor);
|
|
fontChange.gameObject.SetActive(args.isChangeFont);
|
|
gameObject.SetActive(true);
|
|
}
|
|
|
|
void ChangeKeyboard()
|
|
{
|
|
switch (keyboardType)
|
|
{
|
|
case KeyboardType.All:
|
|
ShowOnlyOne(AlphaBoardUnshifted);
|
|
foreach (var key in keyBinds)
|
|
{
|
|
key.SetInteractable(true);
|
|
}
|
|
|
|
break;
|
|
case KeyboardType.NumberOnly:
|
|
ShowOnlyOne(NumBoardUnshifted);
|
|
foreach (var key in keyBinds)
|
|
{
|
|
if (key.keyType == KeyType.Number)
|
|
key.SetInteractable(true);
|
|
else
|
|
key.SetInteractable(false);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
|
|
void ShowOnlyOne(GameObject go)
|
|
{
|
|
AlphaBoardUnshifted.SetActive(false);
|
|
AlphaBoardShifted.SetActive(false);
|
|
NumBoardUnshifted.SetActive(false);
|
|
NumBoardShifted.SetActive(false);
|
|
go.SetActive(true);
|
|
}
|
|
|
|
[SerializeField] private string tmp_Chin;
|
|
|
|
private void OnDisable()
|
|
{
|
|
tmp_Chin = "";
|
|
isChin = false;
|
|
isEnter = false;
|
|
OKAction = null;
|
|
inputField = null;
|
|
foreach (var keyBind in keyBinds_Switch)
|
|
keyBind.GetComponentInChildren<TMP_Text>().text = "英/<size=25>中</size>";
|
|
ShowOnlyOne(AlphaBoardUnshifted);
|
|
}
|
|
|
|
void ClickKey(string keyName)
|
|
{
|
|
if (CheckKey(keyName))
|
|
{
|
|
if (chin.isChin && !chin.isSelect)
|
|
{
|
|
tmp_Chin += keyName;
|
|
chin.PY(tmp_Chin);
|
|
inputField.text += keyName;
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(tmp_Chin))
|
|
{
|
|
var length = inputField.text.Length - tmp_Chin.Length;
|
|
inputField.text = inputField.text.Remove(length);
|
|
tmp_Chin = "";
|
|
}
|
|
|
|
inputField.text += keyName;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool CheckKey(string keyName)
|
|
{
|
|
switch (keyName)
|
|
{
|
|
case "Close":
|
|
gameObject.SetActive(false);
|
|
return false;
|
|
break;
|
|
case ".?123":
|
|
ShowOnlyOne(NumBoardUnshifted);
|
|
return false;
|
|
break;
|
|
case "Remove":
|
|
if (chin.isChin && !chin.isSelect)
|
|
{
|
|
if (!string.IsNullOrEmpty(tmp_Chin))
|
|
{
|
|
tmp_Chin = tmp_Chin.Remove(tmp_Chin.Length - 1);
|
|
chin.PY(tmp_Chin);
|
|
}
|
|
}
|
|
|
|
var s = inputField.text;
|
|
if (s.Length > 0)
|
|
inputField.text = s.Remove(s.Length - 1);
|
|
|
|
return false;
|
|
break;
|
|
case "Lowercase":
|
|
ShowOnlyOne(AlphaBoardUnshifted);
|
|
return false;
|
|
break;
|
|
case "CapitalLetters":
|
|
ShowOnlyOne(AlphaBoardShifted);
|
|
return false;
|
|
break;
|
|
case "ABC":
|
|
ShowOnlyOne(AlphaBoardUnshifted);
|
|
return false;
|
|
break;
|
|
case "!@-":
|
|
ShowOnlyOne(NumBoardUnshifted);
|
|
return false;
|
|
break;
|
|
case "+/\"":
|
|
ShowOnlyOne(NumBoardShifted);
|
|
return false;
|
|
break;
|
|
case "OK":
|
|
OKAction?.Invoke();
|
|
gameObject.SetActive(false);
|
|
return false;
|
|
break;
|
|
case "Switch":
|
|
isChin = !isChin;
|
|
if (isChin)
|
|
foreach (var keyBind in keyBinds_Switch)
|
|
keyBind.GetComponentInChildren<TMP_Text>().text = "中/<size=25>英</size>";
|
|
else
|
|
foreach (var keyBind in keyBinds_Switch)
|
|
keyBind.GetComponentInChildren<TMP_Text>().text = "英/<size=25>中</size>";
|
|
return false;
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public void SetFont(TMP_FontAsset font)
|
|
{
|
|
inputField.fontAsset = font;
|
|
}
|
|
|
|
public void SetColor(Color color)
|
|
{
|
|
inputField.textComponent.color = color;
|
|
}
|
|
|
|
private bool isEnter;
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
isEnter = true;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
isEnter = false;
|
|
}
|
|
}
|
|
} |