128 lines
3.0 KiB
C#
128 lines
3.0 KiB
C#
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class QrTextInput : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField] private List<QrTextSlot> slots;
|
|
[SerializeField] private Image frame;
|
|
[TextArea][SerializeField] private string alphabet = "1";
|
|
|
|
private QrTextSlot CurrentSlot => slots[index];
|
|
|
|
private int index = 0;
|
|
private float LastSwipeLetter = 0;
|
|
|
|
[SerializeField] float minInterval = 0.07f;
|
|
[SerializeField] float maxInterval = 0.3f;
|
|
[SerializeField] float acceleration = 3f;
|
|
[SerializeField] private float interval;
|
|
[SerializeField] AudioClip ClickClip;
|
|
|
|
|
|
private bool CanSwipe => Time.time - LastSwipeLetter > interval;
|
|
|
|
private void OnEnable()
|
|
{
|
|
index = 0;
|
|
interval = maxInterval;
|
|
}
|
|
|
|
public void SwitchSlot(int direction)
|
|
{
|
|
if(direction == 0) {return;}
|
|
|
|
CurrentSlot.SetActive(false);
|
|
index = (index + direction + slots.Count) % slots.Count;
|
|
CurrentSlot.SetActive(true);
|
|
ClickSound();
|
|
}
|
|
|
|
public void DisableAllSlots()
|
|
{
|
|
foreach(QrTextSlot slot in slots) { slot.SetActive(false); }
|
|
}
|
|
public void EnableFirstSlot()
|
|
{
|
|
index = 0;
|
|
CurrentSlot.SetActive(true);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ClickSound()
|
|
{
|
|
if (ClickClip != null) SoundManager.instance.playClip(ClickClip);
|
|
}
|
|
public void SwitchLetter(int direction)
|
|
{
|
|
if (direction == 0) {return;}
|
|
if (!CanSwipe) { return; }
|
|
|
|
LastSwipeLetter = Time.time;
|
|
interval-= acceleration * Time.deltaTime;
|
|
interval = Mathf.Clamp(interval, minInterval, maxInterval);
|
|
|
|
ClickSound();
|
|
|
|
int currentLetterIndex = alphabet.IndexOf(CurrentSlot.GetText());
|
|
if (currentLetterIndex != -1)
|
|
{
|
|
currentLetterIndex = (currentLetterIndex + direction + alphabet.Length) % alphabet.Length;
|
|
CurrentSlot.SetText(alphabet[currentLetterIndex].ToString());
|
|
}
|
|
else
|
|
{
|
|
currentLetterIndex = 0;
|
|
CurrentSlot.SetText(alphabet[0].ToString());
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
bool left = In.GetButton(ButtonPinMapping.left1);
|
|
bool right = In.GetButton(ButtonPinMapping.right1);
|
|
|
|
if (!(left || right))
|
|
{
|
|
interval = maxInterval;
|
|
LastSwipeLetter = 0;
|
|
}
|
|
}
|
|
public string GetText()
|
|
{
|
|
var sb = new StringBuilder();
|
|
foreach (var slot in slots)
|
|
sb.Append(slot.GetText());
|
|
|
|
return sb.ToString().Trim();
|
|
}
|
|
|
|
public void SetText(string text)
|
|
{
|
|
if(text.Length > slots.Count)
|
|
{
|
|
text = text.Substring(0, slots.Count - 1);
|
|
}
|
|
|
|
int min = Mathf.Min(text.Length, slots.Count);
|
|
for (int i = 0; i < min; i++)
|
|
{
|
|
slots[i].SetText(text[i].ToString());
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public void Light(Color color)
|
|
{
|
|
frame.color = color;
|
|
}
|
|
|
|
|
|
}
|