浏览代码

开发:完善对模板的复制和编辑

xlxin 1 年之前
父节点
当前提交
58e4a18c41
共有 3 个文件被更改,包括 59 次插入31 次删除
  1. 50 28
      TileManor/scripts/electron/index.html
  2. 1 1
      TileManor/scripts/electron/level.html
  3. 8 2
      TileManor/scripts/electron/main.js

+ 50 - 28
TileManor/scripts/electron/index.html

@@ -19,7 +19,7 @@
     </style>
 </head>
 <body>
-    <div id="hdr" style="background-color: antiquewhite;">
+    <div id="hdr" style="background-color: antiquewhite; margin-bottom: 20px;">
         <input type="text" id="group-suffix" placeholder="分组文件的后缀">
         <button id="load_grp">加载分组信息</button>
     </div>
@@ -27,13 +27,13 @@
         <div id="nav" style="overflow-y: scroll; height: 90%; width: 5%; background-color: coral;">
             <ul>
                 <li>
-                    <button id="levels">关卡</button>
+                    <button id="levels" style="margin-top: 10px;">关卡</button>
                 </li>
                 <li>
-                    <button id="examples">实例</button>
+                    <button id="examples" style="margin-top: 10px;">实例</button>
                 </li>
                 <li>
-                    <button id="templates">模板</button>
+                    <button id="templates"style="margin-top: 10px;">模板</button>
                 </li>
             </ul>
         </div>
