Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditor / TileSetEditorPanel.cs
  1. using Godot;
  2.  
  3. namespace UI.TileSetEditor;
  4.  
  5. public partial class TileSetEditorPanel : TileSetEditor
  6. {
  7. /// <summary>
  8. /// 是否初始化过纹理
  9. /// </summary>
  10. public bool InitTexture { get; private set; }
  11. /// <summary>
  12. /// 纹理
  13. /// </summary>
  14. public ImageTexture Texture { get; private set; }
  15. /// <summary>
  16. /// 纹理的Image对象
  17. /// </summary>
  18. public Image TextureImage { get; private set; }
  19.  
  20. /// <summary>
  21. /// 背景颜色
  22. /// </summary>
  23. public Color BgColor { get; private set; }
  24. /// <summary>
  25. /// Cell 横轴数量
  26. /// </summary>
  27. public int CellHorizontal { get; private set; }
  28. /// <summary>
  29. /// Cell 纵轴数量
  30. /// </summary>
  31. public int CellVertical { get; private set; }
  32.  
  33. /// <summary>
  34. /// 页签对象
  35. /// </summary>
  36. public UiGrid<Tab, TileSetEditorTabData> TabGrid { get; private set; }
  37.  
  38. public override void OnCreateUi()
  39. {
  40. Texture = new ImageTexture();
  41. S_Back.Instance.Visible = PrevUi != null;
  42. S_Back.Instance.Pressed += OnBackClick;
  43.  
  44. TabGrid = new UiGrid<Tab, TileSetEditorTabData>(S_Tab, typeof(TileSetEditorTabCell));
  45. TabGrid.SetHorizontalExpand(true);
  46. TabGrid.SetCellOffset(new Vector2I(0, 5));
  47. TabGrid.Add(new TileSetEditorTabData()
  48. {
  49. Text = "纹理",
  50. UiName = UiManager.UiNames.TileSetEditorImport,
  51. });
  52. TabGrid.Add(new TileSetEditorTabData()
  53. {
  54. Text = "地形",
  55. UiName = UiManager.UiNames.TileSetEditorTerrain,
  56. });
  57. TabGrid.Add(new TileSetEditorTabData()
  58. {
  59. Text = "组合",
  60. UiName = UiManager.UiNames.TileSetEditorCombination,
  61. });
  62. }
  63.  
  64. public override void OnDestroyUi()
  65. {
  66. TabGrid.Destroy();
  67. if (Texture != null)
  68. {
  69. Texture.Dispose();
  70. }
  71.  
  72. if (TextureImage != null)
  73. {
  74. TextureImage.Dispose();
  75. }
  76. }
  77.  
  78. /// <summary>
  79. /// 初始化数据
  80. /// </summary>
  81. public void InitData(TileSetInfo tileSetInfo)
  82. {
  83. S_Title.Instance.Text = "正在编辑:" + tileSetInfo.Name;
  84. //SetTextureData(Image.LoadFromFile("resource/tileSprite/map1/16x16 dungeon ii wall reconfig v04 spritesheet.png"));
  85. TabGrid.SelectIndex = 0;
  86. }
  87.  
  88. /// <summary>
  89. /// 设置纹理
  90. /// </summary>
  91. public void SetTextureData(Image image)
  92. {
  93. InitTexture = true;
  94. Texture.SetImage(image);
  95. if (TextureImage != null)
  96. {
  97. TextureImage.Dispose();
  98. }
  99. TextureImage = image;
  100. CellHorizontal = image.GetWidth() / GameConfig.TileCellSize;
  101. CellVertical = image.GetHeight() / GameConfig.TileCellSize;
  102. //派发事件
  103. EventManager.EmitEvent(EventEnum.OnSetTileTexture, Texture);
  104. }
  105.  
  106. /// <summary>
  107. /// 设置背景颜色
  108. /// </summary>
  109. public void SetBgColor(Color color)
  110. {
  111. BgColor = color;
  112. //派发事件
  113. EventManager.EmitEvent(EventEnum.OnSetTileSetBgColor, color);
  114. }
  115. /// <summary>
  116. /// 将二维位置转换为索引的函数
  117. /// </summary>
  118. public int CellPositionToIndex(Vector2I pos)
  119. {
  120. return pos.Y * CellHorizontal + pos.X;
  121. }
  122. /// <summary>
  123. /// 返回Cell的坐标是否在纹理区域内
  124. /// </summary>
  125. public bool IsCellPositionInTexture(Vector2I cell)
  126. {
  127. return cell.X >= 0 && cell.Y >= 0 && cell.X < CellHorizontal && cell.Y < CellVertical;
  128. }
  129.  
  130. //返回上一级按钮点击
  131. private void OnBackClick()
  132. {
  133. OpenPrevUi();
  134. }
  135. }