Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / TileSetEditorTerrainPanel.cs
  1. using Godot;
  2. using UI.TileSetEditor;
  3.  
  4. namespace UI.TileSetEditorTerrain;
  5.  
  6. public partial class TileSetEditorTerrainPanel : TileSetEditorTerrain
  7. {
  8. /// <summary>
  9. /// 父Ui
  10. /// </summary>
  11. public TileSetEditorPanel EditorPanel;
  12.  
  13. /// <summary>
  14. /// 当前选中的地形索引
  15. /// </summary>
  16. public int CurrTerrainIndex => TerrainTabGrid.SelectIndex;
  17.  
  18. /// <summary>
  19. /// 当前选中的地形
  20. /// </summary>
  21. public TileSetTerrainInfo CurrTerrain
  22. {
  23. get
  24. {
  25. var terrain = EditorPanel.TileSetSourceInfo.Terrain;
  26. if (CurrTerrainIndex < 0 || CurrTerrainIndex >= terrain.Count)
  27. {
  28. return null;
  29. }
  30. return terrain[CurrTerrainIndex];
  31. }
  32. }
  33. /// <summary>
  34. /// 正在拖拽的图块
  35. /// </summary>
  36. public MaskCell DraggingCell { get; set; }
  37.  
  38. public UiGrid<TerrainTab, TileSetTerrainInfo> TerrainTabGrid;
  39. public UiGrid<RightCell, byte> TerrainGrid3x3;
  40. public UiGrid<RightCell, byte> TerrainGrid2x2;
  41. public UiGrid<RightCell, byte> TerrainGridMiddle;
  42. public UiGrid<RightCell, byte> TerrainGridFloor;
  43. public UiGrid<BottomCell, Rect2I> MaskGrid;
  44.  
  45. private bool _refreshGridConnect = false;
  46.  
  47. public override void OnCreateUi()
  48. {
  49. EditorPanel = (TileSetEditorPanel)ParentUi;
  50. S_DragSprite.Instance.Visible = false;
  51. //改变选中的TileSet资源
  52. AddEventListener(EventEnum.OnSelectTileSetSource, OnSelectTileSetSource);
  53. //改变纹理事件
  54. AddEventListener(EventEnum.OnSetTileTexture, OnSetTileTexture);
  55. //背景颜色改变
  56. AddEventListener(EventEnum.OnSetTileSetBgColor, OnChangeTileSetBgColor);
  57.  
  58. //地形页签
  59. TerrainTabGrid = CreateUiGrid<TerrainTab, TileSetTerrainInfo, TerrainTabCell>(S_TerrainTab);
  60. TerrainTabGrid.SetCellOffset(Vector2I.Zero);
  61. TerrainTabGrid.SetColumns(1);
  62. TerrainTabGrid.SetHorizontalExpand(true);
  63.  
  64. MaskGrid = CreateUiGrid<BottomCell, Rect2I, MaskCell>(S_BottomCell);
  65. MaskGrid.SetCellOffset(Vector2I.Zero);
  66. MaskGrid.GridContainer.MouseFilter = MouseFilterEnum.Ignore;
  67.  
  68. TerrainGrid3x3 = InitTopGrid(S_TerrainRoot.L_TerrainTexture1.Instance, GameConfig.TerrainBit3x3, TileSetTerrainInfo.TerrainLayerType);
  69. TerrainGrid2x2 = InitTopGrid(S_TerrainRoot.L_TerrainTexture4.Instance, GameConfig.TerrainBit2x2, TileSetTerrainInfo.TerrainLayerType);
  70. TerrainGridMiddle = InitTopGrid(S_TerrainRoot.L_TerrainTexture2.Instance, GameConfig.TerrainBitMiddle, TileSetTerrainInfo.MiddleLayerType);
  71. TerrainGridFloor = InitTopGrid(S_TerrainRoot.L_TerrainTexture3.Instance, GameConfig.TerrainBitFloor, TileSetTerrainInfo.FloorLayerType);
  72. //删除地形按钮
  73. S_DeleteButton.Instance.Pressed += OnDeleteTerrainClick;
  74. //添加地形按钮
  75. S_AddButton.Instance.Pressed += OnAddTerrainClick;
  76. //编辑地形
  77. S_EditButton.Instance.Pressed += OnEditTerrainClick;
  78. OnSetTileTexture(EditorPanel.Texture);
  79. OnChangeTileSetBgColor(EditorPanel.BgColor);
  80. OnChangeTerrainType(-1);
  81. //改变选中的地形
  82. TerrainTabGrid.SelectEvent += OnChangeTerrain;
  83. }
  84.  
  85. public override void OnDestroyUi()
  86. {
  87. }
  88.  
  89. public override void Process(float delta)
  90. {
  91. S_MaskBrush.Instance.Visible = DraggingCell == null;
  92.  
  93. if (_refreshGridConnect)
  94. {
  95. _refreshGridConnect = false;
  96.  
  97. var terrain = CurrTerrain;
  98. if (terrain != null)
  99. {
  100. if (terrain.TerrainType == 0) //选中47格Terrain
  101. {
  102. TerrainGrid3x3.ForEach(cell => RefreshConnectTerrainCell(terrain, cell));
  103. if (EditorPanel.TileSetSourceIndex == 0 && CurrTerrainIndex == 0) //选中Main Source
  104. {
  105. TerrainGridMiddle.ForEach(cell => RefreshConnectTerrainCell(terrain, cell));
  106. TerrainGridFloor.ForEach(cell => RefreshConnectTerrainCell(terrain, cell));
  107. }
  108. }
  109. else //选中13格Terrain
  110. {
  111. TerrainGrid2x2.ForEach(cell => RefreshConnectTerrainCell(terrain, cell));
  112. }
  113. }
  114. }
  115. }
  116.  
  117. private UiGrid<RightCell, byte> InitTopGrid(Control texture, Vector2I size, byte type)
  118. {
  119. var cellRoot = S_TopBg.L_TerrainRoot.L_CellRoot;
  120. var sRightCell = cellRoot.L_RightCell;
  121. sRightCell.Instance.Position = texture.Position;
  122. var grid = CreateUiGrid<RightCell, byte, TerrainCell>(sRightCell, cellRoot.Instance);
  123. grid.SetCellOffset(Vector2I.Zero);
  124. grid.SetColumns(size.X);
  125. for (var y = 0; y < size.Y; y++)
  126. {
  127. for (var x = 0; x < size.X; x++)
  128. {
  129. grid.Add(type);
  130. }
  131. }
  132. return grid;
  133. }
  134.  
  135. //改变选中的TileSet资源
  136. private void OnSelectTileSetSource(object obj)
  137. {
  138. TerrainTabGrid.RemoveAll();
  139. //加载Terrain
  140. if (obj != null)
  141. {
  142. var terrainList = ((TileSetSourceInfo)obj).Terrain;
  143. if (terrainList.Count > 0)
  144. {
  145. TerrainTabGrid.SetDataList(terrainList.ToArray());
  146. TerrainTabGrid.SelectIndex = 0;
  147. }
  148. }
  149. }
  150.  
  151. private void SetTerrainCellData(TileSetTerrainInfo terrain, UiCell<RightCell, byte> cell)
  152. {
  153. var data = terrain.GetTerrainCell(cell.Index, cell.Data);
  154. if (data != null)
  155. {
  156. var terrainCell = (TerrainCell)cell;
  157. var x = data[0];
  158. var y = data[1];
  159. terrainCell.SetCell(new Rect2I(x, y, GameConfig.TileCellSize, GameConfig.TileCellSize));
  160. }
  161. }
  162. private void RefreshConnectTerrainCell(TileSetTerrainInfo terrain, UiCell<RightCell, byte> cell)
  163. {
  164. var data = terrain.GetTerrainCell(cell.Index, cell.Data);
  165. if (data != null)
  166. {
  167. var terrainCell = (TerrainCell)cell;
  168. var x = data[0];
  169. var y = data[1];
  170. var index = x / GameConfig.TileCellSize + y / GameConfig.TileCellSize * MaskGrid.GetColumns();
  171. var maskCell = (MaskCell)MaskGrid.GetCell(index);
  172. if (maskCell != null)
  173. {
  174. //绑定TerrainCell
  175. maskCell.SetConnectTerrainCell(terrainCell);
  176. }
  177. }
  178. }
  179.  
  180. /// <summary>
  181. /// 放置地形Cell纹理
  182. /// </summary>
  183. public void OnDropCell(MaskCell maskCell)
  184. {
  185. var terrain = CurrTerrain;
  186. if (terrain == null)
  187. {
  188. return;
  189. }
  190. if (terrain.TerrainType == 0) //选中47个Terrain
  191. {
  192. var flag = true;
  193. TerrainGrid3x3.ForEach((cell) =>
  194. {
  195. flag = !((TerrainCell)cell).OnDropCell(maskCell);
  196. return flag;
  197. });
  198. if (EditorPanel.TileSetSourceIndex == 0 && CurrTerrainIndex == 0) //选中Main Source
  199. {
  200. if (flag)
  201. {
  202. TerrainGridMiddle.ForEach((cell) =>
  203. {
  204. flag = !((TerrainCell)cell).OnDropCell(maskCell);
  205. return flag;
  206. });
  207. }
  208. if (flag)
  209. {
  210. TerrainGridFloor.ForEach((cell) =>
  211. {
  212. return ((TerrainCell)cell).OnDropCell(maskCell);
  213. });
  214. }
  215. }
  216. }
  217. else //选中13格Terrain
  218. {
  219. TerrainGrid2x2.ForEach((cell) =>
  220. {
  221. return !((TerrainCell)cell).OnDropCell(maskCell);
  222. });
  223. }
  224. }
  225. //改变TileSet纹理
  226. private void OnSetTileTexture(object arg)
  227. {
  228. S_BottomBg.Instance.OnChangeTileSetTexture();
  229.  
  230. MaskGrid.RemoveAll();
  231. var cellHorizontal = EditorPanel.CellHorizontal;
  232. if (cellHorizontal <= 0)
  233. {
  234. return;
  235. }
  236. var cellVertical = EditorPanel.CellVertical;
  237. MaskGrid.SetColumns(cellHorizontal);
  238. for (var y = 0; y < cellVertical; y++)
  239. {
  240. for (var x = 0; x < cellHorizontal; x++)
  241. {
  242. MaskGrid.Add(new Rect2I(x * GameConfig.TileCellSize, y * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize));
  243. }
  244. }
  245.  
  246. _refreshGridConnect = true;
  247. }
  248. //更改背景颜色
  249. private void OnChangeTileSetBgColor(object obj)
  250. {
  251. S_BottomBg.Instance.Color = EditorPanel.BgColor;
  252. S_TopBg.Instance.Color = EditorPanel.BgColor;
  253. }
  254.  
  255. //切换选中的地形
  256. private void OnChangeTerrain(int index)
  257. {
  258. //清除所有绑定的Terrain
  259. if (index >= 0)
  260. {
  261. TerrainGrid3x3.ForEach(cell => ((TerrainCell)cell).ClearCell());
  262. TerrainGridMiddle.ForEach(cell => ((TerrainCell)cell).ClearCell());
  263. TerrainGridFloor.ForEach(cell => ((TerrainCell)cell).ClearCell());
  264. TerrainGrid2x2.ForEach(cell => ((TerrainCell)cell).ClearCell());
  265. }
  266. S_TopBg.Instance.SetHoverCell(null);
  267. S_BottomBg.Instance.SetHoverCell(null);
  268. var terrain = CurrTerrain;
  269. if (terrain != null)
  270. {
  271. if (terrain.TerrainType == 0) //选中47个Terrain
  272. {
  273. TerrainGrid3x3.ForEach(cell => SetTerrainCellData(terrain, cell));
  274. if (EditorPanel.TileSetSourceIndex == 0 && CurrTerrainIndex == 0) //选中Main Source
  275. {
  276. TerrainGridMiddle.ForEach(cell => SetTerrainCellData(terrain, cell));
  277. TerrainGridFloor.ForEach(cell => SetTerrainCellData(terrain, cell));
  278. }
  279. }
  280. else //选中13格Terrain
  281. {
  282. TerrainGrid2x2.ForEach(cell => SetTerrainCellData(terrain, cell));
  283. }
  284. }
  285. if (index >= 0)
  286. {
  287. OnChangeTerrainType(TerrainTabGrid.GetCell(index).Data.TerrainType);
  288. }
  289. else
  290. {
  291. OnChangeTerrainType(-1);
  292. }
  293. }
  294. /// <summary>
  295. /// 切换Terrain类型
  296. /// </summary>
  297. private void OnChangeTerrainType(long index)
  298. {
  299. if (EditorPanel.TileSetSourceIndex == 0 && CurrTerrainIndex == 0) //选中 Main Source
  300. {
  301. S_TerrainTexture2.Instance.Visible = true;
  302. S_TerrainTexture3.Instance.Visible = true;
  303. TerrainGridMiddle.Visible = true;
  304. TerrainGridFloor.Visible = true;
  305. S_TerrainTexture1.L_Label.Instance.Text = "顶部墙壁";
  306. }
  307. else
  308. {
  309. S_TerrainTexture2.Instance.Visible = false;
  310. S_TerrainTexture3.Instance.Visible = false;
  311. TerrainGridMiddle.Visible = false;
  312. TerrainGridFloor.Visible = false;
  313. S_TerrainTexture1.L_Label.Instance.Text = "地形";
  314. }
  315. if (index == 0) //47 格子
  316. {
  317. S_TerrainRoot.L_TerrainTexture1.Instance.Visible = true;
  318. TerrainGrid3x3.Visible = true;
  319. S_TerrainRoot.L_TerrainTexture4.Instance.Visible = false;
  320. TerrainGrid2x2.Visible = false;
  321. }
  322. else if (index == 1) //13 格子
  323. {
  324. S_TerrainRoot.L_TerrainTexture1.Instance.Visible = false;
  325. TerrainGrid3x3.Visible = false;
  326. S_TerrainRoot.L_TerrainTexture4.Instance.Visible = true;
  327. TerrainGrid2x2.Visible = true;
  328. }
  329. else
  330. {
  331. S_TerrainRoot.L_TerrainTexture1.Instance.Visible = false;
  332. TerrainGrid3x3.Visible = false;
  333. S_TerrainRoot.L_TerrainTexture4.Instance.Visible = false;
  334. TerrainGrid2x2.Visible = false;
  335. }
  336. }
  337.  
  338. //删除地形
  339. private void OnDeleteTerrainClick()
  340. {
  341. if (EditorPanel.TileSetSourceIndex == 0 && CurrTerrainIndex == 0) //不能删除 Main Terrain
  342. {
  343. EditorWindowManager.ShowTips("警告", "不允许删除 Main Terrain!");
  344. return;
  345. }
  346.  
  347. var terrain = CurrTerrain;
  348. if (terrain != null)
  349. {
  350. EditorWindowManager.ShowConfirm("提示", $"是否删除地形'{terrain.Name}'?", (v) =>
  351. {
  352. if (v)
  353. {
  354. var index = TerrainTabGrid.SelectIndex;
  355. //执行删除操作
  356. EditorPanel.TileSetSourceInfo.Terrain.Remove(terrain);
  357. TerrainTabGrid.RemoveByIndex(index);
  358. if (index == TerrainTabGrid.Count)
  359. {
  360. TerrainTabGrid.SelectIndex = TerrainTabGrid.Count - 1;
  361. }
  362. else
  363. {
  364. TerrainTabGrid.SelectIndex = index;
  365. }
  366. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  367. }
  368. });
  369. }
  370. else
  371. {
  372. EditorWindowManager.ShowTips("提示", "清选择一个Terrain!");
  373. }
  374. }
  375.  
  376. //创建地形
  377. private void OnAddTerrainClick()
  378. {
  379. EditorWindowManager.ShowCreateTerrain(EditorPanel.TileSetSourceInfo, terrainInfo =>
  380. {
  381. //执行添加操作
  382. EditorPanel.TileSetSourceInfo.Terrain.Add(terrainInfo);
  383. TerrainTabGrid.Add(terrainInfo, true);
  384. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  385. });
  386. }
  387. //编辑地形(重命名)
  388. private void OnEditTerrainClick()
  389. {
  390. if (EditorPanel.TileSetSourceIndex == 0 && CurrTerrainIndex == 0) //不能删除 Main Terrain
  391. {
  392. EditorWindowManager.ShowTips("警告", "不允许重命名 Main Terrain!");
  393. return;
  394. }
  395. var terrain = CurrTerrain;
  396. if (terrain != null)
  397. {
  398. EditorWindowManager.ShowInput("提示", $"是否删除地形'{terrain.Name}'?", terrain.Name, (v, isClose) =>
  399. {
  400. if (string.IsNullOrEmpty(v))
  401. {
  402. EditorWindowManager.ShowTips("错误", $"名称不允许为空!");
  403. return false;
  404. }
  405. if (terrain.Name != v && EditorPanel.TileSetSourceInfo.Terrain.FindIndex(info => info.Name == v) >= 0)
  406. {
  407. EditorWindowManager.ShowTips("错误", $"已经有相同名称的Terrain了!");
  408. return false;
  409. }
  410.  
  411. terrain.Name = v;
  412. //刷新页签
  413. ((TerrainTabCell)TerrainTabGrid.GetCell(CurrTerrainIndex)).RefreshData();
  414. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  415. return true;
  416. });
  417. }
  418. else
  419. {
  420. EditorWindowManager.ShowTips("提示", "清选择一个Terrain!");
  421. }
  422. }
  423. }