"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StylusPressOnAppObjectSC = void 0;
const core_1 = require("@vived/core");
const Stylus_1 = require("../../Stylus");
const PressAppObjectUC_1 = require("../UC/PressAppObjectUC");
class StylusPressOnAppObjectSC extends core_1.AppObjectController {
    constructor(appObject) {
        super(appObject, StylusPressOnAppObjectSC.type);
        this.startPosition = core_1.Vector3.Zero();
        this.endPosition = core_1.Vector3.Zero();
        this.pressedID = null;
        this.isPressed = false;
        this.allow = (button) => {
            return true;
        };
        this.onButtonDown = (button) => {
            if (!this.stylus)
                return;
            if (!this.pressUC)
                return;
            if (!this.allow(button))
                return;
            this.startPosition = this.stylus.transform.localPosition;
            this.endPosition = this.startPosition;
            this.isPressed = true;
            this.pressedID = this.pressUC?.tryPressAppObject(this.stylus.pickRay);
        };
        this.onButtonUp = (button) => {
            this.isPressed = false;
            if (button !== 0)
                return;
            if (!this.pressUC)
                return;
            // Set pressedID to null
            this.pressUC.pressAppObject(this.pressedID, this.isPressed);
        };
        this.onStylusMove = () => {
            if (!this.pressUC) {
                return;
            }
            if (this.isPressed) {
                this.endPosition = this.stylus?.transform.localPosition;
                const movement = core_1.Vector3.Subtract(this.startPosition, this.endPosition)
                    .magnitude;
                if (movement > StylusPressOnAppObjectSC.maxTapDistancePx) {
                    // Set pressedID to null
                    this.pressUC.pressAppObject(null, this.isPressed);
                }
            }
        };
        this.stylus = Stylus_1.StylusEntity.get(this.appObjects);
        if (!this.stylus) {
            this.error("Unable to find StylusEntity");
            return;
        }
        this.pressUC = appObject.getComponent(PressAppObjectUC_1.PressAppObjectUC.type);
        if (!this.pressUC) {
            this.error("Unable to get PressAppObjectUC from the App Object");
            return;
        }
        this.stylus.registerController(this);
    }
}
exports.StylusPressOnAppObjectSC = StylusPressOnAppObjectSC;
StylusPressOnAppObjectSC.type = "StylusPressOnAppObjectSC";
StylusPressOnAppObjectSC.maxTapDistancePx = 5;
//# sourceMappingURL=StylusPressOnAppObjectSC.js.map