Files
oops-plugin-framework/assets/libs/gui/button/ButtonTouchLong.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

96 lines
2.4 KiB
TypeScript

/*
* @Author: dgflash
* @Date: 2022-04-14 17:08:01
* @LastEditors: dgflash
* @LastEditTime: 2022-04-14 18:15:42
*/
import type { EventTouch } from 'cc';
import { EventHandler, _decorator } from 'cc';
import ButtonEffect from './ButtonEffect';
const { ccclass, property, menu } = _decorator;
/** 长按按钮 */
@ccclass('ButtonTouchLong')
@menu('OopsFramework/Button/ButtonTouchLong (长按按钮)')
export class ButtonTouchLong extends ButtonEffect {
@property({
tooltip: '长按时间(秒)'
})
time = 1;
@property({
type: [EventHandler],
tooltip: '长按事件回调'
})
longPressEvents: EventHandler[] = [];
/** 已经过的时间 */
protected _passTime = 0;
/** 是否已触发长按 */
protected _isTouchLong = false;
/** 触摸事件引用 */
protected _event: EventTouch | null = null;
onLoad() {
this._isTouchLong = false;
super.onLoad();
}
/** 触摸开始 */
protected onTouchStart(event: EventTouch) {
this._event = event;
this._passTime = 0;
this._isTouchLong = false;
super.onTouchStart(event);
}
/** 触摸结束 */
protected onTouchEnd(event: EventTouch) {
if (this._passTime >= this.time) {
event.propagationStopped = true;
}
this.removeTouchLong();
super.onTouchEnd(event);
}
/** 移除长按状态 */
protected removeTouchLong() {
this._event = null;
this._passTime = 0;
this._isTouchLong = false;
}
/** 引擎更新事件 */
protected update(dt: number) {
// 仅在有触摸事件且未触发长按时才计算
if (this._event && !this._isTouchLong) {
this._passTime += dt;
if (this._passTime >= this.time) {
this._isTouchLong = true;
// 触发长按事件
this.longPressEvents.forEach((event) => {
event.emit([event.customEventData]);
});
// 长按触摸音效(只播放一次)
this.playEffect();
this.removeTouchLong();
}
}
}
/** 组件销毁时的清理工作 */
onDestroy() {
// 清理事件引用
this._event = null;
this.longPressEvents = [];
super.onDestroy();
}
}