import { AppObjectController, Vector3 } from "@vived/core";
import { StylusEntity } from "../../Stylus";
import { PressAppObjectUC } from "../UC/PressAppObjectUC";
export class StylusPressOnAppObjectSC extends AppObjectController {
    constructor(appObject) {
        super(appObject, StylusPressOnAppObjectSC.type);
        this.startPosition = Vector3.Zero();
        this.endPosition = 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 = Vector3.Subtract(this.startPosition, this.endPosition)
                    .magnitude;
                if (movement > StylusPressOnAppObjectSC.maxTapDistancePx) {
                    // Set pressedID to null
                    this.pressUC.pressAppObject(null, this.isPressed);
                }
            }
        };
        this.stylus = StylusEntity.get(this.appObjects);
        if (!this.stylus) {
            this.error("Unable to find StylusEntity");
            return;
        }
        this.pressUC = appObject.getComponent(PressAppObjectUC.type);
        if (!this.pressUC) {
            this.error("Unable to get PressAppObjectUC from the App Object");
            return;
        }
        this.stylus.registerController(this);
    }
}
StylusPressOnAppObjectSC.type = "StylusPressOnAppObjectSC";
StylusPressOnAppObjectSC.maxTapDistancePx = 5;
//# sourceMappingURL=StylusPressOnAppObjectSC.js.map