From 6cf7b02065eea407ab2bb48387403ca4e22877e4 Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Wed, 28 Jul 2021 20:51:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E5=9D=97=E7=9A=84=E4=B8=8D=E9=80=8F?= =?UTF-8?q?=E6=98=8E=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _docs/api.md | 11 ++- _server/CodeMirror/defs.js | 14 +++- _server/MotaAction.g4 | 45 +++++++++++- _server/MotaActionParser.js | 14 +++- _server/editor_blocklyconfig.js | 1 + libs/control.js | 5 +- libs/events.js | 15 +++- libs/maps.js | 123 +++++++++++++++++++++++++++----- libs/ui.js | 7 +- runtime.d.ts | 4 +- 10 files changed, 205 insertions(+), 34 deletions(-) diff --git a/_docs/api.md b/_docs/api.md index 75f925c3..f6f162bc 100644 --- a/_docs/api.md +++ b/_docs/api.md @@ -1393,7 +1393,7 @@ loadOneSound: fn(name: string) addGlobalAnimate: fn(block?: block) 添加一个全局动画 -animateBlock: fn(loc?: [number]|[[number]], type?: string, time?: number, callback?: fn()) +animateBlock: fn(loc?: [number]|[[number]], type?: string|number, time?: number, callback?: fn()) 显示/隐藏某个块时的动画效果 animateSetBlock: fn(number: number|string, x: number, y: number, floorId?: string, time?: number, callback?: fn()) @@ -1581,6 +1581,9 @@ floorId: 地图id,不填视为当前地图 showDisable: 可选,true表示隐藏的图块也会被表示出来 返回值:事件层矩阵,注意对其阵元的访问是[y][x] +getMapBlockOpacity: fn(floorId?: string, x?: number, y?: number, flags?: ?) -> bool +获得某个点的不透明度 + getMapBlocksObj: fn(floorId?: string, noCache?: bool) 以x,y的形式返回每个点的事件 @@ -1727,6 +1730,9 @@ floorId: 地图id,不填视为当前地图 setMapBlockDisabled: fn(floorId?: string, x?: number, y?: number, disabled?: bool) 设置某个点图块的强制启用或禁用状态 +setMapBlockOpacity: fn(floorId?: string, x?: number, y?: number, opacity?: bool) +设置某个点图块的不透明度 + showBgFgMap: fn(name?: string, loc?: [number]|[[number]], floorId?: string, callback?: fn()) 显示前景/背景地图 @@ -1940,8 +1946,9 @@ resizeCanvas: fn(name: string, x: number, y: number) saveCanvas: fn(name: string|CanvasRenderingContext2D) 保存某个canvas状态 -setAlpha: fn(name: string|CanvasRenderingContext2D, alpha: number) +setAlpha: fn(name: string|CanvasRenderingContext2D, alpha: number) -> number 设置某个canvas接下来绘制的不透明度;不会影响已经绘制的内容 +返回设置之前画布的不透明度。 如果需要修改画布本身的不透明度请使用setOpacity 参考资料:https://www.w3school.com.cn/tags/canvas_globalalpha.asp diff --git a/_server/CodeMirror/defs.js b/_server/CodeMirror/defs.js index 201ebd6e..ee1975ac 100644 --- a/_server/CodeMirror/defs.js +++ b/_server/CodeMirror/defs.js @@ -3161,6 +3161,14 @@ var terndefs_f6783a0a_522d_417e_8407_94c67b692e50 = [ "!doc": "设置某个点图块的强制启用或禁用状态", "!type": "fn(floorId?: string, x?: number, y?: number, disabled?: bool)" }, + "getMapBlockOpacity": { + "!doc": "获得某个点图块的不透明度", + "!type": "fn(floorId?: string, x?: number, y?: number, flags?: ?) -> number" + }, + "setMapBlockOpacity": { + "!doc": "设置某个点图块的不透明度", + "!type": "fn(floorId?: string, x?: number, y?: number, opacity?: number)" + }, "decompressMap": { "!doc": "解压缩地图", "!type": "fn(mapArr: [[number]], floorId?: string) -> [[number]]" @@ -3299,7 +3307,7 @@ var terndefs_f6783a0a_522d_417e_8407_94c67b692e50 = [ }, "animateBlock": { "!doc": "显示/隐藏某个块时的动画效果", - "!type": "fn(loc?: [number]|[[number]], type?: string, time?: number, callback?: fn())" + "!type": "fn(loc?: [number]|[[number]], type?: string|number, time?: number, callback?: fn())" }, "loadMap": { "!doc": "将存档中的地图信息重新读取出来", @@ -3466,9 +3474,9 @@ var terndefs_f6783a0a_522d_417e_8407_94c67b692e50 = [ "!type": "fn(name: string|CanvasRenderingContext2D, text: string, maxWidth?: number, font?: string)" }, "setAlpha": { - "!doc": "设置某个canvas接下来绘制的不透明度;不会影响已经绘制的内容
如果需要修改画布本身的不透明度请使用setOpacity", + "!doc": "设置某个canvas接下来绘制的不透明度;不会影响已经绘制的内容
返回设置之前画布的不透明度
如果需要修改画布本身的不透明度请使用setOpacity", "!url": "https://www.w3school.com.cn/tags/canvas_globalalpha.asp", - "!type": "fn(name: string|CanvasRenderingContext2D, alpha: number)" + "!type": "fn(name: string|CanvasRenderingContext2D, alpha: number) -> number" }, "setLineWidth": { "!doc": "设置某个canvas的线宽度", diff --git a/_server/MotaAction.g4 b/_server/MotaAction.g4 index 61e0ff03..4b9659eb 100644 --- a/_server/MotaAction.g4 +++ b/_server/MotaAction.g4 @@ -42,22 +42,24 @@ return code; //事件 事件编辑器入口之一 event_m - : '事件' BGNL? Newline '覆盖触发器' Bool '启用' Bool '通行状态' B_0_List '显伤' Bool BGNL? Newline action+ BEND + : '事件' BGNL? Newline '覆盖触发器' Bool '启用' Bool '通行状态' B_0_List '显伤' Bool '不透明度' Number BGNL? Newline action+ BEND /* event_m tooltip : 编辑魔塔的事件 helpUrl : /_docs/#/instruction -default : [false,null,null,null,null] +default : [false,true,null,true,1.0,null] B_0_List_0=eval(B_0_List_0); +if (Number_0 < 0 || Number_0 > 1) throw '不透明度需要在0~1之间'; var code = { 'trigger': Bool_0?'action':null, 'enable': Bool_1, 'noPass': B_0_List_0, 'displayDamage': Bool_2, + 'opacity': Number_0, 'data': 'data_asdfefw' } -if (!Bool_0 && Bool_1 && (B_0_List_0===null) && Bool_2) code = 'data_asdfefw'; +if (!Bool_0 && Bool_1 && (B_0_List_0===null) && Bool_2 && Number_0 == 1.0) code = 'data_asdfefw'; code=JSON.stringify(code,null,2).split('"data_asdfefw"').join('[\n'+action_0+']\n'); return code; */; @@ -760,6 +762,7 @@ action | setNameMap_s | show_s | hide_s + | setBlockOpacity_s | trigger_s | insert_1_s | insert_2_s @@ -1368,6 +1371,42 @@ var code = '{"type": "hide"'+floorstr+IdString_0+Bool_0+IntString_0+Bool_1+'},\n return code; */; +setBlockOpacity_s + : '设置图块不透明度' 'x' EvalString? ',' 'y' EvalString? '楼层' IdString? '不透明度' Number '动画时间' IntString? '不等待执行完毕' Bool? Newline + + +/* setBlockOpacity_s +tooltip : setBlockOpacity: 设置图块不透明度 +helpUrl : /_docs/#/instruction +default : ["","","",1.0,"",false] +selectPoint : ["EvalString_0", "EvalString_1", "IdString_0"] +allFloorIds : ['IdString_0'] +colour : this.mapColor +var floorstr = ''; +if (EvalString_0 && EvalString_1) { + var pattern1 = MotaActionFunctions.pattern.id; + if(pattern1.test(EvalString_0) || pattern1.test(EvalString_1)){ + EvalString_0=MotaActionFunctions.PosString_pre(EvalString_0); + EvalString_1=MotaActionFunctions.PosString_pre(EvalString_1); + EvalString_0=[EvalString_0,EvalString_1] + } else { + var pattern2 = /^([+-]?\d+)(,[+-]?\d+)*$/; + if(!pattern2.test(EvalString_0) || !pattern2.test(EvalString_1))throw new Error('坐标格式错误,请右键点击帮助查看格式'); + EvalString_0=EvalString_0.split(','); + EvalString_1=EvalString_1.split(','); + if(EvalString_0.length!==EvalString_1.length)throw new Error('坐标格式错误,请右键点击帮助查看格式'); + for(var ii=0;ii 1) throw new Error('不透明度需要在0~1之间'); +IdString_0 = IdString_0 && (', "floorId": "'+IdString_0+'"'); +IntString_0 = IntString_0 ?(', "time": '+IntString_0):''; +Bool_0 = Bool_0 ?', "async": true':''; +var code = '{"type": "setBlockOpacity"'+floorstr+IdString_0+', "opacity": '+Number_0+IntString_0+Bool_0+'},\n'; +return code; +*/; + trigger_s : '触发系统事件' 'x' PosString? ',' 'y' PosString? Newline diff --git a/_server/MotaActionParser.js b/_server/MotaActionParser.js index 7fbe855f..05532f6e 100644 --- a/_server/MotaActionParser.js +++ b/_server/MotaActionParser.js @@ -9,7 +9,7 @@ ActionParser.prototype.parse = function (obj,type) { if(typeof(obj)===typeof('')) obj={'data':[obj]}; if(obj instanceof Array) obj={'data':obj}; return MotaActionBlocks['event_m'].xmlText([ - obj.trigger==='action',obj.enable,obj.noPass,obj.displayDamage,this.parseList(obj.data) + obj.trigger==='action',obj.enable,obj.noPass,obj.displayDamage,obj.opacity,this.parseList(obj.data) ]); case 'autoEvent': @@ -352,6 +352,18 @@ ActionParser.prototype.parseAction = function() { this.next = MotaActionBlocks['setBlock_s'].xmlText([ data.number||0,x_str.join(','),y_str.join(','),data.floorId||'',data.time,data.async||false,this.next]); break; + case "setBlockOpacity": // 设置图块不透明度 + data.loc=data.loc||[]; + if (!(data.loc[0] instanceof Array)) + data.loc = [data.loc]; + var x_str=[],y_str=[]; + data.loc.forEach(function (t) { + x_str.push(t[0]); + y_str.push(t[1]); + }) + this.next = MotaActionBlocks['setBlockOpacity_s'].xmlText([ + x_str.join(','),y_str.join(','),data.floorId||'',data.opacity,data.time,data.async||false,this.next]); + break; case "turnBlock": // 事件转向 data.loc=data.loc||[]; if (!(data.loc[0] instanceof Array)) diff --git a/_server/editor_blocklyconfig.js b/_server/editor_blocklyconfig.js index 58257976..329bdd72 100644 --- a/_server/editor_blocklyconfig.js +++ b/_server/editor_blocklyconfig.js @@ -153,6 +153,7 @@ editor_blocklyconfig=(function(){ MotaActionBlocks['show_s'].xmlText(), MotaActionBlocks['hide_s'].xmlText(), MotaActionBlocks['setBlock_s'].xmlText(), + MotaActionBlocks['setBlockOpacity_s'].xmlText(), MotaActionBlocks['turnBlock_s'].xmlText(), MotaActionBlocks['moveHero_s'].xmlText(), MotaActionBlocks['move_s'].xmlText(), diff --git a/libs/control.js b/libs/control.js index f80eb677..f9fc5ba9 100644 --- a/libs/control.js +++ b/libs/control.js @@ -1249,10 +1249,9 @@ control.prototype._drawDamage_draw = function (ctx, onMap) { if (px < -32 || px > core.__PIXELS__ + 32 || py < -32 || py > core.__PIXELS__ + 32) return; } - var alpha = cacheCtx.globalAlpha; - cacheCtx.globalAlpha = one.alpha; + var alpha = core.setAlpha(cacheCtx, one.alpha); core.fillBoldText(cacheCtx, one.text, px, py, one.color); - cacheCtx.globalAlpha = alpha; + core.setAlpha(cacheCtx, alpha); }); cacheCtx.translate(0, 0); diff --git a/libs/events.js b/libs/events.js index 8b2952c3..c2b91747 100644 --- a/libs/events.js +++ b/libs/events.js @@ -1202,7 +1202,7 @@ events.prototype.__precompile_getArray = function () { "choices", "confirm", "fillText", "fillBoldText", "drawTextContent" ]; var locs = [ - "show", "hide", "setBlock", "showFloorImg", "hideFloorImg", "showBgFgMap", + "show", "hide", "setBlock", "setBlockOpacity", "showFloorImg", "hideFloorImg", "showBgFgMap", "hideBgFgMap", "setBgFgBlock", "animate", "setViewport", "move", "jumoHero", "changeFloor", "changePos", "showTextImage", "showGif", "openDoor", "closeDoor", "battle", "trigger", "insert", "setEnemyOnPoint", "resetEnemyOnPoint" @@ -1389,6 +1389,19 @@ events.prototype._action_setBlock = function (data, x, y, prefix) { } } +events.prototype._action_setBlockOpacity = function (data, x, y, prefix) { + data.loc = this.__action_getLoc2D(data.loc, x, y, prefix); + if (data.time > 0 && data.floorId == core.status.floorId) { + this.__action_doAsyncFunc(data.async, core.animateBlock, data.loc, data.opacity, data.time); + } + else { + data.loc.forEach(function (t) { + core.setMapBlockOpacity(data.floorId, t[0], t[1], data.opacity); + }); + core.doAction(); + } +} + events.prototype._action_turnBlock = function (data, x, y, prefix) { data.loc = this.__action_getLoc2D(data.loc, x, y, prefix); data.loc.forEach(function (t) { diff --git a/libs/maps.js b/libs/maps.js index 405f1359..c9c28573 100644 --- a/libs/maps.js +++ b/libs/maps.js @@ -129,7 +129,13 @@ maps.prototype.extractBlocksForUI = function (map, flags) { var event = (floor.events || {})[j + "," + i]; if (event != null && event.enable === false) continue; } - map.blocks.push(Object.assign({}, this.getBlockByNumber(number), {x: j, y: i})); + var opacity = this.getMapBlockOpacity(floorId, j, i, flags); + if (opacity == null) { + // 检查初始不透明度 + var event = (floor.events || {})[j + "," + i]; + if (event != null && event.opacity != null) opacity = event.opacity; + } + map.blocks.push(Object.assign({}, this.getBlockByNumber(number), {x: j, y: i, opacity: opacity})); } } } @@ -177,11 +183,14 @@ maps.prototype.getIdOfThis = function (id) { ////// 数字和ID的对应关系 ////// maps.prototype.initBlock = function (x, y, id, addInfo, eventFloor) { var disable = null; + var opacity = null; if (eventFloor != null) { disable = this.isMapBlockDisabled(eventFloor.floorId, x, y); + opacity = this.getMapBlockOpacity(eventFloor.floorId, x, y); } var block = {'x': x, 'y': y, 'id': id}; if (disable != null) block.disable = disable; + if (opacity != null) block.opacity = opacity; if (id == 17) block.event = {"cls": "terrains", "id": "airwall", "cannotIn":["up", "down", "left", "right"]}; else if (id in this.blocksInfo) block.event = JSON.parse(JSON.stringify(this.blocksInfo[id])); @@ -239,6 +248,10 @@ maps.prototype._addEvent = function (block, x, y, event) { if (block.disable == null && event.enable != null) { block.disable = !event.enable; } + // 覆盖opacity + if (block.opacity == null && event.opacity != null) { + block.opacity = event.opacity; + } // 覆盖animate if (event.animate === false) { block.event.animate = 1; @@ -305,6 +318,46 @@ maps.prototype._processInvalidMap = function (mapArr, width, height) { return map; } +////// 获得某个点的不透明度 ////// +maps.prototype.getMapBlockOpacity = function (floorId, x, y, flags) { + if (flags == null) flags = (core.status.hero || {}).flags; + if (flags == null) return null; + var __opacity__ = flags.__opacity__ || {}; + floorId = floorId || core.status.floorId; + if (!floorId) return null; + if ((flags.__removed__ || []).indexOf(floorId) >= 0) return null; + var index = x + y * core.floors[floorId].width; + return (__opacity__[floorId]||{})[index]; +} + +////// 设置某个点的不透明度 ////// +maps.prototype.setMapBlockOpacity = function (floorId, x, y, opacity) { + if (window.flags == null) return; + floorId = floorId || core.status.floorId; + if (!floorId) return; + if (!window.flags.__opacity__) window.flags.__opacity__ = {}; + if ((window.flags.__removed__ || []).indexOf(floorId) >= 0) return; + var index = x + y * core.floors[floorId].width; + var __opacity__ = window.flags.__opacity__; + if (!__opacity__[floorId]) __opacity__[floorId] = {}; + if (opacity == null) delete __opacity__[floorId][index]; + else __opacity__[floorId][index] = opacity; + + ////// 重绘该点图块 + var block = core.getBlock(x, y, floorId, true); + if (block != null) { + block.opacity = opacity; + if (floorId == core.status.floorId && !block.disable) { + if (block.event.cls == 'autotile') { + core.drawMap(); + } else { + core.drawBlock(block); + core.addGlobalAnimate(block); + } + } + } +} + ////// 某个点图块是否被强制启用或禁用 maps.prototype.isMapBlockDisabled = function (floorId, x, y, flags) { if (flags == null) flags = (core.status.hero || {}).flags; @@ -855,6 +908,7 @@ maps.prototype.drawBlock = function (block, animate, ctx) { var blockInfo = this.getBlockInfo(block); if (blockInfo == null) return; if (blockInfo.cls != 'tileset') blockInfo.posX = animate % block.event.animate; + blockInfo.opacity = block.opacity; if (!block.name) this._drawBlockInfo(blockInfo, block.x, block.y, ctx); else @@ -867,11 +921,17 @@ maps.prototype._drawBlockInfo = function (blockInfo, x, y, ctx) { var py = 32 * y - 32 * core.bigmap.posY; if (ctx == null) ctx = 'event'; + var alpha = null; + if (blockInfo.opacity != null) alpha = core.setAlpha(ctx, blockInfo.opacity); core.clearMap(ctx, px, py, 32, 32); core.drawImage(ctx, image, posX * 32, posY * height + height - 32, 32, 32, px, py, 32, 32); + if (alpha != null) core.setAlpha(ctx, alpha); if (height > 32) { + alpha = null; + if (blockInfo.opacity != null) alpha = core.setAlpha('event2', block.opacity); core.clearMap('event2', px, py + 32 - height, 32, height - 32) core.drawImage('event2', image, posX * 32, posY * height, 32, height - 32, px, py + 32 - height, 32, height - 32); + if (alpha != null) core.setAlpha('event2', alpha); } } @@ -890,11 +950,8 @@ maps.prototype._drawBlockInfo_bgfg = function (blockInfo, name, x, y, ctx) { core.drawImage(ctx, core.material.groundCanvas.canvas, px, py); } var alpha = null; - if (name == 'fg' && this._drawBlockInfo_shouldBlurFg(x, y)) { - ctx = core.getContextByName(ctx); - alpha = ctx.globalAlpha; - core.setAlpha(ctx, 0.6); - } + if (blockInfo.opacity != null) alpha = core.setAlpha(ctx, blockInfo.opacity); + else if (name == 'fg' && this._drawBlockInfo_shouldBlurFg(x, y)) alpha = core.setAlpha(ctx, 0.6); core.drawImage(ctx, image, posX * 32, posY * height, 32, height, px, py + 32 - height, 32, height); if (alpha != null) core.setAlpha(ctx, alpha); } @@ -967,13 +1024,19 @@ maps.prototype._drawMap_drawBlockInfo = function (ctx, block, blockInfo, arr, on } if (blockInfo.cls == 'autotile') { // Autotile单独处理 + var alpha = null; + if (block.opacity != null) alpha = core.setAlpha(ctx, block.opacity); this._drawAutotile(ctx, arr, block, 32, 0, 0, 0, onMap); + if (alpha != null) core.setAlpha(ctx, alpha); if (onMap) this.addGlobalAnimate(block); return; } if (!onMap) { var height = blockInfo.height; + var alpha = null; + if (block.opacity != null) alpha = core.setAlpha(ctx, block.opacity); core.drawImage(ctx, blockInfo.image, 32 * blockInfo.posX, height * blockInfo.posY, 32, height, 32 * block.x, 32 * block.y + 32 - height, 32, height); + if (alpha != null) core.setAlpha(ctx, alpha); return; } this.drawBlock(block, null, ctx); @@ -1398,12 +1461,18 @@ maps.prototype._drawAutotileAnimate = function (block, animate) { var cv = block.name?core.canvas[block.name]:core.canvas.event; cv.clearRect(32 * x - 32 * core.bigmap.posX, 32 * y - 32 * core.bigmap.posY, 32, 32); if (block.name) { + var alpha = null; + if (block.opacity != null) alpha = core.setAlpha(cv, block.opacity); if (block.name == 'bg') core.drawImage('bg', core.material.groundCanvas.canvas, 32 * x - 32 * core.bigmap.posX, 32 * y - 32 * core.bigmap.posY); this._drawAutotile(cv, this._getBgFgMapArray(block.name), block, 32, 0, 0, animate, true); + if (alpha != null) core.setAlpha(cv, alpha); } else { + var alpha = null; + if (block.opacity != null) alpha = core.setAlpha(cv, block.opacity); this._drawAutotile(cv, this.getMapArray(), block, 32, 0, 0, animate, true); + if (alpha != null) core.setAlpha(cv, alpha); } } @@ -2444,23 +2513,26 @@ maps.prototype.animateBlock = function (loc, type, time, callback) { if (core.status.replay.speed == 24) time = 1; if (typeof loc[0] == 'number' && typeof loc[1] == 'number') loc = [loc]; + if (type != 'show' && type != 'hide' && type != 'remove' && typeof type != 'number') { + if (callback) callback(); + } // --- 检测所有是0的点 var list = this._animateBlock_getList(loc, type); if (list.length == 0) { if (callback) callback(); return; } - this._animateBlock_drawList(list, type != 'show' ? 1 : 0); + this._animateBlock_drawList(list, 0); time /= Math.max(core.status.replay.speed, 1) - this._animateBlock_doAnimate(loc, list, type, 10 / time, callback); + this._animateBlock_doAnimate(loc, list, type, time, callback); } -maps.prototype._animateBlock_doAnimate = function (loc, list, type, delta, callback) { - var opacity = type != 'show' ? 1 : 0; +maps.prototype._animateBlock_doAnimate = function (loc, list, type, time, callback) { + var step = 0, steps = Math.max(parseInt(time / 10), 1); var animate = setInterval(function () { - opacity += type != 'show' ? -delta : delta; - core.maps._animateBlock_drawList(list, opacity); - if (opacity >= 1 || opacity <= 0) { + step++; + core.maps._animateBlock_drawList(list, step / steps); + if (step == steps) { delete core.animateFrame.asyncId[animate]; clearInterval(animate); list.forEach(function (t) { @@ -2471,6 +2543,10 @@ maps.prototype._animateBlock_doAnimate = function (loc, list, type, delta, callb if (type == 'show') core.showBlock(t[0], t[1]); else if (type == 'hide') core.hideBlock(t[0], t[1]); else if (type == 'remove') core.removeBlock(t[0], t[1]); + else { + core.setMapBlockOpacity(null, t[0], t[1], type); + core.showBlock(t[0], t[1]); + } }); if (callback) callback(); } @@ -2483,6 +2559,8 @@ maps.prototype._animateBlock_getList = function (loc, type) { var list = []; loc.forEach(function (t) { var block = core.getBlock(t[0], t[1], null, true); + var fromOpacity = block.opacity; + if (fromOpacity == null) fromOpacity = 1.0; if (block == null) return; var blockInfo = core.maps.getBlockInfo(block); @@ -2490,30 +2568,41 @@ maps.prototype._animateBlock_getList = function (loc, type) { list.push({ 'x': t[0], 'y': t[1] }); return; } + if (typeof type == 'number' && block.disable) return; // 该点是否已经被启用/删除 if ((type == 'show' && !block.disable) || ((type == 'hide' || type == 'remove') && block.disable)) { list.push({ 'x': t[0], 'y': t[1] }); return; } - if (type == 'hide' || type == 'remove') { + var toOpacity = type; + if (type == 'show') { + toOpacity = fromOpacity; + fromOpacity = 0.0; + } + else if (type == 'hide' || type == 'remove') { + core.hideBlock(t[0], t[1]); // 暂时先隐藏 + toOpacity = 0.0; + } + else { core.hideBlock(t[0], t[1]); // 暂时先隐藏 } var canvases = core.maps._initDetachedBlock(blockInfo, t[0], t[1], block.event.displayDamage !== false); list.push({ - 'x': t[0], 'y': t[1], 'blockInfo': blockInfo, 'canvases': canvases + 'x': t[0], 'y': t[1], 'blockInfo': blockInfo, 'canvases': canvases, + 'fromOpacity': fromOpacity, 'toOpacity': toOpacity, }); }); return list; } -maps.prototype._animateBlock_drawList = function (list, opacity) { +maps.prototype._animateBlock_drawList = function (list, progress) { list.forEach(function (t) { if (t.blockInfo) - core.maps._moveDetachedBlock(t.blockInfo, t.x * 32, t.y * 32, opacity, t.canvases); + core.maps._moveDetachedBlock(t.blockInfo, t.x * 32, t.y * 32, t.fromOpacity + progress * (t.toOpacity - t.fromOpacity), t.canvases); }); } diff --git a/libs/ui.js b/libs/ui.js index b1a68833..fac02d0a 100644 --- a/libs/ui.js +++ b/libs/ui.js @@ -447,10 +447,13 @@ ui.prototype.loadCanvas = function (name) { if (ctx) ctx.restore(); } -////// 设置某个canvas的alpha值 ////// +////// 设置某个canvas的alpha值,并返回设置之前的alpha值 ////// ui.prototype.setAlpha = function (name, alpha) { var ctx = this.getContextByName(name); - if (ctx) ctx.globalAlpha = alpha; + if (!ctx) return null; + var previousAlpha = ctx.globalAlpha; + ctx.globalAlpha = alpha; + return previousAlpha; } ////// 设置某个canvas的透明度;尽量不要使用本函数,而是全部换成setAlpha实现 ////// diff --git a/runtime.d.ts b/runtime.d.ts index 941f0eec..fb10a0a7 100644 --- a/runtime.d.ts +++ b/runtime.d.ts @@ -2268,8 +2268,8 @@ declare class ui { /** 设置某个canvas的线宽度 */ setLineWidth(name: string | CanvasRenderingContext2D, lineWidth: number): void - /** 设置某个canvas的alpha值 */ - setAlpha(name: string | CanvasRenderingContext2D, alpha: number): void + /** 设置某个canvas的alpha值;返回设置之前画布的不透明度。 */ + setAlpha(name: string | CanvasRenderingContext2D, alpha: number): number /** 设置某个canvas的透明度;尽量不要使用本函数,而是全部换成setAlpha实现 */ setOpacity(name: string | CanvasRenderingContext2D, opacity: number): void