PsdToFgui.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { config } from "./main";
  2. import { $typeof } from "puerts";
  3. import FairyEditor = CS.FairyEditor;
  4. import FileUtils from "./FileUtils";
  5. const App = FairyEditor.App;
  6. export default class PsdToFgui {
  7. public start() {
  8. let pluginPath = FairyEditor.App.pluginManager.basePath;
  9. let filterExtensions: string[] = ["psd"];
  10. let filter = new CS.SFB.ExtensionFilter("psd",...filterExtensions);
  11. // let filters: CS.SFB.ExtensionFilter[] = [filter];
  12. let filters = CS.System.Array.CreateInstance($typeof(CS.SFB.ExtensionFilter), 1) as CS.System.Array$1<CS.SFB.ExtensionFilter>;
  13. filters.set_Item(0, filter);
  14. FairyEditor.IOUtil.BrowseForOpen("选择psd文件", pluginPath, filters, this.onSelectedPsd);
  15. }
  16. private async onSelectedPsd(psdPath: string) {
  17. if (!psdPath) {
  18. App.consoleView.Log(`没有选择psd文件.`);
  19. return;
  20. }
  21. let psd2fguiPluginsPath = config.psd2fguiPluginsPath;
  22. if (!CS.System.IO.Directory.Exists(psd2fguiPluginsPath)) {
  23. psd2fguiPluginsPath = FairyEditor.App.project.basePath + "/psd2fgui";
  24. if (!CS.System.IO.Directory.Exists(psd2fguiPluginsPath)) {
  25. App.consoleView.LogWarning(`需要先配置psd2fgui插件. 路径:${psd2fguiPluginsPath}`);
  26. return;
  27. }
  28. }
  29. App.consoleView.Log(`psd2fgui插件路径:${psd2fguiPluginsPath} psdPath:${psdPath}`);
  30. let pkgPath = psdPath.replace(".psd", ".fairypackage");
  31. // let pkgFolder = psdPath.replace(".psd", "-fairypackage");
  32. // --nopack
  33. let result = FairyEditor.ProcessUtil.Start(`node convert ${psdPath} --ignore-font`, null, psd2fguiPluginsPath, false);
  34. if (!result) {
  35. while(!CS.System.IO.File.Exists(pkgPath)) {
  36. FairyEditor.App.consoleView.Log(`psd2fgui sleep wait for ${pkgPath}`);
  37. await FileUtils.sleep(1000)
  38. }
  39. App.consoleView.Log(`psd2fgui completed. 使用菜单<资源->导入资源包> 可以导入该资源包`);
  40. // App.Alert(`psd2fgui completed. 使用菜单<资源->导入资源包> 可以导入该资源包`);
  41. }
  42. else {
  43. App.consoleView.LogError(`psd2fgui 失败. result:${result}`);
  44. }
  45. // App.consoleView.Log(`psdToFgui result:${result} exist:${CS.System.IO.File.Exists(pkgPath)}`);
  46. // let pkg = FairyEditor.App.project.CreatePackage(pkgPath);
  47. // App.consoleView.Log(`psdToFgui pkgName:${pkg?.name ?? "null"}`);
  48. // if (pkg) {
  49. // FairyEditor.App.libView.ShowImportResourcesDialog(pkg);
  50. // }
  51. }
  52. }