Skip to content

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

7.9 KB
Raw Blame
import { BrushCategory, BrushPresetId, BrushSettings, CspBrush } from '../types/editor';
import {
  ALL_BUILTIN_BRUSHES,
  getBuiltinBrushConfig,
  isDecorationCategory,
  isDecorationPresetId,
} from './presetBrushes';
import {
  DEFAULT_ERASER_SETTINGS,
  ERASER_PRESET_ID,
  NEUTRAL_BRUSH_DEFAULTS,
  getEffectiveBrushSettings,
  getSavedBrushSettings,
  migrateLegacyPresetId,
  saveActiveDecorationPresetId,
  saveActivePresetId,
} from './brushPresetManager';
import { prepareBrushSettingsForCsp } from './brushStorage';
import { toolStoreActions } from '../stores';

export const DND_BRUSH_MIME = 'application/x-inkstudio-brush';
export const DND_QUICK_BRUSH_REORDER_MIME = 'application/x-inkstudio-quick-brush-reorder';

export type QuickBrushCategory = BrushCategory | 'custom' | 'eraser';

export interface ResolvedQuickBrush {
  id: BrushPresetId;
  name: string;
  category: QuickBrushCategory;
  desc?: string;
  tipDataUrl?: string;
  isColorTip?: boolean;
  hardness: number;
  thickness: number;
  thicknessVertical: boolean;
  angle: number;
  isEraser?: boolean;
  isCustom?: boolean;
  customBrush?: CspBrush;
  previewSettings?: Partial<BrushSettings>;
}

export interface CategoryAccentStyle {
  barColor: string | null;
  cardTint: string | null;
  label: string;
}

export function getCategoryAccentStyle(category: QuickBrushCategory): CategoryAccentStyle {
  switch (category) {
    case 'pen':
      return {
        barColor: null,
        cardTint: null,
        label: 'Pen',
      };
    case 'sketch':
      return {
        barColor: '#d9a45b',
        cardTint: 'rgba(217, 164, 91, 0.08)',
        label: 'Sketch',
      };
    case 'brush':
      return {
        barColor: '#6ea8d8',
        cardTint: 'rgba(110, 168, 216, 0.08)',
        label: 'Brush',
      };
    case 'blend':
      return {
        barColor: '#9b87c4',
        cardTint: 'rgba(155, 135, 196, 0.14)',
        label: 'Blend',
      };
    case 'natural-paint':
      return {
        barColor: '#5ab4a8',
        cardTint: 'rgba(90, 180, 168, 0.15)',
        label: 'Natural Paint',
      };
    case 'texture-fx':
      return {
        barColor: '#b388eb',
        cardTint: 'rgba(179, 136, 235, 0.14)',
        label: 'Texture & FX',
      };
    case 'deco-background':
      return {
        barColor: '#34d399',
        cardTint: 'rgba(52, 211, 153, 0.12)',
        label: 'Background',
      };
    case 'deco-clothing':
      return {
        barColor: '#f472b6',
        cardTint: 'rgba(244, 114, 182, 0.12)',
        label: 'Clothing',
      };
    case 'deco-effect':
      return {
        barColor: '#a78bfa',
        cardTint: 'rgba(167, 139, 250, 0.14)',
        label: 'Effect',
      };
    case 'eraser':
      return {
        barColor: '#e58aa3',
        cardTint: 'rgba(229, 138, 163, 0.11)',
        label: 'Eraser',
      };
    case 'custom':
    default:
      return {
        barColor: '#8d99ae',
        cardTint: 'rgba(141, 153, 174, 0.10)',
        label: 'Imported',
      };
  }
}

