海淀天下城电子沙盘单机版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
11 KiB

4 years ago
using AX.MessageSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FloorsPanelManager : MonoBehaviour
{
public Transform NeiParent;
public Transform WaiParent;
public List<FloorMessage> AllNei;//存储所有楼层内层信息
public List<FloorMessage> AllWai;//存储所有楼层的外层信息
public Transform FloorBtnsParentPanel;//所有楼层按钮显示的父对象
private GameObject FloorBtnPrefab;
private ToggleGroup ParentToggleGroup;
private Button OutBtn;
private Button InBtn;
public Scrollbar scrollbar;
private List<UIFloor> ParentArea = new List<UIFloor>();
void Start()
{
if (AreaNameDic.type==SceneType.chemicalBuilding)
{
gameObject.SetActive(false);
return;
}
if (FloorBtnPrefab == null)
{
FloorBtnPrefab = Resources.Load("UI/FloorItem") as GameObject;
}
Initialization();
OutBtn = GetComponent<UIMoveManager>().MoveOut;
InBtn = GetComponent<UIMoveManager>().MoveIn;
scrollbar = transform.Find("Scroll View/Scrollbar Vertical").GetComponent<Scrollbar>();
scrollbar.onValueChanged.AddListener(scrollbar_valueChanged);
OutBtn.onClick.AddListener(OutBtn_Click);
InBtn.onClick.AddListener(InBtn_Click);
MessageDispatcher.AddListener("ReplayFrame", ReplayFrameFloors);
MessageDispatcher.AddListener("ReplayEvent", ReplayEventFloors);
MessageDispatcher.AddListener("ToolsOn", ToolsOnChanged);
}
private void scrollbar_valueChanged(float value)
{
GlobalVariable.FloorsScrollBarValue = value;
if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
{
var eventData = new EventData();
eventData.time = RecordManager.Instance.RecordTimer;
eventData.cloneObjType = CloneObjType.None;
eventData.eventType = RecordEventType.FloorsScrollBar;
eventData.json = scrollbar.value.ToString();
RecordManager.Instance.jsonData.eventDataList.Add(eventData);
}
}
private void InBtn_Click()
{
GlobalVariable.FloorsOn = true;
AddRecord(GlobalVariable.FloorsOn);
MessageDispatcher.SendMessage("FloorsOn");
}
private void OutBtn_Click()
{
GlobalVariable.FloorsOn = false;
AddRecord(GlobalVariable.FloorsOn);
//foreach (Transform item in FloorBtnsParentPanel)
//{
// if (item.GetComponent<UIFloor>().floorNum == 0)
// {
// if (item.GetComponent<Toggle>().isOn)
// {
// item.GetComponent<Toggle>().isOn = false;
// }
// }
//}
}
private void AddRecord(bool IsOn)
{
if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
{
var eventData = new EventData();
eventData.time = RecordManager.Instance.RecordTimer;
eventData.cloneObjType = CloneObjType.None;
eventData.eventType = RecordEventType.FloorsOn;
eventData.json = IsOn.ToString();
RecordManager.Instance.jsonData.eventDataList.Add(eventData);
}
}
private void OnDestroy()
{
MessageDispatcher.RemoveListener("ReplayFrame", ReplayFrameFloors);
MessageDispatcher.RemoveListener("ReplayEvent", ReplayEventFloors);
MessageDispatcher.RemoveListener("ToolsOn", ToolsOnChanged);
}
private void ToolsOnChanged(IMessage obj)
{
if (GlobalVariable.FloorsOn)
{
OutBtn_Click();
GetComponent<UIMoveManager>().MoveOut_Click();
}
}
private void ReplayEventFloors(IMessage obj)
{
var eventData = (EventData)obj.Data;
if (eventData.eventType == RecordEventType.FloorsOn)
{
bool IsOn = bool.Parse(eventData.json);
if (IsOn)
{
InBtn_Click();
GetComponent<UIMoveManager>().MoveIn_Click();
}
else
{
OutBtn_Click();
GetComponent<UIMoveManager>().MoveOut_Click();
}
}
else if (eventData.eventType == RecordEventType.FloorsScrollBar)
{
scrollbar.value = float.Parse(eventData.json);
}
else if (eventData.eventType == RecordEventType.UIFloor)
{
FloorData data = JsonUtility.FromJson<FloorData>(eventData.json);
Setting(data);
}
}
private void ReplayFrameFloors(IMessage obj)
{
var objectData = (ObjectData)obj.Data;
if (objectData.cloneObjType == CloneObjType.FloorsOn)
{
bool IsOn = bool.Parse(objectData.json);
if (IsOn)
{
if (GlobalVariable.FloorsOn == false)
{
InBtn_Click();
GetComponent<UIMoveManager>().MoveIn_Click();
}
}
else
{
if (GlobalVariable.FloorsOn == true)
{
OutBtn_Click();
GetComponent<UIMoveManager>().MoveOut_Click();
}
}
}
else if (objectData.cloneObjType == CloneObjType.FloorsScrollBar)
{
scrollbar.value = float.Parse(objectData.json);
}
else if (objectData.cloneObjType == CloneObjType.UIFloor)
{
FloorData data = JsonUtility.FromJson<FloorData>(objectData.json);
Setting(data);
}
}
private void Setting(FloorData data)
{
if (data.buildNum == null || data.isOn == false)
{
foreach (var item in ParentArea)
{
if (item.GetComponent<Toggle>().isOn)
{
item.GetComponent<Toggle>().isOn = false;
}
}
return;
}
UIFloor parent = null;
foreach (var item in ParentArea)
{
if (item.GetComponent<Toggle>().isOn)
{
item.GetComponent<Toggle>().isOn = false;
}
if (item.buildNum == data.buildNum)
parent = item;
}
if (Mathf.Abs(data.floorNum) == 0)
{
parent.GetComponent<Toggle>().isOn = true;
}
else
{
parent.GetComponent<Toggle>().isOn = true;
foreach (var item in parent.MyChildFloors)
{
if (item.GetComponent<UIFloor>().floorNum == data.floorNum
&& item.GetComponent<UIFloor>().interlayerNum == data.interlayerNum)
{
item.GetComponent<Toggle>().isOn = true;
}
}
}
}
private void Initialization()//初始化数据信息
{
NeiParent = GameObject.Find("Scene").transform.Find("ShiNei");
WaiParent = GameObject.Find("Scene").transform.Find("ZhuTi");
foreach (Transform item in FloorBtnsParentPanel)
{
Destroy(item.gameObject);
}
if (GetComponent<ToggleGroup>())
ParentToggleGroup = GetComponent<ToggleGroup>();
else
ParentToggleGroup = gameObject.AddComponent<ToggleGroup>();
ParentToggleGroup.allowSwitchOff = true;
var MaxNumNei = NeiParent.transform.childCount;
for (int i = 0; i < MaxNumNei; i++)
{
Transform child = NeiParent.GetChild(i);
GameObject floorBtnParent = Instantiate(FloorBtnPrefab, FloorBtnsParentPanel);
ToggleGroup floorBtnParentGroup = floorBtnParent.AddComponent<ToggleGroup>();
floorBtnParentGroup.allowSwitchOff = true;
UIFloor floorParent = floorBtnParent.GetComponent<UIFloor>();
floorParent.GetComponent<Toggle>().group = ParentToggleGroup;
int length = child.name.IndexOf("ShiNei");
string name = child.name.Substring(0, length);
floorParent.MyText.text = AreaNameDic.GetAreaName(name);
floorParent.buildNum = name;
floorParent.floorNum = 0;
floorParent.interlayerNum = 0;
floorParent.HasChild.SetActive(true);
floorParent.floorsPanelManager = this;
floorParent.MyTarget = GetAreaTargetManager.Instance.GetTarget(name);
ParentArea.Add(floorParent);
var childNum = child.transform.childCount;
for (int j = 0; j < childNum; j++)
{
Transform childchild = child.GetChild(j);
GameObject floorBtn = Instantiate(FloorBtnPrefab, FloorBtnsParentPanel);
UIFloor floor = floorBtn.GetComponent<UIFloor>();
floor.GetComponent<Toggle>().group = floorBtnParentGroup;
FloorMessage msg = childchild.GetComponent<FloorMessage>();
floor.MyTarget = GetTarget(childchild);
floor.buildNum = msg.buildNum;
floor.buildNum = msg.buildNum;
floor.floorNum = msg.floorNum;
floor.interlayerNum = msg.interlayerNum;
floor.MyText.text = GetName(floor.floorNum, floor.interlayerNum);
floor.floorsPanelManager = this;
floorBtn.gameObject.SetActive(false);
floorParent.MyChildFloors.Add(floorBtn.transform);
if (msg != null)
AllNei.Add(msg);
}
}
var MaxNumWai = WaiParent.childCount;
for (int i = 0; i < MaxNumWai; i++)
{
Transform child = WaiParent.GetChild(i);
var childNum = child.transform.childCount;
for (int j = 0; j < childNum; j++)
{
Transform childchild = child.GetChild(j);
FloorMessage msg = childchild.GetComponent<FloorMessage>();
if (msg != null)
AllWai.Add(msg);
}
}
}
Transform GetTarget(Transform parent)
{
Transform target = null;
if (parent.childCount <= 4)
{
Transform child = parent.GetChild(0);
int count = child.childCount;
for (int i = 0; i < count; i++)
{
Transform t = child.GetChild(i);
if (t.name.ToUpper().Contains("_FLOOR") || t.name.ToUpper().Contains("-FLOOR"))
{
target = t;
break;
}
}
}
else
{
int count = parent.childCount;
for (int i = 0; i < count; i++)
{
Transform t = parent.GetChild(i);
if (t.name.ToUpper().Contains("_FLOOR"))
{
target = t;
break;
}
}
}
return target;
}
private string GetName(int floorNum, int interlayerNum)
{
string result = "";
if (floorNum < 0)
{
result += "B";
floorNum = -floorNum;
}
else
{
result += "F";
}
result += floorNum.ToString();
if (interlayerNum > 0)
{
result += "M";
result += interlayerNum.ToString();
}
return result;
}
}