Files
18xchat/assets/Scripts/chat18x/ui/components/SimpleToggle.ts
T
xionglijia d6c3c775b5 美术更新
- detail页面修改
- ui布局修改
2025-08-25 20:15:42 +08:00

53 lines
1.3 KiB
TypeScript

import { _decorator, Button, Component, Node, NodeEventType, Sprite } from "cc";
const { ccclass, property } = _decorator;
@ccclass("SimpleToggle")
export class SimpleToggle extends Component {
@property(Sprite)
selectedSprite: Sprite = null;
@property(Sprite)
unSelectedSprite: Sprite = null;
callback: () => {};
toggleGroupCallBack: Function;
btn: Button;
protected onLoad(): void {
this.node.on(NodeEventType.TOUCH_START, this.onClickThis, this);
this.btn = this.getComponent(Button);
}
init(isSelected: boolean, callBack: () => {}) {
this.selectedSprite.enabled = isSelected;
this.unSelectedSprite.enabled = !isSelected;
if (this.callback) {
this.onDestroy();
}
this.callback = callBack;
this.node.on(NodeEventType.TOUCH_START, this.onClickThis, this);
}
refresh(isSelected: boolean) {
this.selectedSprite.enabled = isSelected;
this.unSelectedSprite.enabled = !isSelected;
}
onClickThis() {
if (this.callback) {
this.callback();
}
if (this.toggleGroupCallBack) {
this.toggleGroupCallBack(this.node);
}
this.btn.interactable = false;
}
protected onDestroy(): void {
this.node.off(NodeEventType.TOUCH_START, this.onClickThis, this);
this.callback = null;
}
}