export function resolveQuickBrush(
  presetId: BrushPresetId,
  customBrushes: CspBrush[]
): ResolvedQuickBrush | null {
  const resolvedId = migrateLegacyPresetId(presetId);

  if (resolvedId === ERASER_PRESET_ID || resolvedId === 'eraser') {
    return {
      id: ERASER_PRESET_ID,
      name: 'Eraser',
      category: 'eraser',
      desc: 'Standard Eraser Tool',
      hardness: DEFAULT_ERASER_SETTINGS.hardness ?? 0.9,
      thickness: 1.0,
      thicknessVertical: false,
      angle: 0,
      isEraser: true,
      previewSettings: DEFAULT_ERASER_SETTINGS,
    };
  }

  const builtin = ALL_BUILTIN_BRUSHES.find((b) => b.id === resolvedId);
  if (builtin) {
    let cfg: Partial<BrushSettings> | null = null;
    try {
      cfg = getBuiltinBrushConfig(builtin.id);
    } catch {
      cfg = null;
    }
    const tipDataUrl = cfg?.customTipDataUrl || cfg?.subTips?.[0]?.dataUrl;
    const angleDynamics = builtin.settings.customTipAngleDynamics;
    const staticAngle =
      !angleDynamics || angleDynamics === 'none' ? (builtin.settings.customTipAngle ?? 0) : 0;
    return {
      id: builtin.id,
      name: builtin.name,
      category: builtin.category,
      desc: builtin.desc,
      tipDataUrl,
      isColorTip: Boolean(builtin.settings.isColorTip),
      hardness: builtin.settings.hardness ?? 1.0,
      thickness: builtin.settings.thickness ?? 1.0,
      thicknessVertical: Boolean(builtin.settings.thicknessVertical),
      angle: staticAngle,
      previewSettings: cfg ?? builtin.settings,
    };
  }

  const custom = customBrushes.find((b) => b.id === presetId || b.id === resolvedId);
  if (custom) {
    const tipDataUrl = custom.tipDataUrl || custom.subTips?.[0]?.dataUrl;
    const staticAngle =
      !custom.angleDynamics || custom.angleDynamics === 'none' ? (custom.angle ?? 0) : 0;
    return {
      id: custom.id,
      name: custom.name,
      category: custom.category || 'custom',
      desc: custom.badge ? `${custom.badge} brush` : 'Custom brush',
      tipDataUrl,
      isColorTip: Boolean(custom.isColorTip),
      hardness: custom.hardness ?? 0.9,
      thickness: custom.thickness ?? 1.0,
      thicknessVertical: Boolean(custom.thicknessVertical),
      angle: staticAngle,
      isCustom: true,
      customBrush: custom,
      previewSettings: {
        ...NEUTRAL_BRUSH_DEFAULTS,
        preset: custom.id,
        brushName: custom.name,
        size: custom.size,
        minSizeFactor: custom.minSizeFactor,
        flow: custom.flow,
        opacity: custom.opacity,
        spacing: custom.spacing,
        hardness: custom.hardness,
        thickness: custom.thickness,
        thicknessVertical: custom.thicknessVertical,
        customTipDataUrl: tipDataUrl,
        customTipAngle: custom.angle,
        customTipAngleDynamics: custom.angleDynamics,
        isColorTip: custom.isColorTip,
        isRibbon: custom.isRibbon,
        subTips: custom.subTips,
        dynamics: custom.dynamics,
        colorMixing: custom.colorMixing,
        renderMode: custom.renderMode,
        bristle: custom.bristle,
        naturalPaint: custom.naturalPaint,
      },
    };
  }

  return null;
}

export async function activateQuickBrush(
  resolved: ResolvedQuickBrush,
  customBrushes: CspBrush[]
): Promise<void> {
  if (resolved.isEraser) {
    toolStoreActions.selectTool('eraser');
    return;
  }

  const targetIsDeco =
    isDecorationPresetId(resolved.id) || isDecorationCategory(resolved.category);

  if (resolved.customBrush) {
    toolStoreActions.selectTool(targetIsDeco ? 'decoration' : 'brush');
    if (targetIsDeco) {
      saveActiveDecorationPresetId(resolved.customBrush.id);
    } else {
      saveActivePresetId(resolved.customBrush.id);
    }
    const prepared = await prepareBrushSettingsForCsp(resolved.customBrush);
    const saved = getSavedBrushSettings(resolved.customBrush.id);
    toolStoreActions.updateBrushSettings({
      ...NEUTRAL_BRUSH_DEFAULTS,
      ...prepared,
      ...saved,
      preset: resolved.customBrush.id,
    });
    return;
  }

  toolStoreActions.selectTool(targetIsDeco ? 'decoration' : 'brush');
  if (targetIsDeco) {
    saveActiveDecorationPresetId(resolved.id);
  } else {
    saveActivePresetId(resolved.id);
  }
  const effective = getEffectiveBrushSettings(resolved.id, customBrushes);
  toolStoreActions.updateBrushSettings({
    ...NEUTRAL_BRUSH_DEFAULTS,
    ...effective,
    preset: resolved.id,
  });
}

export function clampQuickBrushPosition(
  x: number,
  y: number,
  panelWidth: number,
  panelHeight: number,
  viewport?: { width: number; height: number }
): { x: number; y: number } {
  const vw = viewport?.width ?? (typeof window !== 'undefined' ? window.innerWidth : 1280);
  const vh = viewport?.height ?? (typeof window !== 'undefined' ? window.innerHeight : 800);
  const minX = 8;
  const minY = 40;
  const maxX = Math.max(minX, vw - Math.max(48, panelWidth) - 8);
  const maxY = Math.max(minY, vh - Math.max(48, panelHeight) - 8);
  return {
    x: Math.max(minX, Math.min(maxX, Math.round(x))),
    y: Math.max(minY, Math.min(maxY, Math.round(y))),
  };
}