NLDClient-yudde/ProjectNLD/Assets/Editor/MapEditor/MapGridWindow.cs

564 lines
18 KiB
C#
Raw Normal View History

2023-11-16 18:27:03 +08:00
using Gameplay;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using System;
using System.Collections;
using System.Collections.Generic;
2024-10-09 14:18:50 +08:00
using System.Linq;
2023-11-16 18:27:03 +08:00
using TGS;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem.EnhancedTouch;
2024-10-09 15:07:23 +08:00
using ObjectFieldAlignment = Sirenix.OdinInspector.ObjectFieldAlignment;
2023-11-16 18:27:03 +08:00
namespace MapEditor
{
public class MapGridWindow : OdinEditorWindow
{
#region Inner Class
2023-11-19 14:01:26 +08:00
[Serializable]
2023-11-17 18:16:53 +08:00
public class RegionArea
{
2024-10-09 13:38:54 +08:00
[HorizontalGroup("1")] [ReadOnly] public int id;
2023-11-19 14:01:26 +08:00
2024-10-09 13:38:54 +08:00
[HorizontalGroup("1")] [ReadOnly] [LabelText("数量")]
2023-11-17 18:16:53 +08:00
public int regionCount;
2023-11-19 14:01:26 +08:00
public List<int> positions => _region.positions;
private MapData.Region _region;
public RegionArea(MapData.Region region)
{
_region = region;
}
[Button("编辑"), HorizontalGroup("1")]
2023-11-17 18:16:53 +08:00
private void EditorRegion()
{
var map = thisWindow._map;
2023-11-19 14:01:26 +08:00
thisWindow._currEditRegionArea = this;
var regionslist = map.MapData.regions;
2023-11-17 18:16:53 +08:00
for (int i = 0; i < regionslist.Count; i++)
{
map.SetRegionColor(i, false, regionColor);
}
2023-11-19 14:01:26 +08:00
map.SetRegionColor(_region, true, regionColor);
2023-11-17 18:16:53 +08:00
canEditorRegion = true;
}
2023-11-19 14:01:26 +08:00
public void AddPosition(int cellIndex)
{
if (positions.Contains(cellIndex))
return;
positions.Add(cellIndex);
regionCount++;
thisWindow._map.TerrainGridSystem.CellToggleRegionSurface(cellIndex, true, regionColor);
}
public void RemovePosition(int cellIndex)
{
if (!positions.Contains(cellIndex))
return;
positions.Remove(cellIndex);
regionCount--;
thisWindow._map.TerrainGridSystem.CellHideRegionSurface(cellIndex);
}
2023-11-17 18:16:53 +08:00
}
2023-11-16 18:27:03 +08:00
#endregion
#region Fields And Properties
2024-10-09 15:07:23 +08:00
private const string BuffPrefabPath = "Art/Other/hud";
2024-10-09 13:38:54 +08:00
2023-11-16 18:27:03 +08:00
private static MapGridWindow thisWindow;
//1.#FFBF0072 2.#FF960072
private static readonly Color regionColor = MapUtils.GetRGBAColor("FF960072");
2023-11-17 18:16:53 +08:00
private static readonly Color cellBorderColor = new Color(229f, 229f, 229f, 237f);
2023-11-16 18:27:03 +08:00
private static readonly float cellBorderWidth = 1f;
private static List<TerrainTypeProperty> _terrainTypeConfig;
private static bool canEditorRegion = false;
private bool EditorNewRegion = false;
2023-11-19 14:01:26 +08:00
2023-11-16 18:27:03 +08:00
private string _mapDataJsonStr = "";
private GameObject selectedGameObject;
private Vector3 previousPosition;
private Vector3 previousRotation;
2023-11-19 14:01:26 +08:00
2023-11-16 18:27:03 +08:00
private MapData _currentMapData = null;
2023-11-19 14:01:26 +08:00
private RegionArea _currEditRegionArea;
2023-12-20 12:47:57 +08:00
private MapManager _currMapManager;
2023-11-19 22:57:17 +08:00
2023-11-16 18:27:03 +08:00
private Map _map;
2023-11-19 14:01:26 +08:00
2024-10-09 13:38:54 +08:00
private Dictionary<int, GameObject> _buffPrefabDic;
2024-10-09 15:07:23 +08:00
private Dictionary<int, string> _buffPathDic;
2024-10-09 13:38:54 +08:00
2023-11-16 18:27:03 +08:00
#endregion
#region Editor
2023-11-19 22:57:17 +08:00
[Button("保存")]
2023-11-17 18:16:53 +08:00
private void RebuildPrefab()
{
2024-10-09 15:07:23 +08:00
_currMapManager.SaveBuffObjCellIndex(_buffPathDic);
2023-12-20 12:47:57 +08:00
_currMapManager.SaveMapData();
2023-11-17 18:16:53 +08:00
}
2023-11-16 18:27:03 +08:00
2023-11-21 15:04:56 +08:00
[Button("清除网格数据")]
private void ClearGridData()
{
if (EditorUtility.DisplayDialog("清理网格数据", "您是否确认清理地图网格数据", "确认", "取消"))
{
2023-12-18 19:42:46 +08:00
_currentMapData.ClearGridData();
2023-11-21 15:04:56 +08:00
_map.TerrainGridSystem.TerritoryDestroyAll();
ClearRegionColor();
UpdateRegion();
2023-12-20 12:47:57 +08:00
_currMapManager.SaveMapData();
2024-10-09 13:38:54 +08:00
}
2023-11-21 15:04:56 +08:00
}
2024-10-09 13:38:54 +08:00
2023-11-16 18:27:03 +08:00
[BoxGroup("地块信息")]
[LabelText("刷地块"), Indent]
2024-10-09 14:18:50 +08:00
[OnValueChanged("OnCanBrushTerrainTypeChanged")]
2023-11-16 18:27:03 +08:00
[LabelWidth(100)]
[SerializeField]
2023-11-19 22:57:17 +08:00
[ShowIf("@!CanEditorAreaType")]
2023-11-16 18:27:03 +08:00
private bool CanBrushTerrainType;
2024-10-09 14:18:50 +08:00
private void OnCanBrushTerrainTypeChanged()
2023-11-16 18:27:03 +08:00
{
if (CanBrushTerrainType && _map != null)
{
CanEditorAreaType = false;
2024-10-09 14:18:50 +08:00
CanBrushBuffCellIndex = false;
2023-11-16 18:27:03 +08:00
_map.TerrainGridSystem.colorizeTerritories = true;
}
}
[HorizontalGroup("地块信息/Territory")]
[ValueDropdown("GetTerrainTypeProperties")]
[Indent]
[LabelText("选择地块")]
[ShowIf("CanBrushTerrainType")]
public int TerrainTypeIndex = 0;
[ShowIf("CanBrushTerrainType")]
[HorizontalGroup("地块信息/Territory")]
[Button(SdfIconType.GearFill, "编辑地块")]
public void EditTerrainTypeConfig()
{
var window = GetWindow<TerrainTypeConfigWindow>();
window.Show();
}
2024-10-09 14:18:50 +08:00
[BoxGroup("指示信息")]
[LabelText("刷指示信息"), Indent]
[OnValueChanged("OnCanBrushBuffCellIndexChanged")]
[LabelWidth(100)]
[SerializeField]
[ShowIf("@!CanEditorAreaType")]
private bool CanBrushBuffCellIndex;
private void OnCanBrushBuffCellIndexChanged()
{
if (CanBrushBuffCellIndex && _map != null)
{
CanEditorAreaType = false;
CanBrushTerrainType = false;
}
}
2024-10-09 15:07:23 +08:00
[BoxGroup("指示信息/GameObj")]
[PreviewField(150, ObjectFieldAlignment.Center)]
[LabelText("指示预览图"), ShowIf("CanBrushBuffCellIndex")]
[AssetList(Path = BuffPrefabPath)]
public GameObject buffObject;
2023-11-16 18:27:03 +08:00
[BoxGroup("地区信息")]
[HorizontalGroup("地区信息/Region")]
[LabelText("编辑地区"), Indent]
[OnValueChanged("OnCanEditorAreaTypeChanged")]
[LabelWidth(100)]
[SerializeField]
private bool CanEditorAreaType;
private void OnCanEditorAreaTypeChanged()
{
var map = _map;
if (CanEditorAreaType)
{
CanBrushTerrainType = false;
2024-10-09 14:18:50 +08:00
CanBrushBuffCellIndex = false;
2023-11-16 18:27:03 +08:00
}
else
{
2023-11-20 12:25:41 +08:00
ClearRegionColor();
2023-11-19 23:41:18 +08:00
SceneView.RepaintAll();
2023-11-16 18:27:03 +08:00
canEditorRegion = false;
}
}
2023-11-19 14:01:26 +08:00
[LabelText("地区列表")]
2023-11-17 18:16:53 +08:00
[VerticalGroup("地区信息/RegionInfo")]
[ShowIf("CanEditorAreaType")]
[VerticalGroup("地区信息/RegionInfo"), Indent]
2023-11-19 22:57:17 +08:00
[ListDrawerSettings(CustomAddFunction = "CustomAddRegion", CustomRemoveIndexFunction = "CustomRemoveRegion",
DraggableItems = false)]
2023-11-19 14:01:26 +08:00
public List<RegionArea> RegionList = new();
private void CustomAddRegion()
{
var region = new MapData.Region();
_currentMapData.regions.Add(region);
UpdateRegion();
}
private void CustomRemoveRegion(int index)
{
2023-12-18 19:42:46 +08:00
ClearRegionColor();
2023-11-19 14:01:26 +08:00
_currentMapData.regions.RemoveAt(index);
UpdateRegion();
}
2023-11-16 18:27:03 +08:00
#endregion
#region MainLife
2023-12-15 18:32:21 +08:00
public static void OpenWindow(MapManager mapSceneEditor)
2023-11-17 18:16:53 +08:00
{
MapEditorSettings.Init();
thisWindow = GetWindow<MapGridWindow>();
thisWindow.name = "MapGridWindow";
thisWindow.position = GUIHelper.GetEditorWindowRect().AlignCenter(800, 600);
2023-12-20 12:47:57 +08:00
thisWindow._currMapManager = mapSceneEditor;
2023-11-17 18:16:53 +08:00
thisWindow._currentMapData = null;
thisWindow.ShowTab();
2023-11-19 22:57:17 +08:00
thisWindow.LoadMap(mapSceneEditor.TGS, mapSceneEditor.MapData, mapSceneEditor.Map);
2024-10-09 13:38:54 +08:00
thisWindow.LoadBuffPrefab();
2023-11-19 22:57:17 +08:00
UnityEditor.Compilation.CompilationPipeline.compilationStarted += OnCompilationStart;
2023-11-17 18:16:53 +08:00
}
2023-11-16 18:27:03 +08:00
2023-11-19 22:57:17 +08:00
private static void OnCompilationStart(object obj)
{
thisWindow?.Close();
thisWindow = null;
}
2024-10-09 13:38:54 +08:00
private void LoadBuffPrefab()
{
DestroyBuffObj();
_buffPrefabDic.Clear();
2024-10-09 14:18:50 +08:00
var buffObjCells = _map.MapData.showBuffPoints;
foreach (var cell in buffObjCells)
2024-10-09 13:38:54 +08:00
{
2024-10-09 15:07:23 +08:00
var buffObj = AssetDatabase.LoadAssetAtPath<GameObject>(cell.path);
if (buffObj == null)
{
DebugUtil.LogError("读不到指示图标: {0}", cell.path);
}
_buffPrefabDic.Add(cell.cellIndex, CreateBuffObj(cell.cellIndex, buffObj));
_buffPathDic.Add(cell.cellIndex, cell.path);
2024-10-09 13:38:54 +08:00
}
}
private void DestroyBuffObj()
{
if (_buffPrefabDic == null) return;
foreach (var buff in _buffPrefabDic)
{
if (buff.Value != null)
DestroyImmediate(buff.Value);
}
_buffPrefabDic.Clear();
2024-10-09 15:07:23 +08:00
_buffPathDic.Clear();
2024-10-09 13:38:54 +08:00
}
2023-11-19 22:57:17 +08:00
private void LoadMap(TerrainGridSystem tgs, MapData data, Map map)
2023-11-17 18:16:53 +08:00
{
_currentMapData = data;
2023-11-19 22:57:17 +08:00
_map = map;
2023-12-18 19:42:46 +08:00
//MapEditorUtils.LoadMap(tgs, data);
2023-11-17 18:16:53 +08:00
_terrainTypeConfig = TerrainTypeConfig.TerrainTypeProperties;
_map.TerrainGridSystem.highlightEffect = HighlightEffect.Default;
2023-12-18 19:42:46 +08:00
// for (int i = 0; i < _map.MapData.BlocksData.Count; i++)
// {
// if (_map.MapData.BlocksData[i].TerrainTypeIndex != -1)
// {
// if (_terrainTypeConfig[_map.MapData.BlocksData[i].TerrainTypeIndex].emptyBlock)
// {
// _map.TerrainGridSystem.CellSetBorderVisible(i, false);
// }
// }
// }
2023-11-16 18:27:03 +08:00
2023-11-17 18:16:53 +08:00
//MapEditorUtils.ResetCamera();
// if (Camera.main != null)
// {
// var cameraController = Camera.main.gameObject.GetComponent<CameraController>();
// //cameraController.SwitchCameraView(false);
// }
2023-11-19 22:57:17 +08:00
2023-11-21 19:52:50 +08:00
tgs.OnMouseClickOnGrid += OnMouseClickCell;
2023-12-21 16:07:20 +08:00
//tgs.OnMouseMoveOnGrid += OnMouseMoveOnCell;
2023-11-19 14:01:26 +08:00
UpdateRegion();
2023-11-17 18:16:53 +08:00
}
2023-11-16 18:27:03 +08:00
2023-11-17 18:16:53 +08:00
protected override void OnEnable()
{
EnhancedTouchSupport.Enable();
TerrainTypeConfig.Load();
2024-10-09 13:38:54 +08:00
_buffPrefabDic = new Dictionary<int, GameObject>();
2024-10-09 15:07:23 +08:00
_buffPathDic = new Dictionary<int, string>();
2023-11-17 18:16:53 +08:00
base.OnEnable();
}
protected override void OnDisable()
{
2024-10-09 13:38:54 +08:00
DestroyBuffObj();
_buffPrefabDic = null;
2024-10-09 15:07:23 +08:00
_buffPathDic = null;
2023-11-17 18:16:53 +08:00
EnhancedTouchSupport.Disable();
base.OnDisable();
}
2023-11-20 12:25:41 +08:00
protected override void OnGUI()
{
base.OnGUI();
2023-12-20 12:47:57 +08:00
if (_currMapManager == null || _currMapManager.MapData != _currentMapData)
2023-11-20 12:25:41 +08:00
{
Debug.Log("地图数据变更,请重新点击编辑网格!");
Close();
}
}
2023-11-19 14:01:26 +08:00
protected override void OnDestroy()
{
base.OnDestroy();
2024-10-09 13:38:54 +08:00
DestroyBuffObj();
if (_currMapManager != null)
2023-12-20 12:47:57 +08:00
_currMapManager.SaveMapData();
2023-11-20 12:25:41 +08:00
ClearRegionColor();
2023-12-18 19:42:46 +08:00
//SetCellBorderType();
2023-11-21 19:52:50 +08:00
_map.TerrainGridSystem.OnMouseClickOnGrid -= OnMouseClickCell;
2023-12-21 16:07:20 +08:00
//_map.TerrainGridSystem.OnMouseMoveOnGrid -= OnMouseMoveOnCell;
2023-12-18 19:42:46 +08:00
//_map.TerrainGridSystem.TerritoryDestroyAll();
2023-11-19 22:57:17 +08:00
UnityEditor.Compilation.CompilationPipeline.compilationStarted -= OnCompilationStart;
2023-11-19 14:01:26 +08:00
}
2023-11-16 18:27:03 +08:00
#endregion
#region Other
2023-11-19 14:01:26 +08:00
private void UpdateRegion()
{
RegionList.Clear();
if (_currentMapData != null)
{
int index = 0;
foreach (var region in _currentMapData.regions)
{
RegionArea ra = new RegionArea(region);
ra.id = index;
ra.regionCount = ra.positions.Count;
RegionList.Add(ra);
index++;
}
}
}
2023-11-16 18:27:03 +08:00
2023-11-21 19:52:50 +08:00
private void OnMouseClickCell(TerrainGridSystem tgs, int cellIndex, int buttonIndex)
{
int leftButton = 0;
2024-10-09 13:38:54 +08:00
OnCellOperation(tgs, cellIndex, buttonIndex == leftButton);
2023-11-21 19:52:50 +08:00
}
2024-10-09 13:38:54 +08:00
private void OnMouseMoveOnCell(TerrainGridSystem tgs, int cellIndex, bool isControl, bool isShift)
2023-11-21 19:52:50 +08:00
{
2024-10-09 13:38:54 +08:00
OnCellOperation(tgs, cellIndex, isControl);
2023-11-21 19:52:50 +08:00
}
2024-10-09 13:38:54 +08:00
2023-11-21 19:52:50 +08:00
private void OnCellOperation(TerrainGridSystem tgs, int cellIndex, bool isOpTrue)
2023-11-16 18:27:03 +08:00
{
2024-10-09 14:18:50 +08:00
if (!CanBrushTerrainType && !CanEditorAreaType && !CanBrushBuffCellIndex)
2023-11-16 18:27:03 +08:00
{
return;
}
2023-11-19 22:57:17 +08:00
if (CanEditorAreaType && _currEditRegionArea == null)
2023-11-19 14:01:26 +08:00
return;
2023-11-16 18:27:03 +08:00
var map = _map;
2023-11-21 19:52:50 +08:00
if (isOpTrue && TerrainTypeIndex >= 0 && CanBrushTerrainType)
2023-11-16 18:27:03 +08:00
{
SetCellTerritory(tgs, cellIndex, TerrainTypeIndex);
}
2023-11-21 19:52:50 +08:00
else if (!isOpTrue && CanBrushTerrainType)
2023-11-16 18:27:03 +08:00
{
2023-12-21 16:07:20 +08:00
if (_currentMapData.BlocksData[cellIndex].TerrainTypeIndex == TerrainTypeIndex)
{
2024-10-09 13:38:54 +08:00
ClearCellTerritory(tgs, cellIndex);
2023-12-21 16:07:20 +08:00
}
2023-11-16 18:27:03 +08:00
}
2023-11-21 19:52:50 +08:00
if (isOpTrue && canEditorRegion)
2023-11-16 18:27:03 +08:00
{
2023-11-19 14:01:26 +08:00
_currEditRegionArea.AddPosition(cellIndex);
2023-11-16 18:27:03 +08:00
}
2023-11-21 19:52:50 +08:00
else if (!isOpTrue && canEditorRegion)
2023-11-16 18:27:03 +08:00
{
2023-11-19 14:01:26 +08:00
_currEditRegionArea.RemovePosition(cellIndex);
2023-11-16 18:27:03 +08:00
}
2024-10-09 14:18:50 +08:00
if (isOpTrue && CanBrushBuffCellIndex)
{
AddBuffCell(cellIndex);
}
else if (!isOpTrue && CanBrushBuffCellIndex)
{
RemoveBuffCell(cellIndex);
}
2023-11-16 18:27:03 +08:00
}
private void SetCellTerritory(TerrainGridSystem tgs, int cellIndex, int TerrainTypeIndex)
{
var map = _map;
// Key: TerrainTypeIndex Value: tgs.territories.index
if (!map.TerrainTypeIndexDic.ContainsKey(TerrainTypeIndex))
{
Territory territory = tgs.TerritoryCreate(cellIndex);
map.TerrainTypeIndexDic.Add(TerrainTypeIndex, map.TerrainTypeIndexDic.Count);
territory.fillColor = _terrainTypeConfig[TerrainTypeIndex].Color;
}
2024-11-07 15:05:36 +08:00
if (!_terrainTypeConfig[TerrainTypeIndex].selectable)
2023-11-16 18:27:03 +08:00
{
map.TerrainGridSystem.CellSetBorderVisible(cellIndex, false);
}
map?.SetTerrainType(cellIndex, TerrainTypeIndex);
tgs.CellSetTerritory(cellIndex, map.TerrainTypeIndexDic[TerrainTypeIndex]);
Debug.Log(
$"Set Territory, cellIndex ({cellIndex}), terrain type ({TerrainTypeIndex}), name ({_terrainTypeConfig[TerrainTypeIndex].Color})");
}
private void ClearCellTerritory(TerrainGridSystem tgs, int cellIndex)
{
var map = _map;
map.TerrainGridSystem.CellSetBorderVisible(cellIndex, true);
var tIndex = tgs.CellGetTerritoryIndex(cellIndex);
if (tIndex != -1)
{
tgs.CellSetTerritory(cellIndex, -1);
map?.SetTerrainType(cellIndex, -1);
2024-10-09 13:38:54 +08:00
2023-11-16 18:27:03 +08:00
Debug.Log($"Cell Clear Territory, cellIndex({cellIndex})");
}
}
private bool banSaveMapList = false;
2023-11-17 18:16:53 +08:00
private void SetCellBorderType()
{
var map = _map;
if (map != null)
{
map.TerrainGridSystem.cellBorderColor = cellBorderColor / 255f;
map.TerrainGridSystem.cellBorderThickness = cellBorderWidth;
}
}
2023-11-16 18:27:03 +08:00
private static IEnumerable GetTerrainTypeProperties()
{
_terrainTypeConfig = TerrainTypeConfig.TerrainTypeProperties;
var list = new ValueDropdownList<int>();
if (_terrainTypeConfig != null)
{
for (int i = 0; i < _terrainTypeConfig.Count; i++)
{
list.Add(_terrainTypeConfig[i].name, i);
}
}
return list;
}
2023-11-20 12:25:41 +08:00
private void ClearRegionColor()
{
if (_map == null)
return;
for (int i = 0; i < _map.MapData.regions.Count; i++)
{
_map.SetRegionColor(i, false, regionColor);
}
}
2024-10-09 14:18:50 +08:00
private void AddBuffCell(int cellIndex)
{
// 阵地buff指示
2024-10-09 15:07:23 +08:00
if (!_buffPrefabDic.ContainsKey(cellIndex) && buffObject != null)
2024-10-09 14:18:50 +08:00
{
2024-10-09 15:07:23 +08:00
var buff = CreateBuffObj(cellIndex, buffObject);
2024-10-09 14:18:50 +08:00
_buffPrefabDic.Add(cellIndex, buff);
2024-10-09 15:07:23 +08:00
var path = "Assets/" + BuffPrefabPath + "/" + buffObject.name+".prefab";
_buffPathDic.Add(cellIndex, path);
DebugUtil.Log("格子{0}添加指示图标,路径为{1}", cellIndex, path);
2024-10-09 14:18:50 +08:00
}
}
private void RemoveBuffCell(int cellIndex)
{
if (_buffPrefabDic.TryGetValue(cellIndex, out var buff))
{
if (buff != null)
{
DestroyImmediate(buff);
}
_buffPrefabDic.Remove(cellIndex);
2024-10-09 15:07:23 +08:00
_buffPathDic.Remove(cellIndex);
2024-10-09 14:18:50 +08:00
DebugUtil.Log("格子{0}移除指示图标", cellIndex);
}
}
2024-10-09 15:07:23 +08:00
private GameObject CreateBuffObj(int cellIndex, GameObject buffObj)
2024-10-09 13:38:54 +08:00
{
var posV3 = _map.TGSCellIndex2WorldPosition(cellIndex);
2024-10-09 15:07:23 +08:00
var buff = Instantiate(buffObj, posV3, buffObj.transform.rotation);
2024-10-09 13:38:54 +08:00
buff.SetActive(true);
2024-10-12 13:41:38 +08:00
/*var buffParticle = buff.transform.Find("EndNotice").gameObject;
2024-10-09 13:38:54 +08:00
var particleSystem = buffParticle.GetComponentInChildren<ParticleSystem>();
if (particleSystem != null)
{
particleSystem.Play();
SceneView.RepaintAll();
2024-10-12 13:41:38 +08:00
}*/
2024-10-09 13:38:54 +08:00
return buff;
}
2023-11-16 18:27:03 +08:00
#endregion
}
}