"use strict";
cc._RF.push(module, '37771WfhHtGvYKayrQ3+i+n', 'Draggable');
// scripts/Draggable.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var Draggable = /** @class */ (function (_super) {
    __extends(Draggable, _super);
    function Draggable() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        // 是否正在拖拽
        _this._isDragging = false;
        // 触摸点与节点原点的偏移（解决点击位置偏移问题）
        _this._touchOffset = cc.v2(0, 0);
        return _this;
    }
    Draggable.prototype.onLoad = function () {
        // 注册触摸事件
        this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
    };
    Draggable.prototype.onDestroy = function () {
        // 注销触摸事件，防止内存泄漏
        this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
    };
    Draggable.prototype._onTouchStart = function (event) {
        this._isDragging = true;
        // 获取触摸点在世界坐标的位置
        var touchWorldPos = event.touch.getLocation();
        // 转换为节点父节点的本地坐标
        var nodeParent = this.node.parent;
        var touchLocalPos = nodeParent.convertToNodeSpaceAR(touchWorldPos);
        // 计算触摸点与节点原点的偏移
        this._touchOffset = touchLocalPos.sub(new cc.Vec2(this.node.position.x, this.node.position.y));
        // 可选：将节点置顶，避免被遮挡
        // this.node.setSiblingIndex(nodeParent.children.length - 1);
    };
    Draggable.prototype._onTouchMove = function (event) {
        if (!this._isDragging)
            return;
        var touchWorldPos = event.touch.getLocation();
        var nodeParent = this.node.parent;
        var touchLocalPos = nodeParent.convertToNodeSpaceAR(touchWorldPos);
        // 用触摸位置减去偏移量，更新节点位置
        var newPos = touchLocalPos.sub(this._touchOffset);
        this.node.setPosition(newPos.x, newPos.y);
        cc.systemEvent.emit('drawline');
    };
    Draggable.prototype._onTouchEnd = function (event) {
        this._isDragging = false;
        cc.systemEvent.emit('drawline');
        cc.systemEvent.emit('arrowMove');
    };
    Draggable = __decorate([
        ccclass('Draggable')
    ], Draggable);
    return Draggable;
}(cc.Component));
exports.default = Draggable;

cc._RF.pop();