Files
HX_MapEditor/Assets/Scripts/UI/UICellInfo.cs
2025-06-15 20:14:45 +08:00

162 lines
4.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///格子信息 - 绿色块
///功能:加载格子数据,重新计算格子,显示格子,隐藏格子
public class UICellInfo : MonoBehaviour
{
public Text txtMapWidth;
public Text txtMapHeight;
public Text txtTotalCells;
public Text txtMoveCells;
public Text txtCellRows;
public Text txtCellCols;
public InputField txtCellWidth;
public InputField txtCellHeight;
public bool bMapOpened;
private void Start()
{
MapManager.Instance.onLoadFinishedCallback = OnLoadFinished;
UIWindow.Instance.uiCreateMap.onCreatedMapCallback += OnCreatedMap;
Cleanup();
}
void Cleanup()
{
txtMapWidth.text = string.Empty;
txtMapHeight.text = string.Empty;
txtTotalCells.text = string.Empty;
txtMoveCells.text = string.Empty;
txtCellRows.text = string.Empty;
txtCellCols.text = string.Empty;
txtCellWidth.text = string.Empty;
txtCellHeight.text = string.Empty;
bMapOpened = false;
}
void OnCreatedMap(int mapId, int mapWidth, int mapHeight)
{
bMapOpened = true;
txtMapWidth.text = mapWidth.ToString();
txtMapHeight.text = mapHeight.ToString();
}
void OnLoadFinished()
{
txtMapWidth.text = MapManager.Instance.mapWidth.ToString();
txtMapHeight.text = MapManager.Instance.mapHeight.ToString();
bMapOpened = true;
}
public void CloseMap()
{
Cleanup();
UIWindow.Instance.uiCellEditor.togEdit.isOn = false;
UIWindow.Instance.uiCellEditor.togPathTest.isOn = false;
}
public void LoadCells()
{
if (!bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
if (!MapManager.Instance.LoadObsXml())
CalculationCells();
txtCellWidth.text = MapManager.Instance.CellWidth.ToString();
txtCellHeight.text = MapManager.Instance.CellHeight.ToString();
txtCellRows.text = MapManager.Instance.CellRows.ToString();
txtCellCols.text = MapManager.Instance.CellCols.ToString();
txtTotalCells.text = (MapManager.Instance.CellRows * MapManager.Instance.CellCols).ToString();
}
public void ShowCells()
{
if (!bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
if (MapManager.Instance.CellRows <= 0)
{
UIWindow.Instance.ShowMessage("请先加载网格数据");
return;
}
UIWindow.Instance.uiCreateMap.MapBG.GetComponent<CanvasGroup>().blocksRaycasts = false;
MapManager.Instance.CreateCells();
txtMoveCells.text = MapManager.Instance.GetLayCellCount(CellLayer.Move).ToString();
}
public void HideCells()
{
if (!bMapOpened)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
if (MapManager.Instance.CellRows <= 0)
{
UIWindow.Instance.ShowMessage("请先加载网格数据");
return;
}
UIWindow.Instance.uiCreateMap.MapBG.GetComponent<CanvasGroup>().blocksRaycasts = true;
MapManager.Instance.HideCells();
}
public void CalculationCells()
{
if (!bMapOpened)
return;
if (string.IsNullOrEmpty(txtMapWidth.text)
|| string.IsNullOrEmpty(txtMapWidth.text)
|| string.IsNullOrEmpty(txtCellHeight.text)
|| string.IsNullOrEmpty(txtCellHeight.text))
{
UIWindow.Instance.ShowMessage("请输入有效数字");
return;
}
int mapWidth = Convert.ToInt32(txtMapWidth.text);
int mapHeight = Convert.ToInt32(txtMapHeight.text);
int cellWidth = Convert.ToInt32(txtCellWidth.text);
int cellHeight = Convert.ToInt32(txtCellHeight.text);
if (mapWidth <= 0 || mapHeight <= 0 || cellWidth <= 0 || cellHeight <= 0)
{
UIWindow.Instance.ShowMessage("请输入大于0的数值");
return;
}
CalculationCells(mapWidth, mapHeight, cellWidth, cellHeight);
}
public void CalculationCells(int mapWidth, int mapHeight, int cellWidth, int cellHeight)
{
int row = mapHeight / cellHeight;
int col = mapWidth / cellWidth;
txtCellRows.text = row.ToString();
txtCellCols.text = col.ToString();
txtTotalCells.text = (row * col).ToString();
MapManager.Instance.CalculationCells(cellWidth, cellHeight, mapWidth, mapHeight);
}
}