Files
Simulation/Assets/WorkSpace/LH/LogicData/Panel_ConnectedObject.cs
2025-06-25 14:41:15 +09:00

133 lines
4.3 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;
float contentSpacing = 10f;
// 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 += SetConnectedDataItem;
logicUIManager.onLogicItemDeSelected += UnsetConnectedDataItem;
FindAnyObjectByType<Panel_PlacedObject>().onPlacedObjectSelected += SetConnectedDataItem;
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 SetConnectedDataItem(ILogicItem item)
{
gameObject.SetActive(true);
ClearItem();
GetObjectByName(item.Name);
float itemHeight = logicItem.GetComponent<RectTransform>().rect.height+ contentSpacing;
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(SimulationModel go)
{
gameObject.SetActive(true);
ClearItem();
if (go)
{
dataMap["이름"] = go.name;
dataMap["위치"] = go.transform.position.ToString();
dataMap["ID"] = go.modelID;
}
else
{
dataMap["이름"] = "";
dataMap["위치"] = "";
dataMap["ID"] = "";
}
float itemHeight = logicItem.GetComponent<RectTransform>().rect.height+ contentSpacing;
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);
}
}
}