| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { Component, EventHandler, EventTouch, v3, Vec3, _decorator } from "cc";
- import { DragSystem } from "./DragSystem";
- import { DragTarget } from "./DragTarget";
- const { ccclass, property } = _decorator;
- /**
- * 可以被拖拽的组件
- * @description 如果需要新创建UI,那么请自己创建预制体,并在预制体的update中更新其坐标为本组件的worldPosition
- * @author 袁浩
- */
- @ccclass("DragSource")
- export class DragSource extends Component {
- @property({ tooltip: "所属分组" })
- public group: string = "group";
- @property({ tooltip: "是否更新自己的坐标" })
- public updatePosition = false;
- @property({ tooltip: "当被拖拽", type: EventHandler })
- public onDrag: EventHandler[] = [];
- @property({ tooltip: "当被拖拽放下", type: EventHandler })
- public onDrop: EventHandler[] = [];
- @property({ tooltip: "当被拖拽中", type: EventHandler })
- public onDraging: EventHandler[] = [];
- /**
- * 当前世界位置
- */
- public worldPosition: Vec3;
- start() {
- DragSystem.addSource(this);
- }
- onDestroy() {
- DragSystem.removeSource(this);
- }
- update() {
- if (this.updatePosition && this.worldPosition) {
- this.node.worldPosition = this.worldPosition;
- }
- }
- public draging(event: EventTouch) {
- for (let i = 0; i < this.onDraging.length; i++) {
- this.onDraging[i] && this.onDraging[i].emit([event]);
- }
- }
- public drop(event: EventTouch) {
- for (let i = 0; i < this.onDrop.length; i++) {
- this.onDrop[i] && this.onDrop[i].emit([event]);
- }
- }
- public drag(event: EventTouch) {
- for (let i = 0; i < this.onDrag.length; i++) {
- this.onDrag[i] && this.onDrag[i].emit([event]);
- }
- }
- }
|