"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MouseOrbit = void 0;
const core_1 = require("@vived/core");
const MouseInput_1 = require("../../MouseInput");
const Tools_1 = require("../../Tools");
const OrbitSceneRoot_1 = require("../UC/OrbitSceneRoot");
class MouseOrbit 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, MouseOrbit.type);
        this.isOrbiting = false;
        this.allow = (e) => {
            const orbitTool = this.toolRepo?.getTool("ORBIT");
            if (!orbitTool)
                return false;
            const orbitToolIsActive = orbitTool.state === Tools_1.ToolState.ACTIVE;
            if (e.button === 0 && orbitToolIsActive) {
                return true;
            }
            else if (e.button === 2) {
                return true;
            }
            return false;
        };
        this.onDown = (e) => {
            this.isOrbiting = false;
            if (!this.orbitSceneRoot)
                return;
            if (!this.allow(e))
                return;
            const pixelPosition = e.position;
            this.orbitSceneRoot.initializeOrbit(pixelPosition);
            this.isOrbiting = true;
        };
        this.onMove = (e) => {
            if (!this.orbitSceneRoot)
                return;
            if (!this.isOrbiting)
                return;
            const currentPixelPos = e.position;
            this.orbitSceneRoot.doOrbit(currentPixelPos);
        };
        this.onUp = () => {
            if (!this.orbitSceneRoot)
                return;
            this.orbitSceneRoot.finishOrbit();
            this.isOrbiting = false;
        };
        const mouseInput = MouseInput_1.MouseInputEntity.get(this.appObjects);
        if (mouseInput) {
            mouseInput.registerController(this);
        }
        else {
            this.warn("Unable to find MouseInputEntity");
        }
    }
}
exports.MouseOrbit = MouseOrbit;
MouseOrbit.type = "MouseOrbit";
//# sourceMappingURL=MouseOrbit.js.map