diff --git a/_server/editor.js b/_server/editor.js index 52611ab8..812d901d 100644 --- a/_server/editor.js +++ b/_server/editor.js @@ -41,10 +41,19 @@ editor.info /////////// 数据相关 /////////// editor.prototype.init = function (callback) { + + var useCompress = main.useCompress; + main.useCompress = false; + editor.airwallImg = new Image(); + editor.airwallImg.src = './project/images/airwall.png'; - editor_util_wrapper(editor); - editor_game_wrapper(editor, main, core); - editor_table_wrapper(editor); + main.init('editor', function () { + editor_util_wrapper(editor); + editor_game_wrapper(editor, main, core); + editor_table_wrapper(editor); + editor_unsorted_1_wrapper(editor); + afterMainInit(); + }); var afterMainInit = function () { editor.game.fixFunctionInGameData(); @@ -54,6 +63,7 @@ editor.prototype.init = function (callback) { editor_file = editor_file(editor, function () { editor.file = editor_file; editor_mode = editor_mode(editor); + editor_unsorted_2_wrapper(editor_mode); editor.mode = editor_mode; core.resetGame(core.firstData.hero, null, core.firstData.floorId, core.initStatus.maps); core.changeFloor(core.status.floorId, null, core.firstData.hero.loc, null, function () { @@ -85,11 +95,13 @@ editor.prototype.init = function (callback) { editor_multi = editor_multi(); editor_blockly = editor_blockly(); + + if (editor.useCompress == null) editor.useCompress = useCompress; if (Boolean(callback)) callback(); } - afterMainInit(); + } editor.prototype.mapInit = function () { @@ -209,8 +221,8 @@ editor.prototype.updateMap = function () { //ctx.clearRect(x*32, y*32, 32, 32); if (tileInfo == 0) return; - if (typeof(tileInfo) == typeof([][0]) || !hasOwnProp(tileInfo, 'idnum')) {//未定义块画红块 - if (typeof(tileInfo) != typeof([][0]) && hasOwnProp(tileInfo, 'images')) { + if (typeof(tileInfo) == typeof([][0]) || !Object.prototype.hasOwnProperty.call(tileInfo, 'idnum')) {//未定义块画红块 + if (typeof(tileInfo) != typeof([][0]) && Object.prototype.hasOwnProperty.call(tileInfo, 'images')) { ctx.drawImage(core.material.images[tileInfo.images], 0, tileInfo.y * 32, 32, 32, x * 32, y * 32, 32, 32); } ctx.strokeStyle = 'red'; @@ -463,842 +475,11 @@ editor.prototype.setSelectBoxFromInfo=function(thisevent){ } editor.prototype.listen = function () { - - document.body.onmousedown = function (e) { - //console.log(e); - var clickpath = []; - var getpath=function(e) { - var path = []; - var currentElem = e.target; - while (currentElem) { - path.push(currentElem); - currentElem = currentElem.parentElement; - } - if (path.indexOf(window) === -1 && path.indexOf(document) === -1) - path.push(document); - if (path.indexOf(window) === -1) - path.push(window); - return path; - } - getpath(e).forEach(function (node) { - if (!node.getAttribute) return; - var id_ = node.getAttribute('id'); - if (id_) { - if (['left', 'left1', 'left2', 'left3', 'left4', 'left5', 'left8', 'mobileview'].indexOf(id_) !== -1) clickpath.push('edit'); - clickpath.push(id_); - } - }); - - var unselect=true; - for(var ii=0,thisId;thisId=['edit','tip','brushMod','brushMod2','brushMod3','layerMod','layerMod2','layerMod3','viewportButtons'][ii];ii++){ - if (clickpath.indexOf(thisId) !== -1){ - unselect=false; - break; - } - } - if (unselect) { - if (clickpath.indexOf('eui') === -1) { - if (selectBox.isSelected()) { - editor_mode.onmode(''); - editor.file.saveFloorFile(function (err) { - if (err) { - printe(err); - throw(err) - } - ;printf('地图保存成功'); - }); - } - selectBox.isSelected(false); - editor.info = {}; - } - } - //editor.mode.onmode(''); - if (e.button!=2 && !editor.isMobile){ - editor.hideMidMenu(); - } - if (clickpath.indexOf('down') !== -1 && editor.isMobile && clickpath.indexOf('midMenu') === -1){ - editor.hideMidMenu(); - } - if(clickpath.length>=2 && clickpath[0].indexOf('id_')===0){editor.lastClickId=clickpath[0]} - } - - var eui=document.getElementById('eui'); - var uc = eui.getContext('2d'); - - function fillPos(pos) { - uc.fillStyle = '#' + ~~(Math.random() * 8) + ~~(Math.random() * 8) + ~~(Math.random() * 8); - uc.fillRect(pos.x * 32 + 12 - core.bigmap.offsetX, pos.y * 32 + 12 - core.bigmap.offsetY, 8, 8); - }//在格子内画一个随机色块 - - function eToLoc(e) { - var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft - var scrollTop = document.documentElement.scrollTop || document.body.scrollTop - var xx=e.clientX,yy=e.clientY - if(editor.isMobile){xx=e.touches[0].clientX,yy=e.touches[0].clientY} - editor.loc = { - 'x': scrollLeft + xx - mid.offsetLeft - mapEdit.offsetLeft, - 'y': scrollTop + yy - mid.offsetTop - mapEdit.offsetTop, - 'size': editor.isMobile?(32*innerWidth*0.96/core.__PIXELS__):32 - }; - return editor.loc; - }//返回可用的组件内坐标 - - function locToPos(loc, addViewportOffset) { - var offsetX=0, offsetY=0; - if (addViewportOffset){ - offsetX=core.bigmap.offsetX/32; - offsetY=core.bigmap.offsetY/32; - } - editor.pos = {'x': ~~(loc.x / loc.size)+offsetX, 'y': ~~(loc.y / loc.size)+offsetY} - return editor.pos; - } - - var holdingPath = 0; - var stepPostfix = null;//用于存放寻路检测的第一个点之后的后续移动 - - var mouseOutCheck = 2; - - function clear1() { - if (mouseOutCheck > 1) { - mouseOutCheck--; - setTimeout(clear1, 1000); - return; - } - holdingPath = 0; - stepPostfix = []; - uc.clearRect(0, 0, core.__PIXELS__, core.__PIXELS__); - }//用于鼠标移出canvas时的自动清除状态 - - eui.oncontextmenu=function(e){e.preventDefault()} - - eui.ondblclick = function(e) { - // 双击地图可以选中素材 - var loc = eToLoc(e); - var pos = locToPos(loc,true); - editor.setSelectBoxFromInfo(editor[editor.layerMod][pos.y][pos.x]); - return; - } - - eui.onmousedown = function (e) { - if (e.button==2){ - var loc = eToLoc(e); - var pos = locToPos(loc,true); - editor.showMidMenu(e.clientX,e.clientY); - return; - } - if (!selectBox.isSelected()) { - var loc = eToLoc(e); - var pos = locToPos(loc,true); - editor_mode.onmode('nextChange'); - editor_mode.onmode('loc'); - //editor_mode.loc(); - //tip.whichShow(1); - if(editor.isMobile)editor.showMidMenu(e.clientX,e.clientY); - return; - } - - - holdingPath = 1; - mouseOutCheck = 2; - setTimeout(clear1); - e.stopPropagation(); - uc.clearRect(0, 0, core.__PIXELS__, core.__PIXELS__); - var loc = eToLoc(e); - var pos = locToPos(loc,true); - stepPostfix = []; - stepPostfix.push(pos); - fillPos(pos); - } - - eui.onmousemove = function (e) { - if (!selectBox.isSelected()) { - //tip.whichShow(1); - return; - } - - if (holdingPath == 0) { - return; - } - mouseOutCheck = 2; - e.stopPropagation(); - var loc = eToLoc(e); - var pos = locToPos(loc,true); - var pos0 = stepPostfix[stepPostfix.length - 1] - var directionDistance = [pos.y - pos0.y, pos0.x - pos.x, pos0.y - pos.y, pos.x - pos0.x] - var max = 0, index = 4; - for (var i = 0; i < 4; i++) { - if (directionDistance[i] > max) { - index = i; - max = directionDistance[i]; - } - } - var pos = [{'x': 0, 'y': 1}, {'x': -1, 'y': 0}, {'x': 0, 'y': -1}, {'x': 1, 'y': 0}, false][index] - if (pos) { - pos.x += pos0.x; - pos.y += pos0.y; - stepPostfix.push(pos); - fillPos(pos); - } - } - - eui.onmouseup = function (e) { - if (!selectBox.isSelected()) { - //tip.whichShow(1); - return; - } - holdingPath = 0; - e.stopPropagation(); - if (stepPostfix && stepPostfix.length) { - editor.preMapData = JSON.parse(JSON.stringify({map:editor.map,fgmap:editor.fgmap,bgmap:editor.bgmap})); - if(editor.brushMod!=='line'){ - var x0=stepPostfix[0].x; - var y0=stepPostfix[0].y; - var x1=stepPostfix[stepPostfix.length-1].x; - var y1=stepPostfix[stepPostfix.length-1].y; - if(x0>x1){x0^=x1;x1^=x0;x0^=x1;}//swap - if(y0>y1){y0^=y1;y1^=y0;y0^=y1;}//swap - stepPostfix=[]; - for(var jj=y0;jj<=y1;jj++){ - for(var ii=x0;ii<=x1;ii++){ - stepPostfix.push({x:ii,y:jj}) - } - } - } - currDrawData.pos = JSON.parse(JSON.stringify(stepPostfix)); - currDrawData.info = JSON.parse(JSON.stringify(editor.info)); - reDo = null; - // console.log(stepPostfix); - if(editor.brushMod==='tileset' && core.tilesets.indexOf(editor.info.images)!==-1){ - var imgWidth=~~(core.material.images.tilesets[editor.info.images].width/32); - var x0=stepPostfix[0].x; - var y0=stepPostfix[0].y; - var idnum=editor.info.idnum; - for (var ii = 0; ii < stepPostfix.length; ii++){ - if(stepPostfix[ii].y!=y0){ - y0++; - idnum+=imgWidth; - } - editor[editor.layerMod][stepPostfix[ii].y][stepPostfix[ii].x] = editor.ids[editor.indexs[idnum+stepPostfix[ii].x-x0]]; - } - } else { - for (var ii = 0; ii < stepPostfix.length; ii++) - editor[editor.layerMod][stepPostfix[ii].y][stepPostfix[ii].x] = editor.info; - } - // console.log(editor.map); - editor.updateMap(); - holdingPath = 0; - stepPostfix = []; - uc.clearRect(0, 0, core.__PIXELS__, core.__PIXELS__); - } - } - - /* - document.getElementById('mid').onkeydown = function (e) { - console.log(e); - if (e.keyCode==37) { - editor.moveViewport(-1, 0); - } - if (e.keyCode==38) { - editor.moveViewport(0, -1); - } - if (e.keyCode==39) { - editor.moveViewport(1, 0); - } - if (e.keyCode==40) { - editor.moveViewport(0, 1); - } - } - */ - - document.getElementById('mid').onmousewheel = function (e) { - e.preventDefault(); - var wheel = function (direct) { - var index=editor.core.floorIds.indexOf(editor.currentFloorId); - var toId = editor.currentFloorId; - - if (direct>0 && index0) - toId = editor.core.floorIds[index-1]; - else return; - - editor_mode.onmode('nextChange'); - editor_mode.onmode('floor'); - document.getElementById('selectFloor').value = toId; - editor.changeFloor(toId); - } - - try { - if (e.wheelDelta) - wheel(Math.sign(e.wheelDelta)) - else if (e.detail) - wheel(Math.sign(e.detail)); - } - catch (ee) { - console.log(ee); - } - } - - editor.preMapData = null; - var currDrawData = { - pos: [], - info: {} - }; - var reDo = null; - var shortcut = core.getLocalStorage('shortcut',{48: 0, 49: 0, 50: 0, 51: 0, 52: 0, 53: 0, 54: 0, 55: 0, 56: 0, 57: 0}); - document.body.onkeydown = function (e) { - - // 监听Ctrl+S保存 - if (e.ctrlKey && e.keyCode == 83) { - e.preventDefault(); - if (editor_multi.id != "") { - editor_multi.confirm(); // 保存脚本编辑器 - } - else if (editor_blockly.id != "") { - editor_blockly.confirm(); // 保存事件编辑器 - } - else { - editor_mode.saveFloor(); - } - return; - } - - // 如果是开启事件/脚本编辑器状态,则忽略 - if (editor_multi.id!="" || editor_blockly.id!="") - return; - - // 禁止快捷键的默认行为 - if (e.ctrlKey && [89, 90, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1) - e.preventDefault(); - if (e.altKey && [48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1) - e.preventDefault(); - //Ctrl+z 撤销上一步undo - if (e.keyCode == 90 && e.ctrlKey && editor.preMapData && currDrawData.pos.length && selectBox.isSelected()) { - editor.map = JSON.parse(JSON.stringify(editor.preMapData.map)); - editor.fgmap = JSON.parse(JSON.stringify(editor.preMapData.fgmap)); - editor.bgmap = JSON.parse(JSON.stringify(editor.preMapData.bgmap)); - editor.updateMap(); - reDo = JSON.parse(JSON.stringify(currDrawData)); - currDrawData = {pos: [], info: {}}; - editor.preMapData = null; - } - //Ctrl+y 重做一步redo - if (e.keyCode == 89 && e.ctrlKey && reDo && reDo.pos.length && selectBox.isSelected()) { - editor.preMapData = JSON.parse(JSON.stringify({map:editor.map,fgmap:editor.fgmap,bgmap:editor.bgmap})); - for (var j = 0; j < reDo.pos.length; j++) - editor.map[reDo.pos[j].y][reDo.pos[j].x] = JSON.parse(JSON.stringify(reDo.info)); - - editor.updateMap(); - currDrawData = JSON.parse(JSON.stringify(reDo)); - reDo = null; - } - - // PGUP和PGDOWN切换楼层 - if (e.keyCode==33) { - e.preventDefault(); - var index=editor.core.floorIds.indexOf(editor.currentFloorId); - if (index0) { - var toId = editor.core.floorIds[index-1]; - editor_mode.onmode('nextChange'); - editor_mode.onmode('floor'); - document.getElementById('selectFloor').value = toId; - editor.changeFloor(toId); - } - } - //ctrl + 0~9 切换到快捷图块 - if (e.ctrlKey && [48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1){ - editor.setSelectBoxFromInfo(JSON.parse(JSON.stringify(shortcut[e.keyCode]||0))); - } - //alt + 0~9 改变快捷图块 - if (e.altKey && [48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1){ - var infoToSave = JSON.stringify(editor.info||0); - if(infoToSave==JSON.stringify({}))return; - shortcut[e.keyCode]=JSON.parse(infoToSave); - printf('已保存该快捷图块, ctrl + '+(e.keyCode-48)+' 使用.') - core.setLocalStorage('shortcut',shortcut); - } - var focusElement = document.activeElement; - if (!focusElement || focusElement.tagName.toLowerCase()=='body') { - // wasd平移大地图 - if (e.keyCode==87) - editor.moveViewport(0,-1) - else if (e.keyCode==65) - editor.moveViewport(-1,0) - else if (e.keyCode==83) - editor.moveViewport(0,1); - else if (e.keyCode==68) - editor.moveViewport(1,0); - } - } - - var getScrollBarHeight = function () { - var outer = document.createElement("div"); - outer.style.visibility = "hidden"; - outer.style.width = "100px"; - outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps - - document.body.appendChild(outer); - - var widthNoScroll = outer.offsetWidth; - // force scrollbars - outer.style.overflow = "scroll"; - - // add innerdiv - var inner = document.createElement("div"); - inner.style.width = "100%"; - outer.appendChild(inner); - - var widthWithScroll = inner.offsetWidth; - - // remove divs - outer.parentNode.removeChild(outer); - - return widthNoScroll - widthWithScroll; - } - var scrollBarHeight = getScrollBarHeight(); - - var dataSelection = document.getElementById('dataSelection'); - var iconLib=document.getElementById('iconLib'); - iconLib.onmousedown = function (e) { - e.stopPropagation(); - if (!editor.isMobile && e.clientY>=(635 - 5 - scrollBarHeight)) return; - var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft; - var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; - var loc = { - 'x': scrollLeft + e.clientX + iconLib.scrollLeft - right.offsetLeft - iconLib.offsetLeft, - 'y': scrollTop + e.clientY + iconLib.scrollTop - right.offsetTop - iconLib.offsetTop, - 'size': 32 - }; - editor.loc = loc; - var pos = locToPos(loc); - for (var spriter in editor.widthsX) { - if (pos.x >= editor.widthsX[spriter][1] && pos.x < editor.widthsX[spriter][2]) { - var ysize = spriter.endsWith('48') ? 48 : 32; - loc.ysize = ysize; - pos.images = editor.widthsX[spriter][0]; - pos.y = ~~(loc.y / loc.ysize); - if(core.tilesets.indexOf(pos.images)==-1)pos.x = editor.widthsX[spriter][1]; - var autotiles = core.material.images['autotile']; - if (pos.images == 'autotile') { - var imNames = Object.keys(autotiles); - if ((pos.y + 1) * ysize > editor.widthsX[spriter][3]) - pos.y = ~~(editor.widthsX[spriter][3] / ysize) - 4; - else { - for (var i = 0; i < imNames.length; i++) { - if (pos.y >= 4 * i && pos.y < 4 * (i + 1)) { - pos.images = imNames[i]; - pos.y = 4 * i; - } - } - } - } else if ((pos.y + 1) * ysize > editor.widthsX[spriter][3]) - pos.y = ~~(editor.widthsX[spriter][3] / ysize) - 1; - - selectBox.isSelected(true); - // console.log(pos,core.material.images[pos.images].height) - dataSelection.style.left = pos.x * 32 + 'px'; - dataSelection.style.top = pos.y * ysize + 'px'; - dataSelection.style.height = ysize - 6 + 'px'; - - if (pos.x == 0 && pos.y == 0) { - // editor.info={idnum:0, id:'empty','images':'清除块', 'y':0}; - editor.info = 0; - } else if(pos.x == 0 && pos.y == 1){ - editor.info = editor.ids[editor.indexs[17]]; - } else { - if (hasOwnProp(autotiles, pos.images)) editor.info = {'images': pos.images, 'y': 0}; - else if (pos.images == 'terrains') editor.info = {'images': pos.images, 'y': pos.y - 2}; - else if (core.tilesets.indexOf(pos.images)!=-1) editor.info = {'images': pos.images, 'y': pos.y, 'x': pos.x-editor.widthsX[spriter][1]}; - else editor.info = {'images': pos.images, 'y': pos.y}; - - for (var ii = 0; ii < editor.ids.length; ii++) { - if ((core.tilesets.indexOf(pos.images)!=-1 && editor.info.images == editor.ids[ii].images - && editor.info.y == editor.ids[ii].y && editor.info.x == editor.ids[ii].x) - || (hasOwnProp(autotiles, pos.images) && editor.info.images == editor.ids[ii].id - && editor.info.y == editor.ids[ii].y) - || (core.tilesets.indexOf(pos.images)==-1 && editor.info.images == editor.ids[ii].images - && editor.info.y == editor.ids[ii].y ) - ) { - - editor.info = editor.ids[ii]; - break; - } - } - } - tip.infos(JSON.parse(JSON.stringify(editor.info))); - editor_mode.onmode('nextChange'); - editor_mode.onmode('enemyitem'); - //editor_mode.enemyitem(); - } - } - } - - var midMenu=document.getElementById('midMenu'); - midMenu.oncontextmenu=function(e){e.preventDefault()} - editor.lastRightButtonPos=[{x:0,y:0},{x:0,y:0}]; - editor.showMidMenu=function(x,y){ - editor.lastRightButtonPos=JSON.parse(JSON.stringify( - [editor.pos,editor.lastRightButtonPos[0]] - )); - var locStr='('+editor.lastRightButtonPos[1].x+','+editor.lastRightButtonPos[1].y+')'; - var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft; - var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; - - // 检测是否是上下楼 - var thisevent = editor.map[editor.pos.y][editor.pos.x]; - if (thisevent.id=='upFloor') { - addFloorEvent.style.display='block'; - addFloorEvent.children[0].innerHTML='绑定上楼事件'; - } - else if (thisevent.id=='downFloor') { - addFloorEvent.style.display='block'; - addFloorEvent.children[0].innerHTML='绑定下楼事件'; - } - else addFloorEvent.style.display='none'; - - chooseThis.children[0].innerHTML='选中此点'+'('+editor.pos.x+','+editor.pos.y+')' - copyLoc.children[0].innerHTML='复制事件'+locStr+'到此处'; - moveLoc.children[0].innerHTML='交换事件'+locStr+'与此事件的位置'; - midMenu.style='top:'+(y+scrollTop)+'px;left:'+(x+scrollLeft)+'px;'; - } - editor.hideMidMenu=function(){ - if(editor.isMobile){ - setTimeout(function(){ - midMenu.style='display:none'; - },200) - } else { - midMenu.style='display:none'; - } - } - - var addFloorEvent = document.getElementById('addFloorEvent'); - addFloorEvent.onmousedown = function(e) { - editor.hideMidMenu(); - e.stopPropagation(); - var thisevent = editor.map[editor.pos.y][editor.pos.x]; - if (thisevent.id=='upFloor') { - editor.currentFloorData.changeFloor[editor.pos.x+","+editor.pos.y] = {"floorId": ":next", "stair": "downFloor"}; - } - else if (thisevent.id=='downFloor') { - editor.currentFloorData.changeFloor[editor.pos.x+","+editor.pos.y] = {"floorId": ":before", "stair": "upFloor"}; - } - editor.file.saveFloorFile(function (err) { - if (err) { - printe(err); - throw(err) - } - ;printf('添加楼梯事件成功'); - editor.drawPosSelection(); - editor_mode.showMode('loc'); - }); - } - - var chooseThis = document.getElementById('chooseThis'); - chooseThis.onmousedown = function(e){ - editor.hideMidMenu(); - e.stopPropagation(); - selectBox.isSelected(false); - - editor_mode.onmode('nextChange'); - editor_mode.onmode('loc'); - //editor_mode.loc(); - //tip.whichShow(1); - if(editor.isMobile)editor.showdataarea(false); - } - - var chooseInRight = document.getElementById('chooseInRight'); - chooseInRight.onmousedown = function(e){ - editor.hideMidMenu(); - e.stopPropagation(); - var thisevent = editor[editor.layerMod][editor.pos.y][editor.pos.x]; - editor.setSelectBoxFromInfo(thisevent); - } - - var fields = Object.keys(editor.file.comment._data.floors._data.loc._data); - - var copyLoc = document.getElementById('copyLoc'); - copyLoc.onmousedown = function(e){ - editor.hideMidMenu(); - e.stopPropagation(); - editor.preMapData = null; - reDo = null; - editor_mode.onmode(''); - var now = editor.pos; - var last = editor.lastRightButtonPos[1]; - var lastevent = editor.map[last.y][last.x]; - var lastinfo = 0; - if(lastevent==0){ - lastinfo = 0; - } else { - var ids=editor.indexs[lastevent.idnum]; - ids=ids[0]?ids[0]:ids; - lastinfo=editor.ids[ids]; - } - editor.map[now.y][now.x]=lastinfo; - editor.updateMap(); - fields.forEach(function(v){ - editor.currentFloorData[v][now.x+','+now.y]=editor.currentFloorData[v][last.x+','+last.y] - }) - editor.file.saveFloorFile(function (err) { - if (err) { - printe(err); - throw(err) - } - ;printf('复制事件成功'); - editor.drawPosSelection(); - }); - } - - var moveLoc = document.getElementById('moveLoc'); - moveLoc.onmousedown = function(e){ - editor.hideMidMenu(); - e.stopPropagation(); - editor.preMapData = null; - reDo = null; - var thisevent = editor.map[editor.pos.y][editor.pos.x]; - if(thisevent==0){ - editor.info = 0; - } else { - var ids=editor.indexs[thisevent.idnum]; - ids=ids[0]?ids[0]:ids; - editor.info=editor.ids[ids]; - } - editor_mode.onmode(''); - var now = editor.pos; - var last = editor.lastRightButtonPos[1]; - - var lastevent = editor.map[last.y][last.x]; - var lastinfo = 0; - if(lastevent==0){ - lastinfo = 0; - } else { - var ids=editor.indexs[lastevent.idnum]; - ids=ids[0]?ids[0]:ids; - lastinfo=editor.ids[ids]; - } - editor.map[last.y][last.x]=editor.info; - editor.map[now.y][now.x]=lastinfo; - editor.updateMap(); - - fields.forEach(function(v){ - var temp_atsfcytaf=editor.currentFloorData[v][now.x+','+now.y]; - editor.currentFloorData[v][now.x+','+now.y]=editor.currentFloorData[v][last.x+','+last.y]; - editor.currentFloorData[v][last.x+','+last.y]=temp_atsfcytaf; - }) - editor.file.saveFloorFile(function (err) { - if (err) { - printe(err); - throw(err) - } - ;printf('两位置的事件已互换'); - editor.drawPosSelection(); - }); - } - - var _clearPoint = function (clearPoint) { - editor.hideMidMenu(); - editor.preMapData = null; - reDo = null; - editor.info = 0; - editor_mode.onmode(''); - var now = editor.pos; - if (clearPoint) - editor.map[now.y][now.x]=editor.info; - editor.updateMap(); - fields.forEach(function(v){ - delete editor.currentFloorData[v][now.x+','+now.y]; - }) - editor.file.saveFloorFile(function (err) { - if (err) { - printe(err); - throw(err) - } - ;printf(clearPoint?'清空该点和事件成功':'只清空该点事件成功'); - editor.drawPosSelection(); - }); - } - - var clearEvent = document.getElementById('clearEvent'); - clearEvent.onmousedown = function (e) { - e.stopPropagation(); - _clearPoint(false); - } - - var clearLoc = document.getElementById('clearLoc'); - clearLoc.onmousedown = function(e){ - e.stopPropagation(); - _clearPoint(true); - } - - var brushMod=document.getElementById('brushMod'); - brushMod.onchange=function(){ - editor.brushMod=brushMod.value; - } - - var brushMod2=document.getElementById('brushMod2'); - if(brushMod2)brushMod2.onchange=function(){ - editor.brushMod=brushMod2.value; - } - - var brushMod3=document.getElementById('brushMod3'); - if(brushMod3)brushMod3.onchange=function(){ - editor.brushMod=brushMod3.value; - } - - var bgc = document.getElementById('bg'), fgc = document.getElementById('fg'), - evc = document.getElementById('event'), ev2c = document.getElementById('event2'); - - var layerMod=document.getElementById('layerMod'); - layerMod.onchange=function(){ - editor.layerMod=layerMod.value; - [bgc,fgc,evc,ev2c].forEach(function (x) { - x.style.opacity = 1; - }); - - // 手机端.... - if (editor.isMobile) { - if (layerMod.value == 'bgmap') { - [fgc,evc,ev2c].forEach(function (x) { - x.style.opacity = 0.3; - }); - } - if (layerMod.value == 'fgmap') { - [bgc,evc,ev2c].forEach(function (x) { - x.style.opacity = 0.3; - }); - } - } - } - - var layerMod2=document.getElementById('layerMod2'); - if(layerMod2)layerMod2.onchange=function(){ - editor.layerMod=layerMod2.value; - [fgc,evc,ev2c].forEach(function (x) { - x.style.opacity = 0.3; - }); - bgc.style.opacity = 1; - } - - var layerMod3=document.getElementById('layerMod3'); - if(layerMod3)layerMod3.onchange=function(){ - editor.layerMod=layerMod3.value; - [bgc,evc,ev2c].forEach(function (x) { - x.style.opacity = 0.3; - }); - fgc.style.opacity = 1; - } - - var viewportButtons=document.getElementById('viewportButtons'); - for(var ii=0,node;node=viewportButtons.children[ii];ii++){ - (function(x,y){ - node.onclick=function(){ - editor.moveViewport(x,y); - } - })([-1,0,0,1][ii],[0,-1,1,0][ii]); - } - + // 移动至 editor_unsorted_1.js }//绑定事件 editor.prototype.mobile_listen=function(){ - if(!editor.isMobile)return; - - var mobileview=document.getElementById('mobileview'); - var editModeSelect=document.getElementById('editModeSelect'); - var mid=document.getElementById('mid'); - var right=document.getElementById('right'); - var mobileeditdata=document.getElementById('mobileeditdata'); - - - editor.showdataarea=function(callShowMode){ - mid.style='z-index:-1;opacity: 0;'; - right.style='z-index:-1;opacity: 0;'; - mobileeditdata.style=''; - if(callShowMode)editor.mode.showMode(editModeSelect.value); - editor.hideMidMenu(); - } - mobileview.children[0].onclick=function(){ - editor.showdataarea(true) - } - mobileview.children[1].onclick=function(){ - mid.style=''; - right.style='z-index:-1;opacity: 0;'; - mobileeditdata.style='z-index:-1;opacity: 0;'; - editor.lastClickId=''; - } - mobileview.children[3].onclick=function(){ - mid.style='z-index:-1;opacity: 0;'; - right.style=''; - mobileeditdata.style='z-index:-1;opacity: 0;'; - editor.lastClickId=''; - } - - - var gettrbyid=function(){ - if(!editor.lastClickId)return false; - thisTr = document.getElementById(editor.lastClickId); - input = thisTr.children[2].children[0].children[0]; - field = thisTr.children[0].getAttribute('title'); - cobj = JSON.parse(thisTr.children[1].getAttribute('cobj')); - return [thisTr,input,field,cobj]; - } - mobileeditdata.children[0].onclick=function(){ - var info = gettrbyid() - if(!info)return; - info[1].ondblclick() - } - mobileeditdata.children[1].onclick=function(){ - var info = gettrbyid() - if(!info)return; - printf(info[2]) - } - mobileeditdata.children[2].onclick=function(){ - var info = gettrbyid() - if(!info)return; - printf(info[0].children[1].getAttribute('title')) - } - - //===== - - document.body.ontouchstart=document.body.onmousedown; - document.body.onmousedown=null; - - - var eui=document.getElementById('eui'); - eui.ontouchstart=eui.onmousedown - eui.onmousedown=null - eui.ontouchmove=eui.onmousemove - eui.onmousemove=null - eui.ontouchend=eui.onmouseup - eui.onmouseup=null - - - var chooseThis = document.getElementById('chooseThis'); - chooseThis.ontouchstart=chooseThis.onmousedown - chooseThis.onmousedown=null - var chooseInRight = document.getElementById('chooseInRight'); - chooseInRight.ontouchstart=chooseInRight.onmousedown - chooseInRight.onmousedown=null - var copyLoc = document.getElementById('copyLoc'); - copyLoc.ontouchstart=copyLoc.onmousedown - copyLoc.onmousedown=null - var moveLoc = document.getElementById('moveLoc'); - moveLoc.ontouchstart=moveLoc.onmousedown - moveLoc.onmousedown=null - var clearLoc = document.getElementById('clearLoc'); - clearLoc.ontouchstart=clearLoc.onmousedown - clearLoc.onmousedown=null - + // 移动至 editor_unsorted_1.js } editor = new editor(); \ No newline at end of file diff --git a/_server/editor_file.js b/_server/editor_file.js index 224232ae..16e23ad0 100644 --- a/_server/editor_file.js +++ b/_server/editor_file.js @@ -945,8 +945,8 @@ editor_file = function (editor, callback) { var alertWhenCompress = function(){ if(editor.useCompress===true){ - editor.useCompress=null; - setTimeout("alert('当前游戏使用的是压缩文件,修改完成后请重新压缩')",1000) + editor.useCompress='alerted'; + setTimeout("alert('当前游戏使用的是压缩文件,修改完成后请使用启动服务.exe->Js代码压缩工具重新压缩,或者把main.js的useCompress改成false来使用原始文件')",1000) } } diff --git a/_server/editor_game.js b/_server/editor_game.js index cb5a5b23..efd447bf 100644 --- a/_server/editor_game.js +++ b/_server/editor_game.js @@ -34,7 +34,7 @@ editor_game_wrapper = function (editor, main, core) { editor.MAX_NUM=MAX_NUM; var getInfoById = function (id) { var block = maps.initBlock(0, 0, id); - if (hasOwnProp(block, 'event')) { + if (Object.prototype.hasOwnProperty.call(block, 'event')) { return block; } } diff --git a/_server/editor_mode.js b/_server/editor_mode.js index b442f75c..240baee0 100644 --- a/_server/editor_mode.js +++ b/_server/editor_mode.js @@ -249,581 +249,7 @@ editor_mode = function (editor) { editor_mode.prototype.listen = function (callback) { - var newIdIdnum = document.getElementById('newIdIdnum'); - newIdIdnum.children[2].onclick = function () { - if (newIdIdnum.children[0].value && newIdIdnum.children[1].value) { - var id = newIdIdnum.children[0].value; - var idnum = parseInt(newIdIdnum.children[1].value); - if (!core.isset(idnum)) { - printe('不合法的idnum'); - return; - } - if (!/^[0-9a-zA-Z_]+$/.test(id)) { - printe('不合法的id,请使用字母、数字或下划线') - return; - } - editor.file.changeIdAndIdnum(id, idnum, editor_mode.info, function (err) { - if (err) { - printe(err); - throw(err) - } - printe('添加id的idnum成功,请F5刷新编辑器'); - }); - } else { - printe('请输入id和idnum'); - } - } - - newIdIdnum.children[4].onclick = function () { - editor.file.autoRegister(editor_mode.info, function (err) { - if (err) { - printe(err); - throw(err) - } - printe('该列所有剩余项全部自动注册成功,请F5刷新编辑器'); - }) - } - - var selectFloor = document.getElementById('selectFloor'); - editor.file.getFloorFileList(function (floors) { - var outstr = []; - floors[0].forEach(function (floor) { - outstr.push(["\n'].join('')); - }); - selectFloor.innerHTML = outstr.join(''); - selectFloor.value = core.status.floorId; - selectFloor.onchange = function () { - editor_mode.onmode('nextChange'); - editor_mode.onmode('floor'); - editor.changeFloor(selectFloor.value); - } - }); - - var saveFloor = document.getElementById('saveFloor'); - editor_mode.saveFloor = function () { - editor_mode.onmode(''); - editor.file.saveFloorFile(function (err) { - if (err) { - printe(err); - throw(err) - } - ;printf('保存成功'); - }); - } - saveFloor.onclick = editor_mode.saveFloor; - - var newMap = document.getElementById('newMap'); - var newFileName = document.getElementById('newFileName'); - newMap.onclick = function () { - if (!newFileName.value) return; - if (core.floorIds.indexOf(newFileName.value)>=0) { - printe("该楼层已存在!"); - return; - } - if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(newFileName.value)) { - printe("楼层名不合法!请使用字母、数字、下划线,且不能以数字开头!"); - return; - } - var width = parseInt(document.getElementById('newMapWidth').value); - var height = parseInt(document.getElementById('newMapHeight').value); - if (!core.isset(width) || !core.isset(height) || width1000) { - printe("新建地图的宽高都不得小于"+core.__SIZE__+",且宽高之积不能超过1000"); - return; - } - - editor_mode.onmode(''); - editor.file.saveNewFile(newFileName.value, function (err) { - if (err) { - printe(err); - throw(err) - } - core.floorIds.push(newFileName.value); - 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]) - } - ;printe('新建成功,请F5刷新编辑器生效'); - }); - }); - } - - var newMaps = document.getElementById('newMaps'); - var newFloors = document.getElementById('newFloors'); - newMaps.onclick = function () { - if (newFloors.style.display == 'none') newFloors.style.display = 'block'; - else newFloors.style.display = 'none'; - } - - var createNewMaps = document.getElementById('createNewMaps'); - createNewMaps.onclick = function () { - var floorIds = document.getElementById('newFloorIds').value; - if (!floorIds) return; - var from = parseInt(document.getElementById('newMapsFrom').value), - to = parseInt(document.getElementById('newMapsTo').value); - if (!core.isset(from) || !core.isset(to) || from>to || from<0 || to<0) { - printe("请输入有效的起始和终止楼层"); - return; - } - if (to-from >= 100) { - printe("一次最多创建99个楼层"); - return; - } - var floorIdList = []; - for (var i = from; i<=to; i++) { - var floorId = floorIds.replace(/\${(.*?)}/g, function (word, value) { - return eval(value); - }); - if (core.floorIds.indexOf(floorId)>=0) { - printe("要创建的楼层 "+floorId+" 已存在!"); - return; - } - if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(floorId)) { - printe("楼层名 "+floorId+" 不合法!请使用字母、数字、下划线,且不能以数字开头!"); - return; - } - if (floorIdList.indexOf(floorId)>=0) { - printe("尝试重复创建楼层 "+floorId+" !"); - return; - } - floorIdList.push(floorId); - } - - var width = parseInt(document.getElementById('newMapsWidth').value); - var height = parseInt(document.getElementById('newMapsHeight').value); - if (!core.isset(width) || !core.isset(height) || width1000) { - printe("新建地图的宽高都不得小于"+core.__SIZE__+",且宽高之积不能超过1000"); - return; - } - editor_mode.onmode(''); - - editor.file.saveNewFiles(floorIdList, from, to, function (err) { - if (err) { - printe(err); - throw(err) - } - core.floorIds = core.floorIds.concat(floorIdList); - 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]) - } - ;printe('批量创建 '+floorIdList[0]+'~'+floorIdList[floorIdList.length-1]+' 成功,请F5刷新编辑器生效'); - }); - }); - } - - var ratio = 1; - var appendPicCanvas = document.getElementById('appendPicCanvas'); - var bg = appendPicCanvas.children[0]; - var source = appendPicCanvas.children[1]; - var source_ctx=source.getContext('2d'); - var picClick = appendPicCanvas.children[2]; - var sprite = appendPicCanvas.children[3]; - var sprite_ctx=sprite.getContext('2d'); - var appendPicSelection = document.getElementById('appendPicSelection'); - - [source_ctx,sprite_ctx].forEach(function(ctx){ - ctx.mozImageSmoothingEnabled = false; - ctx.webkitImageSmoothingEnabled = false; - ctx.msImageSmoothingEnabled = false; - ctx.imageSmoothingEnabled = false; - }) - - var selectAppend = document.getElementById('selectAppend'); - var selectAppend_str = []; - ["terrains", "animates", "enemys", "enemy48", "items", "npcs", "npc48", "autotile"].forEach(function (image) { - selectAppend_str.push(["\n'].join('')); - }); - selectAppend.innerHTML = selectAppend_str.join(''); - selectAppend.onchange = function () { - - var value = selectAppend.value; - - if (value == 'autotile') { - editor_mode.appendPic.imageName = 'autotile'; - for (var jj=0;jj<4;jj++) appendPicSelection.children[jj].style = 'display:none'; - if (editor_mode.appendPic.img) { - sprite.style.width = (sprite.width = editor_mode.appendPic.img.width) / ratio + 'px'; - sprite.style.height = (sprite.height = editor_mode.appendPic.img.height) / ratio + 'px'; - sprite_ctx.clearRect(0, 0, sprite.width, sprite.height); - sprite_ctx.drawImage(editor_mode.appendPic.img, 0, 0); - } - return; - } - - var ysize = selectAppend.value.endsWith('48') ? 48 : 32; - editor_mode.appendPic.imageName = value; - var img = core.material.images[value]; - editor_mode.appendPic.toImg = img; - var num = ~~img.width / 32; - editor_mode.appendPic.num = num; - editor_mode.appendPic.index = 0; - var selectStr = ''; - for (var ii = 0; ii < num; ii++) { - appendPicSelection.children[ii].style = 'left:0;top:0;height:' + (ysize - 6) + 'px'; - selectStr += '{"x":0,"y":0},' - } - editor_mode.appendPic.selectPos = eval('[' + selectStr + ']'); - for (var jj = num; jj < 4; jj++) { - appendPicSelection.children[jj].style = 'display:none'; - } - sprite.style.width = (sprite.width = img.width) / ratio + 'px'; - sprite.style.height = (sprite.height = img.height + ysize) / ratio + 'px'; - sprite_ctx.drawImage(img, 0, 0); - } - selectAppend.onchange(); - - var getPixel=editor.util.getPixel - var setPixel=editor.util.setPixel - - var autoAdjust = function (image, callback) { - var changed = false; - - // Step 1: 检测白底 - var tempCanvas = document.createElement('canvas').getContext('2d'); - tempCanvas.canvas.width = image.width; - tempCanvas.canvas.height = image.height; - tempCanvas.mozImageSmoothingEnabled = false; - tempCanvas.webkitImageSmoothingEnabled = false; - tempCanvas.msImageSmoothingEnabled = false; - tempCanvas.imageSmoothingEnabled = false; - tempCanvas.drawImage(image, 0, 0); - var imgData = tempCanvas.getImageData(0, 0, image.width, image.height); - var trans = 0, white = 0, black=0; - for (var i=0;iblack && white>trans*10 && confirm("看起来这张图片是以纯白为底色,是否自动调整为透明底色?")) { - for (var i=0;iwhite && black>trans*10 && confirm("看起来这张图片是以纯黑为底色,是否自动调整为透明底色?")) { - for (var i=0;i= num) editor_mode.appendPic.index = ii + 1 - num; - else editor_mode.appendPic.index++; - editor_mode.appendPic.selectPos[ii] = pos; - appendPicSelection.children[ii].style = [ - 'left:', pos.x * 32, 'px;', - 'top:', pos.y * pos.ysize, 'px;', - 'height:', pos.ysize - 6, 'px;' - ].join(''); - } - - var appendConfirm = document.getElementById('appendConfirm'); - appendConfirm.onclick = function () { - - var confirmAutotile = function () { - var image = editor_mode.appendPic.img; - if (image.width % 96 !=0 || image.height != 128) { - printe("不合法的Autotile图片!"); - return; - } - var imgData = source_ctx.getImageData(0,0,image.width,image.height); - sprite_ctx.putImageData(imgData, 0, 0); - var imgbase64 = sprite.toDataURL().split(',')[1]; - - // Step 1: List文件名 - fs.readdir('./project/images', function (err, data) { - if (err) { - printe(err); - throw(err); - } - - // Step 2: 选择Autotile文件名 - var filename; - for (var i=1;;++i) { - filename = 'autotile'+i; - if (data.indexOf(filename+".png")==-1) break; - } - - // Step 3: 写入文件 - fs.writeFile('./project/images/'+filename+".png", imgbase64, 'base64', function (err, data) { - if (err) { - printe(err); - throw(err); - } - // Step 4: 自动注册 - editor_file.registerAutotile(filename, function (err) { - if (err) { - printe(err); - throw(err); - } - printe('自动元件'+filename+'注册成功,请F5刷新编辑器'); - }) - - }) - - }) - - } - - if (selectAppend.value == 'autotile') { - confirmAutotile(); - return; - } - - var ysize = selectAppend.value.endsWith('48') ? 48 : 32; - for (var ii = 0, v; v = editor_mode.appendPic.selectPos[ii]; ii++) { - // var imgData = source_ctx.getImageData(v.x * 32, v.y * ysize, 32, ysize); - // sprite_ctx.putImageData(imgData, ii * 32, sprite.height - ysize); - // sprite_ctx.drawImage(editor_mode.appendPic.img, v.x * 32, v.y * ysize, 32, ysize, ii * 32, height, 32, ysize) - - sprite_ctx.drawImage(source_ctx.canvas, v.x*32, v.y*ysize, 32, ysize, 32*ii, sprite.height - ysize, 32, ysize); - } - var dt = sprite_ctx.getImageData(0, 0, sprite.width, sprite.height); - var imgbase64 = sprite.toDataURL().split(',')[1]; - fs.writeFile('./project/images/' + editor_mode.appendPic.imageName + '.png', imgbase64, 'base64', function (err, data) { - if (err) { - printe(err); - throw(err) - } - printe('追加素材成功,请F5刷新编辑器,或继续追加当前素材'); - sprite.style.height = (sprite.height = (sprite.height+ysize)) + "px"; - sprite_ctx.putImageData(dt, 0, 0); - }); - } - - var editModeSelect = document.getElementById('editModeSelect'); - editModeSelect.onchange = function () { - editor_mode.onmode('nextChange'); - editor_mode.onmode(editModeSelect.value); - if(editor.isMobile)editor.showdataarea(false); - } - - editor_mode.checkUnique = function (thiseval) { - if (!(thiseval instanceof Array)) return false; - var map = {}; - for (var i = 0; i=2 && clickpath[0].indexOf('id_')===0){editor.lastClickId=clickpath[0]} + } + + var eui=document.getElementById('eui'); + var uc = eui.getContext('2d'); + + function fillPos(pos) { + uc.fillStyle = '#' + ~~(Math.random() * 8) + ~~(Math.random() * 8) + ~~(Math.random() * 8); + uc.fillRect(pos.x * 32 + 12 - core.bigmap.offsetX, pos.y * 32 + 12 - core.bigmap.offsetY, 8, 8); + }//在格子内画一个随机色块 + + function eToLoc(e) { + var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft + var scrollTop = document.documentElement.scrollTop || document.body.scrollTop + var xx=e.clientX,yy=e.clientY + if(editor.isMobile){xx=e.touches[0].clientX,yy=e.touches[0].clientY} + editor.loc = { + 'x': scrollLeft + xx - mid.offsetLeft - mapEdit.offsetLeft, + 'y': scrollTop + yy - mid.offsetTop - mapEdit.offsetTop, + 'size': editor.isMobile?(32*innerWidth*0.96/core.__PIXELS__):32 + }; + return editor.loc; + }//返回可用的组件内坐标 + + function locToPos(loc, addViewportOffset) { + var offsetX=0, offsetY=0; + if (addViewportOffset){ + offsetX=core.bigmap.offsetX/32; + offsetY=core.bigmap.offsetY/32; + } + editor.pos = {'x': ~~(loc.x / loc.size)+offsetX, 'y': ~~(loc.y / loc.size)+offsetY} + return editor.pos; + } + + var holdingPath = 0; + var stepPostfix = null;//用于存放寻路检测的第一个点之后的后续移动 + + var mouseOutCheck = 2; + + function clear1() { + if (mouseOutCheck > 1) { + mouseOutCheck--; + setTimeout(clear1, 1000); + return; + } + holdingPath = 0; + stepPostfix = []; + uc.clearRect(0, 0, core.__PIXELS__, core.__PIXELS__); + }//用于鼠标移出canvas时的自动清除状态 + + eui.oncontextmenu=function(e){e.preventDefault()} + + eui.ondblclick = function(e) { + // 双击地图可以选中素材 + var loc = eToLoc(e); + var pos = locToPos(loc,true); + editor.setSelectBoxFromInfo(editor[editor.layerMod][pos.y][pos.x]); + return; + } + + eui.onmousedown = function (e) { + if (e.button==2){ + var loc = eToLoc(e); + var pos = locToPos(loc,true); + editor.showMidMenu(e.clientX,e.clientY); + return; + } + if (!selectBox.isSelected()) { + var loc = eToLoc(e); + var pos = locToPos(loc,true); + editor_mode.onmode('nextChange'); + editor_mode.onmode('loc'); + //editor_mode.loc(); + //tip.whichShow(1); + if(editor.isMobile)editor.showMidMenu(e.clientX,e.clientY); + return; + } + + + holdingPath = 1; + mouseOutCheck = 2; + setTimeout(clear1); + e.stopPropagation(); + uc.clearRect(0, 0, core.__PIXELS__, core.__PIXELS__); + var loc = eToLoc(e); + var pos = locToPos(loc,true); + stepPostfix = []; + stepPostfix.push(pos); + fillPos(pos); + } + + eui.onmousemove = function (e) { + if (!selectBox.isSelected()) { + //tip.whichShow(1); + return; + } + + if (holdingPath == 0) { + return; + } + mouseOutCheck = 2; + e.stopPropagation(); + var loc = eToLoc(e); + var pos = locToPos(loc,true); + var pos0 = stepPostfix[stepPostfix.length - 1] + var directionDistance = [pos.y - pos0.y, pos0.x - pos.x, pos0.y - pos.y, pos.x - pos0.x] + var max = 0, index = 4; + for (var i = 0; i < 4; i++) { + if (directionDistance[i] > max) { + index = i; + max = directionDistance[i]; + } + } + var pos = [{'x': 0, 'y': 1}, {'x': -1, 'y': 0}, {'x': 0, 'y': -1}, {'x': 1, 'y': 0}, false][index] + if (pos) { + pos.x += pos0.x; + pos.y += pos0.y; + stepPostfix.push(pos); + fillPos(pos); + } + } + + eui.onmouseup = function (e) { + if (!selectBox.isSelected()) { + //tip.whichShow(1); + return; + } + holdingPath = 0; + e.stopPropagation(); + if (stepPostfix && stepPostfix.length) { + editor.preMapData = JSON.parse(JSON.stringify({map:editor.map,fgmap:editor.fgmap,bgmap:editor.bgmap})); + if(editor.brushMod!=='line'){ + var x0=stepPostfix[0].x; + var y0=stepPostfix[0].y; + var x1=stepPostfix[stepPostfix.length-1].x; + var y1=stepPostfix[stepPostfix.length-1].y; + if(x0>x1){x0^=x1;x1^=x0;x0^=x1;}//swap + if(y0>y1){y0^=y1;y1^=y0;y0^=y1;}//swap + stepPostfix=[]; + for(var jj=y0;jj<=y1;jj++){ + for(var ii=x0;ii<=x1;ii++){ + stepPostfix.push({x:ii,y:jj}) + } + } + } + currDrawData.pos = JSON.parse(JSON.stringify(stepPostfix)); + currDrawData.info = JSON.parse(JSON.stringify(editor.info)); + reDo = null; + // console.log(stepPostfix); + if(editor.brushMod==='tileset' && core.tilesets.indexOf(editor.info.images)!==-1){ + var imgWidth=~~(core.material.images.tilesets[editor.info.images].width/32); + var x0=stepPostfix[0].x; + var y0=stepPostfix[0].y; + var idnum=editor.info.idnum; + for (var ii = 0; ii < stepPostfix.length; ii++){ + if(stepPostfix[ii].y!=y0){ + y0++; + idnum+=imgWidth; + } + editor[editor.layerMod][stepPostfix[ii].y][stepPostfix[ii].x] = editor.ids[editor.indexs[idnum+stepPostfix[ii].x-x0]]; + } + } else { + for (var ii = 0; ii < stepPostfix.length; ii++) + editor[editor.layerMod][stepPostfix[ii].y][stepPostfix[ii].x] = editor.info; + } + // console.log(editor.map); + editor.updateMap(); + holdingPath = 0; + stepPostfix = []; + uc.clearRect(0, 0, core.__PIXELS__, core.__PIXELS__); + } + } + + /* + document.getElementById('mid').onkeydown = function (e) { + console.log(e); + if (e.keyCode==37) { + editor.moveViewport(-1, 0); + } + if (e.keyCode==38) { + editor.moveViewport(0, -1); + } + if (e.keyCode==39) { + editor.moveViewport(1, 0); + } + if (e.keyCode==40) { + editor.moveViewport(0, 1); + } + } + */ + + document.getElementById('mid').onmousewheel = function (e) { + e.preventDefault(); + var wheel = function (direct) { + var index=editor.core.floorIds.indexOf(editor.currentFloorId); + var toId = editor.currentFloorId; + + if (direct>0 && index0) + toId = editor.core.floorIds[index-1]; + else return; + + editor_mode.onmode('nextChange'); + editor_mode.onmode('floor'); + document.getElementById('selectFloor').value = toId; + editor.changeFloor(toId); + } + + try { + if (e.wheelDelta) + wheel(Math.sign(e.wheelDelta)) + else if (e.detail) + wheel(Math.sign(e.detail)); + } + catch (ee) { + console.log(ee); + } + } + + editor.preMapData = null; + var currDrawData = { + pos: [], + info: {} + }; + var reDo = null; + var shortcut = core.getLocalStorage('shortcut',{48: 0, 49: 0, 50: 0, 51: 0, 52: 0, 53: 0, 54: 0, 55: 0, 56: 0, 57: 0}); + document.body.onkeydown = function (e) { + + // 监听Ctrl+S保存 + if (e.ctrlKey && e.keyCode == 83) { + e.preventDefault(); + if (editor_multi.id != "") { + editor_multi.confirm(); // 保存脚本编辑器 + } + else if (editor_blockly.id != "") { + editor_blockly.confirm(); // 保存事件编辑器 + } + else { + editor_mode.saveFloor(); + } + return; + } + + // 如果是开启事件/脚本编辑器状态,则忽略 + if (editor_multi.id!="" || editor_blockly.id!="") + return; + + // 禁止快捷键的默认行为 + if (e.ctrlKey && [89, 90, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1) + e.preventDefault(); + if (e.altKey && [48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1) + e.preventDefault(); + //Ctrl+z 撤销上一步undo + if (e.keyCode == 90 && e.ctrlKey && editor.preMapData && currDrawData.pos.length && selectBox.isSelected()) { + editor.map = JSON.parse(JSON.stringify(editor.preMapData.map)); + editor.fgmap = JSON.parse(JSON.stringify(editor.preMapData.fgmap)); + editor.bgmap = JSON.parse(JSON.stringify(editor.preMapData.bgmap)); + editor.updateMap(); + reDo = JSON.parse(JSON.stringify(currDrawData)); + currDrawData = {pos: [], info: {}}; + editor.preMapData = null; + } + //Ctrl+y 重做一步redo + if (e.keyCode == 89 && e.ctrlKey && reDo && reDo.pos.length && selectBox.isSelected()) { + editor.preMapData = JSON.parse(JSON.stringify({map:editor.map,fgmap:editor.fgmap,bgmap:editor.bgmap})); + for (var j = 0; j < reDo.pos.length; j++) + editor.map[reDo.pos[j].y][reDo.pos[j].x] = JSON.parse(JSON.stringify(reDo.info)); + + editor.updateMap(); + currDrawData = JSON.parse(JSON.stringify(reDo)); + reDo = null; + } + + // PGUP和PGDOWN切换楼层 + if (e.keyCode==33) { + e.preventDefault(); + var index=editor.core.floorIds.indexOf(editor.currentFloorId); + if (index0) { + var toId = editor.core.floorIds[index-1]; + editor_mode.onmode('nextChange'); + editor_mode.onmode('floor'); + document.getElementById('selectFloor').value = toId; + editor.changeFloor(toId); + } + } + //ctrl + 0~9 切换到快捷图块 + if (e.ctrlKey && [48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1){ + editor.setSelectBoxFromInfo(JSON.parse(JSON.stringify(shortcut[e.keyCode]||0))); + } + //alt + 0~9 改变快捷图块 + if (e.altKey && [48, 49, 50, 51, 52, 53, 54, 55, 56, 57].indexOf(e.keyCode) !== -1){ + var infoToSave = JSON.stringify(editor.info||0); + if(infoToSave==JSON.stringify({}))return; + shortcut[e.keyCode]=JSON.parse(infoToSave); + printf('已保存该快捷图块, ctrl + '+(e.keyCode-48)+' 使用.') + core.setLocalStorage('shortcut',shortcut); + } + var focusElement = document.activeElement; + if (!focusElement || focusElement.tagName.toLowerCase()=='body') { + // wasd平移大地图 + if (e.keyCode==87) + editor.moveViewport(0,-1) + else if (e.keyCode==65) + editor.moveViewport(-1,0) + else if (e.keyCode==83) + editor.moveViewport(0,1); + else if (e.keyCode==68) + editor.moveViewport(1,0); + } + } + + var getScrollBarHeight = function () { + var outer = document.createElement("div"); + outer.style.visibility = "hidden"; + outer.style.width = "100px"; + outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps + + document.body.appendChild(outer); + + var widthNoScroll = outer.offsetWidth; + // force scrollbars + outer.style.overflow = "scroll"; + + // add innerdiv + var inner = document.createElement("div"); + inner.style.width = "100%"; + outer.appendChild(inner); + + var widthWithScroll = inner.offsetWidth; + + // remove divs + outer.parentNode.removeChild(outer); + + return widthNoScroll - widthWithScroll; + } + var scrollBarHeight = getScrollBarHeight(); + + var dataSelection = document.getElementById('dataSelection'); + var iconLib=document.getElementById('iconLib'); + iconLib.onmousedown = function (e) { + e.stopPropagation(); + if (!editor.isMobile && e.clientY>=((core.__size__==13?630:655) - scrollBarHeight)) return; + var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft; + var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; + var loc = { + 'x': scrollLeft + e.clientX + iconLib.scrollLeft - right.offsetLeft - iconLib.offsetLeft, + 'y': scrollTop + e.clientY + iconLib.scrollTop - right.offsetTop - iconLib.offsetTop, + 'size': 32 + }; + editor.loc = loc; + var pos = locToPos(loc); + for (var spriter in editor.widthsX) { + if (pos.x >= editor.widthsX[spriter][1] && pos.x < editor.widthsX[spriter][2]) { + var ysize = spriter.indexOf('48') === -1 ? 32 : 48; + loc.ysize = ysize; + pos.images = editor.widthsX[spriter][0]; + pos.y = ~~(loc.y / loc.ysize); + if(core.tilesets.indexOf(pos.images)==-1)pos.x = editor.widthsX[spriter][1]; + var autotiles = core.material.images['autotile']; + if (pos.images == 'autotile') { + var imNames = Object.keys(autotiles); + if ((pos.y + 1) * ysize > editor.widthsX[spriter][3]) + pos.y = ~~(editor.widthsX[spriter][3] / ysize) - 4; + else { + for (var i = 0; i < imNames.length; i++) { + if (pos.y >= 4 * i && pos.y < 4 * (i + 1)) { + pos.images = imNames[i]; + pos.y = 4 * i; + } + } + } + } else if ((pos.y + 1) * ysize > editor.widthsX[spriter][3]) + pos.y = ~~(editor.widthsX[spriter][3] / ysize) - 1; + + selectBox.isSelected(true); + // console.log(pos,core.material.images[pos.images].height) + dataSelection.style.left = pos.x * 32 + 'px'; + dataSelection.style.top = pos.y * ysize + 'px'; + dataSelection.style.height = ysize - 6 + 'px'; + + if (pos.x == 0 && pos.y == 0) { + // editor.info={idnum:0, id:'empty','images':'清除块', 'y':0}; + editor.info = 0; + } else if(pos.x == 0 && pos.y == 1){ + editor.info = editor.ids[editor.indexs[17]]; + } else { + if (Object.prototype.hasOwnProperty.call(autotiles, pos.images)) editor.info = {'images': pos.images, 'y': 0}; + else if (pos.images == 'terrains') editor.info = {'images': pos.images, 'y': pos.y - 2}; + else if (core.tilesets.indexOf(pos.images)!=-1) editor.info = {'images': pos.images, 'y': pos.y, 'x': pos.x-editor.widthsX[spriter][1]}; + else editor.info = {'images': pos.images, 'y': pos.y}; + + for (var ii = 0; ii < editor.ids.length; ii++) { + if ((core.tilesets.indexOf(pos.images)!=-1 && editor.info.images == editor.ids[ii].images + && editor.info.y == editor.ids[ii].y && editor.info.x == editor.ids[ii].x) + || (Object.prototype.hasOwnProperty.call(autotiles, pos.images) && editor.info.images == editor.ids[ii].id + && editor.info.y == editor.ids[ii].y) + || (core.tilesets.indexOf(pos.images)==-1 && editor.info.images == editor.ids[ii].images + && editor.info.y == editor.ids[ii].y ) + ) { + + editor.info = editor.ids[ii]; + break; + } + } + } + tip.infos(JSON.parse(JSON.stringify(editor.info))); + editor_mode.onmode('nextChange'); + editor_mode.onmode('enemyitem'); + //editor_mode.enemyitem(); + } + } + } + + var midMenu=document.getElementById('midMenu'); + midMenu.oncontextmenu=function(e){e.preventDefault()} + editor.lastRightButtonPos=[{x:0,y:0},{x:0,y:0}]; + editor.showMidMenu=function(x,y){ + editor.lastRightButtonPos=JSON.parse(JSON.stringify( + [editor.pos,editor.lastRightButtonPos[0]] + )); + var locStr='('+editor.lastRightButtonPos[1].x+','+editor.lastRightButtonPos[1].y+')'; + var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft; + var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; + + // 检测是否是上下楼 + var thisevent = editor.map[editor.pos.y][editor.pos.x]; + if (thisevent.id=='upFloor') { + addFloorEvent.style.display='block'; + addFloorEvent.children[0].innerHTML='绑定上楼事件'; + } + else if (thisevent.id=='downFloor') { + addFloorEvent.style.display='block'; + addFloorEvent.children[0].innerHTML='绑定下楼事件'; + } + else addFloorEvent.style.display='none'; + + chooseThis.children[0].innerHTML='选中此点'+'('+editor.pos.x+','+editor.pos.y+')' + copyLoc.children[0].innerHTML='复制事件'+locStr+'到此处'; + moveLoc.children[0].innerHTML='交换事件'+locStr+'与此事件的位置'; + midMenu.style='top:'+(y+scrollTop)+'px;left:'+(x+scrollLeft)+'px;'; + } + editor.hideMidMenu=function(){ + if(editor.isMobile){ + setTimeout(function(){ + midMenu.style='display:none'; + },200) + } else { + midMenu.style='display:none'; + } + } + + var addFloorEvent = document.getElementById('addFloorEvent'); + addFloorEvent.onmousedown = function(e) { + editor.hideMidMenu(); + e.stopPropagation(); + var thisevent = editor.map[editor.pos.y][editor.pos.x]; + if (thisevent.id=='upFloor') { + editor.currentFloorData.changeFloor[editor.pos.x+","+editor.pos.y] = {"floorId": ":next", "stair": "downFloor"}; + } + else if (thisevent.id=='downFloor') { + editor.currentFloorData.changeFloor[editor.pos.x+","+editor.pos.y] = {"floorId": ":before", "stair": "upFloor"}; + } + editor.file.saveFloorFile(function (err) { + if (err) { + printe(err); + throw(err) + } + ;printf('添加楼梯事件成功'); + editor.drawPosSelection(); + editor_mode.showMode('loc'); + }); + } + + var chooseThis = document.getElementById('chooseThis'); + chooseThis.onmousedown = function(e){ + editor.hideMidMenu(); + e.stopPropagation(); + selectBox.isSelected(false); + + editor_mode.onmode('nextChange'); + editor_mode.onmode('loc'); + //editor_mode.loc(); + //tip.whichShow(1); + if(editor.isMobile)editor.showdataarea(false); + } + + var chooseInRight = document.getElementById('chooseInRight'); + chooseInRight.onmousedown = function(e){ + editor.hideMidMenu(); + e.stopPropagation(); + var thisevent = editor[editor.layerMod][editor.pos.y][editor.pos.x]; + editor.setSelectBoxFromInfo(thisevent); + } + + var fields = Object.keys(editor.file.comment._data.floors._data.loc._data); + + var copyLoc = document.getElementById('copyLoc'); + copyLoc.onmousedown = function(e){ + editor.hideMidMenu(); + e.stopPropagation(); + editor.preMapData = null; + reDo = null; + editor_mode.onmode(''); + var now = editor.pos; + var last = editor.lastRightButtonPos[1]; + var lastevent = editor.map[last.y][last.x]; + var lastinfo = 0; + if(lastevent==0){ + lastinfo = 0; + } else { + var ids=editor.indexs[lastevent.idnum]; + ids=ids[0]?ids[0]:ids; + lastinfo=editor.ids[ids]; + } + editor.map[now.y][now.x]=lastinfo; + editor.updateMap(); + fields.forEach(function(v){ + editor.currentFloorData[v][now.x+','+now.y]=editor.currentFloorData[v][last.x+','+last.y] + }) + editor.file.saveFloorFile(function (err) { + if (err) { + printe(err); + throw(err) + } + ;printf('复制事件成功'); + editor.drawPosSelection(); + }); + } + + var moveLoc = document.getElementById('moveLoc'); + moveLoc.onmousedown = function(e){ + editor.hideMidMenu(); + e.stopPropagation(); + editor.preMapData = null; + reDo = null; + var thisevent = editor.map[editor.pos.y][editor.pos.x]; + if(thisevent==0){ + editor.info = 0; + } else { + var ids=editor.indexs[thisevent.idnum]; + ids=ids[0]?ids[0]:ids; + editor.info=editor.ids[ids]; + } + editor_mode.onmode(''); + var now = editor.pos; + var last = editor.lastRightButtonPos[1]; + + var lastevent = editor.map[last.y][last.x]; + var lastinfo = 0; + if(lastevent==0){ + lastinfo = 0; + } else { + var ids=editor.indexs[lastevent.idnum]; + ids=ids[0]?ids[0]:ids; + lastinfo=editor.ids[ids]; + } + editor.map[last.y][last.x]=editor.info; + editor.map[now.y][now.x]=lastinfo; + editor.updateMap(); + + fields.forEach(function(v){ + var temp_atsfcytaf=editor.currentFloorData[v][now.x+','+now.y]; + editor.currentFloorData[v][now.x+','+now.y]=editor.currentFloorData[v][last.x+','+last.y]; + editor.currentFloorData[v][last.x+','+last.y]=temp_atsfcytaf; + }) + editor.file.saveFloorFile(function (err) { + if (err) { + printe(err); + throw(err) + } + ;printf('两位置的事件已互换'); + editor.drawPosSelection(); + }); + } + + var _clearPoint = function (clearPoint) { + editor.hideMidMenu(); + editor.preMapData = null; + reDo = null; + editor.info = 0; + editor_mode.onmode(''); + var now = editor.pos; + if (clearPoint) + editor.map[now.y][now.x]=editor.info; + editor.updateMap(); + fields.forEach(function(v){ + delete editor.currentFloorData[v][now.x+','+now.y]; + }) + editor.file.saveFloorFile(function (err) { + if (err) { + printe(err); + throw(err) + } + ;printf(clearPoint?'清空该点和事件成功':'只清空该点事件成功'); + editor.drawPosSelection(); + }); + } + + var clearEvent = document.getElementById('clearEvent'); + clearEvent.onmousedown = function (e) { + e.stopPropagation(); + _clearPoint(false); + } + + var clearLoc = document.getElementById('clearLoc'); + clearLoc.onmousedown = function(e){ + e.stopPropagation(); + _clearPoint(true); + } + + var brushMod=document.getElementById('brushMod'); + brushMod.onchange=function(){ + editor.brushMod=brushMod.value; + } + + var brushMod2=document.getElementById('brushMod2'); + if(brushMod2)brushMod2.onchange=function(){ + editor.brushMod=brushMod2.value; + } + + var brushMod3=document.getElementById('brushMod3'); + if(brushMod3)brushMod3.onchange=function(){ + editor.brushMod=brushMod3.value; + } + + var bgc = document.getElementById('bg'), fgc = document.getElementById('fg'), + evc = document.getElementById('event'), ev2c = document.getElementById('event2'); + + var layerMod=document.getElementById('layerMod'); + layerMod.onchange=function(){ + editor.layerMod=layerMod.value; + [bgc,fgc,evc,ev2c].forEach(function (x) { + x.style.opacity = 1; + }); + + // 手机端.... + if (editor.isMobile) { + if (layerMod.value == 'bgmap') { + [fgc,evc,ev2c].forEach(function (x) { + x.style.opacity = 0.3; + }); + } + if (layerMod.value == 'fgmap') { + [bgc,evc,ev2c].forEach(function (x) { + x.style.opacity = 0.3; + }); + } + } + } + + var layerMod2=document.getElementById('layerMod2'); + if(layerMod2)layerMod2.onchange=function(){ + editor.layerMod=layerMod2.value; + [fgc,evc,ev2c].forEach(function (x) { + x.style.opacity = 0.3; + }); + bgc.style.opacity = 1; + } + + var layerMod3=document.getElementById('layerMod3'); + if(layerMod3)layerMod3.onchange=function(){ + editor.layerMod=layerMod3.value; + [bgc,evc,ev2c].forEach(function (x) { + x.style.opacity = 0.3; + }); + fgc.style.opacity = 1; + } + + var viewportButtons=document.getElementById('viewportButtons'); + for(var ii=0,node;node=viewportButtons.children[ii];ii++){ + (function(x,y){ + node.onclick=function(){ + editor.moveViewport(x,y); + } + })([-1,0,0,1][ii],[0,-1,1,0][ii]); + } +} + +editor.constructor.prototype.mobile_listen=function () { + if(!editor.isMobile)return; + + var mobileview=document.getElementById('mobileview'); + var editModeSelect=document.getElementById('editModeSelect'); + var mid=document.getElementById('mid'); + var right=document.getElementById('right'); + var mobileeditdata=document.getElementById('mobileeditdata'); + + + editor.showdataarea=function(callShowMode){ + mid.style='z-index:-1;opacity: 0;'; + right.style='z-index:-1;opacity: 0;'; + mobileeditdata.style=''; + if(callShowMode)editor.mode.showMode(editModeSelect.value); + editor.hideMidMenu(); + } + mobileview.children[0].onclick=function(){ + editor.showdataarea(true) + } + mobileview.children[1].onclick=function(){ + mid.style=''; + right.style='z-index:-1;opacity: 0;'; + mobileeditdata.style='z-index:-1;opacity: 0;'; + editor.lastClickId=''; + } + mobileview.children[3].onclick=function(){ + mid.style='z-index:-1;opacity: 0;'; + right.style=''; + mobileeditdata.style='z-index:-1;opacity: 0;'; + editor.lastClickId=''; + } + + + var gettrbyid=function(){ + if(!editor.lastClickId)return false; + thisTr = document.getElementById(editor.lastClickId); + input = thisTr.children[2].children[0].children[0]; + field = thisTr.children[0].getAttribute('title'); + cobj = JSON.parse(thisTr.children[1].getAttribute('cobj')); + return [thisTr,input,field,cobj]; + } + mobileeditdata.children[0].onclick=function(){ + var info = gettrbyid() + if(!info)return; + info[1].ondblclick() + } + mobileeditdata.children[1].onclick=function(){ + var info = gettrbyid() + if(!info)return; + printf(info[2]) + } + mobileeditdata.children[2].onclick=function(){ + var info = gettrbyid() + if(!info)return; + printf(info[0].children[1].getAttribute('title')) + } + + //===== + + document.body.ontouchstart=document.body.onmousedown; + document.body.onmousedown=null; + + + var eui=document.getElementById('eui'); + eui.ontouchstart=eui.onmousedown + eui.onmousedown=null + eui.ontouchmove=eui.onmousemove + eui.onmousemove=null + eui.ontouchend=eui.onmouseup + eui.onmouseup=null + + + var chooseThis = document.getElementById('chooseThis'); + chooseThis.ontouchstart=chooseThis.onmousedown + chooseThis.onmousedown=null + var chooseInRight = document.getElementById('chooseInRight'); + chooseInRight.ontouchstart=chooseInRight.onmousedown + chooseInRight.onmousedown=null + var copyLoc = document.getElementById('copyLoc'); + copyLoc.ontouchstart=copyLoc.onmousedown + copyLoc.onmousedown=null + var moveLoc = document.getElementById('moveLoc'); + moveLoc.ontouchstart=moveLoc.onmousedown + moveLoc.onmousedown=null + var clearLoc = document.getElementById('clearLoc'); + clearLoc.ontouchstart=clearLoc.onmousedown + clearLoc.onmousedown=null +} + +} \ No newline at end of file diff --git a/_server/editor_unsorted_2.js b/_server/editor_unsorted_2.js new file mode 100644 index 00000000..ef032c86 --- /dev/null +++ b/_server/editor_unsorted_2.js @@ -0,0 +1,581 @@ +editor_unsorted_2_wrapper=function(editor_mode){ + + editor_mode.constructor.prototype.listen=function (callback) { + var newIdIdnum = document.getElementById('newIdIdnum'); + newIdIdnum.children[2].onclick = function () { + if (newIdIdnum.children[0].value && newIdIdnum.children[1].value) { + var id = newIdIdnum.children[0].value; + var idnum = parseInt(newIdIdnum.children[1].value); + if (!core.isset(idnum)) { + printe('不合法的idnum'); + return; + } + if (!/^[0-9a-zA-Z_]+$/.test(id)) { + printe('不合法的id,请使用字母、数字或下划线') + return; + } + editor.file.changeIdAndIdnum(id, idnum, editor_mode.info, function (err) { + if (err) { + printe(err); + throw(err) + } + printe('添加id的idnum成功,请F5刷新编辑器'); + }); + } else { + printe('请输入id和idnum'); + } + } + + newIdIdnum.children[4].onclick = function () { + editor.file.autoRegister(editor_mode.info, function (err) { + if (err) { + printe(err); + throw(err) + } + printe('该列所有剩余项全部自动注册成功,请F5刷新编辑器'); + }) + } + + var selectFloor = document.getElementById('selectFloor'); + editor.file.getFloorFileList(function (floors) { + var outstr = []; + floors[0].forEach(function (floor) { + outstr.push(["\n'].join('')); + }); + selectFloor.innerHTML = outstr.join(''); + selectFloor.value = core.status.floorId; + selectFloor.onchange = function () { + editor_mode.onmode('nextChange'); + editor_mode.onmode('floor'); + editor.changeFloor(selectFloor.value); + } + }); + + var saveFloor = document.getElementById('saveFloor'); + editor_mode.saveFloor = function () { + editor_mode.onmode(''); + editor.file.saveFloorFile(function (err) { + if (err) { + printe(err); + throw(err) + } + ;printf('保存成功'); + }); + } + saveFloor.onclick = editor_mode.saveFloor; + + var newMap = document.getElementById('newMap'); + var newFileName = document.getElementById('newFileName'); + newMap.onclick = function () { + if (!newFileName.value) return; + if (core.floorIds.indexOf(newFileName.value)>=0) { + printe("该楼层已存在!"); + return; + } + if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(newFileName.value)) { + printe("楼层名不合法!请使用字母、数字、下划线,且不能以数字开头!"); + return; + } + var width = parseInt(document.getElementById('newMapWidth').value); + var height = parseInt(document.getElementById('newMapHeight').value); + if (!core.isset(width) || !core.isset(height) || width1000) { + printe("新建地图的宽高都不得小于"+core.__SIZE__+",且宽高之积不能超过1000"); + return; + } + + editor_mode.onmode(''); + editor.file.saveNewFile(newFileName.value, function (err) { + if (err) { + printe(err); + throw(err) + } + core.floorIds.push(newFileName.value); + 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]) + } + ;printe('新建成功,请F5刷新编辑器生效'); + }); + }); + } + + var newMaps = document.getElementById('newMaps'); + var newFloors = document.getElementById('newFloors'); + newMaps.onclick = function () { + if (newFloors.style.display == 'none') newFloors.style.display = 'block'; + else newFloors.style.display = 'none'; + } + + var createNewMaps = document.getElementById('createNewMaps'); + createNewMaps.onclick = function () { + var floorIds = document.getElementById('newFloorIds').value; + if (!floorIds) return; + var from = parseInt(document.getElementById('newMapsFrom').value), + to = parseInt(document.getElementById('newMapsTo').value); + if (!core.isset(from) || !core.isset(to) || from>to || from<0 || to<0) { + printe("请输入有效的起始和终止楼层"); + return; + } + if (to-from >= 100) { + printe("一次最多创建99个楼层"); + return; + } + var floorIdList = []; + for (var i = from; i<=to; i++) { + var floorId = floorIds.replace(/\${(.*?)}/g, function (word, value) { + return eval(value); + }); + if (core.floorIds.indexOf(floorId)>=0) { + printe("要创建的楼层 "+floorId+" 已存在!"); + return; + } + if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(floorId)) { + printe("楼层名 "+floorId+" 不合法!请使用字母、数字、下划线,且不能以数字开头!"); + return; + } + if (floorIdList.indexOf(floorId)>=0) { + printe("尝试重复创建楼层 "+floorId+" !"); + return; + } + floorIdList.push(floorId); + } + + var width = parseInt(document.getElementById('newMapsWidth').value); + var height = parseInt(document.getElementById('newMapsHeight').value); + if (!core.isset(width) || !core.isset(height) || width1000) { + printe("新建地图的宽高都不得小于"+core.__SIZE__+",且宽高之积不能超过1000"); + return; + } + editor_mode.onmode(''); + + editor.file.saveNewFiles(floorIdList, from, to, function (err) { + if (err) { + printe(err); + throw(err) + } + core.floorIds = core.floorIds.concat(floorIdList); + 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]) + } + ;printe('批量创建 '+floorIdList[0]+'~'+floorIdList[floorIdList.length-1]+' 成功,请F5刷新编辑器生效'); + }); + }); + } + + var ratio = 1; + var appendPicCanvas = document.getElementById('appendPicCanvas'); + var bg = appendPicCanvas.children[0]; + var source = appendPicCanvas.children[1]; + var source_ctx=source.getContext('2d'); + var picClick = appendPicCanvas.children[2]; + var sprite = appendPicCanvas.children[3]; + var sprite_ctx=sprite.getContext('2d'); + var appendPicSelection = document.getElementById('appendPicSelection'); + + [source_ctx,sprite_ctx].forEach(function(ctx){ + ctx.mozImageSmoothingEnabled = false; + ctx.webkitImageSmoothingEnabled = false; + ctx.msImageSmoothingEnabled = false; + ctx.imageSmoothingEnabled = false; + }) + + var selectAppend = document.getElementById('selectAppend'); + var selectAppend_str = []; + ["terrains", "animates", "enemys", "enemy48", "items", "npcs", "npc48", "autotile"].forEach(function (image) { + selectAppend_str.push(["\n'].join('')); + }); + selectAppend.innerHTML = selectAppend_str.join(''); + selectAppend.onchange = function () { + + var value = selectAppend.value; + + if (value == 'autotile') { + editor_mode.appendPic.imageName = 'autotile'; + for (var jj=0;jj<4;jj++) appendPicSelection.children[jj].style = 'display:none'; + if (editor_mode.appendPic.img) { + sprite.style.width = (sprite.width = editor_mode.appendPic.img.width) / ratio + 'px'; + sprite.style.height = (sprite.height = editor_mode.appendPic.img.height) / ratio + 'px'; + sprite_ctx.clearRect(0, 0, sprite.width, sprite.height); + sprite_ctx.drawImage(editor_mode.appendPic.img, 0, 0); + } + return; + } + + var ysize = selectAppend.value.endsWith('48') ? 48 : 32; + editor_mode.appendPic.imageName = value; + var img = core.material.images[value]; + editor_mode.appendPic.toImg = img; + var num = ~~img.width / 32; + editor_mode.appendPic.num = num; + editor_mode.appendPic.index = 0; + var selectStr = ''; + for (var ii = 0; ii < num; ii++) { + appendPicSelection.children[ii].style = 'left:0;top:0;height:' + (ysize - 6) + 'px'; + selectStr += '{"x":0,"y":0},' + } + editor_mode.appendPic.selectPos = eval('[' + selectStr + ']'); + for (var jj = num; jj < 4; jj++) { + appendPicSelection.children[jj].style = 'display:none'; + } + sprite.style.width = (sprite.width = img.width) / ratio + 'px'; + sprite.style.height = (sprite.height = img.height + ysize) / ratio + 'px'; + sprite_ctx.drawImage(img, 0, 0); + } + selectAppend.onchange(); + + var getPixel=editor.util.getPixel + var setPixel=editor.util.setPixel + + var autoAdjust = function (image, callback) { + var changed = false; + + // Step 1: 检测白底 + var tempCanvas = document.createElement('canvas').getContext('2d'); + tempCanvas.canvas.width = image.width; + tempCanvas.canvas.height = image.height; + tempCanvas.mozImageSmoothingEnabled = false; + tempCanvas.webkitImageSmoothingEnabled = false; + tempCanvas.msImageSmoothingEnabled = false; + tempCanvas.imageSmoothingEnabled = false; + tempCanvas.drawImage(image, 0, 0); + var imgData = tempCanvas.getImageData(0, 0, image.width, image.height); + var trans = 0, white = 0, black=0; + for (var i=0;iblack && white>trans*10 && confirm("看起来这张图片是以纯白为底色,是否自动调整为透明底色?")) { + for (var i=0;iwhite && black>trans*10 && confirm("看起来这张图片是以纯黑为底色,是否自动调整为透明底色?")) { + for (var i=0;i= num) editor_mode.appendPic.index = ii + 1 - num; + else editor_mode.appendPic.index++; + editor_mode.appendPic.selectPos[ii] = pos; + appendPicSelection.children[ii].style = [ + 'left:', pos.x * 32, 'px;', + 'top:', pos.y * pos.ysize, 'px;', + 'height:', pos.ysize - 6, 'px;' + ].join(''); + } + + var appendConfirm = document.getElementById('appendConfirm'); + appendConfirm.onclick = function () { + + var confirmAutotile = function () { + var image = editor_mode.appendPic.img; + if (image.width % 96 !=0 || image.height != 128) { + printe("不合法的Autotile图片!"); + return; + } + var imgData = source_ctx.getImageData(0,0,image.width,image.height); + sprite_ctx.putImageData(imgData, 0, 0); + var imgbase64 = sprite.toDataURL().split(',')[1]; + + // Step 1: List文件名 + fs.readdir('./project/images', function (err, data) { + if (err) { + printe(err); + throw(err); + } + + // Step 2: 选择Autotile文件名 + var filename; + for (var i=1;;++i) { + filename = 'autotile'+i; + if (data.indexOf(filename+".png")==-1) break; + } + + // Step 3: 写入文件 + fs.writeFile('./project/images/'+filename+".png", imgbase64, 'base64', function (err, data) { + if (err) { + printe(err); + throw(err); + } + // Step 4: 自动注册 + editor_file.registerAutotile(filename, function (err) { + if (err) { + printe(err); + throw(err); + } + printe('自动元件'+filename+'注册成功,请F5刷新编辑器'); + }) + + }) + + }) + + } + + if (selectAppend.value == 'autotile') { + confirmAutotile(); + return; + } + + var ysize = selectAppend.value.endsWith('48') ? 48 : 32; + for (var ii = 0, v; v = editor_mode.appendPic.selectPos[ii]; ii++) { + // var imgData = source_ctx.getImageData(v.x * 32, v.y * ysize, 32, ysize); + // sprite_ctx.putImageData(imgData, ii * 32, sprite.height - ysize); + // sprite_ctx.drawImage(editor_mode.appendPic.img, v.x * 32, v.y * ysize, 32, ysize, ii * 32, height, 32, ysize) + + sprite_ctx.drawImage(source_ctx.canvas, v.x*32, v.y*ysize, 32, ysize, 32*ii, sprite.height - ysize, 32, ysize); + } + var dt = sprite_ctx.getImageData(0, 0, sprite.width, sprite.height); + var imgbase64 = sprite.toDataURL().split(',')[1]; + fs.writeFile('./project/images/' + editor_mode.appendPic.imageName + '.png', imgbase64, 'base64', function (err, data) { + if (err) { + printe(err); + throw(err) + } + printe('追加素材成功,请F5刷新编辑器,或继续追加当前素材'); + sprite.style.height = (sprite.height = (sprite.height+ysize)) + "px"; + sprite_ctx.putImageData(dt, 0, 0); + }); + } + + var editModeSelect = document.getElementById('editModeSelect'); + editModeSelect.onchange = function () { + editor_mode.onmode('nextChange'); + editor_mode.onmode(editModeSelect.value); + if(editor.isMobile)editor.showdataarea(false); + } + + editor_mode.checkUnique = function (thiseval) { + if (!(thiseval instanceof Array)) return false; + var map = {}; + for (var i = 0; i, 准备改为和core一致只储存数字 -+ editor.file在修改是不再返回obj和commentobj,只在查询时返回 - ## 功能改进 + [ ] 大地图 @@ -92,6 +94,11 @@ editor: { + [ ] ? 怪物和道具也能像其他类型那样查看“图块信息”(而不只是具体的怪物属性) ++ [ ] 素材区自动换列 + 怪物或道具太多时, 按照每100个进行拆分新开列来显示 + ++ [ ] 多帧素材只显示第一帧 + ## 左侧页面模式 标题? 保存按钮? 添加按钮? 删除按钮? diff --git a/editor-mobile.html b/editor-mobile.html index 0e4fde1f..b60e2836 100644 --- a/editor-mobile.html +++ b/editor-mobile.html @@ -12,6 +12,9 @@ if(innerWidth>innerHeight){ //pic:1242*2208 | chrome info:1340*2380 confirm('宽大于高的设备请使用正常版本的editor, 点击确定跳转')?(window.location='./editor.html'):''; } + if (location.protocol.indexOf("http")!=0) { + alert("请在启动服务中打开本编辑器!不然包括编辑在内的绝大多数功能都无法使用。"); + }
@@ -515,57 +518,29 @@ - - - + - + - + + + - diff --git a/editor.html b/editor.html index 75b7856a..7953d7ba 100644 --- a/editor.html +++ b/editor.html @@ -11,6 +11,9 @@ if(innerWidth
@@ -498,55 +501,28 @@ - - - + - + - + + + -