175 lines
6.4 KiB
C#
175 lines
6.4 KiB
C#
#nullable enable
|
|
using Cysharp.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UVC.Core;
|
|
using UVC.Studio.Config;
|
|
using UVC.UI.Tab;
|
|
|
|
namespace UVC.Studio.Modal.Settings
|
|
{
|
|
public class SettingShortcutTabContent : MonoBehaviour, ITabContent
|
|
{
|
|
|
|
[SerializeField]
|
|
private LayoutGroup? labelGroup;
|
|
|
|
[SerializeField]
|
|
private LayoutGroup? valueGroup;
|
|
|
|
[SerializeField]
|
|
private TextMeshProUGUI? firstLabelTxt;
|
|
|
|
|
|
[SerializeField]
|
|
private TMP_InputField? firstValueTxt;
|
|
|
|
[Inject]
|
|
private Setting? setting;
|
|
|
|
private List<TextMeshProUGUI> labelTxts = new List<TextMeshProUGUI>();
|
|
private List<TMP_InputField> valueTxts = new List<TMP_InputField>();
|
|
|
|
private bool changedValue = false;
|
|
|
|
/// <summary>
|
|
/// 탭 콘텐츠에 데이터를 전달합니다.
|
|
/// </summary>
|
|
/// <param name="data">전달할 데이터 객체</param>
|
|
public async void SetContentData(object? data)
|
|
{
|
|
Debug.Log($"SettingShortcutTabContent SetContentData: {data}");
|
|
|
|
if (setting == null)
|
|
{
|
|
await InjectorAppContext.Instance.WaitForInitializationAsync();
|
|
setting = InjectorAppContext.Instance.Get<Setting>();
|
|
}
|
|
|
|
labelTxts.Clear();
|
|
valueTxts.Clear();
|
|
|
|
if (setting != null && labelGroup != null && valueGroup != null)
|
|
{
|
|
changedValue = false;
|
|
|
|
ShortcutsSetting shortcuts = setting.Data.shortcuts;
|
|
|
|
// menu와 tools의 모든 ShortcutItem 필드를 순회
|
|
var shortcutGroups = new object[] { shortcuts.menu, shortcuts.tools };
|
|
|
|
foreach (var group in shortcutGroups)
|
|
{
|
|
var fields = group.GetType().GetFields();
|
|
|
|
for (int i = 0; i < fields.Length; i++)
|
|
{
|
|
var field = fields[i];
|
|
if (field.FieldType == typeof(ShortcutItem))
|
|
{
|
|
var shortcut = (ShortcutItem?)field.GetValue(group);
|
|
if (shortcut == null) continue;
|
|
if (i == 0)
|
|
{
|
|
firstLabelTxt!.text = shortcut.label;
|
|
firstValueTxt!.text = shortcut.key;
|
|
labelTxts.Add(firstLabelTxt);
|
|
valueTxts.Add(firstValueTxt);
|
|
|
|
// 영문 대문자만 입력되도록 필터링
|
|
firstValueTxt.onValueChanged.AddListener((value) =>
|
|
{
|
|
firstValueTxt.text = value.ToUpper();
|
|
});
|
|
|
|
// 값 변경 리스너 추가
|
|
firstValueTxt.onEndEdit.AddListener((value) =>
|
|
{
|
|
shortcut.key = value;
|
|
changedValue = true;
|
|
});
|
|
|
|
}
|
|
else
|
|
{
|
|
// 라벨 복제
|
|
if (firstLabelTxt != null)
|
|
{
|
|
TextMeshProUGUI labelInstance = Instantiate(firstLabelTxt, labelGroup.transform);
|
|
labelInstance.text = shortcut.label;
|
|
labelTxts.Add(labelInstance);
|
|
}
|
|
|
|
// 값 복제
|
|
if (firstValueTxt != null)
|
|
{
|
|
TMP_InputField valueInstance = Instantiate(firstValueTxt, valueGroup.transform);
|
|
valueInstance.text = shortcut.key;
|
|
valueTxts.Add(valueInstance);
|
|
|
|
// 영문 대문자만 입력되도록 필터링
|
|
valueInstance.onValueChanged.AddListener((value) =>
|
|
{
|
|
valueInstance.text = value.ToUpper();
|
|
});
|
|
|
|
valueInstance.onEndEdit.AddListener((value) =>
|
|
{
|
|
shortcut.key = value;
|
|
changedValue = true;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 탭 전환 시 데이터가 있는 경우 전달 되는 데이터. SetContentData 이후 호출 됨
|
|
/// </summary>
|
|
/// <param name="data">전달할 데이터 객체</param>
|
|
public void UpdateContentData(object? data)
|
|
{
|
|
if (data != null && data is string content)
|
|
{
|
|
Debug.Log($"UpdateContentData: {content}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 닫힐 때 실행되는 로직을 처리합니다.
|
|
/// </summary>
|
|
/// <returns>비동기 닫기 작업을 나타내는 <see cref="UniTask"/>입니다.</returns>
|
|
public async UniTask OnCloseAsync()
|
|
{
|
|
foreach (var labelTxt in labelTxts)
|
|
{
|
|
if(labelTxt != firstLabelTxt)
|
|
{
|
|
Destroy(labelTxt.gameObject);
|
|
}
|
|
}
|
|
|
|
foreach (var valueTxt in valueTxts)
|
|
{
|
|
valueTxt.onValueChanged.RemoveAllListeners();
|
|
valueTxt.onEndEdit.RemoveAllListeners();
|
|
if(valueTxt != firstValueTxt)
|
|
{
|
|
Destroy(valueTxt.gameObject);
|
|
}
|
|
}
|
|
|
|
Debug.Log($"SettingShortcutTabContent OnCloseAsync: changedValue={changedValue} setting == null:{setting == null}");
|
|
if (changedValue && setting != null)
|
|
{
|
|
await setting.SaveAsync();
|
|
Debug.Log("Shortcut settings saved.");
|
|
}
|
|
}
|
|
}
|
|
} |