/****************************************************************** * Copyright(C) 2019 - 2020 Nx Studio * * 组件--本地化文本 * * 2018.05.18 ******************************************************************/ const SCALE = 1.5; cc.Class( { extends: cc.Component, properties: { useFont: { default: true, displayName: "使用全局字体" }, openScale: { default: true, displayName: "清晰缩放" } }, // 编辑器特性 editor: { // 允许当前组件在编辑器模式下运行 executeInEditMode: false, // 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突 disallowMultiple: false, // menu 用来将当前组件添加到组件菜单中,方便用户查找 menu: "Nx/本地化|文本", }, // 展示 onLoad: function() { let label = nx.gui.getComponent( this, "", cc.Label ); if( !label ) { label = nx.gui.getComponent( this, "", cc.RichText ); if( !label ) { return; } } // 合批开启 // label.cacheMode = cc.Label.CacheMode.BITMAP; // 全局字体设定 if( this.useFont && game.gfont ) { label.font = game.gfont; } // 本地化文本 this.setText( label.string ); // 清晰化骚操作 this.rawScaleX = this.node.scaleX; this.rawScaleY = this.node.scaleY; this.rawWidth = this.node.width; this.rawHeight = label.lineHeight; this.rawMaxWidth = label.maxWidth; this.autoScale(); }, // 等比缩放 autoScale: function() { if( !this.openScale ) { return; } let label = nx.gui.getComponent( this, "", cc.Label ); if( !label ) { label = nx.gui.getComponent( this, "", cc.RichText ); if( !label ) { return; } } label.fontSize = Math.floor( label.fontSize * SCALE ); label.node.scaleX = this.rawScaleX / SCALE; label.node.scaleY = this.rawScaleY / SCALE; label.node.width = this.rawWidth * SCALE; // 高度 if( nx.dt.numPositive( label.lineHeight, false ) ) { label.lineHeight = Math.ceil( label.lineHeight * SCALE ); } // 富文本 if( nx.dt.numPositive( this.rawMaxWidth, false ) ) { label.maxWidth = this.rawMaxWidth * SCALE; } // 如果父节点是Layout,自动开启比例缩放 let layout = nx.gui.getComponent( this.node.parent, "", cc.Layout ); if( layout ) { layout.affectedByScale = true; } }, // 设置文本 setText: function( _key ) { let txt = ""; if( nx.dt.strNEmpty( _key ) ) { txt = nx.text.getKey( _key ); } nx.gui.setString( this.node, "", txt ); }, } );