ClientServer/Client/Assets/Scripts/UI/OnKeyboardButton.cs
TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

127 lines
3.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class OnKeyboardButton : MonoBehaviour, ISelectHandler, IDeselectHandler
{
public string key;
public string altKey;
private TextMeshProUGUI buttonText;
private Button thisButton;
private Color defaultTextColor = new Color(0.6431373f, 0.6431373f, 0.6431373f, 1);
[SerializeField] private bool isSymbolsKey;
[SerializeField] private bool isEnterKey;
[SerializeField] private bool isDeleteKey;
[SerializeField] private bool isShiftKey;
[SerializeField] private bool isSpaceKey;
private bool _switchToSymbols;
public bool SwitchToSymbols
{
get => _switchToSymbols;
set
{
_switchToSymbols = value;
if (_switchToSymbols)
{
buttonText.text = altKey;
}
else
{
buttonText.text = key;
}
}
}
private bool _capitalizeCharacters;
public bool CapitalizeCharacters
{
get => _capitalizeCharacters;
set
{
_capitalizeCharacters = value;
if (!IsSymbolsKeys())
{
if (_capitalizeCharacters)
{
key = key.ToUpper();
buttonText.text = key.ToUpper();
}
else
{
key = key.ToLower();
buttonText.text = key.ToLower();
}
}
}
}
private bool IsSymbolsKeys()
{
return SwitchToSymbols || isEnterKey || isDeleteKey || isSpaceKey || isSymbolsKey || isShiftKey;
}
private void Awake()
{
buttonText = GetComponentInChildren<TextMeshProUGUI>();
thisButton = GetComponent<Button>();
CapitalizeCharacters = false;
if (!IsSymbolsKeys())
{
buttonText.text = key.ToLower();
}
else
{
buttonText.text = key;
}
}
void Start()
{
if (!IsSymbolsKeys()) { thisButton.onClick.AddListener(TypeLetter); }
if (isEnterKey) { thisButton.onClick.AddListener(CloseKeyboard); }
if (isSymbolsKey) { thisButton.onClick.AddListener(OnScreenKeyboard.Instance.ToggleSymbols); }
if (isDeleteKey) { thisButton.onClick.AddListener(OnScreenKeyboard.Instance.DeleteLetter); }
if (isShiftKey) { thisButton.onClick.AddListener(OnScreenKeyboard.Instance.ToggleCapitalization); }
if (isSpaceKey) { thisButton.onClick.AddListener(OnScreenKeyboard.Instance.InsertSpace); }
}
private void TypeLetter()
{
OnScreenKeyboard.Instance.TypeLetter(SwitchToSymbols ? altKey : key);
}
public void ToggleSymbols()
{
SwitchToSymbols = !SwitchToSymbols;
}
public void ToggleCapitalization()
{
CapitalizeCharacters = !CapitalizeCharacters;
}
private void CloseKeyboard()
{
StartCoroutine(OnScreenKeyboard.Instance.CloseKeyboardRoutine());
}
public void OnSelect(BaseEventData eventData)
{
buttonText.color = thisButton.colors.selectedColor;
}
public void OnDeselect(BaseEventData eventData)
{
buttonText.color = defaultTextColor;
}
}