Commit 7eb9d211 authored by vertighel's avatar vertighel
Browse files

guider-panel.js: estrae computeTargetCoords in modulo condiviso



Semplifica la logica center/stick/custom duplicata tra resolveTargetCoords
e onTargetChange in un'unica funzione pura, testabile e riusabile.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent 7ba340a7
Loading
Loading
Loading
Loading
+21 −28
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
import { showToast }           from './ui.js';
import { get as getViewer }    from './viewer/viewer-registry.js';
import { resolveValue }        from './ui-core.js';
import { computeTargetCoords } from './guider-target-coords.js';

// --- Constants ---

@@ -47,19 +48,15 @@ function onCameraChange(panelId, camera) { // eslint-disable-line no-unused-var
 * @param {object} state  - mutable per-panel pick state
 */
function resolveTargetCoords(panelId, target, state) {
    if (target === 'center') {
    const { width, height } = state.viewer?.getImageSize() ?? {};
        state.targetX = (width && height) ? Math.round(width  / 2) : null;
        state.targetY = (width && height) ? Math.round(height / 2) : null;
    } else if (target === 'stick') {
        state.targetX = state.centerX;
        state.targetY = state.centerY;
    } else {
    const tx = parseFloat(document.getElementById(`guider-tx-${panelId}`)?.value);
    const ty = parseFloat(document.getElementById(`guider-ty-${panelId}`)?.value);
        state.targetX = isNaN(tx) ? null : tx;
        state.targetY = isNaN(ty) ? null : ty;
    }

    const { x, y } = computeTargetCoords(target, {
        width, height, centerX: state.centerX, centerY: state.centerY, tx, ty,
    });
    state.targetX = x;
    state.targetY = y;
}

/**
@@ -81,19 +78,15 @@ function onTargetChange(panelId, target) {
    tx.disabled = !custom;
    ty.disabled = !custom;

    if (target === 'center') {
    if (target === 'center' || target === 'stick') {
        const viewer = getViewerForPanel(panelId);
        const { width, height } = viewer?.getImageSize() ?? { width: 0, height: 0 };
        if (width && height) {
            tx.value = Math.round(width  / 2);
            ty.value = Math.round(height / 2);
        } else {
            tx.value = '';
            ty.value = '';
        }
    } else if (target === 'stick') {
        tx.value = document.getElementById(`guider-cx-${panelId}`)?.value ?? '';
        ty.value = document.getElementById(`guider-cy-${panelId}`)?.value ?? '';
        const { width, height } = viewer?.getImageSize() ?? {};
        const centerX = parseFloat(document.getElementById(`guider-cx-${panelId}`)?.value);
        const centerY = parseFloat(document.getElementById(`guider-cy-${panelId}`)?.value);

        const { x, y } = computeTargetCoords(target, { width, height, centerX, centerY });
        tx.value = x !== null ? x : '';
        ty.value = y !== null ? y : '';
    } else if (!custom) {
        tx.value = '';
        ty.value = '';
+38 −0
Original line number Diff line number Diff line
// guider-target-coords.js
// Pure computation of guider target coordinates from the target mode.
// Shared by guider-panel.js so the center/stick/custom rules live in one place.

/**
 * Resolves target (X, Y) for a given target mode.
 *
 *   center → image centre (width/2, height/2), rounded; null if size unknown
 *   stick  → the supplied centerX/centerY, unchanged
 *   custom (default) → the supplied tx/ty, unchanged (null if NaN)
 *
 * @param {string} target - 'center' | 'stick' | 'custom' | other
 * @param {object} ctx
 * @param {number} [ctx.width]
 * @param {number} [ctx.height]
 * @param {number} [ctx.centerX]
 * @param {number} [ctx.centerY]
 * @param {number} [ctx.tx]
 * @param {number} [ctx.ty]
 * @returns {{x: number|null, y: number|null}}
 */
export function computeTargetCoords(target, { width, height, centerX, centerY, tx, ty } = {}) {
    if (target === 'center') {
        return (width && height)
            ? { x: Math.round(width / 2), y: Math.round(height / 2) }
            : { x: null, y: null };
    }
    if (target === 'stick') {
        return {
            x: Number.isFinite(centerX) ? centerX : null,
            y: Number.isFinite(centerY) ? centerY : null,
        };
    }
    return {
        x: isNaN(tx) ? null : tx,
        y: isNaN(ty) ? null : ty,
    };
}