123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- "use strict";
- const { dialog } = require("electron");
- const os = require("os");
- const fs = require("fs");
- const exec = require("child_process").exec;
- const utils = require("./utils");
- const EXAMPLE_PATH = `${__dirname}/textMeshPro`;
- const CONFIG_PATH = `${__dirname}/config.json`;
- const TEMP_PATH = `${__dirname}/temp`;
- const TEMP_HIERO_PATH = `${__dirname}/temp/hieroConfig.hiero`;
- let config = {
- hieroPath: "",
- fontPath: "",
- exportName: "",
- exportDir: "",
- /** 导出文本来源 0:输入框 1:txt文件 */
- textFrom: 0,
- textStr: "",
- textPath: "",
- fontSize: 32,
- padding: 5,
- scale: 10,
- width: 1024,
- height: 1024
- };
- function isFileExist(path) {
- return new Promise((resolve, reject) => {
- fs.access(path, fs.constants.F_OK, (err) => {
- resolve(!err);
- });
- });
- }
- async function readConfig() {
- try {
- let exist = await isFileExist(CONFIG_PATH);
- if (!exist) {
- return;
- }
- let data = fs.readFileSync(CONFIG_PATH, "utf-8");
- if (data) {
- config = JSON.parse(data);
- }
- } catch (error) {
- Editor.error(`[textmeshpro-tool readConfig] error`);
- }
- }
- function writeConfig() {
- try {
- let data = JSON.stringify(config);
- fs.writeFileSync(CONFIG_PATH, data);
- Editor.log(`write config: ${CONFIG_PATH}`);
- } catch (error) {
- Editor.error(`[textmeshpro-tool writeConfig] error`);
- Editor.error(error);
- }
- }
- function exportFont() {
- try {
- let check = fs.existsSync(TEMP_PATH);
- if (!check) {
- fs.mkdirSync(TEMP_PATH);
- }
- utils.writeHiero(TEMP_HIERO_PATH, config);
- let cmdStr = `java -jar ${config.hieroPath} -i ${TEMP_HIERO_PATH} -o ${config.exportDir}/${config.exportName}.fnt -b`;
- Editor.log("[textmeshpro-tool] 正在输出字体文件,请耐心等待Hiero窗口自行关闭...");
- let time = Date.now();
- exec(cmdStr, (error, stdout, stderr) => {
- if (error) {
- Editor.error(`[textmeshpro-tool exportFont] exec error: ${error}`);
- return;
- }
- utils.parse(`${config.exportDir}/${config.exportName}.fnt`);
- fs.unlinkSync(`${config.exportDir}/${config.exportName}.fnt`);
- Editor.assetdb.refresh("db://assets/");
- Editor.log(`[textmeshpro-tool] 字体文件输出完毕,耗时:${(Date.now() - time) / 1000}s`);
- });
- } catch (error) {
- Editor.error(`[textmeshpro-tool exportFont] error 字体文件导出失败`);
- Editor.error(error);
- }
- }
- module.exports = {
- load() {
- readConfig();
- },
- unload() {
- },
- messages: {
- importExample() {
- try {
- let check = fs.existsSync(`${Editor.Project.path}/assets/textMeshPro`);
- if (check) {
- dialog.showMessageBox({
- type: "warning",
- title: "warning",
- message: "警告",
- detail: "assets目录下已存在textMeshPro文件夹,为防止误覆盖,请手动导入",
- buttons: ["确定"],
- }).then(res => console.log(res));
- return;
- }
- let cmdStr = "";
- if (os.type() == "Windows_NT") {
- cmdStr = `xcopy ${EXAMPLE_PATH.replace(/\//g, "\\")} ${Editor.Project.path}\\assets\\textMeshPro\\ /e`;
- } else {
- cmdStr = `cp -r ${EXAMPLE_PATH} ${Editor.Project.path}/assets/textMeshPro/`;
- }
- Editor.log(cmdStr);
- exec(cmdStr, (error, stdout, stderr) => {
- if (error) {
- Editor.error(`[textmeshpro-tool importExample] exec error: ${error}`);
- return;
- }
- Editor.assetdb.refresh("db://assets/textMeshPro/");
- });
- } catch (error) {
- Editor.error(`[textmeshpro-tool importExample] 文件导入失败,请尝试手动导入 error: ${error}`);
- Editor.error(error);
- }
- },
- openPanel() {
- Editor.Panel.open("textmeshpro-tool");
- },
- onPanelInit() {
- Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
- },
- onChangeConfig(event, key, value) {
- config[key] = value;
- },
- onClickBtnExportDir() {
- dialog.showOpenDialog({
- defaultPath: config.exportDir || Editor.Project.path,
- properties: ["openDirectory"]
- }).then((res) => {
- if (res.filePaths.length > 0) {
- config.exportDir = res.filePaths[0];
- Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
- }
- });
- },
- onClickBtnFontPath() {
- dialog.showOpenDialog({
- defaultPath: config.fontPath || Editor.Project.path,
- properties: ["openFile"],
- filters: [
- { name: ".ttf", extensions: ["ttf"] }
- ]
- }).then((res) => {
- if (res.filePaths.length > 0) {
- config.fontPath = res.filePaths[0];
- Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
- }
- });
- },
- onClickBtnHiero() {
- dialog.showOpenDialog({
- defaultPath: config.hieroPath || Editor.Project.path,
- properties: ["openFile"],
- filters: [
- { name: ".jar", extensions: ["jar"] }
- ]
- }).then((res) => {
- if (res.filePaths.length > 0) {
- config.hieroPath = res.filePaths[0];
- Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
- }
- });
- },
- onClickBtnTextPath() {
- dialog.showOpenDialog({
- defaultPath: config.textPath || Editor.Project.path,
- properties: ["openFile"],
- filters: [
- { name: ".txt", extensions: ["txt"] }
- ]
- }).then((res) => {
- if (res.filePaths.length > 0) {
- config.textPath = res.filePaths[0];
- Editor.Ipc.sendToPanel("textmeshpro-tool", "refreshConfig", config);
- }
- });
- },
- onClickBtnSave(event, arg) {
- if (arg) {
- config = arg;
- }
- writeConfig();
- },
- onClickBtnExport() {
- exportFont();
- }
- }
- };
|