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