using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
///
/// 编辑网格 - 紫色格子
/// 功能:编辑网格, 寻路测试,格子分层显示
///
public class UICellEditor : MonoBehaviour
{
public Toggle togEdit;
public GameObject editPanel;
public InputField txtBrushRadius;
public Dropdown dropCellType;
public Button btnSetBrush;
public Button btnSaveCells;
#region 测试寻路
public Toggle togPathTest;
public GameObject pathTestPanel;
public InputField txtStartPos; //起点坐标
public InputField txtEndPos; //终点坐标
[HideInInspector]
public InputField curActiveInput;
#endregion
#region 图层编辑
public Toggle togLayer;
public GameObject layerPanel;
public Toggle[] showgLayers;
private int _layers;
#endregion
public Toggle togEffects;
// Start is called before the first frame update
void Start()
{
_layers = 0;
editPanel.SetActive(false);
pathTestPanel.SetActive(false);
layerPanel.SetActive(false);
AddInputNameClickEvent(txtStartPos);
AddInputNameClickEvent(txtEndPos);
UIWindow.Instance.HideMouseOver();
}
private void AddInputNameClickEvent(InputField input) //可以在Awake中调用
{
var eventTrigger = input.gameObject.AddComponent();
UnityAction selectEvent = OnInputFieldClicked;
EventTrigger.Entry onClick = new EventTrigger.Entry()
{
eventID = EventTriggerType.PointerClick
};
onClick.callback.AddListener(selectEvent);
eventTrigger.triggers.Add(onClick);
}
private void OnInputFieldClicked(BaseEventData data)
{
if (data.selectedObject.name.Equals("InputStartCell"))
{
curActiveInput = data.selectedObject.GetComponent();
MapManager.Instance.SetCurPathNodePointIdx(-1);
MapManager.Instance.SetEditCellType(MapManager.EditCellType.PathNodeCell);
}
else if (data.selectedObject.name.Equals("InputEndCell"))
{
curActiveInput = data.selectedObject.GetComponent();
MapManager.Instance.SetCurPathNodePointIdx(-100);
MapManager.Instance.SetEditCellType(MapManager.EditCellType.PathNodeCell);
}
}
public void FindPath()
{
if(string.IsNullOrEmpty(txtStartPos.text))
{
UIWindow.Instance.ShowMessage("起点坐标无效");
return;
}
string[] tmp = txtStartPos.text.Split(',');
if(tmp.Length != 2)
{
UIWindow.Instance.ShowMessage("起点坐标无效");
return;
}
if(!APIs.IsUInt(tmp[0]))
{
UIWindow.Instance.ShowMessage("起点坐标无效");
return;
}
if (!APIs.IsUInt(tmp[1]))
{
UIWindow.Instance.ShowMessage("起点坐标无效");
return;
}
Vector2Int startPos = new Vector2Int(Convert.ToInt32(tmp[0]), Convert.ToInt32(tmp[1]));
if (string.IsNullOrEmpty(txtEndPos.text))
{
UIWindow.Instance.ShowMessage("终点坐标无效");
return;
}
tmp = txtEndPos.text.Split(',');
if (tmp.Length != 2)
{
UIWindow.Instance.ShowMessage("终点坐标无效");
return;
}
if (!APIs.IsUInt(tmp[0]))
{
UIWindow.Instance.ShowMessage("终点坐标无效");
return;
}
if (!APIs.IsUInt(tmp[1]))
{
UIWindow.Instance.ShowMessage("终点坐标无效");
return;
}
Vector2Int endPos = new Vector2Int(Convert.ToInt32(tmp[0]), Convert.ToInt32(tmp[1]));
MapManager.Instance.FindPath(startPos, endPos);
}
//编辑网格
public void OnEditToggleChange()
{
if (togEdit.isOn)
{
if (!UIWindow.Instance.uiCellInfo.bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
editPanel.SetActive(true);
}
else
{
editPanel.SetActive(false);
MapManager.Instance.CloseEditor();
}
}
//寻路测试
public void OnPathTestToggleChange()
{
if (togPathTest.isOn)
{
if (!UIWindow.Instance.uiCellInfo.bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
pathTestPanel.SetActive(true);
}
else
{
pathTestPanel.SetActive(false);
}
}
//图层编辑
public void OnLayerToggleChange()
{
if (togLayer.isOn)
{
if (!UIWindow.Instance.uiCellInfo.bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
layerPanel.SetActive(true);
}
else
{
layerPanel.SetActive(false);
MapManager.Instance.ShowCells();
}
}
//图层编辑
public void OnShowLayerToggleChange()
{
CellLayer[] layers = (CellLayer[])Enum.GetValues(typeof(CellLayer));
_layers = 0;
for (int i= showgLayers.Length - 1; i>=0; i--)
{
if (showgLayers[i].isOn)
_layers |= (int)layers[i];
}
MapManager.Instance.ShowCells();
MapManager.Instance.HideCellsExcludeLayers(_layers);
}
//编辑特效
public void OnShowEffectsToggleChange()
{
UIWindow.Instance.uiCreateMap.MapBG.GetComponent().blocksRaycasts = true;
MapManager.Instance.HideCells();
UIWindow.Instance.ShowMouseOver();
}
public void SetBrush()
{
if (!UIWindow.Instance.uiCellInfo.bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
float brushRadius = Convert.ToSingle(txtBrushRadius.text);
CellType type = (CellType)(1 << dropCellType.value);
MapManager.Instance.SetBrush(brushRadius, type);
MapManager.Instance.StartEditor();
}
public void CleanSafe()
{
MapManager.Instance.CleanSafe();
}
public void SaveCells()
{
if (!UIWindow.Instance.uiCellInfo.bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
MapManager.Instance.SaveCellsXml();
}
}