108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
using UnityEngine;
|
|
using XRLib.UI;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace XED
|
|
{
|
|
public class Panel_APIConnectModal : PanelBase
|
|
{
|
|
public Data api;
|
|
private DataClass selectedUrlData;
|
|
|
|
private TMP_Dropdown Dropdown_URL;
|
|
private RectTransform ClassNameContent;
|
|
|
|
private Button Button_AddClassName;
|
|
private Button Button_Save;
|
|
|
|
private UI_DataBindingItem prf_mqttTopicItem;
|
|
private List<UI_DataBindingItem> topicItems = new List<UI_DataBindingItem>();
|
|
private UI_DataBindingItem selectedTopicItem;
|
|
|
|
public Action<string, DataClass> onClickFilterButton;
|
|
public Action<SaveAPIData> onSaveAPIData;
|
|
|
|
private void TestDataConnected()
|
|
{
|
|
var apiJson = Resources.Load<TextAsset>("Test").text;
|
|
api = JsonConvert.DeserializeObject<Data>(apiJson);
|
|
}
|
|
public override void AfterAwake()
|
|
{
|
|
TestDataConnected();
|
|
prf_mqttTopicItem = Resources.Load<UI_DataBindingItem>("Prefabs/UI/PRF_DataBindingItemItem");
|
|
|
|
SetUrlDropdown();
|
|
Button_AddClassName.onClick.AddListener(AddClassNameItem);
|
|
Button_Save.onClick.AddListener(Save);
|
|
}
|
|
private void SetUrlDropdown()
|
|
{
|
|
Dropdown_URL.ClearOptions();
|
|
List<string> options = new List<string>();
|
|
|
|
foreach(var apiData in api.dataList)
|
|
{
|
|
options.Add(apiData.name);
|
|
}
|
|
Dropdown_URL.AddOptions(options);
|
|
Dropdown_URL.onValueChanged.AddListener(OnUrlValueChanged);
|
|
}
|
|
|
|
private void OnUrlValueChanged(int index)
|
|
{
|
|
selectedUrlData = api.dataList[index];
|
|
}
|
|
|
|
private void OnClickFilterButton(UI_DataBindingItem item)
|
|
{
|
|
selectedTopicItem = item;
|
|
var itemName = item.GetTopicName();
|
|
onClickFilterButton?.Invoke(itemName, selectedUrlData);
|
|
}
|
|
|
|
private void AddClassNameItem()
|
|
{
|
|
var item = Instantiate(prf_mqttTopicItem, ClassNameContent);
|
|
item.onRemove += RemoveTopicItem;
|
|
item.onFilter += OnClickFilterButton;
|
|
topicItems.Add(item);
|
|
|
|
Button_AddClassName.transform.SetAsLastSibling();
|
|
}
|
|
private void RemoveTopicItem(UI_DataBindingItem item)
|
|
{
|
|
topicItems.Remove(item);
|
|
Destroy(item.gameObject);
|
|
}
|
|
private void Save()
|
|
{
|
|
var apiData = GetAPIData();
|
|
onSaveAPIData?.Invoke(apiData);
|
|
}
|
|
public void SetFilterData(SaveFilterData filterData)
|
|
{
|
|
if (selectedTopicItem == null)
|
|
return;
|
|
|
|
selectedTopicItem.SetFilterData(filterData);
|
|
selectedTopicItem.SetChangedInputFieldTopicName();
|
|
}
|
|
private SaveAPIData GetAPIData()
|
|
{
|
|
SaveAPIData saveAPIData = new SaveAPIData();
|
|
saveAPIData.selectedUrl = Dropdown_URL.options[Dropdown_URL.value].text;
|
|
foreach (var topicItem in topicItems)
|
|
{
|
|
saveAPIData.saveFilterData.Add(topicItem.GetFilterData());
|
|
}
|
|
|
|
return saveAPIData;
|
|
}
|
|
}
|
|
}
|