123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using SC.XR.Unity;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public abstract class SCKeyboardMono : MonoBehaviour
- {
- public GameObject keyboard_num;
- public GameObject keyboard_symbol;
- public GameObject keyboard_enUp;
- public GameObject keyboard_enLow;
- public GameObject keyboard_cn;
- public Action OnDoneButtonClick;
- public Action<string> OnTextChange;
- public StringBuilder stringBuilder;
- private StringBuilder preInputStringBuilder;
- public KeyboardPrompt keyboardPrompt;
- public void Initialize()
- {
- if (stringBuilder == null)
- {
- stringBuilder = new StringBuilder();
- }
- if (preInputStringBuilder == null)
- {
- preInputStringBuilder = new StringBuilder();
- }
- keyboardPrompt.Init(OnChineseCharacterClick);
- ShowNum();
- }
- public void OnChineseCharacterClick(string value)
- {
- preInputStringBuilder.Clear();
- stringBuilder.Append(value);
- HideChinesePrompt();
- SetTextString();
- }
- public void OnPinyinKeyClick(string value)
- {
- preInputStringBuilder.Append(value);
- int wordCount = GetChinese(preInputStringBuilder.ToString());
- if (wordCount != 0)
- {
- ShowChinesePrompt();
- }
- else
- {
- if (preInputStringBuilder.Length != 0)
- {
- stringBuilder.Append(preInputStringBuilder.ToString());
- preInputStringBuilder.Clear();
- }
- }
- SetTextString();
- }
- public void OnNormalKeyClick(string value)
- {
- if (preInputStringBuilder.Length != 0)
- {
- stringBuilder.Append(preInputStringBuilder.ToString());
- preInputStringBuilder.Clear();
- HideChinesePrompt();
- }
- stringBuilder.Append(value);
- SetTextString();
- }
- public void OnDoneClick()
- {
- stringBuilder.Append(preInputStringBuilder.ToString());
- preInputStringBuilder.Clear();
- HideChinesePrompt();
- OnDoneButtonClick?.Invoke();
- }
- public void OnClearKeyClick()
- {
- preInputStringBuilder.Clear();
- stringBuilder.Clear();
- HideChinesePrompt();
- SetTextString();
- }
- public void OnDeleteKeyClick()
- {
- if (preInputStringBuilder.Length > 0)
- {
- preInputStringBuilder.Remove(preInputStringBuilder.Length - 1, 1);
- if (preInputStringBuilder.Length != 0)
- {
- GetChinese(preInputStringBuilder.ToString());
- }
- else
- {
- HideChinesePrompt();
- }
- SetTextString();
- return;
- }
- HideChinesePrompt();
- if (stringBuilder.Length > 0)
- {
- stringBuilder.Remove(stringBuilder.Length - 1, 1);
- SetTextString();
- }
- }
- public void OnSpaceClick()
- {
- if (preInputStringBuilder.Length > 0)
- {
- preInputStringBuilder.Clear();
- string firstChineseWord = GetFirstChineseWord();
- if (!string.IsNullOrEmpty(firstChineseWord))
- {
- stringBuilder.Append(GetFirstChineseWord());
- }
- HideChinesePrompt();
- SetTextString();
- return;
- }
- stringBuilder.Append(" ");
- SetTextString();
- }
- //for CN IME
- public void OnShiftClick()
- {
- if (preInputStringBuilder.Length != 0)
- {
- stringBuilder.Append(preInputStringBuilder.ToString());
- preInputStringBuilder.Clear();
- HideChinesePrompt();
- SetTextString();
- }
- }
- public void ShowNum()
- {
- if (preInputStringBuilder.Length != 0)
- {
- stringBuilder.Append(preInputStringBuilder.ToString());
- preInputStringBuilder.Clear();
- HideChinesePrompt();
- SetTextString();
- }
- keyboard_num.SetActive(true);
- keyboard_symbol.SetActive(false);
- keyboard_enUp.SetActive(false);
- keyboard_enLow.SetActive(false);
- keyboard_cn.SetActive(false);
- }
- public void ShowSymbol()
- {
- keyboard_num.SetActive(false);
- keyboard_symbol.SetActive(true);
- keyboard_enUp.SetActive(false);
- keyboard_enLow.SetActive(false);
- keyboard_cn.SetActive(false);
- }
- //Shift
- public void ShowEnUp()
- {
- keyboard_num.SetActive(false);
- keyboard_symbol.SetActive(false);
- keyboard_enUp.SetActive(true);
- keyboard_enLow.SetActive(false);
- keyboard_cn.SetActive(false);
- }
- public void ShowEnLow()
- {
- if (preInputStringBuilder.Length != 0)
- {
- stringBuilder.Append(preInputStringBuilder.ToString());
- preInputStringBuilder.Clear();
- HideChinesePrompt();
- SetTextString();
- }
- keyboard_num.SetActive(false);
- keyboard_symbol.SetActive(false);
- keyboard_enUp.SetActive(false);
- keyboard_enLow.SetActive(true);
- keyboard_cn.SetActive(false);
- }
- public void ShowCN()
- {
- keyboard_num.SetActive(false);
- keyboard_symbol.SetActive(false);
- keyboard_enUp.SetActive(false);
- keyboard_enLow.SetActive(false);
- keyboard_cn.SetActive(true);
- }
- public virtual void SetTextString()
- {
- string result = stringBuilder.ToString() + preInputStringBuilder.ToString();
- OnTextChange?.Invoke(result);
- }
- protected virtual int GetChinese(string englishWord)
- {
- return keyboardPrompt.GetChinese(englishWord);
- }
- protected void ShowChinesePrompt()
- {
- keyboardPrompt.gameObject.SetActive(true);
- }
- protected void HideChinesePrompt()
- {
- keyboardPrompt.gameObject.SetActive(false);
- }
- protected string GetFirstChineseWord()
- {
- return keyboardPrompt.GetFirstChineseWord();
- }
- }
|