"use strict";
cc._RF.push(module, '79777kgUFNH7IobLwg8lArh', 'MapControl');
// Script/MapControl.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 (b.hasOwnProperty(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;
};
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var MapControl = /** @class */ (function (_super) {
    __extends(MapControl, _super);
    function MapControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.map = null;
        _this.camera = null;
        _this.mapContainer = null;
        _this.scaleTime = null;
        _this.maxScale = 1.3;
        _this.minScale = 0.5;
        _this.moveOffset = 0;
        _this.increaseRate = 1000;
        _this.operLock = false; // 操作锁
        _this.isMoving = false; // 是否拖动地图flag
        _this.mapTouchList = []; // 触摸点容器
        _this.singleTouchCb = null; // 单点回调函数
        _this.zoomRatio = 1;
        return _this;
    }
    MapControl.prototype.start = function () {
        if (!this.map)
            this.map = this.node;
        //拥有同一个父级 zIndex才会生效
        this.map.getChildByName('block2').zIndex = 100;
        this.camera = this.node.parent.parent.getChildByName('Main Camera');
        if (!this.mapContainer)
            this.mapContainer = this.node.parent;
        this.addEvent();
    };
    MapControl.prototype.addEvent = function () {
        var _this = this;
        this.map.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
            console.log('TOUCH_MOVE');
            if (_this.operLock)
                return; // 如果触摸操作暂时锁定则不响应
            var touches = event['getTouches'](); // 获取所有触摸点
            if (touches.length >= 2) {
                // 如果容器内触摸点数量超过1则为多点触摸，此处暂时不处理三点及以上的触摸点，可以根据需求来处理
                _this.isMoving = true;
                _this.dealTouchData(touches, _this.camera);
            }
            else if (touches.length === 1) {
                // sigle touch
                var touch = touches[0];
                var startPos = cc.v2(touch.getStartLocation());
                var nowPos = cc.v2(touch.getLocation());
                // 有些设备单点过于灵敏，单点操作会触发TOUCH_MOVE回调，在这里作误差值判断
                if ((Math.abs(nowPos.x - startPos.x) <= _this.moveOffset
                    || Math.abs(nowPos.y - startPos.y) <= _this.moveOffset)
                    && !_this.isMoving) {
                    return;
                }
                var dir = cc.v2(touch.getDelta());
                _this.isMoving = true;
                _this.dealMove(dir, _this.map, _this.camera);
            }
        }, this);
        this.map.on(cc.Node.EventType.TOUCH_END, function (event) {
            if (_this.operLock)
                return cc.log('operate is lock');
            // 需要自行管理touches队列, cocos 的多点触控并不可靠
            if (_this.mapTouchList.length < 2) {
                if (!_this.isMoving) {
                    var worldPos = cc.v2(event['getLocation']());
                    var nodePos = _this.map.convertToNodeSpaceAR(worldPos);
                    _this.dealSelect(nodePos);
                }
                _this.isMoving = false; // 当容器中仅剩最后一个触摸点时讲移动flag还原
            }
            ;
            _this.removeTouchFromContent(event, _this.mapTouchList);
        }, this);
        this.map.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
            if (_this.operLock)
                return;
            if (_this.mapTouchList.length < 2) { // 当容器中仅剩最后一个触摸点时讲移动flag还原
                _this.isMoving = false;
            }
            ;
            _this.removeTouchFromContent(event, _this.mapTouchList);
        }, this);
        this.map.on(cc.Node.EventType.MOUSE_WHEEL, function (event) {
            if (_this.operLock)
                return;
            var location = event['getLocation']();
            var worldPos = cc.v2(location.x, location.y);
            var scrollDelta = event['getScrollY']();
            var scale = (_this.camera.scale + (scrollDelta / _this.increaseRate));
            var target = _this.camera;
            var pos = target.convertToNodeSpaceAR(worldPos);
            _this.smoothOperate(target, pos, scale);
        }, this);
    };
    MapControl.prototype.removeTouchFromContent = function (event, content) {
        var eventToucheIDs = event['getTouches']().map(function (v) { return v.getID(); });
        for (var len = content.length, i = len - 1; i >= 0; --i) {
            if (eventToucheIDs.indexOf(content[i].id) > -1)
                content.splice(i, 1); // 删除触摸
        }
    };
    MapControl.prototype.smoothOperate = function (target, pos, scale) {
        var scX = scale;
        // 当前缩放值与原来缩放值之差
        var disScale = scX - target.scaleX;
        // 当前点击的坐标与缩放值差像乘
        var gapPos = pos.scale(cc.v2(disScale, disScale));
        // 当前node坐标位置减去点击 点击坐标和缩放值的值
        var mapPos = target.getPosition().sub(cc.v2(gapPos.x, gapPos.y));
        // 放大缩小
        if (!this.isOutRangeScale(scale)) {
            scale = (scale * 10 | 0) / 10;
            console.log(scale);
            target.scale = scale;
            target.getComponent(cc.Camera).zoomRatio = scale;
            this.dealScalePos(mapPos, target);
        }
    };
    MapControl.prototype.dealTouchData = function (touches, target) {
        var touch1 = touches[0].touch;
        var touch2 = touches[1].touch;
        var delta1 = cc.v2(touch1.getDelta());
        var delta2 = cc.v2(touch2.getDelta());
        var touchPoint1 = target.convertToNodeSpaceAR(cc.v2(touch1.getLocation()));
        var touchPoint2 = target.convertToNodeSpaceAR(cc.v2(touch2.getLocation()));
        var distance = touchPoint1.sub(touchPoint2);
        var delta = delta1.sub(delta2);
        var scale = 1;
        if (Math.abs(distance.x) > Math.abs(distance.y)) {
            scale = (distance.x + delta.x) / distance.x * target.scaleX;
        }
        else {
            scale = (distance.y + delta.y) / distance.y * target.scaleY;
        }
        var pos = touchPoint2.add(cc.v2(distance.x / 2, distance.y / 2));
        this.smoothOperate(target, pos, scale);
    };
    MapControl.prototype.isOutRangeScale = function (scale) {
        return (scale > this.maxScale || scale < this.minScale);
    };
    MapControl.prototype.dealScaleRange = function (scale) {
        if (scale > this.maxScale) {
            return this.maxScale;
        }
        else if (scale < this.minScale) {
            return this.minScale;
        }
        else {
            return scale;
        }
    };
    MapControl.prototype.dealScalePos = function (pos, target) {
        var worldPos = this.map.convertToWorldSpaceAR(pos);
        var nodePos = target.convertToNodeSpaceAR(worldPos);
        var edge = this.calculateEdge(this.map, nodePos);
        edge.lBorderDelta > 0 && (pos.x -= edge.lBorderDelta);
        edge.rBorderDelta > 0 && (pos.x += edge.rBorderDelta);
        edge.uBorderDelta > 0 && (pos.y += edge.uBorderDelta);
        edge.dBorderDelta > 0 && (pos.y -= edge.dBorderDelta);
        // if (0.8 === target.scale) pos = cc.Vec2.ZERO;
        this.camera.position = pos;
        // target.x = pos.x
        // target.y = pos.y
    };
    MapControl.prototype.dealMove = function (dir, map, carema) {
        var worldPos = map.convertToWorldSpaceAR(dir);
        var nodePos = carema.convertToNodeSpaceAR(worldPos);
        var edge = this.calculateEdge(map, nodePos);
        console.log(carema.x);
        console.log(-dir.x);
        // nodePos.x += dir.x;
        // nodePos.y += dir.y;
        if (edge.lBorderDelta <= 0 && edge.rBorderDelta <= 0) {
            carema.x += (-dir.x);
        }
        if (edge.uBorderDelta <= 0 && edge.dBorderDelta <= 0) {
            carema.y += (-dir.y);
        }
    };
    MapControl.prototype.setSinglTouchCb = function (cb) {
        this.singleTouchCb = cb;
    };
    MapControl.prototype.dealSelect = function (nodePos) {
        cc.log("click map on cc.v2(" + nodePos.x + ", " + nodePos.y + ")");
        // do sth
        if (this.singleTouchCb)
            this.singleTouchCb(nodePos);
    };
    // 计算map的四条边距离容器的距离
    MapControl.prototype.calculateEdge = function (target, nodePos) {
        var realWidth = target.width * this.camera.scaleX;
        var realHeight = target.height * this.camera.scaleY;
        var lBorderDelta = (nodePos.x - realWidth / 2) + cc.winSize.width / 2;
        var rBorderDelta = cc.winSize.width / 2 - (realWidth / 2 + nodePos.x); // <= 0 safe
        var uBorderDelta = cc.winSize.height / 2 - (realHeight / 2 + nodePos.y);
        var dBorderDelta = (nodePos.y - realHeight / 2) + cc.winSize.height / 2;
        return { lBorderDelta: lBorderDelta, rBorderDelta: rBorderDelta, uBorderDelta: uBorderDelta, dBorderDelta: dBorderDelta };
    };
    __decorate([
        property({
            type: cc.Node,
            tooltip: '目标节点，当mapContainer为null时，需要将该组件挂在目标节点，该节点默认为挂载节点'
        })
    ], MapControl.prototype, "map", void 0);
    __decorate([
        property({
            type: cc.Node,
            tooltip: '当该节点为空时，需要将改组件挂在map节点上，该节点默认为挂载节点的父节点'
        })
    ], MapControl.prototype, "mapContainer", void 0);
    __decorate([
        property(cc.Label)
    ], MapControl.prototype, "scaleTime", void 0);
    __decorate([
        property({
            tooltip: '图片缩放最大scale'
        })
    ], MapControl.prototype, "maxScale", void 0);
    __decorate([
        property({
            tooltip: '图片缩放最小scale'
        })
    ], MapControl.prototype, "minScale", void 0);
    __decorate([
        property({
            tooltip: '单点触摸容忍误差'
        })
    ], MapControl.prototype, "moveOffset", void 0);
    __decorate([
        property({
            tooltip: '滚轮缩放比率'
        })
    ], MapControl.prototype, "increaseRate", void 0);
    MapControl = __decorate([
        ccclass
    ], MapControl);
    return MapControl;
}(cc.Component));
module.exports = MapControl;

cc._RF.pop();