调整 editor.file & editor.mode

This commit is contained in:
YouWei Zhao 2018-03-04 11:11:52 +08:00
parent 2f5caae07c
commit df89fd0b7c
6 changed files with 94 additions and 80 deletions

View File

@ -59,38 +59,38 @@ editor.file.saveFloorFileAs
```
5个编辑模式有关的查询
```javascript
editor.file.editItem(editor,'redJewel',[],function(a){console.log(a)});
editor.file.editEnemy(editor,'redBat',[],function(a){console.log(a)});
editor.file.editLoc(editor,2,0,[],function(a){console.log(a)});
editor.file.editFloor(editor,[],function(a){console.log(a)});
editor.file.editTower(editor,[],function(a){console.log(a)});
editor.file.editItem('redJewel',[],function(a){console.log(a)});
editor.file.editEnemy('redBat',[],function(a){console.log(a)});
editor.file.editLoc(2,0,[],function(a){console.log(a)});
editor.file.editFloor([],function(a){console.log(a)});
editor.file.editTower([],function(a){console.log(a)});
```
5个编辑模式有关的编辑
```javascript
editor.info={images: "terrains", y: 9};
editor.file.changeIdAndIdnum(editor,'yellowWall2',16,editor.info,function(a){console.log(a)});
editor.file.editItem(editor,'book',[["change","['items']['name']","怪物手册的新名字"]],function(a){console.log(a)});
editor.file.editEnemy(editor,'redBat',[['change',"['atk']",20]],function(a){console.log(a)});
editor.file.editLoc(editor,2,6,[["change","['afterBattle']",null]],function(a){console.log(a)});
editor.file.editFloor(editor,[["change","['title']",'样板 33 层']],function(a){console.log(a)});
editor.file.editTower(editor,[["change","['values']['lavaDamage']",200]],function(a){console.log(a)});
editor.file.changeIdAndIdnum('yellowWall2',16,editor.info,function(a){console.log(a)});
editor.file.editItem('book',[["change","['items']['name']","怪物手册的新名字"]],function(a){console.log(a)});
editor.file.editEnemy('redBat',[['change',"['atk']",20]],function(a){console.log(a)});
editor.file.editLoc(2,6,[["change","['afterBattle']",null]],function(a){console.log(a)});
editor.file.editFloor([["change","['title']",'样板 33 层']],function(a){console.log(a)});
editor.file.editTower([["change","['values']['lavaDamage']",200]],function(a){console.log(a)});
```
### editor_mode.js
4个生成表格并绑定事件的函数
```javascript
editor_mode.loc();
editor_mode.emenyitem();
editor_mode.floor();
editor_mode.tower();
editor.mode.loc();
editor.mode.emenyitem();
editor.mode.floor();
editor.mode.tower();
```
切换模式
```javascript
editor_mode.onmode('');
editor_mode.onmode('loc');
editor_mode.onmode('emenyitem');
editor_mode.onmode('floor');
editor_mode.onmode('tower');
editor.mode.onmode('');
editor.mode.onmode('loc');
editor.mode.onmode('emenyitem');
editor.mode.onmode('floor');
editor.mode.onmode('tower');
```
在切换模式时,改动才会保存到文件,并且需要刷新页面使得`editor`能看到改动

View File

@ -24,7 +24,10 @@ editor.prototype.init = function(callback){
editor.main=main;
editor.core=core;
editor.fs=fs;
editor_file = editor_file(editor);
editor.file=editor_file;
editor_mode = editor_mode(editor);
editor.mode=editor_mode;
editor.material.images=core.material.images;
editor.listen(); // 开始监听事件
var hard = 'Hard';
@ -308,6 +311,13 @@ editor.prototype.changeFloor = function(floorId,callback) {
});
}
editor.prototype.guid = function() {
return 'id_'+'xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
editor.prototype.listen = function() {
var uc = eui.getContext('2d');

View File

@ -1,6 +1,6 @@
(function(){
editor_file = function(editor){
editor_file = {};
var editor_file = {};
(function(){
var script = document.createElement('script');
@ -28,7 +28,7 @@
})();
editor_file.getFloorFileList = function(editor,callback){
editor_file.getFloorFileList = function(callback){
if (!isset(callback)) {printe('未设置callback');throw('未设置callback')};
/* var fs = editor.fs;
fs.readdir('project/floors',function(err, data){
@ -37,7 +37,7 @@
callback([editor.core.floorIds,null]);
}
//callback([Array<String>,err:String])
editor_file.loadFloorFile = function(editor,filename,callback){
editor_file.loadFloorFile = function(filename,callback){
//filename不含'/'不含'.js'
if (!isset(callback)) {printe('未设置callback');throw('未设置callback')};
/* var fs = editor.fs;
@ -63,7 +63,7 @@
editor.currentFloorData = editor.core.floors[editor.currentFloorId];
}
//callback(err:String)
editor_file.saveFloorFile = function(editor,callback){
editor_file.saveFloorFile = function(callback){
if (!isset(callback)) {printe('未设置callback');throw('未设置callback')};
/* if (!isset(editor.currentFloorId) || !isset(editor.currentFloorData)) {
callback('未选中文件或无数据');
@ -85,7 +85,7 @@
});
}
//callback(err:String)
editor_file.saveFloorFileAs = function(editor,saveAsFilename,callback){
editor_file.saveFloorFileAs = function(saveAsFilename,callback){
//saveAsFilename不含'/'不含'.js'
if (!isset(callback)) {printe('未设置callback');throw('未设置callback')};
if (!isset(editor.currentFloorData)) {
@ -95,13 +95,13 @@
editor.currentFloorData=JSON.parse(JSON.stringify(editor.currentFloorData));
editor.currentFloorData.floorId=saveAsFilename;
editor.currentFloorId=saveAsFilename;
editor_file.saveFloorFile(editor,callback);
editor_file.saveFloorFile(callback);
}
//callback(err:String)
////////////////////////////////////////////////////////////////////
editor_file.changeIdAndIdnum = function(editor,id,idnum,info,callback){
editor_file.changeIdAndIdnum = function(id,idnum,info,callback){
if (!isset(callback)) {printe('未设置callback');throw('未设置callback')};
//检查maps中是否有重复的idnum或id
var change = -1;
@ -159,7 +159,7 @@
callback(null);
}
//callback(err:String)
editor_file.editItem = function(editor,id,actionList,callback){
editor_file.editItem = function(id,actionList,callback){
/*actionList:[
["change","['items']['name']","红宝石的新名字"],
["add","['items']['新的和name同级的属性']",123],
@ -208,7 +208,7 @@
//只有items.cls是items的才有itemEffect和itemEffectTip,keys和constants和tools只有items
}
//callback([obj,commentObj,err:String])
editor_file.editEnemy = function(editor,id,actionList,callback){
editor_file.editEnemy = function(id,actionList,callback){
/*actionList:[
["change","['name']","初级巫师的新名字"],
["add","['新的和name同级的属性']",123],
@ -256,7 +256,7 @@
////////////////////////////////////////////////////////////////////
editor_file.editLoc = function(editor,x,y,actionList,callback){
editor_file.editLoc = function(x,y,actionList,callback){
/*actionList:[
["change","['events']",["\t[老人,magician]领域、夹击。\n请注意领域怪需要设置value为伤害数值可参见样板中初级巫师的写法。"]],
["change","['afterBattle']",null],
@ -304,7 +304,7 @@
////////////////////////////////////////////////////////////////////
editor_file.editFloor = function(editor,actionList,callback){
editor_file.editFloor = function(actionList,callback){
/*actionList:[
["change","['title']",'样板 3 层'],
["change","['color']",null],
@ -348,7 +348,7 @@
////////////////////////////////////////////////////////////////////
editor_file.editTower = function(editor,actionList,callback){
editor_file.editTower = function(actionList,callback){
/*actionList:[
["change","['firstData']['version']",'Ver 1.0.1 (Beta)'],
["change","['values']['lavaDamage']",200],
@ -489,7 +489,7 @@
if (value[0]!='change')return;
eval("editor.currentFloorData"+value[1]+'='+JSON.stringify(value[2]));
});
editor_file.saveFloorFile(editor,callback);
editor_file.saveFloorFile(callback);
return;
}
callback('出错了,要设置的文件名不识别');
@ -523,5 +523,6 @@
// ]
*/
})();
return editor_file;
}
//editor_file = editor_file(editor);

View File

@ -11,31 +11,32 @@
pout.innerHTML='waiting\nwaiting waiting';
var editor={};
editor.fs=fs;
editor_file = editor_file(editor);
editor.file=editor_file;
//代码复制到 http://127.0.0.1:1055/drawMapGUI.html 的console中进行测试
/* editor.file.loadFloorFile(editor,'sample0',function(err){console.log(err);
editor.file.saveFloorFileAs(editor,'aaa',function(err){console.log(err)});
/* editor.file.loadFloorFile('sample0',function(err){console.log(err);
editor.file.saveFloorFileAs('aaa',function(err){console.log(err)});
}); */
//查询
/*
editor_file.editItem(editor,'redJewel',[],function(a){console.log(a)});
editor_file.editEnemy(editor,'redBat',[],function(a){console.log(a)});
editor_file.editLoc(editor,2,6,[],function(a){console.log(a)});
editor_file.editFloor(editor,[],function(a){console.log(a)});
editor.file.editTower(editor,[],function(a){console.log(a)});
editor.file.editItem('redJewel',[],function(a){console.log(a)});
editor.file.editEnemy('redBat',[],function(a){console.log(a)});
editor.file.editLoc(2,6,[],function(a){console.log(a)});
editor.file.editFloor([],function(a){console.log(a)});
editor.file.editTower([],function(a){console.log(a)});
*/
//编辑
/*
editor.info={images: "terrains", y: 9};
editor.file.changeIdAndIdnum(editor,'yellowWall2',16,editor.info,function(a){console.log(a)});
editor_file.editItem(editor,'book',[["change","['items']['name']","怪物手册的新名字"]],function(a){console.log(a)});
editor_file.editEnemy(editor,'redBat',[['change',"['atk']",20]],function(a){console.log(a)});
editor_file.editLoc(editor,2,6,[["change","['afterBattle']",null]],function(a){console.log(a)});
editor_file.editFloor(editor,[["change","['title']",'样板 33 层']],function(a){console.log(a)});
editor.file.editTower(editor,[["change","['values']['lavaDamage']",200]],function(a){console.log(a)});
editor.file.changeIdAndIdnum('yellowWall2',16,editor.info,function(a){console.log(a)});
editor.file.editItem('book',[["change","['items']['name']","怪物手册的新名字"]],function(a){console.log(a)});
editor.file.editEnemy('redBat',[['change',"['atk']",20]],function(a){console.log(a)});
editor.file.editLoc(2,6,[["change","['afterBattle']",null]],function(a){console.log(a)});
editor.file.editFloor([["change","['title']",'样板 33 层']],function(a){console.log(a)});
editor.file.editTower([["change","['values']['lavaDamage']",200]],function(a){console.log(a)});
*/
pout.innerHTML='succeed\nsucceed succeed';

View File

@ -1,3 +1,5 @@
editor_mode = function(editor){
function editor_mode(){
this.ids={
'loc':'left2',
@ -12,8 +14,11 @@ function editor_mode(){
this.info={};
this.appendPic={};
}
editor_mode.prototype.init = function(callback){
if (Boolean(callback))callback();
}
editor_mode.prototype.init_dom_ids = function(callback){
Object.keys(editor_mode.ids).forEach(function(v){
editor_mode.dom[v]=document.getElementById(editor_mode.ids[v]);
@ -90,7 +95,7 @@ editor_mode.prototype.checkIsLeaf = function(obj,commentObj,field){
}
editor_mode.prototype.objToTr = function(obj,commentObj,field){
var guid = editor_mode.guid();
var guid = editor.guid();
var thiseval = eval('obj'+field);
var comment = '';
try {
@ -131,13 +136,6 @@ editor_mode.prototype.objToTd = function(thiseval,comment){
}
}
editor_mode.prototype.guid = function() {
return 'id_'+'xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
editor_mode.prototype.addAction = function(action){
editor_mode.actionList.push(action);
}
@ -147,23 +145,23 @@ editor_mode.prototype.doActionList = function(mode,actionList){
switch (mode) {
case 'loc':
editor_file.editLoc(editor,editor_mode.pos.x,editor_mode.pos.y,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
editor.file.editLoc(editor_mode.pos.x,editor_mode.pos.y,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
break;
case 'emenyitem':
if (editor_mode.info.images=='enemys'){
editor_file.editEnemy(editor,editor_mode.info.id,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
editor.file.editEnemy(editor_mode.info.id,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
} else if (editor_mode.info.images=='items'){
editor_file.editItem(editor,editor_mode.info.id,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
editor.file.editItem(editor_mode.info.id,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
}
break;
case 'floor':
editor_file.editFloor(editor,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
editor.file.editFloor(actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
break;
case 'tower':
editor.file.editTower(editor,actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
editor.file.editTower(actionList,function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])};printf('修改成功')});
break;
default:
break;
@ -186,7 +184,7 @@ editor_mode.prototype.loc = function(callback){
document.getElementById('pos_a6771a78_a099_417c_828f_0a24851ebfce').innerText=editor_mode.pos.x+','+editor_mode.pos.y;
var objs=[];
editor_file.editLoc(editor,editor_mode.pos.x,editor_mode.pos.y,[],function(objs_){objs=objs_;console.log(objs_)});
editor.file.editLoc(editor_mode.pos.x,editor_mode.pos.y,[],function(objs_){objs=objs_;console.log(objs_)});
//只查询不修改时,内部实现不是异步的,所以可以这么写
var tableinfo=editor_mode.objToTable(objs[0],objs[1]);
document.getElementById('table_3d846fc4_7644_44d1_aa04_433d266a73df').innerHTML=tableinfo.HTML;
@ -209,9 +207,9 @@ editor_mode.prototype.emenyitem = function(callback){
var objs=[];
if (editor_mode.info.images=='enemys'){
editor_file.editEnemy(editor,editor_mode.info.id,[],function(objs_){objs=objs_;console.log(objs_)});
editor.file.editEnemy(editor_mode.info.id,[],function(objs_){objs=objs_;console.log(objs_)});
} else if (editor_mode.info.images=='items'){
editor_file.editItem(editor,editor_mode.info.id,[],function(objs_){objs=objs_;console.log(objs_)});
editor.file.editItem(editor_mode.info.id,[],function(objs_){objs=objs_;console.log(objs_)});
} else {
document.getElementById('table_a3f03d4c_55b8_4ef6_b362_b345783acd72').innerHTML='';
return;
@ -226,7 +224,7 @@ editor_mode.prototype.emenyitem = function(callback){
editor_mode.prototype.floor = function(callback){
var objs=[];
editor_file.editFloor(editor,[],function(objs_){objs=objs_;console.log(objs_)});
editor.file.editFloor([],function(objs_){objs=objs_;console.log(objs_)});
//只查询不修改时,内部实现不是异步的,所以可以这么写
var tableinfo=editor_mode.objToTable(objs[0],objs[1]);
document.getElementById('table_4a3b1b09_b2fb_4bdf_b9ab_9f4cdac14c74').innerHTML=tableinfo.HTML;
@ -236,7 +234,7 @@ editor_mode.prototype.floor = function(callback){
editor_mode.prototype.tower = function(callback){
var objs=[];
editor.file.editTower(editor,[],function(objs_){objs=objs_;console.log(objs_)});
editor.file.editTower([],function(objs_){objs=objs_;console.log(objs_)});
//只查询不修改时,内部实现不是异步的,所以可以这么写
var tableinfo=editor_mode.objToTable(objs[0],objs[1]);
document.getElementById('table_b6a03e4c_5968_4633_ac40_0dfdd2c9cde5').innerHTML=tableinfo.HTML;
@ -253,7 +251,7 @@ editor_mode.prototype.listen = function(callback){
if (newIdIdnum.children[0].value && newIdIdnum.children[1].value){
var id = newIdIdnum.children[0].value;
var idnum = parseInt(newIdIdnum.children[1].value);
editor_file.changeIdAndIdnum(editor,id,idnum,editor_mode.info,function(err){
editor.file.changeIdAndIdnum(id,idnum,editor_mode.info,function(err){
if(err){printe(err);throw(err)}
printe('添加id的idnum成功,请F5刷新编辑器');
});
@ -261,7 +259,7 @@ editor_mode.prototype.listen = function(callback){
}
var selectFloor = document.getElementById('selectFloor');
editor_file.getFloorFileList(editor,function(floors){
editor.file.getFloorFileList(function(floors){
var outstr=[];
floors[0].forEach(function(floor){
outstr.push(["<option value='",floor,"'>",floor,'</option>\n'].join(''));
@ -277,7 +275,7 @@ editor_mode.prototype.listen = function(callback){
var saveFloor = document.getElementById('saveFloor');
saveFloor.onclick = function(){
editor_mode.onmode('');
editor_file.saveFloorFile(editor,function(err){if(err){printe(err);throw(err)}});
editor.file.saveFloorFile(function(err){if(err){printe(err);throw(err)}});
}
var saveFloorAs = document.getElementById('saveFloorAs');
@ -285,10 +283,10 @@ editor_mode.prototype.listen = function(callback){
saveFloorAs.onclick = function(){
if (!saveAsName.value)return;
editor_mode.onmode('');
editor_file.saveFloorFileAs(editor,saveAsName.value,function(err){
editor.file.saveFloorFileAs(saveAsName.value,function(err){
if(err){printe(err);throw(err)}
core.floorIds.push(saveAsName.value);
editor.file.editTower(editor,[['change',"['main']['floorIds']",core.floorIds]],function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])}});
editor.file.editTower([['change',"['main']['floorIds']",core.floorIds]],function(objs_){console.log(objs_);if(objs_.slice(-1)[0]!=null){printe(objs_.slice(-1)[0]);throw(objs_.slice(-1)[0])}});
});
}
@ -436,5 +434,9 @@ editor_mode.prototype.listen = function(callback){
if (Boolean(callback))callback();
}
editor_mode = new editor_mode();
editor_mode.init();
var editor_mode = new editor_mode();
editor_mode.init_dom_ids();
return editor_mode;
}
//editor_mode = editor_mode(editor);

View File

@ -324,12 +324,12 @@ function hasOwnProp (obj, key) {
main.init('editor');
editor.init(function(){
editor.pos={x: 0, y: 0};
editor_mode.loc();
editor.mode.loc();
editor.info=editor.ids[editor.indexs[201]];
editor_mode.emenyitem();
editor_mode.floor();
editor_mode.tower();
editor_mode.listen();
editor.mode.emenyitem();
editor.mode.floor();
editor.mode.tower();
editor.mode.listen();
editor_blockly=editor_blockly();
});