"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MousePressOnAppObject = void 0;
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const MouseInput_1 = require("../../MouseInput");
const ZSpaceEntity_1 = require("../../zSpace/Entities/ZSpaceEntity");
const PressAppObjectUC_1 = require("../UC/PressAppObjectUC");
class MousePressOnAppObject extends core_1.AppObjectController {
    constructor(appObject) {
        super(appObject, MousePressOnAppObject.type);
        this.zSpaceEntity = undefined;
        this.startPosition = core_1.Vector2.Zero();
        this.endPosition = core_1.Vector2.Zero();
        this.pressedID = null;
        this.isPressed = false;
        this.allow = (e) => {
            return true;
        };
        this.onDown = (e) => {
            if (e.button !== 0) {
                return;
            }
            if (!this.pressUC) {
                return;
            }
            if (this.zSpaceEntity && this.zSpaceEntity.emulate)
                return;
            if (!this.allow(e))
                return;
            this.startPosition = e.position;
            this.endPosition = this.startPosition;
            this.isPressed = true;
            const ray = (0, Babylon_1.pixelToSceneRay)(e.position, this.appObjects);
            this.pressedID = this.pressUC?.tryPressAppObject(ray);
        };
        this.onUp = (e) => {
            this.isPressed = false;
            if (e.button !== 0) {
                return;
            }
            if (!this.pressUC) {
                return;
            }
            // Set pressedID to null
            this.pressUC.pressAppObject(this.pressedID, this.isPressed);
        };
        this.onMove = (e) => {
            if (!this.pressUC) {
                return;
            }
            if (this.isPressed) {
                this.endPosition = e.position;
                const movement = core_1.Vector2.Subtract(this.startPosition, this.endPosition)
                    .magnitued;
                if (movement > MousePressOnAppObject.maxTapDistancePx) {
                    // Set pressedID to null
                    this.pressUC.pressAppObject(null, this.isPressed);
                }
            }
        };
        this.zSpaceEntity = ZSpaceEntity_1.ZSpaceEntity.get(this.appObjects);
        this.pressUC = appObject.getComponent(PressAppObjectUC_1.PressAppObjectUC.type);
        if (!this.pressUC) {
            console.warn("MousePress has been added to an App Object that does not have a PressAppObjectUC. Add a PressAppObjectUC before adding the Controller.");
            return;
        }
        const mouseInput = MouseInput_1.MouseInputEntity.get(this.appObjects);
        if (mouseInput) {
            mouseInput.registerController(this);
        }
        else {
            this.warn("Unable to find MouseInputEntity");
        }
    }
}
exports.MousePressOnAppObject = MousePressOnAppObject;
MousePressOnAppObject.type = "MousePressOnAppObject";
MousePressOnAppObject.maxTapDistancePx = 5;
//# sourceMappingURL=MousePressOnAppObject.js.map