Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / TileSetEditorTerrainPanel.cs
@小李xl 小李xl on 11 Jan 2024 8 KB 制作2x2地形
  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. /// <summary>
  13. /// 正在拖拽的图块
  14. /// </summary>
  15. public MaskCell DraggingCell { get; set; }
  16.  
  17. public UiGrid<RightCell, byte> TopGrid1;
  18. public UiGrid<RightCell, byte> TopGrid2;
  19. public UiGrid<RightCell, byte> TopGrid3;
  20. public UiGrid<RightCell, byte> TopGrid4;
  21. public UiGrid<BottomCell, Rect2I> BottomGrid;
  22.  
  23. private bool _refreshGridConnect = false;
  24.  
  25. public override void OnCreateUi()
  26. {
  27. EditorPanel = (TileSetEditorPanel)ParentUi;
  28. S_DragSprite.Instance.Visible = false;
  29. //改变选中的TileSet资源
  30. AddEventListener(EventEnum.OnSelectTileSetSource, OnSelectTileSetSource);
  31. //改变纹理事件
  32. AddEventListener(EventEnum.OnSetTileTexture, OnSetTileTexture);
  33. //背景颜色改变
  34. AddEventListener(EventEnum.OnSetTileSetBgColor, OnChangeTileSetBgColor);
  35. BottomGrid = CreateUiGrid<BottomCell, Rect2I, MaskCell>(S_BottomCell);
  36. BottomGrid.SetCellOffset(Vector2I.Zero);
  37. BottomGrid.GridContainer.MouseFilter = MouseFilterEnum.Ignore;
  38.  
  39. TopGrid1 = InitTopGrid(S_TerrainRoot.L_TerrainTexture1.Instance, GameConfig.TerrainBitSize1, TileSetTerrainInfo.TopLayerType);
  40. TopGrid2 = InitTopGrid(S_TerrainRoot.L_TerrainTexture2.Instance, GameConfig.TerrainBitSize2, TileSetTerrainInfo.MiddleLayerType);
  41. TopGrid3 = InitTopGrid(S_TerrainRoot.L_TerrainTexture3.Instance, GameConfig.TerrainBitSize3, TileSetTerrainInfo.FloorLayerType);
  42. TopGrid4 = InitTopGrid(S_TerrainRoot.L_TerrainTexture3.Instance, GameConfig.TerrainBitSize4, TileSetTerrainInfo.Terrain2x2Type);
  43.  
  44. OnSetTileTexture(EditorPanel.Texture);
  45. OnChangeTileSetBgColor(EditorPanel.BgColor);
  46. }
  47.  
  48. public override void OnDestroyUi()
  49. {
  50. }
  51.  
  52. public override void Process(float delta)
  53. {
  54. S_MaskBrush.Instance.Visible = DraggingCell == null;
  55.  
  56. if (_refreshGridConnect)
  57. {
  58. _refreshGridConnect = true;
  59.  
  60. var terrain = EditorPanel.TileSetSourceInfo.Terrain;
  61. TopGrid1.ForEach(cell => RefreshConnectTerrainCell(terrain, cell));
  62. if (EditorPanel.TileSetSourceIndex == 0) //必须选中Main Source
  63. {
  64. TopGrid2.ForEach(cell => RefreshConnectTerrainCell(terrain, cell));
  65. TopGrid3.ForEach(cell => RefreshConnectTerrainCell(terrain, cell));
  66. }
  67. }
  68. }
  69.  
  70. private UiGrid<RightCell, byte> InitTopGrid(Control texture, Vector2I size, byte type)
  71. {
  72. var cellRoot = S_TopBg.L_TerrainRoot.L_CellRoot;
  73. var sRightCell = cellRoot.L_RightCell;
  74. sRightCell.Instance.Position = texture.Position;
  75. var grid = CreateUiGrid<RightCell, byte, TerrainCell>(sRightCell, cellRoot.Instance);
  76. grid.SetCellOffset(Vector2I.Zero);
  77. grid.SetColumns(size.X);
  78. for (var y = 0; y < size.Y; y++)
  79. {
  80. for (var x = 0; x < size.X; x++)
  81. {
  82. grid.Add(type);
  83. }
  84. }
  85. return grid;
  86. }
  87.  
  88. //改变选中的TileSet资源
  89. private void OnSelectTileSetSource(object obj)
  90. {
  91. //先清除所有绑定的Terrain
  92. TopGrid1.ForEach(cell => ((TerrainCell)cell).ClearCell());
  93. TopGrid2.ForEach(cell => ((TerrainCell)cell).ClearCell());
  94. TopGrid3.ForEach(cell => ((TerrainCell)cell).ClearCell());
  95. TopGrid4.ForEach(cell => ((TerrainCell)cell).ClearCell());
  96. S_TopBg.Instance.SetHoverCell(null);
  97. S_BottomBg.Instance.SetHoverCell(null);
  98. var sourceIndex = EditorPanel.TileSetSourceIndex;
  99. if (sourceIndex == 0) //选中Main Source时就只能使用 47 Terrain
  100. {
  101. S_TerrainTexture2.Instance.Visible = true;
  102. S_TerrainTexture3.Instance.Visible = true;
  103. TopGrid2.Visible = true;
  104. TopGrid3.Visible = true;
  105. S_TerrainTexture1.L_Label.Instance.Text = "顶部墙壁";
  106. S_TerrainTypeButton.Instance.Selected = 0;
  107. S_TerrainTypeButton.Instance.Visible = false;
  108. }
  109. else
  110. {
  111. S_TerrainTexture2.Instance.Visible = false;
  112. S_TerrainTexture3.Instance.Visible = false;
  113. TopGrid2.Visible = false;
  114. TopGrid3.Visible = false;
  115. S_TerrainTexture1.L_Label.Instance.Text = "地形";
  116. S_TerrainTypeButton.Instance.Visible = true;
  117. }
  118. //再加载Terrain
  119. if (obj != null)
  120. {
  121. var terrain = ((TileSetSourceInfo)obj).Terrain;
  122. if (sourceIndex == 0) //选中Main Source
  123. {
  124. TopGrid1.ForEach(cell => SetTerrainCellData(terrain, cell));
  125. TopGrid2.ForEach(cell => SetTerrainCellData(terrain, cell));
  126. TopGrid3.ForEach(cell => SetTerrainCellData(terrain, cell));
  127. }
  128. else if (S_TerrainTypeButton.Instance.Selected == 0) //选中47个Terrain
  129. {
  130. TopGrid1.ForEach(cell => SetTerrainCellData(terrain, cell));
  131. }
  132. else //选中13格Terrain
  133. {
  134. TopGrid4.ForEach(cell => SetTerrainCellData(terrain, cell));
  135. }
  136. }
  137. }
  138.  
  139. private void SetTerrainCellData(TileSetTerrainInfo terrain, UiCell<RightCell, byte> cell)
  140. {
  141. var data = terrain.GetTerrainCell(cell.Index, cell.Data);
  142. if (data != null)
  143. {
  144. var terrainCell = (TerrainCell)cell;
  145. var x = data[0];
  146. var y = data[1];
  147. terrainCell.SetCell(new Rect2I(x, y, GameConfig.TileCellSize, GameConfig.TileCellSize));
  148. }
  149. }
  150. private void RefreshConnectTerrainCell(TileSetTerrainInfo terrain, UiCell<RightCell, byte> cell)
  151. {
  152. var data = terrain.GetTerrainCell(cell.Index, cell.Data);
  153. if (data != null)
  154. {
  155. var terrainCell = (TerrainCell)cell;
  156. var x = data[0];
  157. var y = data[1];
  158. var index = x / GameConfig.TileCellSize + y / GameConfig.TileCellSize * BottomGrid.GetColumns();
  159. var maskCell = (MaskCell)BottomGrid.GetCell(index);
  160. if (maskCell != null)
  161. {
  162. //绑定TerrainCell
  163. maskCell.SetConnectTerrainCell(terrainCell);
  164. }
  165. }
  166. }
  167.  
  168. /// <summary>
  169. /// 放置地形Cell纹理
  170. /// </summary>
  171. public void OnDropCell(MaskCell maskCell)
  172. {
  173. if (EditorPanel.TileSetSourceIndex == 0) //选中Main Source
  174. {
  175. var flag = true;
  176. TopGrid1.ForEach((cell) =>
  177. {
  178. flag = !((TerrainCell)cell).OnDropCell(maskCell);
  179. return flag;
  180. });
  181. if (flag)
  182. {
  183. TopGrid2.ForEach((cell) =>
  184. {
  185. flag = !((TerrainCell)cell).OnDropCell(maskCell);
  186. return flag;
  187. });
  188. }
  189. if (flag)
  190. {
  191. TopGrid3.ForEach((cell) =>
  192. {
  193. return ((TerrainCell)cell).OnDropCell(maskCell);
  194. });
  195. }
  196. }
  197. else if (S_TerrainTypeButton.Instance.Selected == 0) //选中47个Terrain
  198. {
  199. TopGrid1.ForEach((cell) =>
  200. {
  201. return !((TerrainCell)cell).OnDropCell(maskCell);
  202. });
  203. }
  204. else //选中13格Terrain
  205. {
  206. TopGrid4.ForEach((cell) =>
  207. {
  208. return !((TerrainCell)cell).OnDropCell(maskCell);
  209. });
  210. }
  211. }
  212. //改变TileSet纹理
  213. private void OnSetTileTexture(object arg)
  214. {
  215. S_BottomBg.Instance.OnChangeTileSetTexture();
  216.  
  217. BottomGrid.RemoveAll();
  218. var cellHorizontal = EditorPanel.CellHorizontal;
  219. if (cellHorizontal <= 0)
  220. {
  221. return;
  222. }
  223. var cellVertical = EditorPanel.CellVertical;
  224. BottomGrid.SetColumns(cellHorizontal);
  225. for (var y = 0; y < cellVertical; y++)
  226. {
  227. for (var x = 0; x < cellHorizontal; x++)
  228. {
  229. BottomGrid.Add(new Rect2I(x * GameConfig.TileCellSize, y * GameConfig.TileCellSize, GameConfig.TileCellSize, GameConfig.TileCellSize));
  230. }
  231. }
  232.  
  233. _refreshGridConnect = true;
  234. }
  235. //更改背景颜色
  236. private void OnChangeTileSetBgColor(object obj)
  237. {
  238. S_BottomBg.Instance.Color = EditorPanel.BgColor;
  239. S_TopBg.Instance.Color = EditorPanel.BgColor;
  240. }
  241. }