import { _decorator, Component, Node, Button, Label, Sprite } from 'cc'; import LanguageUtils, { LanguageType } from '../../Main/Common/LanguageUtils'; import { ViewManager } from '../../Main/Manager/ViewManager'; import AudioManager from '../../Main/Manager/AudioManager'; const { ccclass, property } = _decorator; @ccclass('LanguageToggleButton') export class LanguageToggleButton extends Component { @property(Label) private languageLabel: Label = null; @property(Sprite) private flagIcon: Sprite = null; @property({ displayName: "Panel Prefab Path", tooltip: "语言选择面板的预制体路径" }) private panelPath: string = "prefabs/UI/SimpleLanguagePanel"; private button: Button = null; onLoad() { this.button = this.node.getComponent(Button); if (!this.button) { this.button = this.node.addComponent(Button); } this.bindEvents(); this.updateDisplay(); } onEnable() { this.updateDisplay(); LanguageUtils.onLanguageChanged(this.onLanguageChanged, this); } onDisable() { LanguageUtils.offLanguageChanged(this.onLanguageChanged, this); } private bindEvents() { if (this.button) { this.button.node.on(Button.EventType.CLICK, this.onClick, this); } } private onClick() { AudioManager.I.playSfx("click"); // 快速切换模式(循环切换语言) if (this.isQuickToggleMode()) { this.quickToggleLanguage(); } else { // 打开语言选择面板 this.openLanguagePanel(); } } private isQuickToggleMode(): boolean { // 可以通过按住Shift键或其他条件来判断是否快速切换 return false; } private quickToggleLanguage() { const languages = LanguageUtils.getAvailableLanguages(); const currentLang = LanguageUtils.CurrentLanguage; const currentIndex = languages.indexOf(currentLang); const nextIndex = (currentIndex + 1) % languages.length; LanguageUtils.setLanguage(languages[nextIndex]); } private openLanguagePanel() { // 使用ViewManager打开语言选择面板 if (this.panelPath && this.panelPath.length > 0) { ViewManager.I.openView(this.panelPath); } else { console.warn("Language panel path not set!"); // 如果没有设置面板路径,使用快速切换 this.quickToggleLanguage(); } } private onLanguageChanged(language: LanguageType) { this.updateDisplay(); } private updateDisplay() { const currentLang = LanguageUtils.CurrentLanguage; if (this.languageLabel) { // 显示当前语言的缩写或名称 this.languageLabel.string = this.getLanguageDisplay(currentLang); } // 如果有旗帜图标,可以在这里更新 if (this.flagIcon) { // 需要根据语言加载对应的旗帜图标 // this.loadFlagIcon(currentLang); } } private getLanguageDisplay(language: LanguageType): string { // 可以选择显示缩写或完整名称 const useAbbreviation = true; if (useAbbreviation) { return language.toUpperCase(); } else { return LanguageUtils.getLanguageDisplayName(language); } } onDestroy() { if (this.button) { this.button.node.off(Button.EventType.CLICK, this.onClick, this); } LanguageUtils.offLanguageChanged(this.onLanguageChanged, this); } }