251 lines
7.9 KiB
C#
251 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Samkwang
|
|
{
|
|
public class Panel_MachineDataDashboard : UIPopupBase
|
|
{
|
|
public TMP_Text Text_MachineName;
|
|
public ScrollRect ScrollView_DataInfo;
|
|
public RectTransform Image_Loading;
|
|
public Button Button_Close;
|
|
|
|
public UI_MachineDataInfoItem prf_DataInfoItem;
|
|
|
|
private FieldInfo[] machineDataFields;
|
|
private Dictionary<string, UI_MachineDataInfoItem> dataInfoItems = new();
|
|
|
|
public void Init()
|
|
{
|
|
var textDict = transform.GetChildComponentsByName<TextMeshProUGUI>();
|
|
var scrollDict = transform.GetChildComponentsByName<ScrollRect>();
|
|
var buttonDict = transform.GetChildComponentsByName<Button>();
|
|
var rectDict = transform.GetChildComponentsByName<RectTransform>();
|
|
|
|
Text_MachineName = textDict.GetOrNull(nameof(Text_MachineName));
|
|
ScrollView_DataInfo = scrollDict.GetOrNull(nameof(ScrollView_DataInfo));
|
|
Image_Loading = rectDict.GetOrNull(nameof(Image_Loading));
|
|
Button_Close = buttonDict.GetOrNull(nameof(Button_Close));
|
|
|
|
prf_DataInfoItem = Resources.Load<UI_MachineDataInfoItem>("Prefabs/UI/PRF_UI_MachineDataInfoItem");
|
|
|
|
Button_Close.onClick.AddListener(Close);
|
|
machineDataFields = typeof(EquipmentInfo).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
|
Image_Loading.gameObject.SetActive(true);
|
|
}
|
|
public override void Open()
|
|
{
|
|
gameObject.SetActive(true);
|
|
}
|
|
public override void Close()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
public void SetRealtimeDataA(string machineName, RealtimeInfo_A info)
|
|
{
|
|
// 데이터가 없으면 로딩 표시
|
|
if (info == null)
|
|
{
|
|
Text_MachineName.SetText(machineName);
|
|
ResetList(); // 기존 목록 지우기
|
|
Image_Loading.gameObject.SetActive(true);
|
|
return;
|
|
}
|
|
|
|
// 필드 정보 가져오기 (Reflection)
|
|
var fields = typeof(RealtimeInfo_A).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
// 기계 이름이 바뀌었거나 리스트가 비어있으면 새로 생성
|
|
if (Text_MachineName.text != machineName || dataInfoItems.Count == 0)
|
|
{
|
|
Text_MachineName.SetText(machineName);
|
|
ResetList(); // 기존 아이템 반환
|
|
|
|
CreateMachineDataInfo(fields, info);
|
|
}
|
|
else
|
|
{
|
|
// 이름이 같으면 값만 업데이트
|
|
UpdateMachineDataInfo(fields, info);
|
|
}
|
|
|
|
Image_Loading.gameObject.SetActive(false);
|
|
}
|
|
|
|
public void SetRealtimeDataB(string machineName, RealtimeInfo_B info)
|
|
{
|
|
if (info == null)
|
|
{
|
|
Text_MachineName.SetText(machineName);
|
|
ResetList();
|
|
Image_Loading.gameObject.SetActive(true);
|
|
return;
|
|
}
|
|
|
|
var fields = typeof(RealtimeInfo_B).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
if (Text_MachineName.text != machineName || dataInfoItems.Count == 0)
|
|
{
|
|
Text_MachineName.SetText(machineName);
|
|
ResetList();
|
|
|
|
CreateMachineDataInfo(fields, info);
|
|
}
|
|
else
|
|
{
|
|
UpdateMachineDataInfo(fields, info);
|
|
}
|
|
|
|
Image_Loading.gameObject.SetActive(false);
|
|
}
|
|
private void CreateMachineDataInfo(FieldInfo[] fields, object data)
|
|
{
|
|
foreach (var field in fields)
|
|
{
|
|
// UI 아이템 풀에서 가져오기
|
|
var item = GetItem();
|
|
|
|
// 필드 이름과 값 가져오기
|
|
string fieldName = field.Name;
|
|
object val = field.GetValue(data);
|
|
string fieldValue = val != null ? val.ToString() : "";
|
|
|
|
item.Setting(fieldName, fieldValue);
|
|
|
|
// 딕셔너리에 저장 (업데이트를 위해)
|
|
dataInfoItems.Add(fieldName, item);
|
|
}
|
|
}
|
|
|
|
private void UpdateMachineDataInfo(FieldInfo[] fields, object data)
|
|
{
|
|
foreach (var field in fields)
|
|
{
|
|
string fieldName = field.Name;
|
|
|
|
// 기존에 생성된 아이템이 있는지 확인
|
|
if (!dataInfoItems.ContainsKey(fieldName))
|
|
continue;
|
|
|
|
object val = field.GetValue(data);
|
|
string fieldValue = val != null ? val.ToString() : "";
|
|
|
|
// 값 갱신
|
|
dataInfoItems[fieldName].Setting(fieldName, fieldValue);
|
|
}
|
|
}
|
|
|
|
// 목록 초기화 (아이템을 풀로 반환)
|
|
private void ResetList()
|
|
{
|
|
foreach (var item in dataInfoItems.Values)
|
|
{
|
|
ReturnItem(item);
|
|
}
|
|
dataInfoItems.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 베트남 설비용
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <param name="infos"></param>
|
|
public void SetDataDashobardInfo(string code, List<EquipmentInfo> infos)
|
|
{
|
|
if (!code.Contains("WC")) return; // 방어 코드
|
|
|
|
if (Text_MachineName.text == code)
|
|
{
|
|
for (int i = 0; i < infos.Count; i++)
|
|
{
|
|
UpdateMachineDataInfoList(machineDataFields, infos[i], i);
|
|
}
|
|
Image_Loading.gameObject.SetActive(infos.Count() <= 0);
|
|
}
|
|
else
|
|
{
|
|
Text_MachineName.SetText(code);
|
|
|
|
Image_Loading.gameObject.SetActive(false);
|
|
|
|
dataInfoItems.Clear();
|
|
|
|
// 새로 생성 대신 풀에서 꺼내오기
|
|
for (int i = 0; i < infos.Count; i++)
|
|
{
|
|
CreateMachineDataInfoList(machineDataFields, infos[i], i);
|
|
}
|
|
|
|
Image_Loading.gameObject.SetActive(infos.Count() <= 0);
|
|
}
|
|
}
|
|
|
|
private void CreateMachineDataInfoList(FieldInfo[] fields, object data, int index)
|
|
{
|
|
foreach (var field in fields)
|
|
{
|
|
var item = GetItem();
|
|
|
|
var info = (string)field.GetValue(data);
|
|
item.Setting(field.Name, info);
|
|
|
|
dataInfoItems.Add($"{field}_{index}", item);
|
|
}
|
|
}
|
|
|
|
private void UpdateMachineDataInfoList(FieldInfo[] fields, object data, int index)
|
|
{
|
|
foreach (var field in fields)
|
|
{
|
|
var info = (string)field.GetValue(data);
|
|
|
|
if (!dataInfoItems.ContainsKey($"{field}_{index}"))
|
|
continue;
|
|
|
|
dataInfoItems[$"{field}_{index}"].Setting(field.Name, info);
|
|
}
|
|
}
|
|
|
|
private Queue<UI_MachineDataInfoItem> pool = new Queue<UI_MachineDataInfoItem>();
|
|
|
|
public UI_MachineDataInfoItem GetItem()
|
|
{
|
|
UI_MachineDataInfoItem item;
|
|
if (pool.Count > 0)
|
|
{
|
|
item = pool.Dequeue();
|
|
item.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
item = Instantiate(prf_DataInfoItem, ScrollView_DataInfo.content);
|
|
}
|
|
|
|
item.transform.SetAsLastSibling();
|
|
item.Init();
|
|
return item;
|
|
}
|
|
|
|
public void ReturnItem(UI_MachineDataInfoItem item)
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
pool.Enqueue(item);
|
|
}
|
|
|
|
public void ClearPool()
|
|
{
|
|
foreach (var item in pool)
|
|
{
|
|
Destroy(item.gameObject);
|
|
}
|
|
pool.Clear();
|
|
}
|
|
}
|
|
}
|
|
|