"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StylusFromGamepadHook = void 0;
exports.makeStylusFromGamepadHook = makeStylusFromGamepadHook;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Stylus_1 = require("../../Stylus");
const XR_1 = require("../../XR");
class StylusFromGamepadHook extends core_1.AppObjectUC {
}
exports.StylusFromGamepadHook = StylusFromGamepadHook;
StylusFromGamepadHook.type = "StylusFromGamepadHook";
function makeStylusFromGamepadHook(appObj) {
    return new StylusFromGamepadUCImp(appObj);
}
class StylusFromGamepadUCImp extends StylusFromGamepadHook {
    get stylus() {
        return this.getCachedSingleton(Stylus_1.StylusEntity.type);
    }
    processStylusPose(pose) {
        const matrix = core_2.Matrix.FromFloat32Array(pose.transform.matrix);
        const flippedMatrix = core_2.Matrix.FlipTransformMatrixHand(matrix);
        if (this.stylus) {
            this.stylus.transform.localMatrix = flippedMatrix;
        }
    }
    processButtons(stylusSource) {
        const gamepad = stylusSource.gamepad;
        if (!gamepad) {
            this.warn("No gamepad");
            return;
        }
        const presses = [false, false, false];
        presses[0] = gamepad.buttons[0]?.pressed;
        presses[1] = gamepad.buttons[3]?.pressed;
        presses[2] = gamepad.buttons[4]?.pressed;
        presses.forEach((press, i) => {
            if (this.lastPresses[i] !== press) {
                this.lastPresses[i] = press;
                this.stylus?.updateButtonState(i, press);
            }
        });
    }
    constructor(appObject) {
        super(appObject, StylusFromGamepadHook.type);
        this.lastPresses = [false, false, false];
        this.onNewXRFrame = () => {
            if (!this.xrEntity || !this.stylus)
                return;
            const frame = this.xrEntity.lastXRFrame;
            if (!frame)
                return;
            let stylusSource = null;
            if (frame &&
                frame.session.inputSources &&
                frame.session.inputSources[0] &&
                frame.session.inputSources[0].gripSpace) {
                stylusSource = frame.session.inputSources[0];
            }
            if (stylusSource === null) {
                console.log("No stylus source");
                return;
            }
            this.processButtons(stylusSource);
            if (!this.xrEntity.xrReferenceSpace) {
                this.warn("No reference space");
                return;
            }
            const stylusPose = frame.getPose(stylusSource.targetRaySpace, this.xrEntity.xrReferenceSpace);
            if (stylusPose)
                this.processStylusPose(stylusPose);
        };
        const xrEntity = appObject.getComponent(XR_1.XREntity.type);
        if (!xrEntity) {
            this.error("Added to an App Object that does not have XREntity");
            return;
        }
        this.xrEntity = xrEntity;
        this.xrEntity.addChangeObserver(this.onNewXRFrame);
    }
}
//# sourceMappingURL=StylusFromGamepadHook.js.map