rabidus-test/Assets/BNG Framework/Scripts/UI/VRKeyboardKey.cs

65 lines
1.7 KiB
C#
Raw Permalink Normal View History

2023-07-24 16:38:13 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BNG {
public class VRKeyboardKey : MonoBehaviour {
UnityEngine.UI.Button thisButton;
UnityEngine.UI.Text thisButtonText;
VRKeyboard vrKeyboard;
public string Keycode;
public string KeycodeShift;
[HideInInspector]
public bool UseShiftKey = false;
void Awake() {
thisButton = GetComponent<UnityEngine.UI.Button>();
thisButtonText = GetComponentInChildren<UnityEngine.UI.Text>();
// Assign click event handler
if (thisButton != null) {
thisButton.onClick.AddListener(OnKeyHit);
}
vrKeyboard = GetComponentInParent<VRKeyboard>();
}
public virtual void ToggleShift() {
UseShiftKey = !UseShiftKey;
// Make sure the button exists
if(thisButtonText == null) {
return;
}
// Update text label
if(UseShiftKey && !string.IsNullOrEmpty(KeycodeShift)) {
thisButtonText.text = KeycodeShift;
}
else {
thisButtonText.text = Keycode;
}
}
public virtual void OnKeyHit() {
OnKeyHit(UseShiftKey && !string.IsNullOrEmpty(KeycodeShift) ? KeycodeShift : Keycode);
}
public virtual void OnKeyHit(string key) {
if(vrKeyboard != null) {
vrKeyboard.PressKey(key);
}
else {
Debug.Log("Pressed key " + key + ", but no keyboard was found");
}
}
}
}