@@ -142,10 +142,12 @@
                 editTemplateButton.textContent = '编辑模板';
                 editTemplateButton.style.marginLeft = '10px';
                 editTemplateButton.addEventListener('click', () => {
+                    // 获取当前的模板名称
+                    const templateName = document.getElementById('template-select').value;
                     // 判断模板是在哪个目录下
                     for (let dir in templateFNsByDir) {
-                        if (templateFNsByDir[dir].has(inst.template)) {
-                            window.electron.send('open-app', '/Applications/Tiled.app/Contents/MacOS/Tiled ' + dir + '/' + inst.template + '.json');
+                        if (templateFNsByDir[dir].has(templateName)) {
+                            window.electron.send('open-app', dir, '/Applications/Tiled.app/Contents/MacOS/Tiled ' + dir + '/' + inst.template + '.json');
                             return;
                         }
                     }
@@ -158,7 +160,12 @@
                 copyTemplateButton.textContent = '复制模板';
                 copyTemplateButton.style.marginLeft = '10px';
                 copyTemplateButton.addEventListener('click', () => {
-                    // 复制模板
+                    // 获取当前的实例
+                    const inst = getInstanceByName(document.getElementById('instid-in-inst-detail').textContent.split(':')[1].trim());
+                    if (inst.template == "") {
+                        // 取options里面当前显示的值
+                        inst.template = document.getElementById('template-select').value;
+                    }
                     var path = "";
                     for (let dir in templateFNsByDir) {
                         if (templateFNsByDir[dir].has(inst.template)) {
@@ -176,7 +183,7 @@
                             const filename = arr[arr.length - 1];
                             const prefix = arr.slice(0, arr.length - 1).join('/');
                             // 放入到templateFNsByDir中
-                            templateName = filename.split(".")[0];
+                            const templateName = filename.split(".")[0];
                             if (templateFNsByDir[prefix] == null) {
                                 templateFNsByDir[prefix] = new Set();
                             }
@@ -190,6 +197,8 @@
                             option.value = templateName;
                             option.textContent = templateName;
                             option.selected = true;
+
+                            const templateSelect = document.getElementById('template-select');
                             templateSelect.appendChild(option);
                         }
                     });
@@ -379,11 +388,15 @@
                             }
                         });
                         levelDetails.innerHTML = `
-                            <h2 id='lvid-in-lvdetails'>关卡ID: ${level.lvid}</h2>
-                            <p id='instid-in-lvdetails'>关卡实例名称: ${level.lvinst}</p>`
-                        // 
-                        if (level.lvinst == '') {
-                            // 实例不存在,
+                            <h2 id='lvid-in-lvdetails'>关卡ID: ${level.lvid}</h2>`
+                        // 关卡实例名称
+                        {
+                            const div = document.createElement('div');
+                            const p = document.createElement('p');
+                            p.style.display = 'inline';
+                            p.appendChild(document.createTextNode('关卡实例名称: '));
+                            div.appendChild(p);
+                            
                             // 显示一个下来列表,选择实例
                             const instSelect = document.createElement('select');
                             instSelect.id = 'inst-select';
@@ -391,9 +404,14 @@
                                 const option = document.createElement('option');
                                 option.value = inst.lvinst;
                                 option.textContent = inst.lvinst;
+                                if (inst.lvinst == level.lvinst) {
+                                    option.selected = true;
+                                }
                                 instSelect.appendChild(option);
                             });
-                            levelDetails.appendChild(instSelect);
+                            div.appendChild(instSelect);
+
+                            levelDetails.appendChild(div);
                         }
                         // 加入一个按钮,点击后可以创建实例
                         {
@@ -401,12 +419,12 @@
                             const createInstButton = document.createElement('button');
                             createInstButton.id = 'create-instance-button';
                             createInstButton.textContent = '创建新的实例';
+                            createInstButton.style.marginBottom = '15px';
                             div.appendChild(createInstButton);
                             levelDetails.appendChild(div);
                             // createInstButton.addEventListener('click', () => {
                             //     addNewInst(level);
                             // });
-                            console.log(createInstButton.onclick);
                         }
                         {
                             const div = document.createElement('div');
@@ -558,16 +576,16 @@
             const inst = {
                 lvinst: instName,
                 template: '',
-                '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'
+                'try1(tileCnt|seed|param_template)': '0|100|normal',
+                'try2(tileCnt|seed|param_template)': '0|200|normal',
+                'try3(tileCnt|seed|param_template)': '0|300|normal',
+                'try4(tileCnt|seed|param_template)': '0|400|normal',
+                'try5(tileCnt|seed|param_template)': '0|500|normal',
+                'try6(tileCnt|seed|param_template)': '0|600|normal',
+                'try7(tileCnt|seed|param_template)': '0|700|normal',
+                'try8(tileCnt|seed|param_template)': '0|800|normal',
+                'try9(tileCnt|seed|param_template)': '0|900|normal',
+                'try10(tileCnt|seed|param_template)': '0|1000|normal'
             };
             instances.push(inst);
             const instDetails = document.getElementById('instance-details');
@@ -580,9 +598,13 @@
                     break;
                 }
             }
-            // 刷新页面
-            const instId = document.getElementById('instid-in-lvdetails');
-            instId.textContent = '关卡实例名称: ' + instName;
+            // 刷新页面, 增加一个option,并置为选中
+            const instId = document.getElementById('inst-select');
+            const option = document.createElement('option');
+            option.value = instName;
+            option.textContent = instName;
+            option.selected = true;
+            instId.appendChild(option);
         });
 
         document.getElementById('load_grp').addEventListener('click', function() {

+ 1 - 1
TileManor/scripts/electron/level.html

@@ -1,5 +1,5 @@
 <!-- level.html -->
-<button id="add-level-button">新增关卡</button>
+<button id="add-level-button" style="margin-top: 5px; margin-bottom: 5px;">新增关卡</button>
 <div style="display: flex; flex-direction: row; height: 90%; width: 100%;">
     <div style="overflow-y: scroll; height: 90%; width: 10%;">
         <ul id="level-list" style="height: 100%;">

+ 8 - 2
TileManor/scripts/electron/main.js

@@ -94,6 +94,7 @@ ipcMain.on('open-newlv-prompt', (event, arg) => {
 
 // 复制模板文件到指定的目录
 ipcMain.on('copy-template', (event, templateFN) => {
+    console.log('copy-template');
     prompt({
         title: '复制模板文件',
         label: '请输入新的模板文件路径和文件名:',
@@ -107,11 +108,13 @@ ipcMain.on('copy-template', (event, templateFN) => {
         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) {
+                    console.log('from', templateFN);
+                    console.log('to', r);
+                    console.log('result', err);
                     event.reply('copy-template-replay', "");
                     return;
                 }
@@ -144,8 +147,11 @@ ipcMain.on('open-newinst-prompt', (event, arg) => {
 })
 
 // 打开一个外部的应用
-ipcMain.on('open-app', (event, arg) => {
+ipcMain.on('open-app', (event, dir, arg) => {
     const { exec } = require('child_process');
+    const options = {
+        cwd: __dirname + dir
+    };
     exec(arg, (err, stdout, stderr) => {
         if (err) {
             console.error(err);