Files
aibis-dream/Assets/Scripts/UI/Form/SelectItem.cs
T
2025-04-24 16:55:00 +08:00

70 lines
1.9 KiB
C#

using System;
using AibisDream.Utility;
using UnityEngine;
using UnityEngine.Localization.Components;
using UnityEngine.UI;
namespace AibisDream
{
public class SelectItem : AbsFormItem
{
public override FieldType FieldType => FieldType.Select;
#region 索引
private LocalizeStringEvent _showText;
private Button _leftBtn;
private Button _rightBtn;
#endregion
#region 数据
[Header("可选项")]
public UIFormOption[] options;
private int _curIdx;
#endregion
protected override void InitField()
{
var field = transform.Find("Field");
_showText = field.Find("ShowText").GetComponent<LocalizeStringEvent>();
_leftBtn = field.Find("Left Btn").GetComponent<Button>();
_rightBtn = field.Find("Right Btn").GetComponent<Button>();
_leftBtn.onClick.AddListener(LeftRoll);
_rightBtn.onClick.AddListener(RightRoll);
}
public override void SetValue(string value)
{
var idx = Array.FindIndex(options, item => item.prop == value);
if (idx < 0)
{
Debug.LogError($"{prop} setting value error");
}
_curIdx = idx;
_showText.SetEntry(options[idx].label);
}
private void LeftRoll()
{
_curIdx = CommonUtil.LoopStep(_curIdx, 0, options.Length - 1);
_showText.SetEntry(options[_curIdx].label);
OnValueChanged?.Invoke(prop, options[_curIdx].prop);
}
private void RightRoll()
{
_curIdx = CommonUtil.LoopStep(_curIdx, 0, options.Length - 1, -1);
_showText.SetEntry(options[_curIdx].label);
OnValueChanged?.Invoke(prop, options[_curIdx].prop);
}
}
}