Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / editorTileImage / EditorTileImagePanel.cs
@小李xl 小李xl on 21 Jan 2024 3 KB TileSet导入纹理设置
  1. using Godot;
  2.  
  3. namespace UI.EditorTileImage;
  4.  
  5. public partial class EditorTileImagePanel : EditorTileImage
  6. {
  7. /// <summary>
  8. /// 起始X
  9. /// </summary>
  10. public int StartXValue { get; private set; }
  11. /// <summary>
  12. /// 起始Y
  13. /// </summary>
  14. public int StartYValue { get; private set; }
  15. /// <summary>
  16. /// 间距X
  17. /// </summary>
  18. public int OffsetXValue { get; private set; }
  19. /// <summary>
  20. /// 间距Y
  21. /// </summary>
  22. public int OffsetYValue { get; private set; }
  23. /// <summary>
  24. /// 横轴数量
  25. /// </summary>
  26. public int HCountValue { get; private set; }
  27. /// <summary>
  28. /// 纵轴数量
  29. /// </summary>
  30. public int VCountValue { get; private set; }
  31. /// <summary>
  32. /// 纹理大小
  33. /// </summary>
  34. public Vector2I ImageSize { get; private set; }
  35. /// <summary>
  36. /// 使用的Image对象
  37. /// </summary>
  38. public Image UseImage { get; private set; }
  39. private ImageTexture _texture;
  40. public override void OnCreateUi()
  41. {
  42. S_StartX.Instance.ValueChanged += (v) =>
  43. {
  44. StartXValue = (int)v;
  45. RefreshHVMaxCount();
  46. };
  47. S_StartY.Instance.ValueChanged += (v) =>
  48. {
  49. StartYValue = (int)v;
  50. RefreshHVMaxCount();
  51. };
  52. S_OffsetX.Instance.ValueChanged += (v) =>
  53. {
  54. OffsetXValue = (int)v;
  55. RefreshHVMaxCount();
  56. };
  57. S_OffsetY.Instance.ValueChanged += (v) =>
  58. {
  59. OffsetYValue = (int)v;
  60. RefreshHVMaxCount();
  61. };
  62. S_HCount.Instance.ValueChanged += (v) =>
  63. {
  64. HCountValue = (int)v;
  65. };
  66. S_VCount.Instance.ValueChanged += (v) =>
  67. {
  68. VCountValue = (int)v;
  69. };
  70. }
  71.  
  72. public override void OnDestroyUi()
  73. {
  74. if (UseImage != null)
  75. {
  76. UseImage.Dispose();
  77. UseImage = null;
  78. }
  79.  
  80. if (_texture != null)
  81. {
  82. _texture.Dispose();
  83. _texture = null;
  84. }
  85. }
  86.  
  87. /// <summary>
  88. /// 初始化Ui数据
  89. /// </summary>
  90. public void InitData(Image image)
  91. {
  92. UseImage = image;
  93. ImageSize = image.GetSize();
  94. _texture = ImageTexture.CreateFromImage(image);
  95. S_TileSprite.Instance.Texture = _texture;
  96. S_Bg.Instance.DoFocus();
  97. S_TextureRoot.Instance.Size = image.GetSize();
  98.  
  99. RefreshHVMaxCount();
  100. S_HCount.Instance.Value = S_HCount.Instance.MaxValue;
  101. S_VCount.Instance.Value = S_VCount.Instance.MaxValue;
  102. }
  103.  
  104. /// <summary>
  105. /// 获取处理后的图像
  106. /// </summary>
  107. public Image GetImage()
  108. {
  109. var image = Image.Create(HCountValue * GameConfig.TileCellSize, VCountValue * GameConfig.TileCellSize, false, Image.Format.Rgba8);
  110. var start = new Vector2I(StartXValue, StartYValue);
  111. for (int i = 0; i < HCountValue; i++)
  112. {
  113. for (int j = 0; j < VCountValue; j++)
  114. {
  115. var offset = new Vector2I(i * (OffsetXValue + GameConfig.TileCellSize), j * (OffsetYValue + GameConfig.TileCellSize));
  116. image.BlitRect(UseImage,
  117. new Rect2I(
  118. start + offset,
  119. GameConfig.TileCellSizeVector2I
  120. ),
  121. new Vector2I(i * GameConfig.TileCellSize, j * GameConfig.TileCellSize)
  122. );
  123. }
  124. }
  125.  
  126. return image;
  127. }
  128. //更新最大计数的水平和垂直方向的值
  129. private void RefreshHVMaxCount()
  130. {
  131. var hv = Mathf.FloorToInt(((float)ImageSize.X - StartXValue + OffsetXValue) / (GameConfig.TileCellSize + OffsetXValue));
  132. var vv = Mathf.FloorToInt(((float)ImageSize.Y - StartYValue + OffsetYValue) / (GameConfig.TileCellSize + OffsetYValue));
  133. S_HCount.Instance.MaxValue = hv;
  134. S_VCount.Instance.MaxValue = vv;
  135. }
  136. }