Commit eb2f29c0 authored by rockyl's avatar rockyl

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

parent e9c9c132
This diff is collapsed.
This diff is collapsed.
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 (this.children.length == 0) {
if (viewName) {
view = this._gameStage.instantiateView(viewName);
}
if (!view) {
if (!this._content) {
this._content = new Container();
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;
view.addChild(bg);
this._contentSample.addChild(bg);
let lab = new Label();
lab.name = 'lab';
view.addChild(bg);
lab.fillColor = 'white';
lab.size = 30;
this._contentSample.addChild(lab);
}
view = this._content;
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,16 +33,19 @@ export class Image extends Sprite {
if (url.indexOf(assetScheme) === 0) {
let uuid = url.replace(assetScheme, '');
const assetConfig = getAssetByUUID(uuid);
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);
}
}
private _setSourceDirect(value){
this._source = 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