rp_10012/assets/Loading/scripts/LayoutAdapter.ts
TJH a97065a115
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 42s
rb7
2025-10-14 10:25:52 +08:00

111 lines
4.3 KiB
TypeScript

import { _decorator, Node, Component, screen, view, ResolutionPolicy, Sprite, sys, Label, Widget, Vec3, UITransform } from 'cc';
import { LocalizedLabel } from './i18n/LocalizedLabel';
import { getLanguage } from './comm';
const { ccclass, property } = _decorator;
export let SWITCH_PROTRAIT_MODE = {
"da": "Skift venligst til portrættilstand",
"de": "Bitte wechseln Sie in den Portraitmodus",
"en": "Please switch to portrait mode",
"es": "Por favor, cambie al modo retrato",
"fi": "Vaihda muotokuvatilaan",
"fr": "Veuillez passer en mode portrait",
"id": "Silakan beralih ke mode potret",
"it": "Passare alla modalità verticale",
"ja": "ポートレートモードに切り替えてください",
"ko": "세로 모드로 전환해주세요",
"my": "ပုံတူမုဒ်သို့ ပြောင်းပါ။",
"nl": "Schakel over naar de portretmodus",
"pt": "Por favor, mude para o modo retrato",
"ro": "Treceți la modul portret",
"ru": "Пожалуйста, переключитесь в портретный режим",
"sv": "Växla till porträttläge",
"th": "กรุณาสลับเป็นโหมดแนวตั้ง",
"tr": "Lütfen portre moduna geçin",
"vi": "Vui lòng chuyển sang chế độ chân dung",
"zh": "請切換到縱向模式"
};
@ccclass('LayoutAdapter')
export class LayoutAdapter extends Component {
@property(Node)
BG_01: Node = null;
@property(Node)
RB7Logo: Node = null;
@property(Node)
rotateNode: Node = null;
sprite: Sprite = null;
protected onLoad(): void {
this.sprite = this.BG_01.getComponent(Sprite);
this.rotateNode.active = false;
this.rotateNode.getChildByName('tips').active = false;
}
start(): void {
screen.on('window-resize', this.onWindowResize, this);
screen.on('orientation-change', this.onWindowResize, this);
this.onWindowResize();
this.rotateNode.getChildByName('tips').getComponent(Label).string = SWITCH_PROTRAIT_MODE[getLanguage()];
}
onWindowResize() {
const designSize = view.getDesignResolutionSize();
const screenSize0 = view.getVisibleSize(); // 先记录一份(如果你需要对比)
// 1) 依据宽高比切换策略
const screenRatio = screenSize0.width / screenSize0.height;
const designRatio = designSize.width / designSize.height;
view.setResolutionPolicy(
screenRatio > designRatio
? ResolutionPolicy.FIXED_HEIGHT // 横屏更宽:以高适配
: ResolutionPolicy.FIXED_WIDTH // 竖屏更窄:以宽适配
);
// 2) 下一帧再取"最终可视尺寸"来计算缩放
this.scheduleOnce(() => {
const size = view.getVisibleSize(); // 已更新后的正确值
const texW = this.sprite.spriteFrame.width / 2;
const texH = this.sprite.spriteFrame.height / 2;
const scale = Math.max(size.width / texW, size.height / texH);
this.BG_01.setScale(scale, scale); // 铺满并保持比例
if (this.RB7Logo.active) {
this.RB7Logo.setPosition(new Vec3(0, -size.y / 2 + this.RB7Logo.getComponent(UITransform).height / 2 - 45))
}
if (this.isMobile()) {
this.rotateNode.getChildByName('tips').active = true;
if (size.width > size.height) {
this.rotateNode.getChildByName('block').setScale(scale, scale);
this.rotateNode.active = true;
this.rotateNode.getComponent(Widget).updateAlignment();
} else {
this.rotateNode.getChildByName('block').setScale(scale, scale);
this.rotateNode.active = false;
}
} else {
this.rotateNode.active = false;
}
}, 0);
}
onDestroy() {
screen.off('window-resize', this.onWindowResize, this);
screen.off('orientation-change', this.onWindowResize, this);
}
isMobile(): boolean {
return (
sys.platform === sys.Platform.ANDROID ||
sys.platform === sys.Platform.IOS ||
sys.platform === sys.Platform.MOBILE_BROWSER
);
}
}