Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditorMapTile / TerrainCell.cs
@小李xl 小李xl on 19 Jan 2024 4 KB 绘制自定义地形, 开发中
  1. using Godot;
  2. using static TerrainPeering;
  3.  
  4. namespace UI.MapEditorMapTile;
  5.  
  6. /// <summary>
  7. /// 地形选项
  8. /// </summary>
  9. public class TerrainCell : UiCell<MapEditorMapTile.TerrainItem, TerrainData>
  10. {
  11. private Image _image;
  12. private ImageTexture _texture;
  13. public override void OnInit()
  14. {
  15. _texture = new ImageTexture();
  16. CellNode.L_TerrainPreview.Instance.Texture = _texture;
  17. CellNode.L_Select.Instance.Visible = false;
  18. CellNode.L_ErrorIcon.Instance.Visible = false;
  19. }
  20.  
  21. public override void OnSetData(TerrainData data)
  22. {
  23. //是否可以使用
  24. if (!data.TerrainInfo.Ready)
  25. {
  26. CellNode.L_ErrorIcon.Instance.Visible = true;
  27. CellNode.Instance.TooltipText = "该地形Bit配置未完成,请在TileSet编辑器中配置!";
  28. }
  29. else
  30. {
  31. CellNode.L_ErrorIcon.Instance.Visible = false;
  32. CellNode.Instance.TooltipText = "";
  33. }
  34. if (_image != null)
  35. {
  36. _image.Dispose();
  37. }
  38.  
  39. //创建预览图
  40. _image = Image.Create(
  41. 3 * GameConfig.TileCellSize,
  42. 3 * GameConfig.TileCellSize,
  43. false, Image.Format.Rgba8
  44. );
  45. if (data.TerrainInfo.TerrainType == 0) //3x3
  46. {
  47. CellNode.L_TerrainName.Instance.Text = data.TerrainInfo.Name + "\n3x3";
  48. var src = CellNode.UiPanel.TileSetSourceInfo.GetSourceImage();
  49. SetImageSrc(src, Center | Right | Bottom | RightBottom, new Vector2I(0, 0));
  50. SetImageSrc(src, Center | Left | Right | Bottom | LeftBottom | RightBottom, new Vector2I(GameConfig.TileCellSize, 0));
  51. SetImageSrc(src, Center | Left | Bottom | LeftBottom, new Vector2I(GameConfig.TileCellSize * 2, 0));
  52. SetImageSrc(src, Center | Right | Top | Bottom | RightTop | RightBottom, new Vector2I(0, GameConfig.TileCellSize));
  53. SetImageSrc(src, Center | Left | Right | Bottom | Top | LeftTop | LeftBottom | RightTop | RightBottom, new Vector2I(GameConfig.TileCellSize, GameConfig.TileCellSize));
  54. SetImageSrc(src, Center | Left | Top | Bottom | LeftTop | LeftBottom, new Vector2I(GameConfig.TileCellSize * 2, GameConfig.TileCellSize));
  55. SetImageSrc(src, Center | Right | Top | RightTop, new Vector2I(0, GameConfig.TileCellSize * 2));
  56. SetImageSrc(src, Center | Left | Top | Right | LeftTop | RightTop, new Vector2I(GameConfig.TileCellSize, GameConfig.TileCellSize * 2));
  57. SetImageSrc(src, Center | Left | Top | LeftTop, new Vector2I(GameConfig.TileCellSize * 2, GameConfig.TileCellSize * 2));
  58. }
  59. else //2x2
  60. {
  61. CellNode.L_TerrainName.Instance.Text = data.TerrainInfo.Name + "\n2x2";
  62. var src = CellNode.UiPanel.TileSetSourceInfo.GetSourceImage();
  63. SetImageSrc(src, Center | RightBottom, new Vector2I(0, 0));
  64. SetImageSrc(src, Center | LeftBottom | RightBottom, new Vector2I(GameConfig.TileCellSize, 0));
  65. SetImageSrc(src, Center | LeftBottom, new Vector2I(GameConfig.TileCellSize * 2, 0));
  66. SetImageSrc(src, Center | RightTop | RightBottom, new Vector2I(0, GameConfig.TileCellSize));
  67. SetImageSrc(src, Center | LeftTop | LeftBottom | RightTop | RightBottom, new Vector2I(GameConfig.TileCellSize, GameConfig.TileCellSize));
  68. SetImageSrc(src, Center | LeftTop | LeftBottom, new Vector2I(GameConfig.TileCellSize * 2, GameConfig.TileCellSize));
  69. SetImageSrc(src, Center | RightTop, new Vector2I(0, GameConfig.TileCellSize * 2));
  70. SetImageSrc(src, Center | LeftTop | RightTop, new Vector2I(GameConfig.TileCellSize, GameConfig.TileCellSize * 2));
  71. SetImageSrc(src, Center | LeftTop, new Vector2I(GameConfig.TileCellSize * 2, GameConfig.TileCellSize * 2));
  72. }
  73. _texture.SetImage(_image);
  74. }
  75.  
  76. private void SetImageSrc(Image src, uint bit, Vector2I dst)
  77. {
  78. if (Data.TerrainInfo.T.TryGetValue(bit, out var temp))
  79. {
  80. var pos = Data.TerrainInfo.GetPosition(temp);
  81. _image.BlitRect(src, new Rect2I(pos * GameConfig.TileCellSize, GameConfig.TileCellSizeVector2I), dst);
  82. }
  83. }
  84. public override void OnDestroy()
  85. {
  86. if (_texture != null)
  87. {
  88. _texture.Dispose();
  89. _texture = null;
  90. }
  91. if (_image != null)
  92. {
  93. _image.Dispose();
  94. _image = null;
  95. }
  96. }
  97.  
  98. public override void OnSelect()
  99. {
  100. CellNode.L_Select.Instance.Visible = true;
  101. CellNode.UiPanel.EditorTileMap.SetCurrTerrain(Data);
  102. }
  103.  
  104. public override void OnUnSelect()
  105. {
  106. CellNode.L_Select.Instance.Visible = false;
  107. }
  108. }