Add ultimate xr

This commit is contained in:
2024-08-06 21:58:35 +02:00
parent 864033bf10
commit 7165bacd9d
3952 changed files with 2162037 additions and 35 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b7f841a5ab5c49e1b8882865b2d33115
timeCreated: 1643966683

View File

@@ -0,0 +1,52 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrHiScoresEntry.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using UltimateXR.Core.Components;
using UnityEngine;
using UnityEngine.UI;
namespace UltimateXR.UI.Helpers.HiScores
{
/// <summary>
/// UI component for a hi-scores entry.
/// </summary>
public class UxrHiScoresEntry : UxrComponent
{
#region Inspector Properties/Serialized Fields
[SerializeField] private Text _name;
[SerializeField] private Text _value;
[SerializeField] private Image _image;
#endregion
#region Public Methods
/// <summary>
/// Sets up the content.
/// </summary>
/// <param name="userName">Name to show on the label</param>
/// <param name="value">Value to show as a score</param>
/// <param name="sprite">Optional sprite to show next to the score</param>
public void Setup(string userName, string value, Sprite sprite)
{
_name.text = userName;
_value.text = value;
if (sprite != null)
{
_image.overrideSprite = sprite;
_image.gameObject.SetActive(true);
}
else
{
_image.sprite = null;
//_image.gameObject.SetActive(false);
}
}
#endregion
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a9d54601c17c87e44ae4402dd733147c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,119 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrHiScoresPanelEnterName.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using UltimateXR.Animation.UI;
using UltimateXR.Core.Components;
using UltimateXR.UI.Helpers.Keyboard;
using UltimateXR.UI.UnityInputModule.Controls;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UltimateXR.UI.Helpers.HiScores
{
/// <summary>
/// UI component for a hi-scores panel that requests the user name.
/// </summary>
public class UxrHiScoresPanelEnterName : UxrComponent
{
#region Inspector Properties/Serialized Fields
[SerializeField] private CanvasGroup _canvasGroup;
[SerializeField] private UxrKeyboardUI _keyboard;
[SerializeField] private Text _textCongratulations;
[SerializeField] private Text _textEnterName;
[SerializeField] private UxrControlInput _buttonOk;
[SerializeField] private Text _textButtonOk;
#endregion
#region Public Types & Data
/// <summary>
/// Event called when the user finished entering the name and pressed the OK button.
/// </summary>
public event Action<string> NameEntered;
/// <summary>
/// Gets the <see cref="UxrKeyboardUI" /> component that is used to enter the name.
/// </summary>
public UxrKeyboardUI Keyboard => _keyboard;
#endregion
#region Public Methods
/// <summary>
/// Shows the panel using a fade effect. The panel GameObject should be in an inactive state.
/// </summary>
/// <param name="textCongratulations">Congratulations text</param>
/// <param name="textEnterName">Enter name text on top</param>
/// <param name="textEnter">Enter name text right above the name</param>
/// <param name="fadeDurationSeconds">Seconds it takes for the panel to fade in</param>
/// <param name="fadeDelaySeconds">Seconds to wait before the panel fades in</param>
public void Show(string textCongratulations, string textEnterName, string textEnter, float fadeDurationSeconds, float fadeDelaySeconds = 0.0f)
{
gameObject.SetActive(true);
UxrCanvasAlphaTween.FadeIn(_canvasGroup, fadeDurationSeconds, fadeDelaySeconds);
_textCongratulations.text = textCongratulations;
_textEnterName.text = textEnterName;
_textButtonOk.text = textEnter;
}
#endregion
#region Unity
/// <summary>
/// Subscribes to events.
/// </summary>
protected override void OnEnable()
{
base.OnEnable();
_buttonOk.Clicked += ButtonOk_Clicked;
}
/// <summary>
/// Unsubscribes from events.
/// </summary>
protected override void OnDisable()
{
base.OnDisable();
_buttonOk.Clicked -= ButtonOk_Clicked;
}
/// <summary>
/// Updates the OK button interactive state depending on whether there is currently any content in the name box.
/// </summary>
private void Update()
{
_buttonOk.Enabled = !string.IsNullOrEmpty(_keyboard.CurrentLine);
}
#endregion
#region Event Handling Methods
/// <summary>
/// Called whenever the OK button was clicked. Closes the panel.
/// </summary>
/// <param name="controlInput">Control that was clicked</param>
/// <param name="eventData">Event parameters</param>
private void ButtonOk_Clicked(UxrControlInput controlInput, PointerEventData eventData)
{
if (!string.IsNullOrEmpty(_keyboard.CurrentLine) && !UxrTween.HasActiveTween<UxrCanvasAlphaTween>(_canvasGroup))
{
UxrCanvasAlphaTween.FadeOut(_canvasGroup, 0.2f, 0.0f, t => NameEntered?.Invoke(_keyboard.CurrentLine)).SetFinishedActions(UxrTweenFinishedActions.DeactivateGameObject);
}
}
#endregion
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 263bfbe786344a545b0b5cc503acca73
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,114 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrHiScoresPanelEntries.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using UltimateXR.Animation.UI;
using UltimateXR.Core.Components;
using UltimateXR.UI.UnityInputModule.Controls;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UltimateXR.UI.Helpers.HiScores
{
/// <summary>
/// UI component a hi-scores panel that shows user+score entries.
/// </summary>
public class UxrHiScoresPanelEntries : UxrComponent
{
#region Inspector Properties/Serialized Fields
[SerializeField] private Text _textTitle;
[SerializeField] private CanvasGroup _canvasGroup;
[SerializeField] private Transform _entriesRoot;
[SerializeField] private UxrHiScoresEntry _prefabEntry;
[SerializeField] private UxrControlInput _buttonOk;
[SerializeField] private Text _textButtonOk;
#endregion
#region Public Types & Data
/// <summary>
/// Event called whenever the OK button was clicked.
/// </summary>
public event Action OkButtonClicked;
#endregion
#region Public Methods
/// <summary>
/// Enables the Hi-scores panel so that it shows up with a fade-in effect. The hi-scores panel's root GameObject should
/// be in an inactive state.
/// </summary>
/// <param name="title">Text to show on the title</param>
/// <param name="buttonOk">Text to show as the OK button</param>
/// <param name="fadeDurationSeconds">Seconds it takes for the panel to fade in</param>
/// <param name="fadeDelaySeconds">Seconds to wait before the panel fades in</param>
public void Show(string title, string buttonOk, float fadeDurationSeconds, float fadeDelaySeconds = 0.0f)
{
_textTitle.text = title;
_textButtonOk.text = buttonOk;
gameObject.SetActive(true);
UxrCanvasAlphaTween.FadeIn(_canvasGroup, fadeDurationSeconds, fadeDelaySeconds);
}
/// <summary>
/// Adds a hi-score entry to the panel.
/// </summary>
/// <param name="entryName">Entry name</param>
/// <param name="entryValue">Entry score</param>
/// <param name="icon">Optional icon to show next to the score</param>
public void AddEntry(string entryName, string entryValue, Sprite icon)
{
UxrHiScoresEntry entry = Instantiate(_prefabEntry, _entriesRoot);
entry.Setup(entryName, entryValue, icon);
}
#endregion
#region Unity
/// <summary>
/// Subscribes to events.
/// </summary>
protected override void OnEnable()
{
base.OnEnable();
_buttonOk.Clicked += ButtonOk_Clicked;
}
/// <summary>
/// Unsubscribes from events.
/// </summary>
protected override void OnDisable()
{
base.OnDisable();
_buttonOk.Clicked -= ButtonOk_Clicked;
}
#endregion
#region Event Handling Methods
/// <summary>
/// Called whenever the OK button was clicked. Closes the panel.
/// </summary>
/// <param name="controlInput">Control that was clicked</param>
/// <param name="eventData">Event parameters</param>
private void ButtonOk_Clicked(UxrControlInput controlInput, PointerEventData eventData)
{
if (!UxrTween.HasActiveTween<UxrCanvasAlphaTween>(_canvasGroup))
{
UxrCanvasAlphaTween.FadeOut(_canvasGroup, 0.2f, 0.2f, t => OkButtonClicked?.Invoke()).SetFinishedActions(UxrTweenFinishedActions.DeactivateGameObject);
}
}
#endregion
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 57e9e370504f517459fa8c9ffc9b37b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8266e8fe80b14fe1a6ee770a8d369419
timeCreated: 1643968098

View File

@@ -0,0 +1,23 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrKeyLayoutType.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace UltimateXR.UI.Helpers.Keyboard
{
/// <summary>
/// Enumerates the different layouts of labels in a keyboard key.
/// </summary>
public enum UxrKeyLayoutType
{
/// <summary>
/// Single character label.
/// </summary>
SingleChar,
/// <summary>
/// Key supports multiple outputs depending on shift/alt gr and has multiple labels because of that.
/// </summary>
MultipleChar
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 171929de408c4a0aa3522c0c969b3408
timeCreated: 1643212986

View File

@@ -0,0 +1,27 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrKeyType.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace UltimateXR.UI.Helpers.Keyboard
{
/// <summary>
/// Enumerates the different key types.
/// </summary>
public enum UxrKeyType
{
Printable,
Tab,
Shift,
CapsLock,
Control,
Alt,
AltGr,
Enter,
Backspace,
Del,
ToggleSymbols,
ToggleViewPassword,
Escape
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 91d401316c344f7d873da24bb2bef16b
timeCreated: 1643213319

View File

@@ -0,0 +1,52 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrKeyboardKeyEventArgs.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
namespace UltimateXR.UI.Helpers.Keyboard
{
/// <summary>
/// Key press/release event parameters.
/// </summary>
public class UxrKeyboardKeyEventArgs : EventArgs
{
#region Public Types & Data
/// <summary>
/// Gets the key that was pressed/released.
/// </summary>
public UxrKeyboardKeyUI Key { get; }
/// <summary>
/// Gets whether it was a press (true) or release (false).
/// </summary>
public bool IsPress { get; }
/// <summary>
/// Gets the current line content. If it was a keypress event and the the keypress was the ENTER key then the line
/// before pressing ENTER is passed.
/// </summary>
public string Line { get; }
#endregion
#region Constructors & Finalizer
/// <summary>
/// Constructor.
/// </summary>
/// <param name="key">Key that was pressed</param>
/// <param name="isPress">Is it a press or a release?</param>
/// <param name="line">Current line</param>
public UxrKeyboardKeyEventArgs(UxrKeyboardKeyUI key, bool isPress, string line)
{
Key = key;
IsPress = isPress;
Line = line;
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 02c109b87bfd46afb1d1aa7614d12f47
timeCreated: 1632987224

View File

@@ -0,0 +1,502 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrKeyboardKeyUI.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using UltimateXR.Core;
using UltimateXR.Core.Components;
using UltimateXR.Core.Settings;
using UltimateXR.UI.UnityInputModule.Controls;
using UnityEngine;
using UnityEngine.UI;
#pragma warning disable 0414
namespace UltimateXR.UI.Helpers.Keyboard
{
/// <summary>
/// UI component for a keyboard key.
/// </summary>
[ExecuteInEditMode]
public class UxrKeyboardKeyUI : UxrComponent
{
#region Inspector Properties/Serialized Fields
[SerializeField] private UxrKeyType _keyType;
[SerializeField] private UxrKeyLayoutType _layout;
[SerializeField] private string _printShift;
[SerializeField] private string _printNoShift;
[SerializeField] private string _printAltGr;
[SerializeField] private string _forceLabel;
[SerializeField] private Text _singleLayoutValue;
[SerializeField] private Text _multipleLayoutValueTopLeft;
[SerializeField] private Text _multipleLayoutValueBottomLeft;
[SerializeField] private Text _multipleLayoutValueBottomRight;
[SerializeField] private List<UxrToggleSymbolsPage> _toggleSymbols;
// Hidden in the custom inspector
[SerializeField] private bool _nameDirty;
#endregion
#region Public Types & Data
/// <summary>
/// Gets the key type.
/// </summary>
public UxrKeyType KeyType => _keyType;
/// <summary>
/// Gets the layout use for the labels on the key.
/// </summary>
public UxrKeyLayoutType KeyLayoutType => _layout;
/// <summary>
/// Gets the character used when the key has a single label.
/// </summary>
public char SingleLayoutValue => _singleLayoutValue != null && _singleLayoutValue.text.Length > 0 ? _singleLayoutValue.text[0] : '?';
/// <summary>
/// Gets the character used in the top left corner when the key has multiple labels, because it supports combination
/// with shift and alt gr.
/// </summary>
public char MultipleLayoutValueTopLeft => _multipleLayoutValueTopLeft != null && _multipleLayoutValueTopLeft.text.Length > 0 ? _multipleLayoutValueTopLeft.text[0] : '?';
/// <summary>
/// Gets the character used in the bottom left corner when the key has multiple labels, because it supports combination
/// with shift and alt gr.
/// </summary>
public char MultipleLayoutValueBottomLeft => _multipleLayoutValueBottomLeft != null && _multipleLayoutValueBottomLeft.text.Length > 0 ? _multipleLayoutValueBottomLeft.text[0] : '?';
/// <summary>
/// Gets the character used in the bottom right corner when the key has multiple labels, because it supports
/// combination with shift and alt gr.
/// </summary>
public char MultipleLayoutValueBottomRight => _multipleLayoutValueBottomRight != null ? _multipleLayoutValueBottomRight.text[0] : '?';
/// <summary>
/// Gets whether the key supports combination with shift and alt gr, and has a character specified for the bottom
/// right.
/// </summary>
public bool HasMultipleLayoutValueBottomRight => _multipleLayoutValueBottomRight != null && _multipleLayoutValueBottomRight.text.Length > 0;
/// <summary>
/// Gets whether the key is a letter.
/// </summary>
public bool IsLetterKey => KeyType == UxrKeyType.Printable && char.IsLetter(SingleLayoutValue);
/// <summary>
/// Gets the current symbols group selected, for a key that has a <see cref="KeyType" /> role of
/// <see cref="UxrKeyType.ToggleSymbols" />.
/// </summary>
public UxrToggleSymbolsPage CurrentToggleSymbolsPage => KeyType == UxrKeyType.ToggleSymbols && _toggleSymbols != null && _currentSymbolsIndex < _toggleSymbols.Count ? _toggleSymbols[_currentSymbolsIndex] : null;
/// <summary>
/// Gets the next symbols group, for a key that has a <see cref="KeyType" /> role of
/// <see cref="UxrKeyType.ToggleSymbols" />, that would be selected if pressed.
/// </summary>
public UxrToggleSymbolsPage NextToggleSymbolsPage => KeyType == UxrKeyType.ToggleSymbols && _toggleSymbols != null && _toggleSymbols.Count > 0 ? _toggleSymbols[(_currentSymbolsIndex + 1) % _toggleSymbols.Count] : null;
/// <summary>
/// Gets the <see cref="UxrKeyboardKeyUI" /> component the key belongs to.
/// </summary>
public UxrKeyboardUI Keyboard
{
get
{
if (_keyboard == null)
{
_keyboard = GetComponentInParent<UxrKeyboardUI>();
}
return _keyboard;
}
}
/// <summary>
/// Gets the <see cref="UxrControlInput" /> component for the key.
/// </summary>
public UxrControlInput ControlInput { get; private set; }
/// <summary>
/// Gets or sets whether the key can be interacted with.
/// </summary>
public bool Enabled
{
get => ControlInput.Enabled;
set => ControlInput.Enabled = value;
}
/// <summary>
/// Gets or sets the string that, if non-empty, will override the label content on the key.
/// </summary>
public string ForceLabel
{
get => _forceLabel;
set
{
_forceLabel = value;
SetupKeyLabels();
}
}
#endregion
#region Public Methods
/// <summary>
/// Gets the character that would be printed if the key was pressed.
/// </summary>
/// <param name="shift">Whether shift is pressed</param>
/// <param name="altGr">Whether alt gr is pressed</param>
/// <returns>Character that would be printed</returns>
public char GetSingleLayoutValueNoForceLabel(bool shift, bool altGr)
{
if (shift && !string.IsNullOrEmpty(_printShift))
{
return _printShift[0];
}
if (altGr && !string.IsNullOrEmpty(_printAltGr))
{
return _printAltGr[0];
}
return !string.IsNullOrEmpty(_printNoShift) ? _printNoShift[0] : ' ';
}
/// <summary>
/// Updates the label on the key.
/// </summary>
/// <param name="shiftEnabled">Whether shift is enabled</param>
public void UpdateLetterKeyLabel(bool shiftEnabled)
{
if (KeyType == UxrKeyType.Printable && _singleLayoutValue)
{
_singleLayoutValue.text = shiftEnabled ? _printShift : _printNoShift;
}
}
/// <summary>
/// Sets up the toggle symbol entries.
/// </summary>
/// <param name="entries">Entries</param>
public void SetupToggleSymbolsPages(List<UxrToggleSymbolsPage> entries)
{
if (_keyType == UxrKeyType.ToggleSymbols)
{
_toggleSymbols = entries;
_currentSymbolsIndex = 0;
if (entries != null)
{
for (int i = 0; i < entries.Count; ++i)
{
entries[i].KeysRoot.SetActive(i == 0);
}
}
SetupKeyLabels();
}
}
/// <summary>
/// Sets the default symbols as the ones currently active.
/// </summary>
public void SetDefaultSymbols()
{
if (_keyType == UxrKeyType.ToggleSymbols && _toggleSymbols != null && _toggleSymbols.Count > 0)
{
_currentSymbolsIndex = 0;
for (int i = 0; i < _toggleSymbols.Count; ++i)
{
_toggleSymbols[i].KeysRoot.SetActive(i == _currentSymbolsIndex);
}
SetupKeyLabels();
}
}
/// <summary>
/// Toggles to the next symbols.
/// </summary>
public void ToggleSymbols()
{
if (_keyType == UxrKeyType.ToggleSymbols && _toggleSymbols != null && _toggleSymbols.Count > 0)
{
_currentSymbolsIndex = (_currentSymbolsIndex + 1) % _toggleSymbols.Count;
for (int i = 0; i < _toggleSymbols.Count; ++i)
{
_toggleSymbols[i].KeysRoot.SetActive(i == _currentSymbolsIndex);
}
SetupKeyLabels();
}
}
#endregion
#region Unity
/// <summary>
/// Initializes the component.
/// </summary>
protected override void Awake()
{
base.Awake();
if (_keyboard == null)
{
_keyboard = GetComponentInParent<UxrKeyboardUI>();
}
if (_keyboard == null && !Application.isEditor)
{
if (UxrGlobalSettings.Instance.LogLevelUI >= UxrLogLevel.Warnings)
{
Debug.LogWarning($"{UxrConstants.UiModule} {nameof(UxrKeyboardUI)} component not found in parent hierarchy of key " + name);
}
}
ControlInput = GetComponent<UxrControlInput>();
if (ControlInput == null)
{
if (UxrGlobalSettings.Instance.LogLevelUI >= UxrLogLevel.Errors)
{
Debug.LogError($"{UxrConstants.UiModule} Keyboard key {name} has no control input");
}
}
SetupKeyLabels();
if (_keyboard && Application.isPlaying)
{
_keyboard.RegisterKey(this);
}
}
/// <summary>
/// Called when the component is destroyed.
/// </summary>
protected override void OnDestroy()
{
base.OnDestroy();
if (_keyboard && Application.isPlaying)
{
_keyboard.UnregisterKey(this);
}
}
#if UNITY_EDITOR
/// <summary>
/// Updates the labels and the GameObject's name in editor-mode depending on the labels that are set in the inspector.
/// </summary>
private void Update()
{
if (Application.isEditor)
{
if (!Application.isPlaying)
{
SetupKeyLabels();
}
if (_nameDirty)
{
UpdateName();
}
}
}
#endif
#endregion
#region Private Methods
/// <summary>
/// Sets up the labels on the key based on the current values in the inspector.
/// </summary>
private void SetupKeyLabels()
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "";
}
if (_multipleLayoutValueTopLeft)
{
_multipleLayoutValueTopLeft.text = "";
}
if (_multipleLayoutValueBottomLeft)
{
_multipleLayoutValueBottomLeft.text = "";
}
if (_multipleLayoutValueBottomRight)
{
_multipleLayoutValueBottomRight.text = "";
}
if (!string.IsNullOrEmpty(_forceLabel) && _singleLayoutValue)
{
_singleLayoutValue.text = _forceLabel;
return;
}
if (_keyType == UxrKeyType.Printable)
{
if (_layout == UxrKeyLayoutType.SingleChar)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = _printShift.Length > 0 && (_keyboard.ShiftEnabled || _keyboard.CapsLockEnabled) ? _printShift : _printNoShift;
}
}
else
{
if (_multipleLayoutValueTopLeft)
{
_multipleLayoutValueTopLeft.text = _printShift;
}
if (_multipleLayoutValueBottomLeft)
{
_multipleLayoutValueBottomLeft.text = _printNoShift;
}
if (_multipleLayoutValueBottomRight)
{
_multipleLayoutValueBottomRight.text = _printAltGr;
}
}
}
else if (_keyType == UxrKeyType.Tab)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Tab";
}
}
else if (_keyType == UxrKeyType.Shift)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Shift";
}
}
else if (_keyType == UxrKeyType.CapsLock)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Caps Lock";
}
}
else if (_keyType == UxrKeyType.Control)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Ctrl";
}
}
else if (_keyType == UxrKeyType.Alt)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Alt";
}
}
else if (_keyType == UxrKeyType.AltGr)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Alt Gr";
}
}
else if (_keyType == UxrKeyType.Enter)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Enter";
}
}
else if (_keyType == UxrKeyType.Backspace)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Backspace";
}
}
else if (_keyType == UxrKeyType.Del)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Del";
}
}
else if (_keyType == UxrKeyType.ToggleSymbols)
{
_singleLayoutValue.text = NextToggleSymbolsPage != null ? NextToggleSymbolsPage.Label :
CurrentToggleSymbolsPage != null ? CurrentToggleSymbolsPage.Label : string.Empty;
}
else if (_keyType == UxrKeyType.Escape)
{
if (_singleLayoutValue)
{
_singleLayoutValue.text = "Esc";
}
}
}
/// <summary>
/// Updates the GameObject name based on the labels set up in the inspector.
/// </summary>
private void UpdateName()
{
if (_keyType == UxrKeyType.Printable)
{
if (_singleLayoutValue && _layout == UxrKeyLayoutType.SingleChar)
{
if (_singleLayoutValue.text == " ")
{
name = "Key Space";
}
else
{
name = "Key " + _singleLayoutValue.text;
}
}
else
{
name = "Key" + (!string.IsNullOrEmpty(_printShift) ? " " + _printShift : "") + (!string.IsNullOrEmpty(_printNoShift) ? " " + _printNoShift : "") + (!string.IsNullOrEmpty(_printAltGr) ? " " + _printAltGr : "");
}
}
else if (_keyType == UxrKeyType.ToggleSymbols)
{
name = "Key Toggle Symbols";
}
else if (_keyType == UxrKeyType.ToggleViewPassword)
{
name = "Key Toggle View Password";
}
else if (_singleLayoutValue)
{
name = $"Key {_singleLayoutValue.text}";
}
_nameDirty = false;
}
#endregion
#region Private Types & Data
private UxrKeyboardUI _keyboard;
private int _currentSymbolsIndex;
#endregion
}
}
#pragma warning restore 0414

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 28d709e46a22d17489dcb6f84e149a2e
timeCreated: 1511538150
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,620 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrKeyboardUI.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using UltimateXR.Core.Components;
using UltimateXR.Extensions.System;
using UltimateXR.UI.UnityInputModule.Controls;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UltimateXR.UI.Helpers.Keyboard
{
/// <summary>
/// Component that handles a keyboard in VR for user input
/// </summary>
public class UxrKeyboardUI : UxrComponent
{
#region Inspector Properties/Serialized Fields
[SerializeField] private bool _multiline = true;
[SerializeField] private int _maxLineLength;
[SerializeField] private int _maxLineCount;
[SerializeField] private Text _consoleDisplay;
[SerializeField] private Text _currentLineDisplay;
[SerializeField] private bool _consoleDisplayUsesCursor = true;
[SerializeField] private bool _lineDisplayUsesCursor = true;
[SerializeField] private GameObject _capsLockEnabledObject;
[SerializeField] private bool _capsLockEnabled;
[SerializeField] private bool _previewCaps;
[SerializeField] private GameObject _passwordPreviewRootObject;
[SerializeField] private GameObject _passwordPreviewEnabledObject;
[SerializeField] private bool _isPassword;
[SerializeField] private bool _hidePassword = true;
#endregion
#region Public Types & Data
/// <summary>
/// Event called on key presses/releases.
/// </summary>
public event EventHandler<UxrKeyboardKeyEventArgs> KeyPressed;
/// <summary>
/// Event called on key presses/releases when the input is disabled using <see cref="AllowInput" />.
/// </summary>
public event EventHandler<UxrKeyboardKeyEventArgs> DisallowedKeyPressed;
/// <summary>
/// Event we can subscribe to if we want notifications whenever the current line
/// being typed in using the keyboard changed.
/// </summary>
public event EventHandler<string> CurrentLineChanged;
/// <summary>
/// Contains information about the key in our internal dictionary.
/// </summary>
public class KeyInfo
{
}
/// <summary>
/// Gets whether a shift key is being pressed.
/// </summary>
public bool ShiftEnabled => _shiftEnabled > 0;
/// <summary>
/// Gets whether a Control key is pressed.
/// </summary>
public bool ControlEnabled => _controlEnabled > 0;
/// <summary>
/// Gets the current console text content including the cursor.
/// </summary>
public string ConsoleContentWithCursor => ConsoleContent + CurrentCursor;
/// <summary>
/// Gets the current console line including the cursor.
/// </summary>
public string CurrentLineWithCursor => CurrentLine + CurrentCursor;
/// <summary>
/// Gets the current console cursor (can be empty or the cursor character as a string).
/// </summary>
public string CurrentCursor => AllowInput && Mathf.RoundToInt(Time.time * 1000) / 200 % 2 == 0 ? "_" : string.Empty;
/// <summary>
/// Gets whether caps lock is enabled.
/// </summary>
public bool CapsLockEnabled
{
get => _capsLockEnabled;
set => _capsLockEnabled = value;
}
/// <summary>
/// Gets whether the Alt key is pressed.
/// </summary>
public bool AltEnabled { get; private set; }
/// <summary>
/// Gets whether the Alt GR key is pressed.
/// </summary>
public bool AltGrEnabled { get; private set; }
/// <summary>
/// Gets the current console text content.
/// </summary>
public string ConsoleContent { get; private set; }
/// <summary>
/// Gets the current console line without the cursor.
/// </summary>
public string CurrentLine
{
get => _currentLine;
private set
{
if (value != _currentLine)
{
_currentLine = value;
OnCurrentLineChanged(value);
}
}
}
/// <summary>
/// Gets or sets whether keyboard input is allowed.
/// </summary>
public bool AllowInput { get; set; }
/// <summary>
/// Gets or sets whether the key labels casing changes when the shift of caps lock key is pressed.
/// </summary>
public bool PreviewCaps
{
get => _previewCaps;
set
{
_previewCaps = value;
UpdateLabelsCase();
}
}
/// <summary>
/// Gets or sets whether the keyboard is being used to type in a password. This can be used to hide the content behind
/// asterisk characters.
/// </summary>
public bool IsPassword
{
get => _isPassword;
set => _isPassword = value;
}
/// <summary>
/// Gets or sets whether to hide password characters when <see cref="IsPassword" /> is used.
/// </summary>
public bool HidePassword
{
get => _hidePassword;
set => _hidePassword = value;
}
#endregion
#region Public Methods
/// <summary>
/// Clears the console content.
/// </summary>
public void Clear()
{
_currentLineCount = 1;
ConsoleContent = string.Empty;
CurrentLine = string.Empty;
}
/// <summary>
/// If different symbols are present (through a ToggleSymbols keyboard key), sets the default symbols
/// as the currently enabled. Usually the default symbols are the regular alphabet letters.
/// </summary>
public void EnableDefaultSymbols()
{
if (_keyToggleSymbols != null)
{
_keyToggleSymbols.SetDefaultSymbols();
}
}
/// <summary>
/// Adds content to the console. This method should be used instead of the <see cref="ConsoleContent" /> property since
/// <see cref="ConsoleContent" /> will not process lines.
/// </summary>
/// <param name="newContent">Text content to append</param>
public void AddConsoleContent(string newContent)
{
if (string.IsNullOrEmpty(newContent))
{
return;
}
// Count the number of lines we are adding:
int newLineCount = newContent.GetOccurrenceCount("\n", false);
ConsoleContent += newContent;
_currentLineCount += newLineCount;
// Check if we exceeded the maximum line amount
CheckMaxLines();
}
/// <summary>
/// Called to register a new key in the keyboard.
/// </summary>
/// <param name="key">Key to register</param>
public void RegisterKey(UxrKeyboardKeyUI key)
{
Debug.Assert(key != null, "Keyboard key is null");
Debug.Assert(key.ControlInput != null, "Keyboard key's ControlInput is null");
if (!_keys.ContainsKey(key))
{
_keys.Add(key, new KeyInfo());
key.ControlInput.Pressed += KeyButton_KeyDown;
key.ControlInput.Released += KeyButton_KeyUp;
if (key.KeyType == UxrKeyType.ToggleSymbols)
{
_keyToggleSymbols = key;
}
}
}
/// <summary>
/// Called to unregister a key from the keyboard.
/// </summary>
/// <param name="key">Key to unregister</param>
public void UnregisterKey(UxrKeyboardKeyUI key)
{
Debug.Assert(key != null, "Keyboard key is null");
if (_keys.ContainsKey(key))
{
_keys.Remove(key);
key.ControlInput.Pressed -= KeyButton_KeyDown;
key.ControlInput.Released -= KeyButton_KeyUp;
if (key == _keyToggleSymbols)
{
_keyToggleSymbols = null;
}
}
}
#endregion
#region Unity
/// <summary>
/// Initializes the keyboard and clears the content.
/// </summary>
protected override void Awake()
{
base.Awake();
AllowInput = true;
Clear();
if (_previewCaps)
{
UpdateLabelsCase();
}
}
/// <summary>
/// If there is a console display Text component specified, it becomes updated with the content plus the cursor.
/// If there is a caps lock GameObject specified it is updated to reflect the caps lock state as well.
/// </summary>
private void Update()
{
if (_consoleDisplay != null)
{
_consoleDisplay.text = FormatStringOutput(_consoleDisplayUsesCursor ? ConsoleContentWithCursor : ConsoleContent, _consoleDisplayUsesCursor);
}
if (_currentLineDisplay != null)
{
_currentLineDisplay.text = FormatStringOutput(_lineDisplayUsesCursor ? CurrentLineWithCursor : CurrentLine, _consoleDisplayUsesCursor);
}
if (_capsLockEnabledObject != null)
{
_capsLockEnabledObject.SetActive(_capsLockEnabled);
}
if (_passwordPreviewRootObject != null)
{
_passwordPreviewRootObject.SetActive(IsPassword);
}
if (_passwordPreviewEnabledObject != null)
{
_passwordPreviewEnabledObject.SetActive(!_hidePassword && _isPassword);
}
}
#endregion
#region Event Handling Methods
/// <summary>
/// Called when a keyboard key was pressed.
/// </summary>
/// <param name="controlInput">The control that was pressed</param>
/// <param name="eventData">Event data</param>
private void KeyButton_KeyDown(UxrControlInput controlInput, PointerEventData eventData)
{
UxrKeyboardKeyUI key = controlInput.GetComponent<UxrKeyboardKeyUI>();
if (!AllowInput)
{
// Event notification
DisallowedKeyPressed?.Invoke(this, new UxrKeyboardKeyEventArgs(key, true, null));
return;
}
string lastLine = string.Empty;
if (key.KeyType == UxrKeyType.Printable)
{
if (!(_maxLineLength > 0 && CurrentLine.Length >= _maxLineLength))
{
if (key.KeyLayoutType == UxrKeyLayoutType.SingleChar)
{
if (!string.IsNullOrEmpty(key.ForceLabel))
{
ConsoleContent += key.GetSingleLayoutValueNoForceLabel(_capsLockEnabled || _shiftEnabled > 0, AltGrEnabled);
CurrentLine += key.GetSingleLayoutValueNoForceLabel(_capsLockEnabled || _shiftEnabled > 0, AltGrEnabled);
}
else
{
if (char.IsLetter(key.SingleLayoutValue))
{
char newCar = _capsLockEnabled || _shiftEnabled > 0 ? char.ToUpper(key.SingleLayoutValue) : char.ToLower(key.SingleLayoutValue);
ConsoleContent += newCar;
CurrentLine += newCar;
}
else
{
char newCar = key.GetSingleLayoutValueNoForceLabel(_shiftEnabled > 0 || _capsLockEnabled, AltGrEnabled);
ConsoleContent += newCar;
CurrentLine += newCar;
}
}
}
else if (key.KeyLayoutType == UxrKeyLayoutType.MultipleChar)
{
if (_shiftEnabled > 0)
{
ConsoleContent += key.MultipleLayoutValueTopLeft;
CurrentLine += key.MultipleLayoutValueTopLeft;
}
else if (AltGrEnabled)
{
if (key.HasMultipleLayoutValueBottomRight)
{
ConsoleContent += key.MultipleLayoutValueBottomRight;
CurrentLine += key.MultipleLayoutValueBottomRight;
}
}
else
{
ConsoleContent += key.MultipleLayoutValueBottomLeft;
CurrentLine += key.MultipleLayoutValueBottomLeft;
}
}
}
}
else if (key.KeyType == UxrKeyType.Tab)
{
string tab = " ";
int charsAddedCount = _maxLineLength > 0 ? CurrentLine.Length + tab.Length > _maxLineLength ? _maxLineLength - CurrentLine.Length : tab.Length : tab.Length;
ConsoleContent += tab.Substring(0, charsAddedCount);
CurrentLine += tab.Substring(0, charsAddedCount);
}
else if (key.KeyType == UxrKeyType.Shift)
{
_shiftEnabled++;
if (_previewCaps)
{
UpdateLabelsCase();
}
}
else if (key.KeyType == UxrKeyType.CapsLock)
{
_capsLockEnabled = !_capsLockEnabled;
if (_previewCaps)
{
UpdateLabelsCase();
}
}
else if (key.KeyType == UxrKeyType.Control)
{
_controlEnabled++;
}
else if (key.KeyType == UxrKeyType.Alt)
{
AltEnabled = true;
}
else if (key.KeyType == UxrKeyType.AltGr)
{
AltGrEnabled = true;
}
else if (key.KeyType == UxrKeyType.Enter)
{
#if !UNITY_WSA
lastLine = string.Copy(CurrentLine);
#else
lastLine = string.Empty + CurrentLine;
#endif
if (_multiline)
{
ConsoleContent += "\n";
CurrentLine = string.Empty;
_currentLineCount++;
CheckMaxLines();
}
}
else if (key.KeyType == UxrKeyType.Backspace)
{
if (CurrentLine.Length > 0)
{
ConsoleContent = ConsoleContent.Substring(0, ConsoleContent.Length - 1);
CurrentLine = CurrentLine.Substring(0, CurrentLine.Length - 1);
}
}
else if (key.KeyType == UxrKeyType.Del)
{
}
else if (key.KeyType == UxrKeyType.ToggleSymbols)
{
key.ToggleSymbols();
}
else if (key.KeyType == UxrKeyType.ToggleViewPassword)
{
_hidePassword = !_hidePassword;
}
else if (key.KeyType == UxrKeyType.Escape)
{
}
// Event notification
KeyPressed?.Invoke(this, new UxrKeyboardKeyEventArgs(key, true, key.KeyType == UxrKeyType.Enter ? lastLine : CurrentLine));
}
/// <summary>
/// Called when a keyboard keypress was released.
/// </summary>
/// <param name="controlInput">The control that was released</param>
/// <param name="eventData">Event data</param>
private void KeyButton_KeyUp(UxrControlInput controlInput, PointerEventData eventData)
{
UxrKeyboardKeyUI key = controlInput.GetComponent<UxrKeyboardKeyUI>();
if (!AllowInput)
{
// Event notification
DisallowedKeyPressed?.Invoke(this, new UxrKeyboardKeyEventArgs(key, false, null));
return;
}
if (key.KeyType == UxrKeyType.Printable)
{
}
else if (key.KeyType == UxrKeyType.Tab)
{
}
else if (key.KeyType == UxrKeyType.Shift)
{
_shiftEnabled--;
}
else if (key.KeyType == UxrKeyType.CapsLock)
{
}
else if (key.KeyType == UxrKeyType.Control)
{
_controlEnabled--;
}
else if (key.KeyType == UxrKeyType.Alt)
{
AltEnabled = false;
}
else if (key.KeyType == UxrKeyType.AltGr)
{
AltGrEnabled = false;
}
else if (key.KeyType == UxrKeyType.Enter)
{
}
else if (key.KeyType == UxrKeyType.Backspace)
{
}
else if (key.KeyType == UxrKeyType.Del)
{
}
else if (key.KeyType == UxrKeyType.Escape)
{
}
// Event notification
KeyPressed?.Invoke(this, new UxrKeyboardKeyEventArgs(key, false, CurrentLine));
}
#endregion
#region Event Trigger Methods
/// <summary>
/// Event trigger for the <see cref="CurrentLineChanged" /> event.
/// </summary>
/// <param name="value">New line value</param>
protected virtual void OnCurrentLineChanged(string value)
{
CurrentLineChanged?.Invoke(this, value);
}
#endregion
#region Private Methods
/// <summary>
/// Formats the given string to show it to the user. This is mainly used to make sure that passwords are hidden behind
/// asterisk characters.
/// </summary>
/// <param name="content">Content to format using the current settings</param>
/// <param name="isUsingCursor">
/// Tells whether content is a string that may have a cursor appended
/// </param>
/// <returns>Formatted string ready to show to the user</returns>
private string FormatStringOutput(string content, bool isUsingCursor)
{
if (string.IsNullOrEmpty(content))
{
return string.Empty;
}
return _isPassword && _hidePassword ? new string('*', content.Length - CurrentCursor.Length) + CurrentCursor : content;
}
/// <summary>
/// Checks if the maximum number of lines was reached in the console and if so removes lines from the beginning.
/// </summary>
private void CheckMaxLines()
{
if (_maxLineCount > 0 && _currentLineCount > _maxLineCount)
{
int linesCounted = 0;
for (int i = 0; i < ConsoleContent.Length; ++i)
{
if (ConsoleContent[i] == '\n')
{
linesCounted++;
if (linesCounted == _currentLineCount - _maxLineCount)
{
ConsoleContent = ConsoleContent.Remove(0, i + 1);
_currentLineCount -= linesCounted;
break;
}
}
}
}
}
/// <summary>
/// Updates uppercase/lowercase labels depending on the shift and caps lock state.
/// </summary>
private void UpdateLabelsCase()
{
if (_keys == null)
{
return;
}
foreach (KeyValuePair<UxrKeyboardKeyUI, KeyInfo> keyPair in _keys)
{
if (keyPair.Key.IsLetterKey)
{
keyPair.Key.UpdateLetterKeyLabel(ShiftEnabled || CapsLockEnabled);
}
}
}
#endregion
#region Private Types & Data
private readonly Dictionary<UxrKeyboardKeyUI, KeyInfo> _keys = new Dictionary<UxrKeyboardKeyUI, KeyInfo>();
private string _currentLine;
private int _currentLineCount;
private int _shiftEnabled;
private int _controlEnabled;
private UxrKeyboardKeyUI _keyToggleSymbols;
#endregion
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 85e65ed8ff0832245869f1b6941e9eb1
timeCreated: 1511538177
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,53 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrToggleSymbolsPage.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using UnityEngine;
namespace UltimateXR.UI.Helpers.Keyboard
{
/// <summary>
/// Symbols page for keyboard symbols. A keyboard may have multiple symbol pages.
/// </summary>
[Serializable]
public class UxrToggleSymbolsPage
{
#region Inspector Properties/Serialized Fields
[SerializeField] private GameObject _keysRoot;
[SerializeField] private string _label;
#endregion
#region Public Types & Data
/// <summary>
/// Gets the root <see cref="GameObject" /> where all the keys in the page hang from.
/// </summary>
public GameObject KeysRoot => _keysRoot;
/// <summary>
/// Gets the label that describes the symbols in the page.
/// </summary>
public string Label => _label;
#endregion
#region Constructors & Finalizer
/// <summary>
/// Constructor.
/// </summary>
/// <param name="keysRoot">Root where are symbols in the page hang from</param>
/// <param name="label">Label that describes the symbols in the page</param>
public UxrToggleSymbolsPage(GameObject keysRoot, string label)
{
_keysRoot = keysRoot;
_label = label;
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e13b8d6b491b4f19b22ae91cc599f1c5
timeCreated: 1643213310