Commit eb2f29c0 authored by rockyl's avatar rockyl

过程、脚本、自定义模块代码抽离功能

parent e9c9c132
......@@ -128,7 +128,6 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
for (var i = 0, len = eventTypes[type].length; i < len; i++) {
var ee = eventTypes[type][i];
if (ee.fn === listener && ee.context === context) {
console.log("已添加过该事件");
return;
}
}
......@@ -1615,7 +1614,6 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
this._host.addEventListener(Event.ENTER_FRAME, this.onEnterFrame, this);
};
AdjustProxy.prototype.onRemovedFromStage = function (e) {
this._host.parent.removeEventListener(Event.RESIZE, this.onResize);
this._host.removeEventListener(Event.RESIZE, this.onResize, this);
this._host.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
};
......@@ -1723,7 +1721,7 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
node[targetKey] = value;
}
function getDataByPath(scope, path, throwException) {
var func = new Function('scope', "return scope." + path);
var func = new Function('scope', "return scope" + (path === undefined ? '' : ('.' + path)));
try {
return func(scope);
}
......@@ -2070,38 +2068,38 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
enumerable: true,
configurable: true
});
ScriptBase.prototype.mounted = function () {
};
ScriptBase.prototype.destroy = function () {
};
ScriptBase.prototype.update = function (t) {
};
ScriptBase.prototype.awake = function () {
};
ScriptBase.prototype.sleep = function () {
};
return ScriptBase;
}());
function registerScriptDef(id, def) {
scriptDefs[id] = def;
Object.defineProperty(def, 'disabled', {
Object.defineProperty(ScriptBase.prototype, "disabled", {
get: function () {
return !!this._disabled;
return this._disabled;
},
set: function (v) {
if (this._disabled !== v) {
this._disabled = v;
if (this._disabled) {
this.awake();
this.sleep();
}
else {
this.sleep();
this.awake();
}
}
},
enumerable: true,
configurable: true
});
ScriptBase.prototype.mounted = function () {
};
ScriptBase.prototype.destroy = function () {
};
ScriptBase.prototype.update = function (t) {
};
ScriptBase.prototype.awake = function () {
};
ScriptBase.prototype.sleep = function () {
};
return ScriptBase;
}());
function registerScriptDef(id, def) {
scriptDefs[id] = def;
}
function registerScripts(scripts) {
for (var id in scripts) {
......@@ -2158,8 +2156,10 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
enumerable: true,
configurable: true
});
ScriptsProxy.prototype.get = function (name) {
return this._scripts.filter(function (script) { return script.constructor['name'] === name; });
ScriptsProxy.prototype.get = function (id) {
return this._scripts.filter(function (script) {
return script.constructor['id'] === id;
});
};
ScriptsProxy.prototype.onAddedToStage = function () {
for (var _i = 0, _a = this._scripts; _i < _a.length; _i++) {
......@@ -2219,8 +2219,9 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
DataCenter.prototype.getGroup = function (name) {
return this.store[name];
};
DataCenter.prototype.getDataByPath = function (path, throwException) {
return getDataByPath(this.store, path, throwException);
DataCenter.prototype.getDataByPath = function (path, groupName, throwException) {
var scope = groupName === undefined ? this.store : this.getGroup(groupName) || this.store;
return getDataByPath(scope, path, throwException);
};
DataCenter.prototype.getDataByName = function (name, throwException) {
var watcher = this.getWatcher(name);
......@@ -2236,10 +2237,13 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
var name = args[0];
var watcher = this.getWatcher(name);
try {
var data = this.getDataByPath(watcher.path, true);
var data = this.getDataByPath(watcher.path, undefined, true);
if (args[1] !== undefined) {
data = data[args[1]];
}
if (data === undefined) {
data = '';
}
result = result.replace(new RegExp('\\$\\{' + escape.replace(/\|/g, '\\|') + '\\}', 'g'), data);
}
catch (e) {
......@@ -2247,13 +2251,27 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
}
return result;
};
DataCenter.prototype.mutate = function (name, data, path, dispatch) {
DataCenter.prototype.increase = function (groupName, step, path, dispatch) {
if (dispatch === void 0) { dispatch = true; }
if (step < 0 || step > 0) {
var data = this.getDataByPath(path, groupName);
if (data === undefined) {
data = 0;
}
else {
data = parseInt(data);
}
data += step;
this.mutate(groupName, data, path, dispatch);
}
};
DataCenter.prototype.mutate = function (groupName, data, path, dispatch) {
if (dispatch === void 0) { dispatch = true; }
var group = this.getGroup(name);
var group = this.getGroup(groupName);
if (!group) {
this.registerGroup(name);
this.registerGroup(groupName);
}
if (data) {
if (data !== undefined) {
if (path) {
var func = new Function('scope', 'data', "scope." + path + "=data");
try {
......@@ -2264,13 +2282,13 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
}
}
else {
this.registerGroup(name, data);
this.registerGroup(groupName, data);
}
}
if (dispatch) {
for (var _i = 0, _a = this.watchers; _i < _a.length; _i++) {
var watcher = _a[_i];
if (watcher.path.indexOf(name) === 0) {
if (watcher.path.indexOf(groupName) === 0) {
this.dispatchEvent(watcher.name, {
name: watcher.name,
path: watcher.path,
......@@ -2280,7 +2298,7 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
}
}
globalEvent.dispatchEvent(DATA_CENTER_EVENT, {
name: name,
name: groupName,
path: path,
data: data,
});
......@@ -2427,6 +2445,7 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
console.log();
}
globalContext = this._vm.globalContext;
globalContext.dataCenter = globalContext.gameStage.dataCenter;
return [4, func(payload, this._config.props, this._target, globalContext, this._vm)];
case 1:
result = _a.sent();
......@@ -10143,6 +10162,9 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
url = urlJoin(url, queryStr);
}
_req_1.open(method, url, true);
if (!isGet) {
_req_1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
_req_1.responseType = type;
if (isGet) {
_req_1.send();
......@@ -10280,7 +10302,7 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
_this._textAlign = exports.TEXT_ALIGN.LEFT;
_this._verticalAlign = exports.VERTICAL_ALIGN.UP;
_this._lineSpacing = 14;
_this._lineType = exports.TEXT_lINETYPE.SINGLE;
_this._lineType = exports.TEXT_lINETYPE.MULTI;
_this._text = "";
_this._font = "Arial";
_this._size = 12;
......@@ -10418,6 +10440,7 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
TextField.prototype._setText = function (value) {
this._text = value;
this.dirty = true;
this.dispatchEvent(Event.RESIZE);
};
Object.defineProperty(TextField.prototype, "font", {
get: function () {
......@@ -10606,24 +10629,34 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
}
}
else {
var measureCache = {};
if (!textWidth) {
for (var i = 0, l = hardLines.length; i < l; i++) {
var str = hardLines[i];
if (!str)
continue;
textWidth = Math.max(s._getMeasuredWidth(str), textWidth);
var lineWidth = 0;
for (var _i = 0, str_1 = str; _i < str_1.length; _i++) {
var char = str_1[_i];
var charWidth = measureCache[char];
if (charWidth === undefined) {
charWidth = measureCache[char] = s._getMeasuredWidth(char);
}
lineWidth += charWidth;
}
textWidth = Math.max(lineWidth, textWidth);
}
}
for (var i = 0, l = hardLines.length; i < l; i++) {
var str = hardLines[i];
if (!str)
continue;
var w = s._getMeasuredWidth(str[0]);
var w = measureCache[str[0]];
var lineStr = str[0];
var wordW = 0;
var strLen = str.length;
for (var j = 1; j < strLen; j++) {
wordW = ctx.measureText(str[j]).width;
wordW = measureCache[str[j]];
w += wordW;
if (w > textWidth) {
realLines[realLines.length] = lineStr;
......@@ -12285,7 +12318,10 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
if (url.indexOf(assetScheme) === 0) {
var uuid = url.replace(assetScheme, '');
var assetConfig = getAssetByUUID(uuid);
url = assetConfig.url;
if (assetConfig) {
url = assetConfig.url;
this.texture = Texture.fromImage(url);
}
}
}
else {
......@@ -12293,12 +12329,13 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
if (assetConfig) {
url = assetConfig.url;
}
this.texture = Texture.fromImage(url);
}
this.texture = Texture.fromImage(url);
};
Image.prototype._setSourceDirect = function (value) {
this._source = value;
this.updateSource();
this.dispatchEvent(Event.RESIZE);
};
Image.prototype._setSource = function (value) {
if (value !== this._source) {
......@@ -12321,7 +12358,6 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
else {
this._setSourceDirect(value);
}
this.dispatchEvent(Event.RESIZE);
}
};
Image.prototype.unregisterEvents = function () {
......@@ -12366,9 +12402,29 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
else {
_super.prototype._setText.call(this, value);
}
this.dispatchEvent(Event.RESIZE);
}
};
Object.defineProperty(Label.prototype, "htmlText", {
get: function () {
return this._htmlText;
},
set: function (v) {
if (this._htmlText != v) {
this._htmlText = v;
var el = this._el;
if (!el) {
el = this._el = document.createElement('div');
}
el.innerHTML = v;
document.body.append(el);
var pureText = el.innerText;
document.body.removeChild(el);
this.text = pureText;
}
},
enumerable: true,
configurable: true
});
Label.prototype.unregisterEvents = function () {
for (var _i = 0, _a = this._registeredEvents; _i < _a.length; _i++) {
var item = _a[_i];
......@@ -12478,6 +12534,97 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
}
}
var Toast = (function (_super) {
tslib_1.__extends(Toast, _super);
function Toast(gameStage) {
var _this = _super.call(this) || this;
_this._gameStage = gameStage;
_this.horizonCenter = 0;
_this.verticalCenter = 0;
return _this;
}
Toast.prototype.show = function (props) {
if (props === void 0) { props = {}; }
if (!props.text) {
return;
}
var contentView = this.getContent(props.viewName);
var lab = contentView.getChildByName('lab');
if (!lab) {
console.warn('[lab] child no exists on toast view');
return;
}
var text = props.text, _a = props.padding, padding = _a === void 0 ? 10 : _a, _b = props.duration, duration = _b === void 0 ? 1000 : _b, _c = props.showDuration, showDuration = _c === void 0 ? 300 : _c, _d = props.hideDuration, hideDuration = _d === void 0 ? 200 : _d;
lab.text = text;
lab.x = lab.y = padding;
this.width = contentView.width = lab.width + padding * 2;
this.height = contentView.height = lab.height + padding * 2;
if (!contentView.visible) {
contentView.visible = true;
contentView.alpha = 0;
}
Tween.get(contentView, null, null, true)
.to({ alpha: 1 }, showDuration)
.wait(duration)
.to({ alpha: 0 }, hideDuration)
.call(this.removeContentView, this);
};
Toast.prototype.hide = function (animation, hideDuration) {
if (animation === void 0) { animation = true; }
if (hideDuration === void 0) { hideDuration = 200; }
if (this._content) {
var contentView = this._content;
if (animation) {
Tween.get(contentView, null, null, true)
.to({ alpha: 0 }, hideDuration)
.call(this.removeContentView, this);
}
else {
Tween.removeTweens(contentView);
this.removeContentView();
}
}
};
Toast.prototype.removeContentView = function () {
this._content.visible = false;
this.removeChild(this._content);
this._content = null;
};
Toast.prototype.getContent = function (viewName) {
var view;
if (this.children.length == 0) {
if (viewName) {
view = this._gameStage.instantiateView(viewName);
}
if (!view) {
if (!this._contentSample) {
this._contentSample = new Container();
var bg = new Rect();
bg.borderRadius = 10;
bg.percentWidth = 100;
bg.percentHeight = 100;
bg.fillColor = 'black';
bg.alpha = 0.7;
this._contentSample.addChild(bg);
var lab = new Label();
lab.name = 'lab';
lab.fillColor = 'white';
lab.size = 30;
this._contentSample.addChild(lab);
}
view = this._contentSample;
}
this._content = view;
this.addChild(view);
}
else {
view = this.getChildAt(0);
}
return view;
};
return Toast;
}(Container));
var GameStage = (function (_super) {
tslib_1.__extends(GameStage, _super);
function GameStage(stage) {
......@@ -12493,6 +12640,7 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
_this.addChild(_this._sceneContainer = new StackContainer());
_this.addChild(blackLayer);
_this.addChild(_this._popupContainer = new StackContainer(false));
_this.addChild(_this._toast = new Toast(_this));
blackLayer['percentWidth'] = 100;
blackLayer['percentHeight'] = 100;
blackLayer.visible = false;
......@@ -12517,6 +12665,13 @@ var tslib = {__extends: __extends,__assign: __assign,__rest: __rest,__decorate:
enumerable: true,
configurable: true
});
Object.defineProperty(GameStage.prototype, "toast", {
get: function () {
return this._toast;
},
enumerable: true,
configurable: true
});
Object.defineProperty(GameStage.prototype, "dataCenter", {
get: function () {
return this._dataCenter;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -25,7 +25,7 @@
},
"scripts": {
"build-webpack": "webpack",
"rollup": "rollup -c -o dist/engine.js --environment BUILD:production",
"rollup": "rollup -c -o dist/engine.js --environment BUILD:production1",
"rollup:debug": "rollup -c -m ",
"rename": "node scripts/rename-hash.js dist/engine.js",
"build": "rm -rf dist&&yarn rollup && yarn rename",
......
......@@ -98,7 +98,7 @@ export class EventDispatcher extends HashObject {
for (var i = 0, len = eventTypes[type].length; i < len; i++) {
let ee: EE = eventTypes[type][i]
if (ee.fn === listener && ee.context === context) {
console.log("已添加过该事件")
//console.log("已添加过该事件")
return
}
}
......
......@@ -31,6 +31,9 @@ export function httpRequest(callback: Function, url: string, method: string = 'g
url = urlJoin(url, queryStr);
}
_req.open(method, url, true);
if(!isGet){
_req.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
}
_req.responseType = type;
if (isGet) {
_req.send();
......
......@@ -229,7 +229,7 @@ export class TextField extends Sprite {
return this._lineType;
}
private _lineType: TEXT_lINETYPE = TEXT_lINETYPE.SINGLE;
private _lineType: TEXT_lINETYPE = TEXT_lINETYPE.MULTI;
/**
* 文本内容
......@@ -252,6 +252,8 @@ export class TextField extends Sprite {
protected _setText(value) {
this._text = value;
this.dirty = true;
this.dispatchEvent(Event.RESIZE);
}
protected _text: string = "";
......@@ -558,22 +560,31 @@ export class TextField extends Sprite {
}
} else {
//textWidth取每行最大值,如果没设置过textWidth
let measureCache = {};
if (!textWidth) {
for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i];
if (!str) continue;
textWidth = Math.max(s._getMeasuredWidth(str), textWidth);
let lineWidth = 0;
for (let char of str) {
let charWidth = measureCache[char];
if (charWidth === undefined) {
charWidth = measureCache[char] = s._getMeasuredWidth(char);
}
lineWidth += charWidth;
}
textWidth = Math.max(lineWidth, textWidth);
}
}
for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i];
if (!str) continue;
let w = s._getMeasuredWidth(str[0]);
let w = measureCache[str[0]];
let lineStr = str[0];
let wordW = 0;
let strLen = str.length;
for (let j = 1; j < strLen; j++) {
wordW = ctx.measureText(str[j]).width;
wordW = measureCache[str[j]];
w += wordW;
if (w > textWidth) {
realLines[realLines.length] = lineStr;
......@@ -633,7 +644,7 @@ export class TextField extends Sprite {
this.anchorTexture = {x: (padding + 0.5) / can.width, y: padding / can.height}
// document.body.appendChild(can)
//document.body.appendChild(can)
// s._bounds.height = maxH;
// s._bounds.width = maxW;
//x,y都是0
......
......@@ -96,15 +96,16 @@ export class Process {
this.updateProps(this._config.props = {}, payload, this._originProps, this._meta.props);
if (metaConfig.script) {
let func;
if(metaConfig.script.indexOf(linkScheme) === 0){
if (metaConfig.script.indexOf(linkScheme) === 0) {
func = this._vm.getScript(metaConfig.script.replace(linkScheme, ''));
}else{
} else {
func = new Function('args', 'props', 'target', 'global', 'vm', warpAsyncScript(metaConfig.script));
}
if(!func){
if (!func) {
console.log();
}
let globalContext = this._vm.globalContext;
globalContext.dataCenter = globalContext.gameStage.dataCenter;
result = await func(payload, this._config.props, this._target, globalContext, this._vm);
}
} else {
......@@ -193,7 +194,7 @@ export class Process {
let linkedValue = this.resolveLinkedProp(value, key);
if (linkedValue !== undefined) {
props[key] = linkedValue;
}else{
} else {
props[key] = undefined;
}
break;
......@@ -219,7 +220,7 @@ export class Process {
if (uuid) {
props[key] = findNodeByUUID(this._vm.globalContext.gameStage, uuid);
}
}else if(originProps[key] !== undefined){
} else if (originProps[key] !== undefined) {
props[key] = originProps[key];
}
......
......@@ -68,7 +68,7 @@ class AdjustProxy {
}
onRemovedFromStage(e) {
this._host.parent.removeEventListener(Event.RESIZE, this.onResize);
//this._host.parent.removeEventListener(Event.RESIZE, this.onResize);
this._host.removeEventListener(Event.RESIZE, this.onResize, this);
this._host.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
}
......
......@@ -28,12 +28,27 @@ export function applyScript(ctor: Function) {
*/
export class ScriptBase {
private _host: Container;
disabled: boolean;
private _disabled: boolean;
get host() {
return this._host
}
get disabled(){
return this._disabled;
}
set disabled(v){
if (this._disabled !== v) {
this._disabled = v;
if (this._disabled) {
this.sleep();
} else {
this.awake();
}
}
}
mounted() {
}
......@@ -63,7 +78,7 @@ export class ScriptBase {
export function registerScriptDef(id, def) {
scriptDefs[id] = def;
Object.defineProperty(def, 'disabled', {
/*Object.defineProperty(def, 'disabled', {
get: function () {
return !!this._disabled;
},
......@@ -79,8 +94,7 @@ export function registerScriptDef(id, def) {
}
},
enumerable: true,
configurable: true
});
});*/
}
export function registerScripts(scripts) {
......@@ -158,10 +172,12 @@ class ScriptsProxy {
/**
* 根据名字获取脚本
* @param name
* @param id
*/
get(name): ScriptBase[] {
return this._scripts.filter(script => script.constructor['name'] === name);
get(id): ScriptBase[] {
return this._scripts.filter(script => {
return script.constructor['id'] === id;
});
}
/**
......
......@@ -47,6 +47,7 @@ export class GameStage extends Container {
this.addChild(this._sceneContainer = new StackContainer());
this.addChild(blackLayer);
this.addChild(this._popupContainer = new StackContainer(false));
this.addChild(this._toast = new Toast(this));
blackLayer['percentWidth'] = 100;
blackLayer['percentHeight'] = 100;
......@@ -74,6 +75,13 @@ export class GameStage extends Container {
return this._popupContainer;
}
/**
* Toast提示
*/
get toast(): Toast {
return this._toast;
}
/**
* 数据中心
*/
......
......@@ -5,6 +5,7 @@
import {Container} from "../../2d/display";
import {GameStage} from "./GameStage";
import {Label, Rect} from "./nodes";
import {Tween} from "../../2d/tween";
export class Toast extends Container {
private _contentSample: Container;
......@@ -15,50 +16,91 @@ export class Toast extends Container {
constructor(gameStage: GameStage) {
super();
this._gameStage = gameStage;
this.horizonCenter = 0;
this.verticalCenter = 0;
}
show(props) {
show(props: any = {}) {
if (!props.text) {
return;
}
const {text, duration, viewName} = props;
let contentView = this.getContent(viewName);
let contentView = this.getContent(props.viewName);
let lab = contentView.getChildByName('lab');
if (!lab) {
console.warn('[lab] child no exists on toast view');
return;
}
const {text, padding = 10, duration = 1000, showDuration = 300, hideDuration = 200} = props;
lab.text = text;
contentView.width = lab.width;
contentView.height = lab.height;
lab.x = lab.y = padding;
this.width = contentView.width = lab.width + padding * 2;
this.height = contentView.height = lab.height + padding * 2;
this.addChild(contentView);
if (!contentView.visible) {
contentView.visible = true;
contentView.alpha = 0;
}
Tween.get(contentView, null, null, true)
.to({alpha: 1}, showDuration)
.wait(duration)
.to({alpha: 0}, hideDuration)
.call(this.removeContentView, this)
}
hide() {
hide(animation = true, hideDuration = 200) {
if (this._content) {
const contentView = this._content;
if(animation){
Tween.get(contentView, null, null, true)
.to({alpha: 0}, hideDuration)
.call(this.removeContentView, this)
}else{
Tween.removeTweens(contentView);
this.removeContentView();
}
}
}
private removeContentView() {
this._content.visible = false;
this.removeChild(this._content);
this._content = null;
}
private getContent(viewName) {
let view;
if (viewName) {
view = this._gameStage.instantiateView(viewName);
}
if (!view) {
if (!this._content) {
this._content = new Container();
let bg = new Rect();
bg.percentWidth = 100;
bg.percentHeight = 100;
bg.alpha = 0.7;
view.addChild(bg);
let lab = new Label();
lab.name = 'lab';
view.addChild(bg);
if (this.children.length == 0) {
if (viewName) {
view = this._gameStage.instantiateView(viewName);
}
view = this._content;
if (!view) {
if (!this._contentSample) {
this._contentSample = new Container();
let bg = new Rect();
bg.borderRadius = 10;
bg.percentWidth = 100;
bg.percentHeight = 100;
bg.fillColor = 'black';
bg.alpha = 0.7;
this._contentSample.addChild(bg);
let lab = new Label();
lab.name = 'lab';
lab.fillColor = 'white';
lab.size = 30;
this._contentSample.addChild(lab);
}
view = this._contentSample;
}
this._content = view;
this.addChild(view);
} else {
view = this.getChildAt(0);
}
return view;
}
}
......@@ -46,10 +46,12 @@ export class DataCenter extends EventDispatcher {
/**
* 根据路径获取数据
* @param path
* @param groupName
* @param throwException
*/
getDataByPath(path, throwException?) {
return getDataByPath(this.store, path, throwException);
getDataByPath(path, groupName?, throwException?) {
let scope = groupName === undefined ? this.store : this.getGroup(groupName) || this.store;
return getDataByPath(scope, path, throwException);
}
/**
......@@ -59,7 +61,7 @@ export class DataCenter extends EventDispatcher {
*/
getDataByName(name, throwException?) {
let watcher = this.getWatcher(name);
if(watcher){
if (watcher) {
return getDataByPath(this.store, watcher.path, throwException);
}
}
......@@ -76,10 +78,13 @@ export class DataCenter extends EventDispatcher {
let name = args[0];
let watcher = this.getWatcher(name);
try {
let data = this.getDataByPath(watcher.path, true);
if(args[1] !== undefined){
let data: any = this.getDataByPath(watcher.path, undefined, true);
if (args[1] !== undefined) {
data = data[args[1]];
}
if (data === undefined) {
data = '';
}
result = result.replace(new RegExp('\\$\\{' + escape.replace(/\|/g, '\\|') + '\\}', 'g'), data);
} catch (e) {
......@@ -88,20 +93,41 @@ export class DataCenter extends EventDispatcher {
return result;
}
/**
* 数值递增
* @param groupName
* @param step
* @param path
* @param dispatch
*/
increase(groupName, step?, path?, dispatch = true) {
if(step < 0 || step > 0){
let data: any = this.getDataByPath(path, groupName);
if (data === undefined) {
data = 0;
} else {
data = parseInt(data);
}
data += step;
this.mutate(groupName, data, path, dispatch);
}
}
/**
* 修改数据
* @param name
* @param groupName
* @param data
* @param path
* @param dispatch
*/
mutate(name, data?, path?, dispatch = true) {
let group = this.getGroup(name);
mutate(groupName, data?, path?, dispatch = true) {
let group = this.getGroup(groupName);
if (!group) {
this.registerGroup(name);
this.registerGroup(groupName);
}
if (data) {
if (data !== undefined) {
if (path) {
let func = new Function('scope', 'data', `scope.${path}=data`);
try {
......@@ -110,13 +136,13 @@ export class DataCenter extends EventDispatcher {
console.warn(e);
}
} else {
this.registerGroup(name, data);
this.registerGroup(groupName, data);
}
}
if (dispatch) {
for (let watcher of this.watchers) {
if (watcher.path.indexOf(name) === 0) {
if (watcher.path.indexOf(groupName) === 0) {
this.dispatchEvent(watcher.name, {
name: watcher.name,
path: watcher.path,
......@@ -127,7 +153,7 @@ export class DataCenter extends EventDispatcher {
}
globalEvent.dispatchEvent(DATA_CENTER_EVENT, {
name,
name: groupName,
path,
data,
})
......
......@@ -33,15 +33,18 @@ export class Image extends Sprite {
if (url.indexOf(assetScheme) === 0) {
let uuid = url.replace(assetScheme, '');
const assetConfig = getAssetByUUID(uuid);
url = assetConfig.url;
if(assetConfig){
url = assetConfig.url;
this.texture = Texture.fromImage(url);
}
}
} else{ //否则就使用素材名
const assetConfig = getAssetByName(url);
if(assetConfig){
url = assetConfig.url;
}
this.texture = Texture.fromImage(url);
}
this.texture = Texture.fromImage(url);
}
private _setSourceDirect(value){
......
......@@ -13,6 +13,8 @@ export class Label extends TextField {
private _originText;
private _escapes = [];
private _registeredEvents = [];
private _htmlText;
private _el;
/**
* 重载文本设置
......@@ -20,7 +22,7 @@ export class Label extends TextField {
* @private
*/
protected _setText(value) {
if(value !== this._text){
if (value !== this._text) {
let matchResult = value.match(ESCAPE_REG_EXP);
if (matchResult) {
......@@ -42,6 +44,29 @@ export class Label extends TextField {
}
}
get htmlText() {
return this._htmlText;
}
/**
* 设置html文本
*/
set htmlText(v) {
if (this._htmlText != v) {
this._htmlText = v;
let el = this._el;
if(!el){
el = this._el = document.createElement('div');
}
el.innerHTML = v;
document.body.append(el);
let pureText = el.innerText;
document.body.removeChild(el);
this.text = pureText;
}
}
/**
* 把之前侦听的全部移除
*/
......
......@@ -45,7 +45,7 @@ export function propertyParse(key, node, properties) {
* @param throwException
*/
export function getDataByPath(scope, path, throwException?) {
let func = new Function('scope', `return scope.${path}`);
let func = new Function('scope', `return scope` + (path === undefined ? '' : ('.' + path)));
try {
return func(scope);
} catch (e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment