Commit f987a9a6 authored by rockyl's avatar rockyl

修改代理

parent 8b44bc20
This diff is collapsed.
This diff is collapsed.
...@@ -6,59 +6,89 @@ ...@@ -6,59 +6,89 @@
import {Event} from "../../2d/events"; import {Event} from "../../2d/events";
/**
* 自适应数据
*/
export class AdjustData {
percentWidth: number = NaN;
percentHeight: number = NaN;
left: number = NaN;
top: number = NaN;
right: number = NaN;
bottom: number = NaN;
horizonCenter: number = NaN;
verticalCenter: number = NaN;
}
function t(v) {
return !isNaN(v) && v !== null && v !== undefined;
}
/** /**
* 应用自适应 * 应用自适应
* @param ctor * @param ctor
*/ */
export function applyAutoAdjust(ctor: Function) { export function applyAutoAdjust(ctor: Function) {
ctor.prototype.applyAutoAdjust = function () { ctor.prototype.applyAutoAdjust = function () {
let adjustData = new AdjustData(); let adjustProxy = this.adjustProxy = new AdjustProxy(this);
this.__sizeDirty = true; this.addEventListener(Event.ADDED_TO_STAGE, adjustProxy.onAddedToStage, adjustProxy);
this.adjustData = adjustData; this.addEventListener(Event.REMOVED_FROM_STAGE, adjustProxy.onRemovedFromStage, adjustProxy);
this.addEventListener(Event.ADDED_TO_STAGE, this.__onAddedToStageAA, this);
this.addEventListener(Event.REMOVED_FROM_STAGE, this.__onRemovedFromStageAA, this);
};
ctor.prototype.__onAddedToStageAA = function () {
this.parent.addEventListener(Event.RESIZE, this.__onResizeAA, this);
this.addEventListener(Event.ENTER_FRAME, this.__onEnterFrameAA, this);
}; };
ctor.prototype.__onRemovedFromStageAA = function () { let temp = new AdjustProxy(null);
this.parent.removeEventListener(Event.RESIZE, this.__onResizeAA); for (let key in temp.data)
this.removeEventListener(Event.ENTER_FRAME, this.__onEnterFrameAA); Object.defineProperty(ctor.prototype, key, {
}; get: function () {
ctor.prototype.__onResizeAA = function () { return this.adjustProxy.data[key];
this.__sizeDirty = true; },
set: function (v) {
const adjustProxy: AdjustProxy = this.adjustProxy;
if (adjustProxy.data[key] !== v) {
adjustProxy.data[key] = v;
adjustProxy.makeDirty();
}
},
enumerable: true,
configurable: true
});
}
/**
* 自适应数据
*/
class AdjustProxy {
data = {
percentWidth: NaN,
percentHeight: NaN,
left: NaN,
top: NaN,
right: NaN,
bottom: NaN,
horizonCenter: NaN,
verticalCenter: NaN,
}; };
ctor.prototype.__onEnterFrameAA = function () {
if (this.__sizeDirty) { private _host;
this.__sizeDirty = false; private _sizeDirty;
constructor(host) {
this._host = host;
this.makeDirty();
}
makeDirty() {
this._sizeDirty = true;
}
onAddedToStage(e) {
this._host.parent.addEventListener(Event.RESIZE, this.onResize, this);
this._host.addEventListener(Event.ENTER_FRAME, this.onEnterFrame, this);
}
onRemovedFromStage(e) {
this._host.parent.removeEventListener(Event.RESIZE, this.onResize);
this._host.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
}
private onResize(e) {
this._sizeDirty = true;
}
private onEnterFrame(e) {
if (this._sizeDirty) {
this._sizeDirty = false;
this.adjustLayout(); this.adjustLayout();
} }
}; }
ctor.prototype.adjustLayout = function () {
const that = this; adjustLayout() {
const {width: pWidth, height: pHeight} = this.parent; const that = this._host;
const {width, height} = this;
const {percentWidth, percentHeight, left, top, right, bottom, horizonCenter, verticalCenter} = this.adjustData; const {width: pWidth, height: pHeight} = that.parent;
const {width, height} = that;
const {percentWidth, percentHeight, left, top, right, bottom, horizonCenter, verticalCenter} = this.data;
const applyPercentWidth = function () { const applyPercentWidth = function () {
if (t(percentWidth)) { if (t(percentWidth)) {
...@@ -74,16 +104,16 @@ export function applyAutoAdjust(ctor: Function) { ...@@ -74,16 +104,16 @@ export function applyAutoAdjust(ctor: Function) {
let pw = true, ph = true; let pw = true, ph = true;
if (t(horizonCenter)) { if (t(horizonCenter)) {
applyPercentWidth(); applyPercentWidth();
this.x = (pWidth - this.width) / 2 + horizonCenter; that.x = (pWidth - that.width) / 2 + horizonCenter;
} else { } else {
if (t(left)) { if (t(left)) {
this.x = left; that.x = left;
if (t(right)) { if (t(right)) {
this.width = pWidth - left - right; that.width = pWidth - left - right;
pw = false; pw = false;
} }
} else if (t(right)) { } else if (t(right)) {
this.x = pWidth - width - right; that.x = pWidth - width - right;
} }
if (pw) { if (pw) {
applyPercentWidth(); applyPercentWidth();
...@@ -92,34 +122,25 @@ export function applyAutoAdjust(ctor: Function) { ...@@ -92,34 +122,25 @@ export function applyAutoAdjust(ctor: Function) {
if (t(verticalCenter)) { if (t(verticalCenter)) {
applyPercentHeight(); applyPercentHeight();
this.y = (pHeight - this.height) / 2 + verticalCenter; that.y = (pHeight - that.height) / 2 + verticalCenter;
} else { } else {
if (t(top)) { if (t(top)) {
this.y = top; that.y = top;
if (t(bottom)) { if (t(bottom)) {
this.height = pHeight - top - bottom; that.height = pHeight - top - bottom;
ph = false; ph = false;
} }
} else if (t(bottom)) { } else if (t(bottom)) {
this.y = pHeight - height - bottom; that.y = pHeight - height - bottom;
} }
if (ph) { if (ph) {
applyPercentHeight(); applyPercentHeight();
} }
} }
}; }
for (let key in new AdjustData())
Object.defineProperty(ctor.prototype, key, { }
get: function () {
return this.adjustData[key]; function t(v) {
}, return !isNaN(v) && v !== null && v !== undefined;
set: function (v) {
if (this.adjustData[key] !== v) {
this.adjustData[key] = v;
this.__sizeDirty = true;
}
},
enumerable: true,
configurable: true
});
} }
...@@ -21,14 +21,14 @@ const eventsConfig = { ...@@ -21,14 +21,14 @@ const eventsConfig = {
*/ */
export function applyEvents(ctor: Function) { export function applyEvents(ctor: Function) {
ctor.prototype.applyEvents = function () { ctor.prototype.applyEvents = function () {
let events = this.events = new Events(); let eventsProxy = this.eventsProxy = new EventsProxy();
for(let k in eventsConfig){ for(let k in eventsConfig){
this.addEventListener(k, events.onEvent, events); this.addEventListener(k, eventsProxy.onEvent, eventsProxy);
} }
}; };
} }
class Events extends HashObject{ class EventsProxy extends HashObject{
constructor(){ constructor(){
super(); super();
} }
......
...@@ -14,20 +14,11 @@ const scriptDefs = {}; ...@@ -14,20 +14,11 @@ const scriptDefs = {};
*/ */
export function applyScript(ctor: Function) { export function applyScript(ctor: Function) {
ctor.prototype.applyScripts = function () { ctor.prototype.applyScripts = function () {
this.scripts = new ScriptsContainer(this); let scriptsProxy = this.scriptsProxy = new ScriptsProxy(this);
this.addEventListener(Event.ENTER_FRAME, this.__onEnterFrameS, this); this.addEventListener(Event.ENTER_FRAME, scriptsProxy.onEnterFrame, scriptsProxy);
this.addEventListener(Event.ADDED_TO_STAGE, this.__onAddedToStageS, this); this.addEventListener(Event.ADDED_TO_STAGE, scriptsProxy.onAddedToStage, scriptsProxy);
this.addEventListener(Event.REMOVED_FROM_STAGE, this.__onRemovedFromStageS, this); this.addEventListener(Event.REMOVED_FROM_STAGE, scriptsProxy.onRemovedFromStage, scriptsProxy);
};
ctor.prototype.__onEnterFrameS = function (e) {
this.scripts.update(e.data);
};
ctor.prototype.__onAddedToStageS = function (e) {
this.scripts.awake();
};
ctor.prototype.__onRemovedFromStageS = function (e) {
this.scripts.sleep();
}; };
} }
...@@ -79,7 +70,7 @@ export function registerScriptDef(def) { ...@@ -79,7 +70,7 @@ export function registerScriptDef(def) {
/** /**
* 脚本容器 * 脚本容器
*/ */
class ScriptsContainer { class ScriptsProxy {
private _host: Container; private _host: Container;
private _scripts: IScript[] = []; private _scripts: IScript[] = [];
...@@ -152,7 +143,7 @@ class ScriptsContainer { ...@@ -152,7 +143,7 @@ class ScriptsContainer {
/** /**
* 唤醒 * 唤醒
*/ */
awake() { onAddedToStage() {
for (let script of this._scripts) { for (let script of this._scripts) {
if (!script.disabled) { if (!script.disabled) {
script.awake(); script.awake();
...@@ -163,7 +154,7 @@ class ScriptsContainer { ...@@ -163,7 +154,7 @@ class ScriptsContainer {
/** /**
* 睡眠 * 睡眠
*/ */
sleep() { onRemovedFromStage() {
for (let script of this._scripts) { for (let script of this._scripts) {
if (!script.disabled) { if (!script.disabled) {
script.sleep(); script.sleep();
...@@ -174,7 +165,8 @@ class ScriptsContainer { ...@@ -174,7 +165,8 @@ class ScriptsContainer {
/** /**
* 更新脚本时钟 * 更新脚本时钟
*/ */
update(t) { onEnterFrame(e) {
let t = e.data;
for (let script of this._scripts) { for (let script of this._scripts) {
if (!script.disabled) { if (!script.disabled) {
script.update(t); script.update(t);
......
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