82 lines
2.2 KiB
C#
82 lines
2.2 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream.UI
|
|
{
|
|
public class TerminalOptionItemView : MonoBehaviour
|
|
{
|
|
[SerializeField] private TMP_Text labelText;
|
|
[SerializeField] private TMP_Text valueText;
|
|
[SerializeField] private Button previousButton;
|
|
[SerializeField] private Button nextButton;
|
|
|
|
private string _propName;
|
|
private UIFormOption[] _options = Array.Empty<UIFormOption>();
|
|
private int _currentIndex;
|
|
private Action<string, string> _onValueChanged;
|
|
|
|
private void Awake()
|
|
{
|
|
previousButton?.onClick.AddListener(Previous);
|
|
nextButton?.onClick.AddListener(Next);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
previousButton?.onClick.RemoveListener(Previous);
|
|
nextButton?.onClick.RemoveListener(Next);
|
|
}
|
|
|
|
public void Init(string propName, string label, UIFormOption[] options, Action<string, string> onValueChanged)
|
|
{
|
|
_propName = propName;
|
|
_options = options ?? Array.Empty<UIFormOption>();
|
|
_onValueChanged = onValueChanged;
|
|
|
|
if (labelText != null)
|
|
{
|
|
labelText.text = label;
|
|
}
|
|
}
|
|
|
|
public void SetValue(string value)
|
|
{
|
|
var index = Array.FindIndex(_options, option => option.prop == value);
|
|
_currentIndex = Mathf.Clamp(index < 0 ? 0 : index, 0, Mathf.Max(0, _options.Length - 1));
|
|
RefreshValue();
|
|
}
|
|
|
|
private void Previous()
|
|
{
|
|
Step(-1);
|
|
}
|
|
|
|
private void Next()
|
|
{
|
|
Step(1);
|
|
}
|
|
|
|
private void Step(int offset)
|
|
{
|
|
if (_options.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_currentIndex = (_currentIndex + offset + _options.Length) % _options.Length;
|
|
RefreshValue();
|
|
_onValueChanged?.Invoke(_propName, _options[_currentIndex].prop);
|
|
}
|
|
|
|
private void RefreshValue()
|
|
{
|
|
if (valueText != null)
|
|
{
|
|
valueText.text = _options.Length == 0 ? string.Empty : $"<{_options[_currentIndex].label}>";
|
|
}
|
|
}
|
|
}
|
|
}
|