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 @@ ...@@ -25,7 +25,7 @@
}, },
"scripts": { "scripts": {
"build-webpack": "webpack", "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 ", "rollup:debug": "rollup -c -m ",
"rename": "node scripts/rename-hash.js dist/engine.js", "rename": "node scripts/rename-hash.js dist/engine.js",
"build": "rm -rf dist&&yarn rollup && yarn rename", "build": "rm -rf dist&&yarn rollup && yarn rename",
......
...@@ -98,7 +98,7 @@ export class EventDispatcher extends HashObject { ...@@ -98,7 +98,7 @@ export class EventDispatcher extends HashObject {
for (var i = 0, len = eventTypes[type].length; i < len; i++) { for (var i = 0, len = eventTypes[type].length; i < len; i++) {
let ee: EE = eventTypes[type][i] let ee: EE = eventTypes[type][i]
if (ee.fn === listener && ee.context === context) { if (ee.fn === listener && ee.context === context) {
console.log("已添加过该事件") //console.log("已添加过该事件")
return return
} }
} }
......
...@@ -31,6 +31,9 @@ export function httpRequest(callback: Function, url: string, method: string = 'g ...@@ -31,6 +31,9 @@ export function httpRequest(callback: Function, url: string, method: string = 'g
url = urlJoin(url, queryStr); url = urlJoin(url, queryStr);
} }
_req.open(method, url, true); _req.open(method, url, true);
if(!isGet){
_req.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
}
_req.responseType = type; _req.responseType = type;
if (isGet) { if (isGet) {
_req.send(); _req.send();
......
...@@ -229,7 +229,7 @@ export class TextField extends Sprite { ...@@ -229,7 +229,7 @@ export class TextField extends Sprite {
return this._lineType; 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 { ...@@ -252,6 +252,8 @@ export class TextField extends Sprite {
protected _setText(value) { protected _setText(value) {
this._text = value; this._text = value;
this.dirty = true; this.dirty = true;
this.dispatchEvent(Event.RESIZE);
} }
protected _text: string = ""; protected _text: string = "";
...@@ -558,22 +560,31 @@ export class TextField extends Sprite { ...@@ -558,22 +560,31 @@ export class TextField extends Sprite {
} }
} else { } else {
//textWidth取每行最大值,如果没设置过textWidth //textWidth取每行最大值,如果没设置过textWidth
let measureCache = {};
if (!textWidth) { if (!textWidth) {
for (let i = 0, l = hardLines.length; i < l; i++) { for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i]; let str = hardLines[i];
if (!str) continue; 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++) { for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i]; let str = hardLines[i];
if (!str) continue; if (!str) continue;
let w = s._getMeasuredWidth(str[0]); let w = measureCache[str[0]];
let lineStr = str[0]; let lineStr = str[0];
let wordW = 0; let wordW = 0;
let strLen = str.length; let strLen = str.length;
for (let j = 1; j < strLen; j++) { for (let j = 1; j < strLen; j++) {
wordW = ctx.measureText(str[j]).width; wordW = measureCache[str[j]];
w += wordW; w += wordW;
if (w > textWidth) { if (w > textWidth) {
realLines[realLines.length] = lineStr; realLines[realLines.length] = lineStr;
...@@ -633,7 +644,7 @@ export class TextField extends Sprite { ...@@ -633,7 +644,7 @@ export class TextField extends Sprite {
this.anchorTexture = {x: (padding + 0.5) / can.width, y: padding / can.height} 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.height = maxH;
// s._bounds.width = maxW; // s._bounds.width = maxW;
//x,y都是0 //x,y都是0
......
...@@ -96,15 +96,16 @@ export class Process { ...@@ -96,15 +96,16 @@ export class Process {
this.updateProps(this._config.props = {}, payload, this._originProps, this._meta.props); this.updateProps(this._config.props = {}, payload, this._originProps, this._meta.props);
if (metaConfig.script) { if (metaConfig.script) {
let func; let func;
if(metaConfig.script.indexOf(linkScheme) === 0){ if (metaConfig.script.indexOf(linkScheme) === 0) {
func = this._vm.getScript(metaConfig.script.replace(linkScheme, '')); func = this._vm.getScript(metaConfig.script.replace(linkScheme, ''));
}else{ } else {
func = new Function('args', 'props', 'target', 'global', 'vm', warpAsyncScript(metaConfig.script)); func = new Function('args', 'props', 'target', 'global', 'vm', warpAsyncScript(metaConfig.script));
} }
if(!func){ if (!func) {
console.log(); console.log();
} }
let globalContext = this._vm.globalContext; let globalContext = this._vm.globalContext;
globalContext.dataCenter = globalContext.gameStage.dataCenter;
result = await func(payload, this._config.props, this._target, globalContext, this._vm); result = await func(payload, this._config.props, this._target, globalContext, this._vm);
} }
} else { } else {
...@@ -193,7 +194,7 @@ export class Process { ...@@ -193,7 +194,7 @@ export class Process {
let linkedValue = this.resolveLinkedProp(value, key); let linkedValue = this.resolveLinkedProp(value, key);
if (linkedValue !== undefined) { if (linkedValue !== undefined) {
props[key] = linkedValue; props[key] = linkedValue;
}else{ } else {
props[key] = undefined; props[key] = undefined;
} }
break; break;
...@@ -219,7 +220,7 @@ export class Process { ...@@ -219,7 +220,7 @@ export class Process {
if (uuid) { if (uuid) {
props[key] = findNodeByUUID(this._vm.globalContext.gameStage, uuid); props[key] = findNodeByUUID(this._vm.globalContext.gameStage, uuid);
} }
}else if(originProps[key] !== undefined){ } else if (originProps[key] !== undefined) {
props[key] = originProps[key]; props[key] = originProps[key];
} }
......
...@@ -68,7 +68,7 @@ class AdjustProxy { ...@@ -68,7 +68,7 @@ class AdjustProxy {
} }
onRemovedFromStage(e) { 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.RESIZE, this.onResize, this);
this._host.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame); this._host.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
} }
......
...@@ -28,12 +28,27 @@ export function applyScript(ctor: Function) { ...@@ -28,12 +28,27 @@ export function applyScript(ctor: Function) {
*/ */
export class ScriptBase { export class ScriptBase {
private _host: Container; private _host: Container;
disabled: boolean; private _disabled: boolean;
get host() { get host() {
return this._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() { mounted() {
} }
...@@ -63,7 +78,7 @@ export class ScriptBase { ...@@ -63,7 +78,7 @@ export class ScriptBase {
export function registerScriptDef(id, def) { export function registerScriptDef(id, def) {
scriptDefs[id] = def; scriptDefs[id] = def;
Object.defineProperty(def, 'disabled', { /*Object.defineProperty(def, 'disabled', {
get: function () { get: function () {
return !!this._disabled; return !!this._disabled;
}, },
...@@ -79,8 +94,7 @@ export function registerScriptDef(id, def) { ...@@ -79,8 +94,7 @@ export function registerScriptDef(id, def) {
} }
}, },
enumerable: true, enumerable: true,
configurable: true });*/
});
} }
export function registerScripts(scripts) { export function registerScripts(scripts) {
...@@ -158,10 +172,12 @@ class ScriptsProxy { ...@@ -158,10 +172,12 @@ class ScriptsProxy {
/** /**
* 根据名字获取脚本 * 根据名字获取脚本
* @param name * @param id
*/ */
get(name): ScriptBase[] { get(id): ScriptBase[] {
return this._scripts.filter(script => script.constructor['name'] === name); return this._scripts.filter(script => {
return script.constructor['id'] === id;
});
} }
/** /**
......
...@@ -47,6 +47,7 @@ export class GameStage extends Container { ...@@ -47,6 +47,7 @@ export class GameStage extends Container {
this.addChild(this._sceneContainer = new StackContainer()); this.addChild(this._sceneContainer = new StackContainer());
this.addChild(blackLayer); this.addChild(blackLayer);
this.addChild(this._popupContainer = new StackContainer(false)); this.addChild(this._popupContainer = new StackContainer(false));
this.addChild(this._toast = new Toast(this));
blackLayer['percentWidth'] = 100; blackLayer['percentWidth'] = 100;
blackLayer['percentHeight'] = 100; blackLayer['percentHeight'] = 100;
...@@ -74,6 +75,13 @@ export class GameStage extends Container { ...@@ -74,6 +75,13 @@ export class GameStage extends Container {
return this._popupContainer; return this._popupContainer;
} }
/**
* Toast提示
*/
get toast(): Toast {
return this._toast;
}
/** /**
* 数据中心 * 数据中心
*/ */
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import {Container} from "../../2d/display"; import {Container} from "../../2d/display";
import {GameStage} from "./GameStage"; import {GameStage} from "./GameStage";
import {Label, Rect} from "./nodes"; import {Label, Rect} from "./nodes";
import {Tween} from "../../2d/tween";
export class Toast extends Container { export class Toast extends Container {
private _contentSample: Container; private _contentSample: Container;
...@@ -15,50 +16,91 @@ export class Toast extends Container { ...@@ -15,50 +16,91 @@ export class Toast extends Container {
constructor(gameStage: GameStage) { constructor(gameStage: GameStage) {
super(); super();
this._gameStage = gameStage; this._gameStage = gameStage;
this.horizonCenter = 0;
this.verticalCenter = 0;
} }
show(props) { show(props: any = {}) {
if (!props.text) { if (!props.text) {
return; return;
} }
const {text, duration, viewName} = props;
let contentView = this.getContent(viewName); let contentView = this.getContent(props.viewName);
let lab = contentView.getChildByName('lab'); let lab = contentView.getChildByName('lab');
if (!lab) { if (!lab) {
console.warn('[lab] child no exists on toast view'); console.warn('[lab] child no exists on toast view');
return; return;
} }
const {text, padding = 10, duration = 1000, showDuration = 300, hideDuration = 200} = props;
lab.text = text; lab.text = text;
contentView.width = lab.width; lab.x = lab.y = padding;
contentView.height = lab.height; 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) { private getContent(viewName) {
let view; let view;
if (viewName) {
view = this._gameStage.instantiateView(viewName); if (this.children.length == 0) {
} if (viewName) {
if (!view) { view = this._gameStage.instantiateView(viewName);
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);
} }
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; return view;
} }
} }
...@@ -46,10 +46,12 @@ export class DataCenter extends EventDispatcher { ...@@ -46,10 +46,12 @@ export class DataCenter extends EventDispatcher {
/** /**
* 根据路径获取数据 * 根据路径获取数据
* @param path * @param path
* @param groupName
* @param throwException * @param throwException
*/ */
getDataByPath(path, throwException?) { getDataByPath(path, groupName?, throwException?) {
return getDataByPath(this.store, path, 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 { ...@@ -59,7 +61,7 @@ export class DataCenter extends EventDispatcher {
*/ */
getDataByName(name, throwException?) { getDataByName(name, throwException?) {
let watcher = this.getWatcher(name); let watcher = this.getWatcher(name);
if(watcher){ if (watcher) {
return getDataByPath(this.store, watcher.path, throwException); return getDataByPath(this.store, watcher.path, throwException);
} }
} }
...@@ -76,10 +78,13 @@ export class DataCenter extends EventDispatcher { ...@@ -76,10 +78,13 @@ export class DataCenter extends EventDispatcher {
let name = args[0]; let name = args[0];
let watcher = this.getWatcher(name); let watcher = this.getWatcher(name);
try { try {
let data = this.getDataByPath(watcher.path, true); let data: any = this.getDataByPath(watcher.path, undefined, true);
if(args[1] !== undefined){ if (args[1] !== undefined) {
data = data[args[1]]; data = data[args[1]];
} }
if (data === undefined) {
data = '';
}
result = result.replace(new RegExp('\\$\\{' + escape.replace(/\|/g, '\\|') + '\\}', 'g'), data); result = result.replace(new RegExp('\\$\\{' + escape.replace(/\|/g, '\\|') + '\\}', 'g'), data);
} catch (e) { } catch (e) {
...@@ -88,20 +93,41 @@ export class DataCenter extends EventDispatcher { ...@@ -88,20 +93,41 @@ export class DataCenter extends EventDispatcher {
return result; 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 data
* @param path * @param path
* @param dispatch * @param dispatch
*/ */
mutate(name, data?, path?, dispatch = true) { mutate(groupName, data?, path?, dispatch = true) {
let group = this.getGroup(name); let group = this.getGroup(groupName);
if (!group) { if (!group) {
this.registerGroup(name); this.registerGroup(groupName);
} }
if (data) { if (data !== undefined) {
if (path) { if (path) {
let func = new Function('scope', 'data', `scope.${path}=data`); let func = new Function('scope', 'data', `scope.${path}=data`);
try { try {
...@@ -110,13 +136,13 @@ export class DataCenter extends EventDispatcher { ...@@ -110,13 +136,13 @@ export class DataCenter extends EventDispatcher {
console.warn(e); console.warn(e);
} }
} else { } else {
this.registerGroup(name, data); this.registerGroup(groupName, data);
} }
} }
if (dispatch) { if (dispatch) {
for (let watcher of this.watchers) { for (let watcher of this.watchers) {
if (watcher.path.indexOf(name) === 0) { if (watcher.path.indexOf(groupName) === 0) {
this.dispatchEvent(watcher.name, { this.dispatchEvent(watcher.name, {
name: watcher.name, name: watcher.name,
path: watcher.path, path: watcher.path,
...@@ -127,7 +153,7 @@ export class DataCenter extends EventDispatcher { ...@@ -127,7 +153,7 @@ export class DataCenter extends EventDispatcher {
} }
globalEvent.dispatchEvent(DATA_CENTER_EVENT, { globalEvent.dispatchEvent(DATA_CENTER_EVENT, {
name, name: groupName,
path, path,
data, data,
}) })
......
...@@ -33,15 +33,18 @@ export class Image extends Sprite { ...@@ -33,15 +33,18 @@ export class Image extends Sprite {
if (url.indexOf(assetScheme) === 0) { if (url.indexOf(assetScheme) === 0) {
let uuid = url.replace(assetScheme, ''); let uuid = url.replace(assetScheme, '');
const assetConfig = getAssetByUUID(uuid); const assetConfig = getAssetByUUID(uuid);
url = assetConfig.url; if(assetConfig){
url = assetConfig.url;
this.texture = Texture.fromImage(url);
}
} }
} else{ //否则就使用素材名 } else{ //否则就使用素材名
const assetConfig = getAssetByName(url); const assetConfig = getAssetByName(url);
if(assetConfig){ if(assetConfig){
url = assetConfig.url; url = assetConfig.url;
} }
this.texture = Texture.fromImage(url);
} }
this.texture = Texture.fromImage(url);
} }
private _setSourceDirect(value){ private _setSourceDirect(value){
......
...@@ -13,6 +13,8 @@ export class Label extends TextField { ...@@ -13,6 +13,8 @@ export class Label extends TextField {
private _originText; private _originText;
private _escapes = []; private _escapes = [];
private _registeredEvents = []; private _registeredEvents = [];
private _htmlText;
private _el;
/** /**
* 重载文本设置 * 重载文本设置
...@@ -20,7 +22,7 @@ export class Label extends TextField { ...@@ -20,7 +22,7 @@ export class Label extends TextField {
* @private * @private
*/ */
protected _setText(value) { protected _setText(value) {
if(value !== this._text){ if (value !== this._text) {
let matchResult = value.match(ESCAPE_REG_EXP); let matchResult = value.match(ESCAPE_REG_EXP);
if (matchResult) { if (matchResult) {
...@@ -42,6 +44,29 @@ export class Label extends TextField { ...@@ -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) { ...@@ -45,7 +45,7 @@ export function propertyParse(key, node, properties) {
* @param throwException * @param throwException
*/ */
export function getDataByPath(scope, path, 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 { try {
return func(scope); return func(scope);
} catch (e) { } 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