"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StylusEntity = void 0;
exports.makeStylusEntity = makeStylusEntity;
const core_1 = require("@vived/core");
const Transform3D_1 = require("../../Transform3D");
class StylusEntity extends core_1.AppObjectEntity {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(StylusEntity.type, appObjects);
    }
}
exports.StylusEntity = StylusEntity;
StylusEntity.type = "StylusEntity";
StylusEntity.clickMovementThreshold = 0.32;
StylusEntity.clickTimeThresholdMS = 350;
function makeStylusEntity(appObj) {
    return new StylusImp(appObj);
}
class StylusImp extends StylusEntity {
    get pickRay() {
        return core_1.ParametricLine.FromPointDirection(this.transform.worldPosition, this.transform.localDirectionToWorld(core_1.Vector3.Forward()));
    }
    get isInUse() {
        return this.memoizedIsInUse.val;
    }
    checkIfStylusIsInUse(worldMatrix) {
        const m = worldMatrix.m;
        if (m[1] === 0 &&
            m[2] === 0 &&
            m[3] === 0 &&
            m[4] === 0 &&
            m[7] === 0 &&
            m[8] === 0 &&
            m[11] === 0 &&
            m[12] === 0 &&
            m[15] === 1) {
            this.memoizedIsInUse.val = false;
        }
        else {
            this.memoizedIsInUse.val = true;
        }
    }
    constructor(appObject) {
        super(appObject, StylusEntity.type);
        this.controllers = [];
        this.buttonStates = [false, false, false];
        this.pressPosition = core_1.Vector3.Zero();
        this.pressTime = 0;
        this.registerController = (controller) => {
            this.controllers.push(controller);
        };
        this.updateButtonState = (button, isPressed) => {
            if (this.buttonStates[button] === isPressed)
                return;
            this.controllers.forEach((controller) => {
                if (isPressed) {
                    if (controller.onButtonDown) {
                        controller.onButtonDown(button);
                    }
                    this.lassPressedButton = button;
                    this.pressPosition = this.transform.localPosition;
                    this.pressTime = Date.now();
                }
                else {
                    if (controller.onButtonUp) {
                        controller.onButtonUp(button);
                    }
                    if (this.isClick(button) && controller.onButtonClick) {
                        controller.onButtonClick(button);
                    }
                }
            });
            this.buttonStates[button] = isPressed;
            this.notifyOnChange();
        };
        this.getButtonState = (button) => {
            if (button < 0 || button >= this.buttonStates.length) {
                return false;
            }
            else {
                return this.buttonStates[button];
            }
        };
        this.isClick = (button) => {
            const buttonIsCorrect = button === this.lassPressedButton;
            const movement = core_1.Vector3.Subtract(this.transform.localPosition, this.pressPosition).magnitude;
            const movementIsSmallEnough = movement < StylusEntity.clickMovementThreshold;
            const passedTime = Date.now() - this.pressTime;
            const pressTimeIsShortEnough = passedTime < StylusEntity.clickTimeThresholdMS;
            if (buttonIsCorrect && movementIsSmallEnough && pressTimeIsShortEnough) {
                return true;
            }
            else {
                return false;
            }
        };
        this.onTransformUpdate = () => {
            this.checkIfStylusIsInUse(this.transform.worldMatrix);
            this.controllers.forEach((controller) => {
                if (controller.onStylusMove) {
                    controller.onStylusMove(this.transform);
                }
            });
            this.notifyOnChange();
        };
        this.memoizedIsInUse = new core_1.MemoizedBoolean(false, this.notifyOnChange);
        this.transform = Transform3D_1.Transform3D.addIfMissing(appObject);
        this.transform.addChangeObserver(this.onTransformUpdate);
    }
}
//# sourceMappingURL=StylusEntity.js.map