139 lines
5.2 KiB
C#
139 lines
5.2 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Octopus.Simulator
|
|
{
|
|
public class Panel_LogicData : MonoBehaviour
|
|
{
|
|
List<GameObject> infoItems = new List<GameObject>();
|
|
[SerializeField]
|
|
LogicDataInfoItem logicItem;
|
|
RectTransform rect;
|
|
[SerializeField]
|
|
RectTransform contentRect;
|
|
|
|
float contentSpacing = 5f;
|
|
float padding = 25f;
|
|
public event Action onPanelUpdated;
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
private void Awake()
|
|
{
|
|
rect = GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
var logicUIManager = FindAnyObjectByType<LogicUIManager>();
|
|
logicUIManager.onLogicItemSelected += SetLogicDataItem;
|
|
logicUIManager.onModelSelected += SetLogicDataItem;
|
|
logicUIManager.onLogicItemDeSelected += UnsetLogicDataItem;
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
void ClearItem()
|
|
{
|
|
foreach (var infoItem in infoItems)
|
|
{
|
|
Destroy(infoItem.gameObject);
|
|
}
|
|
infoItems.Clear();
|
|
}
|
|
|
|
public void SetLogicDataItem(ILogicItem item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
Debug.Log(null);
|
|
gameObject.SetActive(false);
|
|
return;
|
|
}
|
|
gameObject.SetActive(true);
|
|
ClearItem();
|
|
|
|
Type type = item.GetType();
|
|
switch (item.ItemType)
|
|
{
|
|
case LogicItemType.Queue:
|
|
type = (item as LogicQueue).GetType();
|
|
break;
|
|
case LogicItemType.Resource:
|
|
type = (item as LogicResource).GetType();
|
|
break;
|
|
case LogicItemType.Component:
|
|
type = (item as ILogicComponent).GetType();
|
|
break;
|
|
}
|
|
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
|
|
float itemHeight = logicItem.GetComponent<RectTransform>().rect.height+ contentSpacing;
|
|
for (int i = 0; i < fields.Length; i++)
|
|
{
|
|
var rawValue = fields[i].GetValue(item);
|
|
int count = 1;
|
|
switch (rawValue)
|
|
{
|
|
case List<InputQueue> queueList:
|
|
var listItem = CreateLogicListItem(fields[i].Name);
|
|
foreach (var queueItem in queueList)
|
|
{
|
|
listItem.AddItem(count.ToString(),queueItem.queue_name,queueItem.required_items.ToString());
|
|
count++;
|
|
}
|
|
SetRectHeight(fields.Length, itemHeight);
|
|
break;
|
|
|
|
case List<InputStore> storeList:
|
|
var storeLisItem = CreateLogicListItem(fields[i].Name);
|
|
foreach (var storeItem in storeList)
|
|
{
|
|
storeLisItem.AddItem(count.ToString(), storeItem.Queue, storeItem.RequiredItems.ToString());
|
|
count++;
|
|
}
|
|
SetRectHeight(fields.Length, itemHeight);
|
|
break;
|
|
default:
|
|
CreateLogicItem(fields[i].Name, rawValue, itemHeight, i);
|
|
SetRectHeight(fields.Length, itemHeight);
|
|
break;
|
|
}
|
|
}
|
|
onPanelUpdated?.Invoke();
|
|
}
|
|
|
|
private LogicListDataInfoItem CreateLogicListItem(string fieldName)
|
|
{
|
|
var asset = Resources.Load<LogicListDataInfoItem>("UIPrefab/LogicListDataInfoItem");
|
|
var item = Instantiate<LogicListDataInfoItem>(asset, rect);
|
|
item.Set(fieldName);
|
|
infoItems.Add(item.gameObject);
|
|
return item;
|
|
}
|
|
private void CreateLogicItem(string fieldName, object rawValue, float itemHeight,int index)
|
|
{
|
|
var value = rawValue?.ToString() ?? "";
|
|
var iLogicItem = Instantiate(logicItem, rect);
|
|
iLogicItem.GetComponent<RectTransform>().anchoredPosition = new Vector2(iLogicItem.GetComponent<RectTransform>().anchoredPosition.x, -itemHeight * index);
|
|
iLogicItem.Set(fieldName, value);
|
|
infoItems.Add(iLogicItem.gameObject);
|
|
}
|
|
void SetRectHeight(int count,float itemHeight)
|
|
{
|
|
var height = 0f;
|
|
foreach (var item in infoItems)
|
|
{
|
|
var rect = item.GetComponent<RectTransform>();
|
|
height += rect.sizeDelta.y + contentSpacing*2;
|
|
}
|
|
rect.sizeDelta = new Vector2(rect.sizeDelta.x, height);
|
|
//float panelHeight = count * itemHeight+contentSpacing*2+ padding;
|
|
//rect.sizeDelta = new Vector2(rect.sizeDelta.x, panelHeight);
|
|
}
|
|
|
|
public void UnsetLogicDataItem()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
} |