mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-05-20 15:07:40 +08:00
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组件性能
196 lines
6.8 KiB
TypeScript
196 lines
6.8 KiB
TypeScript
|
|
import type { EventTouch } from 'cc';
|
|
import { Node, Vec2, js, v3 } from 'cc';
|
|
|
|
/** 节点拖拽功能 */
|
|
//@ts-ignore
|
|
if (!Node.prototype['__$NodeDragExt$__']) {
|
|
//@ts-ignore
|
|
Node.prototype['__$NodeDragExt$__'] = true;
|
|
|
|
const _DragEvent = {
|
|
DRAG_START: 'drag_start',
|
|
DRAG_MOVE: 'drag_move',
|
|
DRAG_END: 'drag_end'
|
|
};
|
|
|
|
js.mixin(Node, {
|
|
DragEvent: _DragEvent
|
|
});
|
|
|
|
//---------------- Node 添加 拖拽属性 ----------------
|
|
|
|
js.mixin(Node.prototype, {
|
|
_draggable: false,
|
|
_dragging: false,
|
|
_dragTesting: false,
|
|
_dragStartPoint: null,
|
|
_tempVec3: null, // 优化:添加临时向量缓存
|
|
initDrag: function () {
|
|
if (this._draggable) {
|
|
this.on(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
|
this.on(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
|
this.on(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
|
this.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
|
}
|
|
else {
|
|
this.off(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
|
this.off(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
|
this.off(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
|
this.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
|
}
|
|
},
|
|
onTouchBegin_0: function (event: EventTouch) {
|
|
// 优化:延迟创建 Vec2 对象
|
|
if (this._dragStartPoint == null) {
|
|
this._dragStartPoint = new Vec2();
|
|
}
|
|
// DEV && console.log(`NodeDragExt -> onTouchBegin_0 ${this.name}`);
|
|
|
|
// event.preventSwallow = true;
|
|
const pos = event.getUILocation();
|
|
this._dragStartPoint.set(pos);
|
|
this._dragTesting = true;
|
|
},
|
|
onTouchMove_0: function (event: EventTouch) {
|
|
if (!this._dragging && this._draggable && this._dragTesting) {
|
|
const sensitivity = 10;
|
|
const pos = event.getUILocation();
|
|
const dx = this._dragStartPoint.x - pos.x;
|
|
const dy = this._dragStartPoint.y - pos.y;
|
|
// 优化:使用平方比较,避免 Math.abs
|
|
if (dx * dx + dy * dy < sensitivity * sensitivity) {
|
|
return;
|
|
}
|
|
|
|
// event.preventSwallow = true;
|
|
this._dragging = true;
|
|
this._dragTesting = false;
|
|
this.emit(Node.DragEvent.DRAG_START, event);
|
|
}
|
|
|
|
if (this._dragging) {
|
|
const delta = event.getUIDelta();
|
|
// 优化:复用临时向量对象,减少对象创建
|
|
if (!this._tempVec3) {
|
|
this._tempVec3 = v3();
|
|
}
|
|
const tempVec = this._tempVec3;
|
|
tempVec.set(delta.x, delta.y, 0);
|
|
|
|
// /** 这里除以 世界缩放,在有缩放的时候拖拽不至于很怪 */
|
|
// this.position = this.position.add(v3(delta.x / this.worldScale.x, delta.y / this.worldScale.y, 0));
|
|
const pos = this.position;
|
|
this.setPosition(pos.x + tempVec.x, pos.y + tempVec.y, pos.z);
|
|
this.emit(Node.DragEvent.DRAG_MOVE, event);
|
|
}
|
|
},
|
|
|
|
onTouchEnd_0: function (event: EventTouch) {
|
|
if (this._dragging) {
|
|
this._dragging = false;
|
|
this.emit(Node.DragEvent.DRAG_END, event);
|
|
}
|
|
// DEV && console.log(`NodeDragExt -> onTouchEnd_0 ${this.name}, _dragging: ${this._dragging}`);
|
|
},
|
|
|
|
onTouchCancel_0: function (event: EventTouch) {
|
|
if (this._dragging) {
|
|
this._dragging = false;
|
|
this.emit(Node.DragEvent.DRAG_END, event);
|
|
}
|
|
// DEV && console.log(`NodeDragExt -> onTouchCancel_0 ${this.name}, _dragging: ${this._dragging}`);
|
|
},
|
|
|
|
startDrag: function () {
|
|
// 此节点是否在场景中激活
|
|
if (!this.activeInHierarchy) {
|
|
return;
|
|
}
|
|
this.dragBegin();
|
|
},
|
|
|
|
dragBegin: function () {
|
|
this._dragging = true;
|
|
this._dragTesting = true;
|
|
this.on(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
|
this.on(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
|
this.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
|
},
|
|
dragEnd: function () {
|
|
if (this._dragging) {
|
|
this._dragTesting = false;
|
|
this._dragging = false;
|
|
}
|
|
},
|
|
|
|
// 停止拖拽
|
|
stopDrag: function () {
|
|
this.dragEnd();
|
|
},
|
|
|
|
// 移除 touch 事件
|
|
removeDragEvent: function () {
|
|
this.off(Node.EventType.TOUCH_START, this.onTouchBegin_0, this);
|
|
this.off(Node.EventType.TOUCH_MOVE, this.onTouchMove_0, this);
|
|
this.off(Node.EventType.TOUCH_END, this.onTouchEnd_0, this);
|
|
this.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel_0, this);
|
|
}
|
|
});
|
|
|
|
// 如果 node 设置 node.draggable = true, 则启用 拖拽
|
|
Object.defineProperty(Node.prototype, 'draggable', {
|
|
get: function () {
|
|
return this._draggable;
|
|
},
|
|
set: function (value) {
|
|
if (this._draggable != value) {
|
|
this._draggable = value;
|
|
this.initDrag();
|
|
}
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
|
|
Object.defineProperty(Node.prototype, 'dragTesting', {
|
|
get: function () {
|
|
return this._dragTesting;
|
|
},
|
|
set: function (value) {
|
|
if (this._dragTesting != value) {
|
|
this._dragTesting = value;
|
|
}
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
//---------------- Node 添加 拖拽属性 ----------------
|
|
}
|
|
|
|
declare module 'cc' {
|
|
// 这里使用 interface 进行扩展,如果使用 class 则会与现有的 d.ts 有冲突
|
|
export interface Node {
|
|
/** 是否启动拖拽 - true为启动 */
|
|
draggable: boolean;
|
|
/** 开始拖拽 */
|
|
startDrag(): void;
|
|
/** 停止拖拽 */
|
|
stopDrag(): void;
|
|
/** 移除拖拽事件 */
|
|
removeDragEvent(): void;
|
|
}
|
|
|
|
export namespace Node {
|
|
/** 支持的拖拽事件 */
|
|
export class DragEvent {
|
|
/** 拖拽开始事件 */
|
|
static DRAG_START: string;
|
|
/** 拖拽移动事件 */
|
|
static DRAG_MOVE: string;
|
|
/** 拖拽结束事件 */
|
|
static DRAG_END: string;
|
|
}
|
|
}
|
|
}
|