Prechádzať zdrojové kódy

开发:可以保存关卡设置

xlxin 1 rok pred
rodič
commit
52835a2e6a

+ 47 - 27
TileManor/scripts/electron/index.html

@@ -134,6 +134,7 @@
                 td4.appendChild(input3);
                 tr.appendChild(td1);
                 tr.appendChild(td2);
+
                 tr.appendChild(td3);
                 tr.appendChild(td4);
                 tbody.appendChild(tr);
@@ -187,7 +188,8 @@
         }
 
         // 创建一个新实例,并将其关联到指定的关卡中
-        function addNewInst(level, instDetails) {
+        function addNewInst(level) {
+            const instDetails = document.getElementById('instance-details');
             // 获取实例名称
             window.electron.send('open-newinst-prompt', 'some data');
             window.electron.receive('prompt-newinst-reply', (event, arg) => {
@@ -219,9 +221,27 @@
             });
         }
 
-        function saveLvInfo(lvDetails) {
+        function saveLvInfo() {
+            const lvDetails = document.getElementById('level-details');
             // 保存关卡的详细信息
-            
+            const lvid = lvDetails.querySelector('#lvid-in-lvdetails').textContent.split(':')[1].trim();
+            const instid = lvDetails.querySelector('#instid-in-lvdetails').textContent.split(':')[1].trim();
+            const type = lvDetails.querySelector('#type-select-in-lvdetails').value;
+            const ddaType = lvDetails.querySelector('#ddatype-in-lvdetails').textContent.split(':')[1].trim();
+            const ddaPara = lvDetails.querySelector('#ddapara-in-lvdetails').textContent.split(':')[1].trim();
+            var lv = null;
+            for (let i = 0; i < levels.length; i++) {
+                if (levels[i].lvid == lvid) {
+                    lv = levels[i];
+                    break;
+                }
+            }
+            lv['lvinst'] = instid;
+            lv['type(normal=0,hard=1,superHard=2)'] = type;
+            lv['dda_type'] = ddaType;
+            lv['dda_para'] = ddaPara;
+            // 保存到文件
+            window.electron.send('save2csv', '../../conf/levelInfo.csv', levels);
         }
 
         function fillLvInfo() {
@@ -245,8 +265,8 @@
                             }
                         });
                         levelDetails.innerHTML = `
-                            <h2>关卡ID: ${level.lvid}</h2>
-                            <p>关卡实例名称: ${level.lvinst}</p>`
+                            <h2 id='lvid-in-lvdetails'>关卡ID: ${level.lvid}</h2>
+                            <p id='instid-in-lvdetails'>关卡实例名称: ${level.lvinst}</p>`
                         // 
                         if (level.lvinst == '') {
                             // 实例不存在,
@@ -272,38 +292,38 @@
                         console.log(createInstButton.onclick);
 
                         // 增加一个下拉框,选择类型:normal,hard,super-hard
+                        const type = level['type(normal=0,hard=1,superHard=2)'];
                         const typeSelect = document.createElement('select');
-                        typeSelect.id = 'type-select';
+                        typeSelect.id = 'type-select-in-lvdetails';
                         const normalOption = document.createElement('option');
-                        normalOption.value = 'normal';
+                        normalOption.value = '0';
                         normalOption.textContent = 'normal';
+                        normalOption.selected = type == '0';
                         const hardOption = document.createElement('option');
-                        hardOption.value = 'hard';
+                        hardOption.value = '1';
                         hardOption.textContent = 'hard';
+                        hardOption.selected = type == '1';
                         const superHardOption = document.createElement('option');
-                        superHardOption.value = 'super-hard';
+                        superHardOption.value = '2';
                         superHardOption.textContent = 'super-hard';
+                        superHardOption.selected = type == '2';
                         typeSelect.appendChild(normalOption);
                         typeSelect.appendChild(hardOption);
                         typeSelect.appendChild(superHardOption);
-                        levelDetails.innerHTML += `
-                            <p>关卡难度级别:</p>`
+                        const p = document.createElement('p');
+                        p.appendChild(document.createTextNode('关卡难度级别:'));
+                        levelDetails.appendChild(p);
                         levelDetails.appendChild(typeSelect);
-                        // 如果 type(normal=0,hard=1,superHard=2)
-                        type = level[2];
-                        if (type == 0) {
-                            normalOption.selected = true;
-                        } else if (type == 1) {
-                            hardOption.selected = true;
-                        } else if (type == 2) {
-                            superHardOption.selected = true;
-                        }
+                        
                         // 显示dda类型和参数
-                        levelDetails.innerHTML +=
-                            `
-                            <p>DDA类型: ${level.dda_type}</p>
-                            <p>DDA参数: ${level.dda_para}</p>
-                        `;
+                        const ddatype = document.createElement('p');
+                        ddatype.id = 'ddatype-in-lvdetails';
+                        ddatype.textContent = 'DDA类型: ' + level.dda_type;
+                        levelDetails.appendChild(ddatype);
+                        const ddapara = document.createElement('p');
+                        ddapara.id = 'ddapara-in-lvdetails';
+                        ddapara.textContent = 'DDA参数: ' + level.dda_para;
+                        levelDetails.appendChild(ddapara);
 
                         // 增加一个按钮,点击后可以保存
                         const saveButton = document.createElement('button');
@@ -313,9 +333,9 @@
 
                         levelDetails.addEventListener('click', (event) => {
                             if (event.target.id === 'create-instance-button') {
-                                addNewInst(level, instDetails);
+                                addNewInst(level);
                             } else if (event.target.id === 'save-lv-button') {
-                                saveLvInfo(levelDetails);
+                                saveLvInfo();
                             }
                         });
 

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

@@ -2,6 +2,7 @@ const path = require('path');
 const fs = require('fs')
 const csv = require('csv-parser')
 const prompt = require('electron-prompt');
+const createCsvWriter = require('csv-writer').createObjectCsvWriter;
 const { app, BrowserWindow } = require('electron');
 const { ipcMain } = require('electron')
 
@@ -35,6 +36,24 @@ ipcMain.on('parse-csv', (event, path) => {
       })
 })
 
+// 保存数据到csv文件
+ipcMain.on('save2csv', (event, path, data) => {
+    // 保存数据到路径中
+    // 如果数据为空,直接返回
+    if (data.length === 0) {
+        return;
+    }
+    // 从第一个数据对象中提取字段名
+    const headers = Object.keys(data[0]).map(key => ({id: key, title: key}));
+
+    const csvWriter = createCsvWriter({
+        path: path,
+        header: headers
+    });
+    csvWriter.writeRecords(data).then(() => {
+    });
+})
+
 ipcMain.on('get-file-list', (event, dirPath) => {
     console.error('Will read directory: ', dirPath);
     fs.readdir(dirPath, (err, files) => {

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

@@ -3,11 +3,11 @@ const { contextBridge, ipcRenderer } = require('electron')
 contextBridge.exposeInMainWorld(
   'electron',
   {
-    send: (channel, data) => {
+    send: (channel, data1, data2) => {
       // whitelist channels
-      let validChannels = ['parse-csv', 'get-file-list', 'open-newlv-prompt', 'open-newinst-prompt'];
+      let validChannels = ['parse-csv', 'get-file-list', 'open-newlv-prompt', 'open-newinst-prompt', 'save2csv'];
       if (validChannels.includes(channel)) {
-        ipcRenderer.send(channel, data);
+        ipcRenderer.send(channel, data1, data2);
       }
     },
     receive: (channel, func) => {