Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditor / TileSetEditorImportRoot.cs
@小李xl 小李xl on 12 Dec 2023 4 KB 完成TileSet编辑器文件导入
  1. using System.IO;
  2. using Godot;
  3.  
  4. namespace UI.TileSetEditor;
  5.  
  6. public partial class TileSetEditorImportRoot : TextureRect, IUiNodeScript
  7. {
  8. private TileSetEditor.ImportRoot _importRoot;
  9. private DragBinder _dragBinder;
  10. //是否打开了颜色选择器
  11. private bool _isOpenColorPicker;
  12.  
  13. public void SetUiNode(IUiNode uiNode)
  14. {
  15. _importRoot = (TileSetEditor.ImportRoot)uiNode;
  16. _dragBinder = DragUiManager.BindDrag(_importRoot.L_ImportButton.Instance, OnDragCallback);
  17. GetTree().Root.FilesDropped += OnFilesDropped;
  18. _importRoot.L_ImportButton.Instance.Pressed += OnImportButtonClick;
  19. _importRoot.L_ReimportButton.Instance.Pressed += OnReimportButtonClick;
  20. _importRoot.L_ImportColorPicker.Instance.Pressed += OnColorPickerClick;
  21. _importRoot.L_ImportPreviewBg.Instance.Visible = false;
  22. _importRoot.L_ReimportButton.Instance.Visible = false;
  23. }
  24.  
  25. public void OnDestroy()
  26. {
  27. GetTree().Root.FilesDropped -= OnFilesDropped;
  28. _dragBinder.UnBind();
  29. }
  30.  
  31. public override void _Input(InputEvent @event)
  32. {
  33. if (@event is InputEventMouseButton mouseButton)
  34. {
  35. var textureRect = _importRoot.L_Control.L_ImportPreview.Instance;
  36. if (textureRect.Visible)
  37. {
  38. if (mouseButton.ButtonIndex == MouseButton.WheelDown)
  39. {
  40. //缩小
  41. var scale = textureRect.Scale;
  42. scale = new Vector2(Mathf.Max(0.1f, scale.X / 1.1f), Mathf.Max(0.1f, scale.Y / 1.1f));
  43. textureRect.Scale = scale;
  44. }
  45. else if (mouseButton.ButtonIndex == MouseButton.WheelUp)
  46. {
  47. //放大
  48. var scale = textureRect.Scale;
  49. scale = new Vector2(Mathf.Min(20f, scale.X * 1.1f), Mathf.Min(20f, scale.Y * 1.1f));
  50. textureRect.Scale = scale;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. //点击导入按钮
  57. private void OnImportButtonClick()
  58. {
  59. if (_importRoot.UiPanel.Texture != null)
  60. {
  61. return;
  62. }
  63.  
  64. OnReimportButtonClick();
  65. }
  66.  
  67. //重新导入
  68. private void OnReimportButtonClick()
  69. {
  70. EditorWindowManager.ShowOpenFileDialog(["*.png"], (path) =>
  71. {
  72. if (path != null)
  73. {
  74. SetImportTexture(path);
  75. }
  76. });
  77. }
  78. //点击调整背景颜色
  79. private void OnColorPickerClick()
  80. {
  81. if (!_isOpenColorPicker)
  82. {
  83. _isOpenColorPicker = true;
  84. EditorWindowManager.ShowColorPicker(
  85. _importRoot.L_ImportColorPicker.Instance.GlobalPosition,
  86. _importRoot.L_ImportPreviewBg.Instance.Color,
  87. //设置颜色
  88. color => { _importRoot.L_ImportPreviewBg.Instance.Color = color; },
  89. //关闭窗口
  90. () => { _isOpenColorPicker = false; }
  91. );
  92. }
  93. }
  94. //拖拽区域回调
  95. private void OnDragCallback(DragState state, Vector2 position)
  96. {
  97. var sprite2D = _importRoot.L_Control.L_ImportPreview.Instance;
  98. if (state == DragState.DragMove && sprite2D.Visible)
  99. {
  100. sprite2D.Position += position;
  101. }
  102. }
  103. //拖拽文件进入区域
  104. private void OnFilesDropped(string[] files)
  105. {
  106. if (files.Length == 0)
  107. {
  108. return;
  109. }
  110. var flag = GetGlobalRect().HasPoint(GetGlobalMousePosition());
  111. if (flag)
  112. {
  113. var file = files[0];
  114. if (Path.GetExtension(file) != ".png")
  115. {
  116. EditorWindowManager.ShowTips("警告", "只能导入'.png'格式的文件!");
  117. return;
  118. }
  119.  
  120. SetImportTexture(file);
  121. }
  122. }
  123.  
  124. /// <summary>
  125. /// 导入纹理
  126. /// </summary>
  127. /// <param name="file">纹理路径</param>
  128. private void SetImportTexture(string file)
  129. {
  130. Debug.Log("导入文件: " + file);
  131. var imageTexture = ImageTexture.CreateFromImage(Image.LoadFromFile(file));
  132. _importRoot.UiPanel.TexturePath = file;
  133. _importRoot.UiPanel.Texture = imageTexture;
  134. var textureRect = _importRoot.L_Control.L_ImportPreview.Instance;
  135. if (textureRect.Texture != null)
  136. {
  137. textureRect.Texture.Dispose();
  138. }
  139.  
  140. textureRect.Texture = imageTexture;
  141. _importRoot.L_ImportPreviewBg.Instance.Visible = true;
  142. _importRoot.L_ReimportButton.Instance.Visible = true;
  143. //隐藏导入文本和icon
  144. _importRoot.L_ImportLabel.Instance.Visible = false;
  145. _importRoot.L_ImportIcon.Instance.Visible = false;
  146. }
  147. }