"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TouchOrbit = void 0;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Tools_1 = require("../../Tools");
const TouchController_1 = require("../../TouchController");
const OrbitSceneRoot_1 = require("../UC/OrbitSceneRoot");
class TouchOrbit extends core_1.AppObjectController {
    get orbitSceneRoot() {
        return this.getCachedLocalComponent(OrbitSceneRoot_1.OrbitSceneRootUC.type);
    }
    get toolRepo() {
        return this.getCachedSingleton(Tools_1.ToolRepository.type);
    }
    constructor(appObject) {
        super(appObject, TouchOrbit.type);
        this.isOrbiting = false;
        this.ORBIT_FINGER_DISTANCE_PX = 80;
        this.allow = (e) => {
            const orbitTool = this.toolRepo?.getTool("ORBIT");
            if (!orbitTool)
                return false;
            const orbitToolIsActive = orbitTool.state === Tools_1.ToolState.ACTIVE;
            if (e.touches.length === 1 && orbitToolIsActive) {
                return true;
            }
            else if (e.touches.length === 2) {
                const dist = core_2.Vector2.Subtract(e.touches[0].position, e.touches[1].position).magnitued;
                if (dist < this.ORBIT_FINGER_DISTANCE_PX) {
                    return true;
                }
            }
            return false;
        };
        this.onTouchStart = (e) => {
            if (!this.allow(e)) {
                this.isOrbiting = false;
                this.orbitSceneRoot?.cancelOrbit();
                return;
            }
            this.orbitSceneRoot?.initializeOrbit(e.touchCenter);
            this.isOrbiting = true;
        };
        this.onTouchMove = (e) => {
            if (!this.isOrbiting)
                return;
            this.orbitSceneRoot?.doOrbit(e.touchCenter);
        };
        this.onTouchEnd = () => {
            this.isOrbiting = false;
            this.orbitSceneRoot?.finishOrbit();
        };
        const appObjects = appObject.appObjectRepo;
        const touchInput = TouchController_1.TouchInputEntity.get(appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
exports.TouchOrbit = TouchOrbit;
TouchOrbit.type = "TouchOrbit";
//# sourceMappingURL=TouchOrbit.js.map