editor_util

This commit is contained in:
YouWei Zhao 2019-03-23 16:50:10 -04:00
parent f67a84faf4
commit 8eddb7f7b5
10 changed files with 182 additions and 179 deletions

View File

@ -33,7 +33,7 @@
``` js
editor.mapInit();//清空地图
editor.changeFloor('MT2')//切换地图
editor.guid()//产生一个可以作为id的长随机字符串
editor.util.guid()//产生一个可以作为id的长随机字符串
```
`editor.updateMap`中画未定义快的报错

View File

@ -41,31 +41,9 @@ editor.info
/////////// 数据相关 ///////////
editor.prototype.init = function (callback) {
var afterCoreReset = function () {
editor.idsInit(core.maps, core.icons.icons); // 初始化图片素材信息
editor.drawInitData(core.icons.icons); // 初始化绘图
editor.fetchMapFromCore();
editor.updateMap();
editor.buildMark();
editor.drawEventBlock();
editor.pos = {x: 0, y: 0};
editor.mode.loc();
editor.info = editor.ids[editor.indexs[201]];
editor.mode.enemyitem();
editor.mode.floor();
editor.mode.tower();
editor.mode.functions();
editor.mode.commonevent();
editor.mode.showMode('tower');
editor_multi = editor_multi();
editor_blockly = editor_blockly();
if (Boolean(callback)) callback();
}
editor_util_wrapper(editor);
editor_table_wrapper(editor);
var afterMainInit = function () {
core.floors = JSON.parse(JSON.stringify(core.floors, function (k, v) {
@ -90,7 +68,6 @@ editor.prototype.init = function (callback) {
editor.file = editor_file;
editor_mode = editor_mode(editor);
editor.mode = editor_mode;
editor_table_wrapper(editor);
core.resetGame(core.firstData.hero, null, core.firstData.floorId, core.initStatus.maps);
core.changeFloor(core.status.floorId, null, core.firstData.hero.loc, null, function () {
afterCoreReset();
@ -98,6 +75,33 @@ editor.prototype.init = function (callback) {
core.events.setInitData(null);
});
}
var afterCoreReset = function () {
editor.idsInit(core.maps, core.icons.icons); // 初始化图片素材信息
editor.drawInitData(core.icons.icons); // 初始化绘图
editor.fetchMapFromCore();
editor.updateMap();
editor.buildMark();
editor.drawEventBlock();
editor.pos = {x: 0, y: 0};
editor.mode.loc();
editor.info = editor.ids[editor.indexs[201]];
editor.mode.enemyitem();
editor.mode.floor();
editor.mode.tower();
editor.mode.functions();
editor.mode.commonevent();
editor.mode.showMode('tower');
editor_multi = editor_multi();
editor_blockly = editor_blockly();
if (Boolean(callback)) callback();
}
afterMainInit();
}
@ -355,21 +359,6 @@ editor.prototype.moveViewport=function(x,y){
editor.drawPosSelection();
}
/////////// 通用 ///////////
editor.prototype.guid = function () {
return 'id_' + 'xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
editor.prototype.HTMLescape = function (str_) {
return String(str_).split('').map(function (v) {
return '&#' + v.charCodeAt(0) + ';'
}).join('');
}
/////////// 界面交互相关 ///////////
editor.prototype.drawInitData = function (icons) {

View File

@ -94,7 +94,7 @@ editor_file = function (editor, callback) {
}
// format 更改实现方式以支持undefined删除
var tempJsonObj=Object.assign({},editor.currentFloorData);
var tempMap=[['map',editor.guid()],['bgmap',editor.guid()],['fgmap',editor.guid()]];
var tempMap=[['map',editor.util.guid()],['bgmap',editor.util.guid()],['fgmap',editor.util.guid()]];
tempMap.forEach(function(v){
v[2]=tempJsonObj[v[0]];
tempJsonObj[v[0]]=v[1];
@ -764,7 +764,7 @@ editor_file = function (editor, callback) {
var fmap = {};
var fjson = JSON.stringify(functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a, function (k, v) {
if (v instanceof Function) {
var id_ = editor.guid();
var id_ = editor.util.guid();
fmap[id_] = v.toString();
return id_;
} else return v
@ -853,11 +853,11 @@ editor_file = function (editor, callback) {
var plmap = {};
var pljson = JSON.stringify(plugins_bb40132b_638b_4a9f_b028_d3fe47acc8d1, function (k, v) {
if (v instanceof Function) {
var id_ = editor.guid();
var id_ = editor.util.guid();
plmap[id_] = v.toString();
return id_;
} else if(v===null){
var id_ = editor.guid();
var id_ = editor.util.guid();
plmap[id_] = 'null';
return id_;
} return v
@ -980,7 +980,7 @@ editor_file = function (editor, callback) {
var emap = {};
var estr = JSON.stringify(maps_90f36752_8815_4be8_b32b_d7fad1d0542e, function (k, v) {
if (v.id != null) {
var id_ = editor.guid();
var id_ = editor.util.guid();
emap[id_] = JSON.stringify(v);
return id_;
} else return v
@ -1014,7 +1014,7 @@ editor_file = function (editor, callback) {
var emap = {};
var estr = JSON.stringify(enemys_fcae963b_31c9_42b4_b48c_bb48d09f3f80, function (k, v) {
if (v.hp != null) {
var id_ = editor.guid();
var id_ = editor.util.guid();
emap[id_] = JSON.stringify(v);
return id_;
} else return v

View File

@ -474,21 +474,8 @@ editor_mode = function (editor) {
}
selectAppend.onchange();
var getPixel=function(imgData, x, y) {
var offset = (x + y * imgData.width) * 4;
var r = imgData.data[offset+0];
var g = imgData.data[offset+1];
var b = imgData.data[offset+2];
var a = imgData.data[offset+3];
return [r,g,b,a];
}
var setPixel=function(imgData, x, y, rgba) {
var offset = (x + y * imgData.width) * 4;
imgData.data[offset+0]=rgba[0];
imgData.data[offset+1]=rgba[1];
imgData.data[offset+2]=rgba[2];
imgData.data[offset+3]=rgba[3];
}
var getPixel=editor.util.getPixel
var setPixel=editor.util.setPixel
var autoAdjust = function (image, callback) {
var changed = false;
@ -647,113 +634,9 @@ editor_mode = function (editor) {
var nimgData=new ImageData(imgData.width,imgData.height);
// ImageData .data 形如一维数组,依次排着每个点的 R(0~255) G(0~255) B(0~255) A(0~255)
var convert=function(rgba,delta){
var round=Math.round;
// rgbToHsl hue2rgb hslToRgb from https://github.com/carloscabo/colz.git
//--------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2014 Carlos Cabo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//--------------------------------------------
// https://github.com/carloscabo/colz/blob/master/public/js/colz.class.js
var rgbToHsl = function (rgba) {
var arg, r, g, b, h, s, l, d, max, min;
arg = rgba;
if (typeof arg[0] === 'number') {
r = arg[0];
g = arg[1];
b = arg[2];
} else {
r = arg[0][0];
g = arg[0][1];
b = arg[0][2];
}
r /= 255;
g /= 255;
b /= 255;
max = Math.max(r, g, b);
min = Math.min(r, g, b);
l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
//CARLOS
h = round(h * 360);
s = round(s * 100);
l = round(l * 100);
return [h, s, l];
}
//
var hue2rgb = function (p, q, t) {
if (t < 0) { t += 1; }
if (t > 1) { t -= 1; }
if (t < 1 / 6) { return p + (q - p) * 6 * t; }
if (t < 1 / 2) { return q; }
if (t < 2 / 3) { return p + (q - p) * (2 / 3 - t) * 6; }
return p;
}
var hslToRgb = function (hsl) {
var arg, r, g, b, h, s, l, q, p;
arg = hsl;
if (typeof arg[0] === 'number') {
h = arg[0] / 360;
s = arg[1] / 100;
l = arg[2] / 100;
} else {
h = arg[0][0] / 360;
s = arg[0][1] / 100;
l = arg[0][2] / 100;
}
if (s === 0) {
r = g = b = l; // achromatic
} else {
q = l < 0.5 ? l * (1 + s) : l + s - l * s;
p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return [round(r * 255), round(g * 255), round(b * 255)];
}
var rgbToHsl = editor.util.rgbToHsl
var hue2rgb = editor.util.hue2rgb
var hslToRgb = editor.util.hslToRgb
//
var hsl=rgbToHsl(rgba)
hsl[0]=(hsl[0]+delta)%360

View File

@ -103,7 +103,7 @@ editor_multi = function () {
var tmap = {};
var tstr = JSON.stringify(tobj, function (k, v) {
if (typeof(v) === typeof('') && v.slice(0, 8) === 'function') {
var id_ = editor.guid();
var id_ = editor.util.guid();
tmap[id_] = v.toString();
return id_;
} else return v
@ -146,7 +146,7 @@ editor_multi = function () {
var tmap = {};
var tstr = JSON.stringify(tobj, function (k, v) {
if (v instanceof Function) {
var id_ = editor.guid();
var id_ = editor.util.guid();
tmap[id_] = v.toString();
return id_;
} else return v

View File

@ -184,7 +184,7 @@ editor_table_wrapper = function (editor) {
* @param {Object} cobj
*/
editor_table.prototype.objToTr = function (obj, commentObj, field, cfield, vobj, cobj) {
var guid = editor.guid();
var guid = editor.util.guid();
var thiseval = vobj;
var comment = String(cobj._data);
@ -195,14 +195,14 @@ editor_table_wrapper = function (editor) {
shortField = (shortField.length < charlength ? shortField : shortField.slice(0, charlength) + '...');
// 完整的内容转义后供悬停查看
var commentHTMLescape = editor.HTMLescape(comment);
var commentHTMLescape = editor.util.HTMLescape(comment);
// 把长度超过 charlength 的字符改成 固定长度+...的形式
var shortCommentHTMLescape = (comment.length < charlength ? commentHTMLescape : editor.HTMLescape(comment.slice(0, charlength)) + '...');
var shortCommentHTMLescape = (comment.length < charlength ? commentHTMLescape : editor.util.HTMLescape(comment.slice(0, charlength)) + '...');
var cobjstr = Object.assign({}, cobj);
delete cobjstr._data;
// 把cobj塞到第二个td的[cobj]中, 方便绑定事件时取
cobjstr = editor.HTMLescape(JSON.stringify(cobjstr));
cobjstr = editor.util.HTMLescape(JSON.stringify(cobjstr));
var tdstr = editor.table.objToTd(obj, commentObj, field, cfield, vobj, cobj)
var outstr = editor.table.tr(guid, field, shortField, commentHTMLescape, cobjstr, shortCommentHTMLescape, tdstr)

View File

@ -10,13 +10,141 @@ editor_util_wrapper = function (editor) {
return v.toString(16);
});
}
editor_util.prototype.HTMLescape = function (str_) {
return String(str_).split('').map(function (v) {
return '&#' + v.charCodeAt(0) + ';'
}).join('');
}
editor_util.prototype.getPixel = function (imgData, x, y) {
var offset = (x + y * imgData.width) * 4;
var r = imgData.data[offset + 0];
var g = imgData.data[offset + 1];
var b = imgData.data[offset + 2];
var a = imgData.data[offset + 3];
return [r, g, b, a];
}
editor_util.prototype.setPixel = function (imgData, x, y, rgba) {
var offset = (x + y * imgData.width) * 4;
imgData.data[offset + 0] = rgba[0];
imgData.data[offset + 1] = rgba[1];
imgData.data[offset + 2] = rgba[2];
imgData.data[offset + 3] = rgba[3];
}
// rgbToHsl hue2rgb hslToRgb from https://github.com/carloscabo/colz.git
//--------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2014 Carlos Cabo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//--------------------------------------------
// https://github.com/carloscabo/colz/blob/master/public/js/colz.class.js
var round=Math.round;
var rgbToHsl = function (rgba) {
var arg, r, g, b, h, s, l, d, max, min;
arg = rgba;
if (typeof arg[0] === 'number') {
r = arg[0];
g = arg[1];
b = arg[2];
} else {
r = arg[0][0];
g = arg[0][1];
b = arg[0][2];
}
r /= 255;
g /= 255;
b /= 255;
max = Math.max(r, g, b);
min = Math.min(r, g, b);
l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
//CARLOS
h = round(h * 360);
s = round(s * 100);
l = round(l * 100);
return [h, s, l];
}
//
var hue2rgb = function (p, q, t) {
if (t < 0) { t += 1; }
if (t > 1) { t -= 1; }
if (t < 1 / 6) { return p + (q - p) * 6 * t; }
if (t < 1 / 2) { return q; }
if (t < 2 / 3) { return p + (q - p) * (2 / 3 - t) * 6; }
return p;
}
var hslToRgb = function (hsl) {
var arg, r, g, b, h, s, l, q, p;
arg = hsl;
if (typeof arg[0] === 'number') {
h = arg[0] / 360;
s = arg[1] / 100;
l = arg[2] / 100;
} else {
h = arg[0][0] / 360;
s = arg[0][1] / 100;
l = arg[0][2] / 100;
}
if (s === 0) {
r = g = b = l; // achromatic
} else {
q = l < 0.5 ? l * (1 + s) : l + s - l * s;
p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return [round(r * 255), round(g * 255), round(b * 255)];
}
editor_util.prototype.rgbToHsl=rgbToHsl
editor_util.prototype.hue2rgb=hue2rgb
editor_util.prototype.hslToRgb=hslToRgb
editor.constructor.prototype.util = new editor_util();
}
//editor_util_wrapper(editor);

View File

@ -33,6 +33,7 @@ editor: {
util: 组件
}
game: 来自游戏的数据
config: 编辑器配置
mode: 当前的模式(左侧的选择)
map: 当前编辑层的地图
...

View File

@ -525,9 +525,10 @@ if (location.protocol.indexOf("http")!=0) {
<!-- <script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script> -->
<script src='_server/vendor/polyfill.min.js'></script>
<script src='_server/fs.js'></script>
<script src='_server/editor_util.js'></script>
<script src='_server/editor_file.js'></script>
<script src='_server/editor_mode.js'></script>
<script src='_server/editor_table.js'></script>
<script src='_server/editor_mode.js'></script>
<script src='_server/vm.js'></script>
<script src='libs/thirdparty/lz-string.min.js'></script>
<script src='libs/thirdparty/localforage.min.js'></script>

View File

@ -508,9 +508,10 @@ if (location.protocol.indexOf("http")!=0) {
<!-- <script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script> -->
<script src='_server/vendor/polyfill.min.js'></script>
<script src='_server/fs.js'></script>
<script src='_server/editor_util.js'></script>
<script src='_server/editor_file.js'></script>
<script src='_server/editor_mode.js'></script>
<script src='_server/editor_table.js'></script>
<script src='_server/editor_mode.js'></script>
<script src='_server/vm.js'></script>
<script src='libs/thirdparty/lz-string.min.js'></script>
<script src='libs/thirdparty/localforage.min.js'></script>