88 lines
2.0 KiB
JavaScript
88 lines
2.0 KiB
JavaScript
/******************************************************************
|
|
* Copyright(C) 2019 - 2020 Nx Studio
|
|
*
|
|
* NX UI插件 -- 自动缩放
|
|
*
|
|
* 2018.05.18
|
|
******************************************************************/
|
|
|
|
cc.Class( {
|
|
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
},
|
|
|
|
// 编辑器特性
|
|
editor: {
|
|
|
|
// 允许当前组件在编辑器模式下运行
|
|
executeInEditMode: false,
|
|
|
|
// requireComponent 参数用来指定当前组件的依赖组件
|
|
requireComponent: null,
|
|
|
|
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
|
|
disallowMultiple: true,
|
|
|
|
// menu 用来将当前组件添加到组件菜单中,方便用户查找
|
|
menu: "Nx/组件|自动缩放",
|
|
|
|
},
|
|
|
|
// 显示
|
|
onEnable: function() {
|
|
|
|
// 监听尺寸改变
|
|
let parent = this.node.parent;
|
|
if( parent ) {
|
|
parent.on( "size-changed", this.autoScale.bind( this ) );
|
|
}
|
|
},
|
|
|
|
// 关闭
|
|
onDisable: function() {
|
|
|
|
// 解除监听尺寸改变
|
|
let parent = this.node.parent;
|
|
if( parent ) {
|
|
parent.off( "size-changed", this.autoScale.bind( this ) );
|
|
}
|
|
|
|
},
|
|
|
|
// 自动缩放
|
|
autoScale: function() {
|
|
|
|
// 横屏缩放
|
|
if( nx.frame.vget( "IsLandscape" ) ) {
|
|
this.scaleHOR();
|
|
return;
|
|
}
|
|
|
|
// 竖屏缩放
|
|
this.scaleVER();
|
|
},
|
|
|
|
// 横屏缩放
|
|
scaleHOR: function() {
|
|
|
|
// 缩放规则:当前宽高比>MaxWHRate则进行等比扩大
|
|
let st = nx.frame.vget( "MaxWHRate" );
|
|
let fs = cc.view.getFrameSize();
|
|
let rate = fs.width / fs.height;
|
|
if( rate > st ) {
|
|
this.node.scale = rate / st;
|
|
} else {
|
|
this.node.scale = 1;
|
|
}
|
|
|
|
},
|
|
|
|
// 竖屏缩放
|
|
scaleVER: function() {
|
|
nx.warn( "$NxScale:竖屏缩放尚未实现!" );
|
|
},
|
|
|
|
} );
|