Files
Simulation/Assets/WorkSpace/LH/LogicData/Panel_ConnectedObject.cs
2025-06-05 18:27:42 +09:00

120 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Octopus.Simulator
{
public class Panel_ConnectedObject : MonoBehaviour
{
List<LogicDataInfoItem> infoItems = new List<LogicDataInfoItem>();
[SerializeField]
LogicDataInfoItem logicItem;
RectTransform rect;
[SerializeField]
RectTransform contentRect;
Dictionary<string, string> dataMap = new Dictionary<string, string>
{
{ "이름","" },
{ "위치","" },
{ "ID","" }
};
public event Action onPanelUpdated;
float panelPadding = 25f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Awake()
{
rect = GetComponent<RectTransform>();
}
private void Start()
{
var logicUIManager = FindAnyObjectByType<LogicUIManager>();
logicUIManager.onLogicItemSelected += SetLogicDataItem;
logicUIManager.onLogicItemDeSelected += UnsetConnectedDataItem;
gameObject.SetActive(false);
}
void ClearItem()
{
foreach (var infoItem in infoItems)
{
Destroy(infoItem.gameObject);
}
infoItems.Clear();
}
void GetObjectByName(string name)
{
var go = LogicMappingDataBase.GetGameObject(name);
if (go)
{
dataMap["이름"] = go.name;
dataMap["위치"] = go.transform.position.ToString();
dataMap["ID"] = go.modelID;
}
else
{
dataMap["이름"] = "";
dataMap["위치"] = "";
dataMap["ID"] = "";
}
}
public void SetLogicDataItem(ILogicItem item)
{
gameObject.SetActive(true);
ClearItem();
GetObjectByName(item.Name);
float itemHeight = logicItem.GetComponent<RectTransform>().rect.height;
SetRectHeight(itemHeight);
int i = 0;
foreach (var p in dataMap)
{
var rawValue = p.Value;
var value = rawValue?.ToString() ?? "";
var iLogicItem = Instantiate(logicItem, rect);
iLogicItem.GetComponent<RectTransform>().anchoredPosition = new Vector2(iLogicItem.GetComponent<RectTransform>().anchoredPosition.x, -itemHeight * i);
iLogicItem.Set(p.Key, value);
infoItems.Add(iLogicItem);
i++;
}
onPanelUpdated?.Invoke();
}
public void SetConnectedDataItem(string itemName)
{
gameObject.SetActive(true);
ClearItem();
GetObjectByName(itemName);
float itemHeight = logicItem.GetComponent<RectTransform>().rect.height;
SetRectHeight(itemHeight);
int i = 0;
foreach (var p in dataMap)
{
var rawValue = p.Value;
var value = rawValue?.ToString() ?? "";
var iLogicItem = Instantiate(logicItem, rect);
iLogicItem.GetComponent<RectTransform>().anchoredPosition = new Vector2(iLogicItem.GetComponent<RectTransform>().anchoredPosition.x, -itemHeight * i);
iLogicItem.Set(p.Key, value);
infoItems.Add(iLogicItem);
i++;
}
onPanelUpdated?.Invoke();
}
void SetRectHeight(float itemHeight)
{
float panelHeight = dataMap.Count * itemHeight + panelPadding;
rect.sizeDelta = new Vector2(rect.sizeDelta.x, panelHeight);
}
public void UnsetConnectedDataItem()
{
gameObject.SetActive(false);
}
}
}