Browse Source

开发:完善新增实例;新增模板,复制模板的操作

xlxin 1 year ago
parent
commit
8bc988a8ae

+ 67 - 10
TileManor/scripts/electron/index.html

@@ -140,6 +140,7 @@
                 const editTemplateButton = document.createElement('button');
                 editTemplateButton.id = 'edit-template-button';
                 editTemplateButton.textContent = '编辑模板';
+                editTemplateButton.style.marginLeft = '10px';
                 editTemplateButton.addEventListener('click', () => {
                     // 判断模板是在哪个目录下
                     for (let dir in templateFNsByDir) {
@@ -151,6 +152,50 @@
                 });
                 div1.appendChild(editTemplateButton);
 
+                // 增加一个复制模板的按钮
+                const copyTemplateButton = document.createElement('button');
+                copyTemplateButton.id = 'copy-template-button';
+                copyTemplateButton.textContent = '复制模板';
+                copyTemplateButton.style.marginLeft = '10px';
+                copyTemplateButton.addEventListener('click', () => {
+                    // 复制模板
+                    var path = "";
+                    for (let dir in templateFNsByDir) {
+                        if (templateFNsByDir[dir].has(inst.template)) {
+                            path = dir + '/' + inst.template + '.json';
+                            break;
+                        }
+                    }
+                    window.electron.send('copy-template', path);
+                    window.electron.receive('copy-template-replay', (data) => {
+                        if (data.length == "0") {
+                            // 失败了
+                        } else {
+                            // 得到文件名和路径前缀
+                            const arr = data.split('/');
+                            const filename = arr[arr.length - 1];
+                            const prefix = arr.slice(0, arr.length - 1).join('/');
+                            // 放入到templateFNsByDir中
+                            templateName = filename.split(".")[0];
+                            if (templateFNsByDir[prefix] == null) {
+                                templateFNsByDir[prefix] = new Set();
+                            }
+                            templateFNsByDir[prefix].add(templateName);
+                            // 放入到templateFNs中
+                            templateFNs.push(templateName);
+                            // 设置为新的模板
+                            inst.template = templateName;
+                            // 刷新页面,增加templateSelect的选项,并选中新的,同时取消旧的选中状态
+                            const option = document.createElement('option');
+                            option.value = templateName;
+                            option.textContent = templateName;
+                            option.selected = true;
+                            templateSelect.appendChild(option);
+                        }
+                    });
+                });
+                div1.appendChild(copyTemplateButton);
+
                 div.appendChild(div1);
             }
             // 加入表格
@@ -513,19 +558,31 @@
             const inst = {
                 lvinst: instName,
                 template: '',
-                'try1(tileCnt|seed|param_template)': '0|0|0',
-                'try2(tileCnt|seed|param_template)': '0|0|0',
-                'try3(tileCnt|seed|param_template)': '0|0|0',
-                'try4(tileCnt|seed|param_template)': '0|0|0',
-                'try5(tileCnt|seed|param_template)': '0|0|0',
-                'try6(tileCnt|seed|param_template)': '0|0|0',
-                'try7(tileCnt|seed|param_template)': '0|0|0',
-                'try8(tileCnt|seed|param_template)': '0|0|0',
-                'try9(tileCnt|seed|param_template)': '0|0|0',
-                'try10(tileCnt|seed|param_template)': '0|0|0'
+                'try1(tileCnt|seed|param_template)': '0|0|normal',
+                'try2(tileCnt|seed|param_template)': '0|0|normal',
+                'try3(tileCnt|seed|param_template)': '0|0|normal',
+                'try4(tileCnt|seed|param_template)': '0|0|normal',
+                'try5(tileCnt|seed|param_template)': '0|0|normal',
+                'try6(tileCnt|seed|param_template)': '0|0|normal',
+                'try7(tileCnt|seed|param_template)': '0|0|normal',
+                'try8(tileCnt|seed|param_template)': '0|0|normal',
+                'try9(tileCnt|seed|param_template)': '0|0|normal',
+                'try10(tileCnt|seed|param_template)': '0|0|normal'
             };
             instances.push(inst);
+            const instDetails = document.getElementById('instance-details');
             showInstanceInfoAt(instDetails, inst);
+            // 将当前关卡关联到该实例
+            const lvid = document.getElementById('lvid-in-lvdetails').textContent.split(':')[1].trim();
+            for (let i = 0; i < levels.length; i++) {
+                if (levels[i].lvid == lvid) {
+                    levels[i].lvinst = instName;
+                    break;
+                }
+            }
+            // 刷新页面
+            const instId = document.getElementById('instid-in-lvdetails');
+            instId.textContent = '关卡实例名称: ' + instName;
         });
 
         document.getElementById('load_grp').addEventListener('click', function() {

+ 30 - 0
TileManor/scripts/electron/main.js

@@ -92,6 +92,36 @@ ipcMain.on('open-newlv-prompt', (event, arg) => {
     .catch(console.error);
 })
 
+// 复制模板文件到指定的目录
+ipcMain.on('copy-template', (event, templateFN) => {
+    prompt({
+        title: '复制模板文件',
+        label: '请输入新的模板文件路径和文件名:',
+        value: '../../templates/tm_',
+        inputAttrs: {
+            type: 'text'
+        },
+        type: 'input'
+    })
+    .then((r) => {
+        if(r === null) {
+            event.reply('copy-template-replay', "");
+        } else {
+            console.log('result', r);
+            // 将 templateFN 复制到指定的目录 r
+            const { exec } = require('child_process');
+            exec(`cp ${templateFN} ${r}`, (err, stdout, stderr) => {
+                if (err) {
+                    event.reply('copy-template-replay', "");
+                    return;
+                }
+                event.reply('copy-template-replay', r);
+            });
+        }
+    })
+    .catch(console.error);
+})
+
 ipcMain.on('open-newinst-prompt', (event, arg) => {
     prompt({
         title: 'Prompt',

+ 2 - 2
TileManor/scripts/electron/preload.js

@@ -5,13 +5,13 @@ contextBridge.exposeInMainWorld(
   {
     send: (channel, data1, data2) => {
       // whitelist channels
-      let validChannels = ['parse-csv', 'get-file-list', 'open-newlv-prompt', 'open-newinst-prompt', 'save2csv', 'open-app'];
+      let validChannels = ['parse-csv', 'get-file-list', 'open-newlv-prompt', 'open-newinst-prompt', 'save2csv', 'open-app', "copy-template"];
       if (validChannels.includes(channel)) {
         ipcRenderer.send(channel, data1, data2);
       }
     },
     receive: (channel, func, data) => {
-      let validChannels = ['csv-data', 'file-list-data', 'file-list-error', 'prompt-newlv-reply', 'prompt-newinst-reply'];
+      let validChannels = ['csv-data', 'file-list-data', 'file-list-error', 'prompt-newlv-reply', 'prompt-newinst-reply', "copy-template-replay"];
       if (validChannels.includes(channel)) {
         // Deliberately strip event as it includes `sender` 
         ipcRenderer.on(channel, (event, data, ...args) => func(data, ...args));