using Blue;
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 素材显示面板
///
public class UIMaterialsPanel : UIPanelBase, EventObserver
{
///
/// 当前按钮代表景点父物体
///
private GameObject m_ShowObj;
private bool m_IsSelect;
private ObjectValue m_ShowObjValues;
private Transform m_Content;
private Toggle m_BtnPrefab;
///
/// 添加其他素材按钮
///
private Toggle m_AddMaterBtn;
///
/// 整体调整按钮
///
private Toggle m_AllBtn;
private ToggleGroup m_ToggleGroup;
///
/// 当前景点信息
///
private SpoitValue m_CurrentValue;
private List m_Btns;
public EditorPanel EditorPanel { get; set; }
protected override void OnAwake()
{
base.OnAwake();
m_IsSelect = false;
m_Btns = new List();
m_Content = CacheTransform.Find("Scroll View/Viewport/UIMaterialContent");
m_ToggleGroup = m_Content.GetComponent();
m_AddMaterBtn = m_Content.Find("AddMaterialBtn").GetComponent();
m_AllBtn = m_Content.Find("ALLBtn").GetComponent();
m_BtnPrefab = m_Content.Find("MaterialBtn").gameObject.GetComponent();
m_AddMaterBtn.onValueChanged.AddListener(OnClickAddMaterialBtn);
m_AllBtn.onValueChanged.AddListener(OnClickAllBtn);
}
private void OnClickAddMaterialBtn(bool arg0)
{
EventData1 data1 = new EventData1(EventID.EVENT_1, null);
data1.Send();
EditorPanel.ChangeShowUI(false);
}
private void OnClickAllBtn(bool b)
{
if (b)
{
EventData1 data1 = new EventData1(EventID.EVENT_1, m_ShowObj);
data1.Send();
for (int i = 0; i < m_Btns.Count; i++)
{
if (m_Btns[i].gameObject.activeSelf && !m_Btns[i].IsCollider)
{
m_Btns[i].SelectObj(true);
}
}
}
}
public override void Hide()
{
base.Hide();
}
///
/// 隐藏所有按钮,并清除数据
///
private void HideBtns()
{
for (int i = 0; i < m_Btns.Count; i++)
{
m_Btns[i].ClearData();
m_Btns[i].gameObject.SetActive(false);
}
}
///
/// 当前场景的素材
///
///
public void SetMaterial(SpoitValue spoitValue)
{
m_ShowObj = null;
m_ShowObjValues = null;
m_AllBtn.gameObject.SetActive(false);
HideBtns();
m_CurrentValue = spoitValue;
if (m_CurrentValue == null)
{
return;
}
ObjectValue objvalues = GameManager.Instance.GetSpoitObjs(spoitValue.id);
if (objvalues != null)
{
m_ShowObj = objvalues.Object;
m_ShowObjValues = objvalues;
m_ShowObj.transform.localPosition = m_ShowObjValues.nowPos;
m_ShowObj.transform.localEulerAngles = m_ShowObjValues.nowRot;
m_ShowObj.transform.localScale = m_ShowObjValues.nowScale;
if (m_ShowObjValues.ChildrenObj.Count > 0)
{
m_AllBtn.gameObject.SetActive(true);
}
}
////先设置物体触发器
//MaterialBtnItem collider = null;
//if (m_Btns.Count >= 1)
//{
// collider = m_Btns[0];
// m_Btns.RemoveAt(0);
//}
//else
//{
// collider = Instantiate(m_BtnPrefab, m_Content).gameObject.AddComponent();
//}
//if (collider != null)
//{
// collider.gameObject.SetActive(true);
// collider.SetData(GameManager.Instance.GetCurrentColliderValue());
// collider.transform.localScale = new Vector3(1, 1, 1);
// collider.transform.localRotation = Quaternion.identity;
//}
var sucais = m_CurrentValue.material;
var objValue = objvalues.ChildrenObj;
int num = 0;
for (int i = 0; i < sucais.Count; i++)
{
var matObj = sucais[i].materialList;
var objvalue = objValue[i];
for (int m = 0; m < matObj.Count; m++)
{
MaterialBtnItem item = null;
num++;
if (num < m_Btns.Count)
{
item = m_Btns[num];
}
else
{
item = Instantiate(m_BtnPrefab, m_Content).gameObject.GetComponent();
m_Btns.Add(item);
}
item.gameObject.SetActive(true);
item.transform.localScale = new Vector3(1, 1, 1);
item.transform.localRotation = Quaternion.identity;
item.SetData(matObj[m], objvalue.ChildrenObj[m]);
}
}
//if (collider != null)
//{
// m_Btns.Add(collider);
// collider.transform.SetAsLastSibling();
//}
m_AddMaterBtn.transform.SetAsLastSibling();
}
public void ClosePanel()
{
m_ShowObj = null;
m_ShowObjValues = null;
m_CurrentValue = null;
HideBtns();
}
///
/// 初始化ObjectValue数据
///
public void InitObjTransForm()
{
m_ShowObjValues.InitObjTransform();
for (int i = 0; i < m_Btns.Count; i++)
{
m_Btns[i].InitMatObj();
}
for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
{
var obj1 = m_ShowObjValues.ChildrenObj[i];
for (int m = 0; m < obj1.ChildrenObj.Count; m++)
{
var obj2 = obj1.ChildrenObj[m];
obj2.InitObjTransform();
}
}
}
///
/// 保存ObjectValue数据
///
public void SaveMatObj()
{
m_ShowObjValues.SaveObjTransfoem();
for (int i = 0; i < m_Btns.Count; i++)
{
m_Btns[i].SaveMatObj();
}
for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
{
var obj1 = m_ShowObjValues.ChildrenObj[i];
for (int m = 0; m < obj1.ChildrenObj.Count; m++)
{
var obj2 = obj1.ChildrenObj[m];
obj2.SaveObjTransfoem();
}
}
GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
GameManager.Instance.UpdateSpoitValue();
SaveSpoits();
}
public void SaveSpoits()
{
Debug.Log("向服务器发送景点更新信息");
if (m_ShowObj == null)
{
return;
}
Debug.Log("m_ShowObj存在");
UIManager.Instance.ChangeCanvasPos();
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitData);
var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
List saveData = new List();
SendSaveData data = new SendSaveData();
data.id = spoit.id;
data.name = spoit.name;
data.projectId = DataManager.Instance.ProjectID;
data.material = spoit.material;
data.spoittf = spoit.spoitTf;
//data.triggerRange = spoit.SpoitTransform[0];
saveData.Add(data);
string sendData = JsonConvert.SerializeObject(saveData);
//string sendData = JsonMapper.ToJson(saveData);
Debug.Log("更新景点数据: " + sendData);
HttpTool.Instance.PostTest("/viewpoint/update", sendData, SaveCallBack);
//string str = JsonMapper.ToJson(Spoits);
//FileManager.WriteFile(sendData, Application.persistentDataPath + "/a.json");
//Debug.Log(str);
}
private void SaveCallBack(string message)
{
if (message == "UnityWebRequest Error")
{
InstantiateCommand Command =new InstantiateCommand(
InstantiateSystem.Instance.BlueObject.WarningPopUp,
InstantiateSystem.Instance.BlueObject.NetErrorText);
CommandExtensionSystem.Instance.Send(Command);
return;
}
Debug.Log("SaveCallBack: " + message);
JObject jObject = JObject.Parse(message);
string str = jObject["message"].ToString();
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitDataEnd);
((LoadingPanel)UIManager.Instance.GetUI(UINameConfig.LoadingPanel)).TextStr = str;
}
public void RemoveMat(GameObject obj)
{
StartCoroutine(WaitRemove(obj));
}
IEnumerator WaitRemove(GameObject obj)
{
Debug.Log("删除整个景点数据");
//删除整个景点数据
if (obj == m_ShowObj)
{
for (int i = 0; i < obj.transform.childCount; i++)
{
if (obj.transform.GetChild(i) != obj)
{
Destroy(obj.transform.GetChild(i).gameObject);
}
}
yield return new WaitForEndOfFrame();
//m_ShowObj = null;
m_ShowObjValues.ChildrenObj.Clear();
m_IsSelect = false;
var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
spoit.material = new List();
GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex] = spoit;
SaveSpoits();
}
else
{
for (int i = 0; i < m_Btns.Count; i++)
{
m_Btns[i].SaveMatObj();
if (m_Btns[i].MaterialObj == obj)
{
m_Btns[i].IsSelect = false;
//var objvalue = m_Btns[i].ObjValue;
Destroy(obj);
yield return new WaitForEndOfFrame();
}
}
SaveMatObj();
}
GameManager.Instance.UpdateEditorUI();
}
public void HandleEvent(EventData data)
{
switch (data.ID)
{
case EventID.EVENT_1:
EventData1 data1 = (EventData1)data;
if (data1.SelectObj == m_ShowObj)
{
m_IsSelect = true;
}
else
{
m_IsSelect = false;
}
break;
case EventID.EVENT_2:
break;
default:
break;
}
}
}