JM_KA/assets/Script/Manager.ts

90 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-12-28 15:37:22 +00:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class Manager extends cc.Component {
//#region 外調參數
@property({ type: cc.WebView })
2022-01-05 01:01:27 +00:00
public webview: cc.WebView = null;
2021-12-28 15:37:22 +00:00
2022-01-04 14:45:13 +00:00
@property({ type: cc.Node })
2022-01-05 01:01:27 +00:00
public BG: cc.Node = null;
2022-01-04 14:45:13 +00:00
2021-12-28 15:37:22 +00:00
//#endregion
//#region Lifecycle
protected onLoad(): void {
2022-01-04 14:45:13 +00:00
let self: this = this;
window.addEventListener("message", function (e: MessageEvent<any>): void {
let data: any = e.data;
2022-01-05 01:01:27 +00:00
let method: string = data.method;
let value: any = data.value;
2022-01-04 14:45:13 +00:00
if (method && self[method]) {
if (value) {
self[method](...value);
return;
} else if (self[method]) {
self[method]();
return;
}
}
console.log(`not function: ${method}, value: ${value}`);
}, false);
let href: string = window.location.href;
2022-01-05 01:01:27 +00:00
this.webview.url = `https://karolchang.github.io/jm-expense-vue-ts/?host=${href}&ignore=${Date.now()}`;
// this.webview.url = `http://localhost:8080/jm-expense-vue-ts/?host=${href}&ignore=${Date.now()}`;
2022-01-02 02:58:09 +00:00
this.webview.node.active = true;
cc.view.setResizeCallback(this._resize.bind(this));
this._resize();
}
2022-01-04 14:45:13 +00:00
private _closeBG(): void {
2022-01-20 11:20:17 +00:00
if (this.webview.node.opacity === 0) {
this.BG.destroy();
this.webview.node.opacity = 255;
this.webview.node.active = false;
this.webview.node.active = true;
}
2022-01-04 14:45:13 +00:00
}
private _log(msg: string): void {
console.log(msg);
}
private _alert(msg: string): void {
alert(msg);
}
2022-01-02 02:58:09 +00:00
private _resize(): void {
let Canvas: cc.Canvas = cc.Canvas.instance;
2022-01-02 03:16:05 +00:00
let rect: DOMRect = cc.game.canvas.getBoundingClientRect();
/** 判断是否是横屏 */
let landscape: boolean = false;
if (rect.width > rect.height) {
landscape = true;
}
// 根据横竖屏调整节点的位置适配等
let frameSize: cc.Size = cc.view.getFrameSize();
if (landscape) {
// 横屏
cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
if (frameSize.height > frameSize.width) {
cc.view.setFrameSize(frameSize.height, frameSize.width);
}
Canvas.designResolution = cc.size(1920, 1080);
} else {
// 竖屏
cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
if (frameSize.width > frameSize.height) {
cc.view.setFrameSize(frameSize.height, frameSize.width);
}
Canvas.designResolution = cc.size(1080, 1920);
}
2021-12-28 15:37:22 +00:00
}
//#endregion
}