Files
oops-plugin-framework/assets/libs/model-view/ui/BhvButtonGroup.ts
dgflash f2fe9d47b6 1. 存储模块全面优化,修复跨平台兼容性问题,完美支持所有Unicode字符
2. 存储模块性能提升,添加LRU缓存、批量操作支持,优化内存使用
3. 多语言模块性能与内存管理优化,组件查询性能提升
4. 时间模块类型安全与性能优化,使用泛型替代any,对象池机制减少内存分配
5. 事件系统修复双重注册、重复注册等严重问题,实现EventData对象池减少GC压力
6. RandomManager修复4个逻辑BUG,包括边界问题和越界问题
7. 音频模块内存与性能优化,避免重复加载,优化数据结构,添加完整清理机制
8. CCView与CCViewVM合并,支持按需启用MVVM
9. Collection模块优化,AsyncQueue添加队列容量限制,Collection查询性能提升
10. ECS系统全面优化,对象池复用减少内存分配,循环性能提升
11. 优化MVVM组件性能
2026-01-09 21:54:05 +08:00

184 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Color } from 'cc';
import { Button, color, Component, Enum, EventHandler, SpriteFrame, _decorator } from 'cc';
const { ccclass, property, menu } = _decorator;
enum PARAM_TYPE {
CHILDREN_INDEX,
CHILDREN_NAME
}
/**
* 群体事件,适合绑定节点组的回调信息
* 将该组件的所处节点的所有子节点绑定相同的回调对象并将组件名设置到customEventData属性中
*/
@ccclass
@menu('OopsFramework/UI/Button Group (按钮组控制)')
export class BhvButtonGroup extends Component {
@property({
type: Enum(Button.Transition)
})
transition: number = Button.Transition.NONE;
@property({
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.COLOR;
}
})
hoverColor: Color = color(255, 255, 255);
@property({
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.COLOR;
}
})
normalColor: Color = color(214, 214, 214);
@property({
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.COLOR;
}
})
pressedColor: Color = color(211, 211, 211);
@property({
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.COLOR;
}
})
disabledColor: Color = color(124, 124, 124);
@property({
type: SpriteFrame,
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.SPRITE;
}
})
normalSprite: SpriteFrame | null = null;
@property({
type: SpriteFrame,
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.SPRITE;
}
})
pressedSprite: SpriteFrame | null = null;
@property({
type: SpriteFrame,
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.SPRITE;
}
})
hoverSprite: SpriteFrame | null = null;
@property({
type: SpriteFrame,
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.SPRITE;
}
})
disabledSprite: SpriteFrame | null = null;
@property({
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.SCALE || this.transition === Button.Transition.COLOR;
}
})
duration = 1.0;
@property({
visible: function () {
// @ts-ignore
return this.transition === Button.Transition.SCALE;
}
})
zoomScale = 1.1;
@property({
type: Enum(PARAM_TYPE)
})
paramType: PARAM_TYPE = PARAM_TYPE.CHILDREN_INDEX;
@property({
type: [EventHandler]
})
touchEvents: EventHandler[] = [];
@property({
tooltip: '规避3.x引擎BUGEventHandler.component位为空导致找不到触发事件的脚本名的问题',
readonly: true
})
EventHandler_component = 'VMModify';
// 缓存创建的 Button 组件,用于销毁时清理
private _createdButtons: Button[] = [];
onLoad() {
this.node.children.forEach((node, nodeIndex) => {
let comp: Button = node.getComponent(Button)!;
if (comp == null) {
comp = node.addComponent(Button);
// 缓存动态创建的按钮组件
this._createdButtons.push(comp);
}
// 同步属性
// comp.target = node;
// comp.transition = this.transition;
// comp.zoomScale = this.zoomScale;
// comp.disabledSprite = this.disabledSprite;
// comp.hoverSprite = this.hoverSprite;
// comp.normalSprite = this.normalSprite;
// comp.pressedSprite = this.pressedSprite;
// comp.hoverColor = this.hoverColor;
// comp.normalColor = this.normalColor;
// comp.pressedColor = this.pressedColor;
// comp.disabledColor = this.disabledColor;
// 绑定回调事件
this.touchEvents.forEach((event) => {
// 克隆数据,每个节点获取的都是不同的回调
const hd = new EventHandler();//copy对象
hd.component = event.component == '' ? this.EventHandler_component : event.component;
hd.handler = event.handler;
hd.target = event.target;
if (this.paramType === PARAM_TYPE.CHILDREN_INDEX) {
hd.customEventData = nodeIndex.toString();
}
else {
hd.customEventData = node.name;
}
comp.clickEvents.push(hd);
});
});
}
/**
* 组件销毁时清理内存引用
*/
onDestroy() {
// 清理按钮事件
this._createdButtons.forEach(button => {
if (button && button.clickEvents) {
button.clickEvents.length = 0;
}
});
// 清理缓存数组
this._createdButtons.length = 0;
}
}