Merge pull request #361 from zhaouv/refactoring-editor

editor_file
This commit is contained in:
Zhang Chen 2019-04-24 11:00:12 +08:00 committed by GitHub
commit e61d738f36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 121 additions and 102 deletions

View File

@ -42,8 +42,6 @@ editor.util.guid()//产生一个可以作为id的长随机字符串
提供了以下函数进行楼层`map`数组相关的操作
```javascript
editor.file.getFloorFileList
editor.file.loadFloorFile
editor.file.saveFloorFile
editor.file.saveFloorFileAs
```

View File

@ -53,6 +53,7 @@ editor.prototype.init = function (callback) {
editor_file_wrapper(editor);
editor_table_wrapper(editor);
editor_unsorted_1_wrapper(editor);
editor.printe=printe;
afterMainInit();
});

View File

@ -3,6 +3,7 @@ editor_file_wrapper = function (editor) {
}
// 这个函数之后挪到editor.table?
editor_file_proto.prototype.loadCommentjs=function(callback){
var commentjs = {
'comment': 'comment',

View File

@ -7,23 +7,6 @@ editor_file = function (editor, callback) {
///////////////////////////////////////////////////////////////////////////
editor.file.getFloorFileList = function (callback) {
checkCallback(callback);
/* var fs = editor.fs;
fs.readdir('project/floors',function(err, data){
callback([data,err]);
}); */
callback([editor.core.floorIds, null]);
}
//callback([Array<String>,err:String])
editor.file.loadFloorFile = function (filename, callback) {
//filename不含'/'不含'.js'
checkCallback(callback);
editor.currentFloorId = editor.core.status.floorId;
editor.currentFloorData = editor.core.floors[editor.currentFloorId];
}
//callback(err:String)
editor.file.saveFloorFile = function (callback) {
checkCallback(callback);
/* if (!isset(editor.currentFloorId) || !isset(editor.currentFloorData)) {
@ -31,31 +14,16 @@ editor_file = function (editor, callback) {
} */
var filename = 'project/floors/' + editor.currentFloorId + '.js';
var datastr = ['main.floors.', editor.currentFloorId, '=\n'];
if (editor.currentFloorData.map == 'new') {
/*
editor.currentFloorData.map = editor.map.map(function (v) {
return v.map(function () {
return 0
for(var ii=0,name;name=['map','bgmap','fgmap'][ii];ii++){
var mapArray=editor[name].map(function (v) {
return v.map(function (v) {
return v.idnum || v || 0
})
});
*/
var width = parseInt(document.getElementById('newMapWidth').value);
var height = parseInt(document.getElementById('newMapHeight').value);
var row = [];
for (var i=0;i<width;i++) row.push(0);
editor.currentFloorData.map = [];
for (var i=0;i<height;i++) editor.currentFloorData.map.push(row);
}
else{
for(var ii=0,name;name=['map','bgmap','fgmap'][ii];ii++){
var mapArray=editor[name].map(function (v) {
return v.map(function (v) {
return v.idnum || v || 0
})
});
editor.currentFloorData[name]=mapArray;
}
editor.currentFloorData[name]=mapArray;
}
// format 更改实现方式以支持undefined删除
var tempJsonObj=Object.assign({},editor.currentFloorData);
var tempMap=[['map',editor.util.guid()],['bgmap',editor.util.guid()],['fgmap',editor.util.guid()]];
@ -87,39 +55,35 @@ editor_file = function (editor, callback) {
name = saveFilename.substring(2);
title = "主塔 "+name+" 层";
}
editor.currentFloorData = {
var width = parseInt(document.getElementById('newMapsWidth').value);
var height = parseInt(document.getElementById('newMapsHeight').value);
var row = [], map = [];
for (var i=0;i<width;i++) row.push(0);
for (var i=0;i<height;i++) map.push(row);
editor.currentFloorData = Object.assign(JSON.parse(JSON.stringify(editor.file.comment._data.floors_template)), {
floorId: saveFilename,
title: title,
name: name,
width: parseInt(document.getElementById('newMapWidth').value),
height: parseInt(document.getElementById('newMapHeight').value),
canFlyTo: saveStatus?currData.canFlyTo:true,
canUseQuickShop: saveStatus?currData.canUseQuickShop:true,
cannotViewMap: saveStatus?currData.cannotViewMap:false,
cannotMoveDirectly: saveStatus?currData.cannotMoveDirectly:false,
images: [],
item_ratio: saveStatus?currData.item_ratio:1,
defaultGround: saveStatus?currData.defaultGround:"ground",
bgm: saveStatus?currData.bgm:null,
upFloor: null,
downFloor: null,
color: saveStatus?currData.color:null,
weather: saveStatus?currData.weather:null,
firstArrive: [],
eachArrive: [],
parallelDo: "",
events: {},
changeFloor: {},
afterBattle: {},
afterGetItem: {},
afterOpenDoor: {},
cannotMove: {}
};
width: width,
height: height,
map: map,
},saveStatus?{
canFlyTo: currData.canFlyTo,
canUseQuickShop: currData.canUseQuickShop,
cannotViewMap: currData.cannotViewMap,
cannotMoveDirectly: currData.cannotMoveDirectly,
item_ratio: currData.item_ratio,
defaultGround: currData.defaultGround,
bgm: currData.bgm,
color: currData.color,
weather: currData.weather,
}:{});
Object.keys(editor.currentFloorData).forEach(function (t) {
if (editor.currentFloorData[t] == null)
delete editor.currentFloorData[t];
})
editor.currentFloorData.map = "new";
editor.currentFloorId = saveFilename;
editor.file.saveFloorFile(callback);
}
@ -145,35 +109,24 @@ editor_file = function (editor, callback) {
var datas = [];
for (var i=from;i<=to;i++) {
var datastr = ['main.floors.', floorIdList[i-from], '=\n{'];
var data = {
var data = Object.assign(JSON.parse(JSON.stringify(editor.file.comment._data.floors_template)), {
floorId: floorIdList[i-from],
title: calValue(document.getElementById('newFloorTitles').value, i),
name: calValue(document.getElementById('newFloorNames').value, i),
width: width,
height: height,
map: map,
canFlyTo: saveStatus?currData.canFlyTo:true,
canUseQuickShop: saveStatus?currData.canUseQuickShop:true,
cannotViewMap: saveStatus?currData.cannotViewMap:false,
cannotMoveDirectly: saveStatus?currData.cannotMoveDirectly:false,
images: [],
item_ratio: saveStatus?currData.item_ratio:1,
defaultGround: saveStatus?currData.defaultGround:"ground",
bgm: saveStatus?currData.bgm:null,
upFloor: null,
downFloor: null,
color: saveStatus?currData.color:null,
weather: saveStatus?currData.weather:null,
firstArrive: [],
eachArrive: [],
parallelDo: "",
events: {},
changeFloor: {},
afterBattle: {},
afterGetItem: {},
afterOpenDoor: {},
cannotMove: {}
};
},saveStatus?{
canFlyTo: currData.canFlyTo,
canUseQuickShop: currData.canUseQuickShop,
cannotViewMap: currData.cannotViewMap,
cannotMoveDirectly: currData.cannotMoveDirectly,
item_ratio: currData.item_ratio,
defaultGround: currData.defaultGround,
bgm: currData.bgm,
color: currData.color,
weather: currData.weather,
}:{});
Object.keys(data).forEach(function (t) {
if (data[t] == null)
delete data[t];

View File

@ -5,7 +5,10 @@ editor_game_wrapper = function (editor, main, core) {
this.replacerRecord = {}
}
//////////////////// 修改数据相关 ////////////////////
// 三个 replacer 和 replacerRecord 暂时放在此处
editor_game.prototype.replacerForLoading = function (_key, value) {
var rmap = editor.game.replacerRecord;
if (value instanceof Function) {
@ -29,7 +32,7 @@ editor_game_wrapper = function (editor, main, core) {
return value
}
editor_game.prototype.getValue = function(field){
editor_game.prototype.getValue = function (field) {
var rmap = editor.game.replacerRecord;
var value = eval(field)
if (rmap.hasOwnProperty(oldval)) {
@ -39,13 +42,13 @@ editor_game_wrapper = function (editor, main, core) {
}
}
editor_game.prototype.setValue = function(field,value){
editor_game.prototype.setValue = function (field, value) {
var rmap = editor.game.replacerRecord;
var oldval = eval(field)
if (rmap.hasOwnProperty(oldval)) {
rmap[value]=eval(value)
rmap[value] = eval(value)
} else {
eval(field+'='+value)
eval(field + '=' + value)
}
}
@ -62,6 +65,9 @@ editor_game_wrapper = function (editor, main, core) {
data_a1e2fb4a_e986_4524_b0da_9b7ba7c0874d = JSON.parse(JSON.stringify(data_a1e2fb4a_e986_4524_b0da_9b7ba7c0874d, rf));
}
//////////////////// 加载游戏数据相关 ////////////////////
// 初始化数字与地图图块的对应
editor_game.prototype.idsInit = function (maps, icons) {
editor.ids = [0];
editor.indexs = [];
@ -125,6 +131,7 @@ editor_game_wrapper = function (editor, main, core) {
}
}
// 获取当前地图
editor_game.prototype.fetchMapFromCore = function () {
var mapArray = core.maps.saveMap(core.status.floorId);
editor.map = mapArray.map(function (v) {
@ -158,6 +165,16 @@ editor_game_wrapper = function (editor, main, core) {
}
}
// 获取地图列表
editor_game.prototype.getFloorFileList = function (callback) {
// callback([Array<String>,err:String])
editor.util.checkCallback(callback);
/* editor.fs.readdir('project/floors',function(err, data){
callback([data,err]);
}); */
callback([editor.core.floorIds, null]);
}
editor.constructor.prototype.game = new editor_game();
}
//editor_game_wrapper(editor);

View File

@ -37,7 +37,7 @@ editor_unsorted_2_wrapper=function(editor_mode){
}
var selectFloor = document.getElementById('selectFloor');
editor.file.getFloorFileList(function (floors) {
editor.game.getFloorFileList(function (floors) {
var outstr = [];
floors[0].forEach(function (floor) {
outstr.push(["<option value='", floor, "'>", floor, '</option>\n'].join(''));

View File

@ -157,6 +157,17 @@ editor_util_wrapper = function (editor) {
}).join(''))
}
editor_util.prototype.isset = function (val) {
return val != null && !(typeof val == 'number' && isNaN(val));
}
editor_util.prototype.checkCallback=function(callback){
if (!editor.util.isset(callback)) {
editor.printe('未设置callback');
throw('未设置callback')
}
}
editor.constructor.prototype.util = new editor_util();
}
//editor_util_wrapper(editor);

View File

@ -1,18 +1,27 @@
# 重构
总体思路
+ 按功能拆分文件
+ 左侧页面模块化, 方便添加
+ 不同的模式的文件操作尽可能模块化
目前主要在重构editor_file, 思路是editor.file负责把editor.game内的游戏数据格式化成字符串以及写入到文件, 由editor.game来修改数据
+ editor.file维护一些标记, 描述哪些数据需要格式化并写入, 在save时写入文件(自动保存的话就是每次修改数据都触发save)
+ editor.game修改数据, 并修改editor.file中的标记
+ 此思路下editor.file的大部分内容会挪到editor.game, editor.game和editor.table可能会再进一步合并拆分
editor_file之后是更改editor.map的储存方式, 现有的存对象的模式要在对象和数字间来回转换, 非常繁琐和奇怪
再之后是把editor_unsorted_*.js整理清晰
## 文件结构
+ [ ] editor_blockly 图块化事件编辑器
+ [ ] editor_multi 多行文本编辑器
+ [x] editor_table 处理表格的生成, 及其响应的事件, 从原editor\_mode中分离
+ [ ] editor_file 调用fs.js编辑文件, 把原editor\_file模块化
+ [ ] editor_game 处理来自core的数据, 导入为editor的数据, 从原editor中分离. **只有此文件允许`\s(main|core)`形式的调用**(以及其初始化`editor_game_wrapper(editor, main, core);`)
+ [x] editor_util 生成guid等函数, 从editor分离
+ [ ] editor_file 调用fs.js编辑文件, 把原editor\_file模块化, 并且只负责文件写入
+ [ ] editor_game 处理游戏数据, 导入为editor的数据, 编辑数据, 从原editor和editor_file中抽离. **只有此文件允许`\s(main|core)`形式的调用**(以及其初始化`editor_game_wrapper(editor, main, core);`)
+ [x] editor_util 生成guid/处理颜色 等函数, 从editor分离
+ [ ] editor 执行初始化流程加组合各组件
+ [ ] 原editor_mode 移除
+ [x] 原vm 移除

View File

@ -230,7 +230,7 @@ var comment_c456ea59_6018_45ef_8bcc_211a24c627dc = {
"_type": "select",
"_select": {
"values": [
null,
"null",
"openDoor",
"passNet",
"changeLight",
@ -245,9 +245,9 @@ var comment_c456ea59_6018_45ef_8bcc_211a24c627dc = {
"_type": "select",
"_select": {
"values": [
null,
true,
false
"null",
"true",
"false"
]
},
"_data": "该图块是否不可通行true代表不可通行false代表可通行null代表使用系统缺省值"
@ -462,6 +462,35 @@ var comment_c456ea59_6018_45ef_8bcc_211a24c627dc = {
}
}
}
},
"floors_template": {
"floorId": "to be covered",
"title": "new floor",
"name": "new floor",
"width": 13,
"height": 13,
"canFlyTo": true,
"canUseQuickShop": true,
"cannotViewMap": false,
"cannotMoveDirectly": false,
"images": [],
"item_ratio": 1,
"defaultGround": "ground",
"bgm": null,
"upFloor": null,
"downFloor": null,
"color": null,
"weather": null,
"firstArrive": [],
"eachArrive": [],
"parallelDo": "",
"events": {},
"changeFloor": {},
"afterBattle": {},
"afterGetItem": {},
"afterOpenDoor": {},
"cannotMove": {}
}
}
}