154 lines
6.4 KiB
C#
154 lines
6.4 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Octopus.Simulator.Networks;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
|
|
namespace Octopus.Simulator
|
|
{
|
|
public class LogicDataManager : MonoBehaviour
|
|
{
|
|
public SimulationData currentData;
|
|
public LogicWebConfig config;
|
|
[SerializeField]
|
|
Button logicPrefab;
|
|
[SerializeField]
|
|
RectTransform LogicWindow;
|
|
[SerializeField]
|
|
Image linePrefab;
|
|
|
|
Dictionary<Button, ILogicItem> logicButtonDict = new Dictionary<Button, ILogicItem>();
|
|
Dictionary<string, Button> dataMap = new Dictionary<string, Button>();
|
|
Dictionary<string, Button> idMap = new Dictionary<string, Button>();
|
|
|
|
Panel_LogicData panelLogicData;
|
|
Panel_ConnectedObject panelConnectedObject;
|
|
Panel_PlacedObject panelPlacedObject;
|
|
public event Action<ILogicItem> onLogicButtonClicked;
|
|
private void Awake()
|
|
{
|
|
panelLogicData = FindAnyObjectByType<Panel_LogicData>();
|
|
panelConnectedObject = FindAnyObjectByType<Panel_ConnectedObject>();
|
|
panelPlacedObject = FindAnyObjectByType<Panel_PlacedObject>();
|
|
}
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
RequestInfo();
|
|
onLogicButtonClicked += panelLogicData.OnLogicDataSelected;
|
|
onLogicButtonClicked += panelConnectedObject.OnLogicDataSelected;
|
|
onLogicButtonClicked += panelPlacedObject.OnLogicDataSelected;
|
|
panelPlacedObject.onPlacedObjectSelected += panelConnectedObject.OnLogicDataSelected;
|
|
FindAnyObjectByType<Panel_SimulationUI>().onLogicBTNClicked += SetActive;
|
|
LogicWindow.gameObject.SetActive(false);
|
|
}
|
|
|
|
void ClearLogicBTN()
|
|
{
|
|
foreach (var pair in logicButtonDict)
|
|
{
|
|
Destroy(pair.Key.gameObject);
|
|
}
|
|
logicButtonDict.Clear();
|
|
}
|
|
|
|
void SetActive()
|
|
{
|
|
LogicWindow.gameObject.SetActive(!LogicWindow.gameObject.activeSelf);
|
|
}
|
|
|
|
void RequestInfo()
|
|
{
|
|
ClearLogicBTN();
|
|
WebManager.webManager.Request_Get($"{WebManager.webManager.apiConfig.logic}/33", (flag, value) =>
|
|
{
|
|
if (flag)
|
|
{
|
|
var info = JsonConvert.DeserializeObject<SimulationInfo>(value);
|
|
GetDataFromInfo(info.data);
|
|
}
|
|
});
|
|
}
|
|
|
|
void GetDataFromInfo(SimulationData Data)
|
|
{
|
|
currentData = Data;
|
|
config = Data.webConfig;
|
|
QueueVisulization(Data.data.queues);
|
|
ResourceVisulization(Data.data.resources);
|
|
ComponentVisulization(Data.data.components);
|
|
ConnectEdge();
|
|
}
|
|
|
|
void QueueVisulization(List<LogicQueue> queues)
|
|
{
|
|
foreach (var queue in queues)
|
|
{
|
|
var logicButton = Instantiate(logicPrefab, LogicWindow);
|
|
var item = logicButton.GetComponent<LogicItemButton>();
|
|
item.Setup(queue);
|
|
logicButtonDict.Add(logicButton, queue);
|
|
dataMap.Add(queue.name, logicButton);
|
|
idMap.Add(config.nodes.Find(n => n.data.name == queue.name).id, logicButton);
|
|
logicButton.onClick.AddListener(() => OnLogicButtonClick(queue));
|
|
logicButton.GetComponent<RectTransform>().anchoredPosition = new Vector2(config.nodes.Find(n => n.data.name==queue.name).position.x, -config.nodes.Find(n => n.data.name == queue.name).position.y);
|
|
}
|
|
}
|
|
|
|
void ResourceVisulization(List<LogicResource> resources)
|
|
{
|
|
foreach (var resource in resources)
|
|
{
|
|
var logicButton = Instantiate(logicPrefab, LogicWindow);
|
|
var item = logicButton.GetComponent<LogicItemButton>();
|
|
item.Setup(resource);
|
|
logicButtonDict.Add(logicButton, resource);
|
|
dataMap.Add(resource.name, logicButton);
|
|
idMap.Add(config.nodes.Find(n => n.data.name == resource.name).id, logicButton);
|
|
logicButton.onClick.AddListener(() => OnLogicButtonClick(resource));
|
|
logicButton.GetComponent<RectTransform>().anchoredPosition = new Vector2(config.nodes.Find(n => n.data.name == resource.name).position.x, -config.nodes.Find(n => n.data.name == resource.name).position.y);
|
|
}
|
|
}
|
|
|
|
void ComponentVisulization(List<ILogicComponent> components)
|
|
{
|
|
foreach (var component in components)
|
|
{
|
|
var logicButton = Instantiate(logicPrefab, LogicWindow);
|
|
var item = logicButton.GetComponent<LogicItemButton>();
|
|
item.Setup(component);
|
|
logicButtonDict.Add(logicButton, component);
|
|
dataMap.Add(component.Name, logicButton);
|
|
idMap.Add(config.nodes.Find(n => n.data.name == component.Name).id, logicButton);
|
|
logicButton.onClick.AddListener(() => OnLogicButtonClick(component));
|
|
logicButton.GetComponent<RectTransform>().anchoredPosition = new Vector2(config.nodes.Find(n => n.data.name == component.Name).position.x, -config.nodes.Find(n => n.data.name == component.Name).position.y);
|
|
}
|
|
}
|
|
|
|
void ConnectEdge()
|
|
{
|
|
foreach(var edge in config.edges)
|
|
{
|
|
var line=Instantiate(linePrefab, LogicWindow);
|
|
line.rectTransform.anchoredPosition = (idMap[edge.source].GetComponent<RectTransform>().anchoredPosition+ idMap[edge.target].GetComponent<RectTransform>().anchoredPosition)/2f;
|
|
Vector2 direction = idMap[edge.target].GetComponent<RectTransform>().anchoredPosition - idMap[edge.source].GetComponent<RectTransform>().anchoredPosition;
|
|
float length = direction.magnitude;
|
|
line.rectTransform.sizeDelta = new Vector2(length,line.rectTransform.sizeDelta.y);
|
|
float angleRad = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg; // ¶óµð¾È
|
|
line.transform.rotation = Quaternion.Euler(0, 0, angleRad);
|
|
}
|
|
}
|
|
|
|
void drawLine()
|
|
{
|
|
|
|
}
|
|
|
|
void OnLogicButtonClick(ILogicItem item)
|
|
{
|
|
onLogicButtonClicked?.Invoke(item);
|
|
}
|
|
}
|
|
} |