JM_KA/assets/Script/Manager.ts

120 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-01-21 11:01:58 +08:00
import Text_to_Speech from "./Text_to_Speech";
2021-12-28 23:37:22 +08:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class Manager extends cc.Component {
//#region 外調參數
@property({ type: cc.WebView })
2022-01-05 09:01:27 +08:00
public webview: cc.WebView = null;
2021-12-28 23:37:22 +08:00
2022-01-04 22:45:13 +08:00
@property({ type: cc.Node })
2022-01-05 09:01:27 +08:00
public BG: cc.Node = null;
2022-01-04 22:45:13 +08:00
2021-12-28 23:37:22 +08:00
//#endregion
2022-01-21 11:01:58 +08:00
//#region private
2022-01-22 11:34:53 +08:00
private _text_to_Speech: Text_to_Speech;
2022-01-21 11:01:58 +08:00
//#endregion
2021-12-28 23:37:22 +08:00
//#region Lifecycle
protected onLoad(): void {
2022-04-19 12:05:22 +08:00
if (CC_DEBUG) {
console.log("Debug");
}
2022-01-04 22:45:13 +08:00
let self: this = this;
2022-01-22 11:34:53 +08:00
this._text_to_Speech = new Text_to_Speech();
2022-01-04 22:45:13 +08:00
window.addEventListener("message", function (e: MessageEvent<any>): void {
let data: any = e.data;
2022-01-05 09:01:27 +08:00
let method: string = data.method;
let value: any = data.value;
2022-04-19 11:06:46 +08:00
self.Birdge(method, ...value);
2022-01-04 22:45:13 +08:00
}, false);
2022-01-21 11:01:58 +08:00
if (!window["Bridge"]) {
window["Bridge"] = function (method: string = "", value: string = ""): void {
2022-04-19 11:06:46 +08:00
self.Birdge(method, value);
2022-01-21 11:01:58 +08:00
};
}
2022-01-04 22:45:13 +08:00
let href: string = window.location.href;
2022-04-26 17:25:54 +08:00
// let url: string = `https://karolchang.github.io/jm-expense-vue-ts/?host=${href}&ignore=${Date.now()}`;
// let url: string = `http://220.134.195.1/public/bonus_casino/html5/jianmiau/BJ_Casino_Rank/?v=${Date.now()}`;
2022-06-23 10:58:57 +08:00
// let url: string = `http://karol.jianmiau.cf/jm-expense-vue-ts/?v=${Date.now()}`;
let url: string = `https://jm-expense-2022.firebaseapp.com/login?v=${Date.now()}`;
2022-04-19 11:06:46 +08:00
this.webview.url = url;
2022-01-05 09:01:27 +08:00
// this.webview.url = `http://localhost:8080/jm-expense-vue-ts/?host=${href}&ignore=${Date.now()}`;
2022-01-02 10:58:09 +08:00
this.webview.node.active = true;
cc.view.setResizeCallback(this._resize.bind(this));
this._resize();
}
2022-01-24 09:11:23 +08:00
/**
* @example
* CallParent('Speak', '我愛豬涵')
*/
2022-01-21 11:01:58 +08:00
public Birdge(method: string, value: string = ""): void {
let self: this = this;
if (method && self[method]) {
if (value) {
self[method](value);
return;
} else if (self[method]) {
self[method]();
return;
}
}
console.log(`not function: ${method}, value: ${value}`);
}
2022-01-24 09:11:23 +08:00
public CloseBG(): void {
this.BG.destroy();
2022-01-04 22:45:13 +08:00
}
2022-01-24 09:11:23 +08:00
public Log(msg: string): void {
2022-01-04 22:45:13 +08:00
console.log(msg);
}
2022-01-22 11:34:53 +08:00
public Speak(msg: string): void {
2022-01-21 11:01:58 +08:00
this._text_to_Speech.speak(msg);
}
2022-01-24 09:11:23 +08:00
public Alert(msg: string): void {
2022-01-04 22:45:13 +08:00
alert(msg);
}
2022-01-02 10:58:09 +08:00
private _resize(): void {
let Canvas: cc.Canvas = cc.Canvas.instance;
2022-01-02 11:16:05 +08: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 23:37:22 +08:00
}
//#endregion
}