Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

3.2 KB
Raw Blame
/**
 * Native Windows tablet packet unpacking and WebView zoom coordinate helpers.
 *
 * Native raster/mask brush strokes are consumed directly in Rust `ink-engine` via its lock-free
 * WinTab / Windows Ink SPSC ring (zero WebView IPC). Non-native tools and vector layers use
 * standard DOM PointerEvents without a separate legacy JS tablet channel.
 */

import { getActiveNativeWebviewZoom } from './interfaceScale';

export type TabletInputMode = 'auto' | 'wintab' | 'windows-ink' | 'off';

export interface NativeTabletStatus {
  active: boolean;
  backend: 'wintab' | 'windows-ink' | 'none';
  wintabAvailable: boolean;
  windowsInkAvailable: boolean;
  deviceName: string | null;
  maxPressure: number;
}

export interface NativeTabletSample {
  clientX: number;
  clientY: number;
  pressure: number;
  tiltX: number;
  tiltY: number;
  twist: number;
  timestamp: number;
  buttons: number; // bit 0=tip, 1=barrel1, 2=barrel2, 3=eraser
  phase: 0 | 1 | 2 | 3 | 4; // 0=hover, 1=down, 2=move/drag, 3=up, 4=leave
  backend: 1 | 2; // 1=wintab, 2=windows-ink
}

const SAMPLE_WIRE_BYTES = 36;

const DEFAULT_TABLET_STATUS: NativeTabletStatus = {
  active: false,
  backend: 'none',
  wintabAvailable: false,
  windowsInkAvailable: false,
  deviceName: null,
  maxPressure: 1024,
};

/**
 * Unpacks a binary batch frame emitted by `src-tauri/src/tablet_input.rs`.
 */
export function unpackTabletSampleBatch(
  raw: ArrayBuffer | Uint8Array | number[]
): NativeTabletSample[] {
  const bytes =
    raw instanceof Uint8Array
      ? raw
      : raw instanceof ArrayBuffer
        ? new Uint8Array(raw)
        : new Uint8Array(raw);

  if (bytes.byteLength < 4) return [];
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
  const count = view.getUint32(0, true);
  const maxCount = Math.floor((bytes.byteLength - 4) / SAMPLE_WIRE_BYTES);
  const n = Math.min(count, maxCount);
  const out: NativeTabletSample[] = new Array(n);

  let off = 4;
  for (let i = 0; i < n; i++) {
    out[i] = {
      clientX: view.getFloat32(off, true),
      clientY: view.getFloat32(off + 4, true),
      pressure: view.getFloat32(off + 8, true),
      tiltX: view.getFloat32(off + 12, true),
      tiltY: view.getFloat32(off + 16, true),
      twist: view.getFloat32(off + 20, true),
      timestamp: view.getFloat64(off + 24, true),
      buttons: view.getUint16(off + 32, true),
      phase: bytes[off + 34] as NativeTabletSample['phase'],
      backend: bytes[off + 35] as NativeTabletSample['backend'],
    };
    off += SAMPLE_WIRE_BYTES;
  }
  return out;
}

export function getCachedTabletStatus(): NativeTabletStatus {
  return DEFAULT_TABLET_STATUS;
}

/**
 * Converts OS logical client coordinates into zoomed WebView CSS pixel coordinates when
 * native WebView interface scaling is active.
 */
export function adjustTabletSamplesForWebviewZoom(
  samples: NativeTabletSample[],
  zoomFactor: number = getActiveNativeWebviewZoom()
): NativeTabletSample[] {
  if (!Number.isFinite(zoomFactor) || zoomFactor <= 0 || Math.abs(zoomFactor - 1) <= 1e-4) {
    return samples;
  }
  for (let i = 0; i < samples.length; i++) {
    samples[i].clientX /= zoomFactor;
    samples[i].clientY /= zoomFactor;
  }
  return samples;
}