74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
|
|
"use strict";
|
||
|
|
cc._RF.push(module, '8f5f6QEz+9M+aB8sCSyme2a', 'nx.fx.scale');
|
||
|
|
// Scripts/nx/cmp/flex/nx.fx.scale.js
|
||
|
|
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/******************************************************************
|
||
|
|
* 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 onEnable() {
|
||
|
|
// 监听尺寸改变
|
||
|
|
var parent = this.node.parent;
|
||
|
|
if (parent) {
|
||
|
|
parent.on("size-changed", this.autoScale.bind(this));
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 关闭
|
||
|
|
onDisable: function onDisable() {
|
||
|
|
// 解除监听尺寸改变
|
||
|
|
var parent = this.node.parent;
|
||
|
|
if (parent) {
|
||
|
|
parent.off("size-changed", this.autoScale.bind(this));
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 自动缩放
|
||
|
|
autoScale: function autoScale() {
|
||
|
|
// 横屏缩放
|
||
|
|
if (nx.frame.vget("IsLandscape")) {
|
||
|
|
this.scaleHOR();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 竖屏缩放
|
||
|
|
this.scaleVER();
|
||
|
|
},
|
||
|
|
// 横屏缩放
|
||
|
|
scaleHOR: function scaleHOR() {
|
||
|
|
// 缩放规则:当前宽高比>MaxWHRate则进行等比扩大
|
||
|
|
var st = nx.frame.vget("MaxWHRate");
|
||
|
|
var fs = cc.view.getFrameSize();
|
||
|
|
var rate = fs.width / fs.height;
|
||
|
|
if (rate > st) {
|
||
|
|
this.node.scale = rate / st;
|
||
|
|
} else {
|
||
|
|
this.node.scale = 1;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 竖屏缩放
|
||
|
|
scaleVER: function scaleVER() {
|
||
|
|
nx.warn("$NxScale:竖屏缩放尚未实现!");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
cc._RF.pop();
|