import { _decorator, Node, UITransform, Component, CCFloat, Widget } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('Kit_otherSizeBind')
// 监听一个节点的大小变化,并进行绑定
export default class Kit_otherSizeBind extends Component {
@property(Node)
bindNode:Node = null;
@property(CCFloat)
max_width:number = 0;
@property(CCFloat)
max_height:number = 0;
@property(CCFloat)
height_dis:number = 1;
ui_transform:UITransform = null;
start(): void {
if(this.bindNode){
let widget = this.node.getComponent(Widget);
if(widget){
widget.enabled = false;
}
this.ui_transform = this.node.getComponent(UITransform);
if(!this.max_width){
this.max_width = this.ui_transform.width;
}
if(!this.max_height){
this.max_height = this.ui_transform.height;
}
this.bindNode.on(Node.EventType.SIZE_CHANGED, this.onSizeChanged, this);
this.updateSize();
}
}
onSizeChanged(event) {
this.updateSize()
}
updateSize(){
let { width, height } = this.bindNode.getComponent(UITransform).contentSize;
this.ui_transform.width = width > this.max_width ? this.max_width : width;
if(this.height_dis){
this.ui_transform.height = (height > this.max_height ? this.max_height : height) - this.height_dis;
}else{
this.ui_transform.height = height > this.max_height ? this.max_height : height;
}
}
}