main.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. const { dialog } = require("electron");
  3. const os = require("os");
  4. const fs = require("fs");
  5. const exec = require("child_process").exec;
  6. const utils = require("./utils");
  7. const EXAMPLE_PATH = `${__dirname}/textMeshPro`;
  8. const CONFIG_PATH = `${__dirname}/config.json`;
  9. const TEMP_PATH = `${__dirname}/temp`;
  10. const TEMP_HIERO_PATH = `${__dirname}/temp/hieroConfig.hiero`;
  11. let config = {
  12. hieroPath: "",
  13. fontPath: "",
  14. exportName: "",
  15. exportDir: "",
  16. /** 导出文本来源 0:输入框 1:txt文件 */
  17. textFrom: 0,
  18. textStr: "",
  19. textPath: "",
  20. fontSize: 32,
  21. padding: 5,
  22. scale: 10,
  23. width: 1024,
  24. height: 1024
  25. };
  26. function isFileExist(path) {
  27. return new Promise((resolve, reject) => {
  28. fs.access(path, fs.constants.F_OK, (err) => {
  29. resolve(!err);
  30. });
  31. });
  32. }
  33. async function readConfig() {
  34. try {
  35. let exist = await isFileExist(CONFIG_PATH);
  36. if (!exist) {
  37. return;
  38. }
  39. let data = fs.readFileSync(CONFIG_PATH, "utf-8");
  40. if (data) {
  41. config = JSON.parse(data);
  42. }
  43. } catch (error) {
  44. Editor.error(`[textmeshpro-tool readConfig] error`);
  45. }
  46. }
  47. function writeConfig() {
  48. try {
  49. let data = JSON.stringify(config);
  50. fs.writeFileSync(CONFIG_PATH, data);
  51. Editor.log(`write config: ${CONFIG_PATH}`);
  52. } catch (error) {
  53. Editor.error(`[textmeshpro-tool writeConfig] error`);
  54. Editor.error(error);
  55. }
  56. }
  57. function exportFont() {
  58. try {
  59. let check = fs.existsSync(TEMP_PATH);
  60. if (!check) {
  61. fs.mkdirSync(TEMP_PATH);
  62. }
  63. utils.writeHiero(TEMP_HIERO_PATH, config);
  64. let cmdStr = `java -jar ${config.hieroPath} -i ${TEMP_HIERO_PATH} -o ${config.exportDir}/${config.exportName}.fnt -b`;
  65. Editor.log("[textmeshpro-tool] 正在输出字体文件,请耐心等待Hiero窗口自行关闭...");
  66. let time = Date.now();
  67. exec(cmdStr, (error, stdout, stderr) => {
  68. if (error) {
  69. Editor.error(`[textmeshpro-tool exportFont] exec error: ${error}`);
  70. return;
  71. }
  72. utils.parse(`${config.exportDir}/${config.exportName}.fnt`);
  73. fs.unlinkSync(`${config.exportDir}/${config.exportName}.fnt`);
  74. Editor.assetdb.refresh("db://assets/");
  75. Editor.log(`[textmeshpro-tool] 字体文件输出完毕,耗时:${(Date.now() - time) / 1000}s`);
  76. });
  77. } catch (error) {
  78. Editor.error(`[textmeshpro-tool exportFont] error 字体文件导出失败`);
  79. Editor.error(error);
  80. }
  81. }
  82. module.exports = {
  83. load() {
  84. readConfig();
  85. },
  86. unload() {
  87. },
  88. messages: {
  89. importExample() {
  90. try {
  91. let check = fs.existsSync(`${Editor.Project.path}/assets/textMeshPro`);
  92. if (check) {
  93. dialog.showMessageBox({
  94. type: "warning",
  95. title: "warning",
  96. message: "警告",
  97. detail: "assets目录下已存在textMeshPro文件夹,为防止误覆盖,请手动导入",
  98. buttons: ["确定"],
  99. }).then(res => console.log(res));
  100. return;
  101. }
  102. let cmdStr = "";
  103. if (os.type() == "Windows_NT") {
  104. cmdStr = `xcopy ${EXAMPLE_PATH.replace(/\//g, "\\")} ${Editor.Project.path}\\assets\\textMeshPro\\ /e`;
  105. } else {
  106. cmdStr = `cp -r ${EXAMPLE_PATH} ${Editor.Project.path}/assets/textMeshPro/`;
  107. }
  108. Editor.log(cmdStr);
  109. exec(cmdStr, (error, stdout, stderr) => {
  110. if (error) {
  111. Editor.error(`[textmeshpro-tool importExample] exec error: ${error}`);
  112. return;
  113. }
  114. Editor.assetdb.refresh("db://assets/textMeshPro/");
  115. });
  116. } catch (error) {
  117. Editor.error(`[textmeshpro-tool importExample] 文件导入失败,请尝试手动导入 error: ${error}`);
  118. Editor.error(error);
  119. }
  120. },
  121. openPanel() {
  122. Editor.Panel.open("textmeshpro-tool");
  123. },
  124. onPanelInit() {
  125. Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
  126. },
  127. onChangeConfig(event, key, value) {
  128. config[key] = value;
  129. },
  130. onClickBtnExportDir() {
  131. dialog.showOpenDialog({
  132. defaultPath: config.exportDir || Editor.Project.path,
  133. properties: ["openDirectory"]
  134. }).then((res) => {
  135. if (res.filePaths.length > 0) {
  136. config.exportDir = res.filePaths[0];
  137. Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
  138. }
  139. });
  140. },
  141. onClickBtnFontPath() {
  142. dialog.showOpenDialog({
  143. defaultPath: config.fontPath || Editor.Project.path,
  144. properties: ["openFile"],
  145. filters: [
  146. { name: ".ttf", extensions: ["ttf"] }
  147. ]
  148. }).then((res) => {
  149. if (res.filePaths.length > 0) {
  150. config.fontPath = res.filePaths[0];
  151. Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
  152. }
  153. });
  154. },
  155. onClickBtnHiero() {
  156. dialog.showOpenDialog({
  157. defaultPath: config.hieroPath || Editor.Project.path,
  158. properties: ["openFile"],
  159. filters: [
  160. { name: ".jar", extensions: ["jar"] }
  161. ]
  162. }).then((res) => {
  163. if (res.filePaths.length > 0) {
  164. config.hieroPath = res.filePaths[0];
  165. Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
  166. }
  167. });
  168. },
  169. onClickBtnTextPath() {
  170. dialog.showOpenDialog({
  171. defaultPath: config.textPath || Editor.Project.path,
  172. properties: ["openFile"],
  173. filters: [
  174. { name: ".txt", extensions: ["txt"] }
  175. ]
  176. }).then((res) => {
  177. if (res.filePaths.length > 0) {
  178. config.textPath = res.filePaths[0];
  179. Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
  180. }
  181. });
  182. },
  183. onClickBtnSave(event, arg) {
  184. if (arg) {
  185. config = arg;
  186. }
  187. writeConfig();
  188. },
  189. onClickBtnExport() {
  190. exportFont();
  191. }
  192. }
  193. };