A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
import React, { useRef, useCallback, useState } from 'react';
import {
X,
Box,
Sun,
Magnet,
HelpCircle,
Eye,
EyeOff,
RotateCw,
Move,
Maximize2,
Ban,
Upload,
Layers,
Undo2,
Redo2,
Shapes,
} from 'lucide-react';
import {
Gizmo3DMode,
TOOL_3D_BEHAVIOR_RULES,
Tool3DMode,
} from '../utils/tool3DRules';
import { PRIMITIVE_3D_ITEMS, Primitive3DType } from '../utils/primitives3D';
import { LightSourceWidget } from './LightSourceWidget';
import { Primitive3DIcon } from './Primitive3DIcon';
import { store3DActions, use3DStore } from '../stores';
import { useI18n } from '../i18n';
export type Csp3DToolMode = Tool3DMode;
interface Csp3DToolbarProps {
is3DToolActive: boolean;
onActivate3DTool: () => void;
onDirectDrag: (mode: Tool3DMode, deltaX: number, deltaY: number, snap: boolean) => void;
onDirectDragEnd: () => void;
onTriggerImport: () => void;
onCreatePrimitive: (type: Primitive3DType) => void;
onSnapToGround: () => void;
onUndo: () => void;
onRedo: () => void;
onOpenLTModal: () => void;
isLTModalOpen?: boolean;
onBakeTo2D: (closeAfter: boolean) => void;
onClose: () => void;
}
const OBJECT_MODES: Array<{ mode: Tool3DMode; label: string; title: string }> = [
{
mode: 'smart',
label: 'Smart',
title: 'Smart CSP mode: drag model to transform, drag background to orbit',
},
{
mode: 'model-rotate',
label: 'Rotate',
title: 'Rotate model freely (click to select or drag button directly)',
},
{
mode: 'model-move',
label: 'Move',
title: 'Move model in view plane (click to select or drag button directly)',
},
{
mode: 'model-scale',
label: 'Scale',
title: 'Scale model uniformly (click to select or drag button directly)',
},
{
mode: 'model-plane-move',
label: 'Floor',
title: 'Slide model along ground floor plane (click or drag)',
},
];
const CAMERA_MODES: Array<{ mode: Tool3DMode; label: string; title: string }> = [
{ mode: 'camera-orbit', label: 'Orbit', title: 'Orbit camera around model (click or drag)' },
{ mode: 'camera-pan', label: 'Pan', title: 'Pan camera in screen plane (click or drag)' },
{ mode: 'camera-zoom', label: 'Zoom', title: 'Zoom camera distance (click or drag)' },
];
const GIZMO_BUTTONS: Array<{
mode: Gizmo3DMode;
label: string;
icon: React.ReactNode;
}> = [
{ mode: 'rotate', label: 'Rotate Gizmo', icon: <RotateCw className="w-3 h-3" /> },
{ mode: 'translate', label: 'Move Gizmo', icon: <Move className="w-3 h-3" /> },
{ mode: 'scale', label: 'Scale Gizmo', icon: <Maximize2 className="w-3 h-3" /> },
{ mode: 'none', label: 'Hide Gizmo', icon: <Ban className="w-3 h-3" /> },
];
export const Csp3DToolbar: React.FC<Csp3DToolbarProps> = ({
is3DToolActive,
onActivate3DTool,
onDirectDrag,
onDirectDragEnd,
onTriggerImport,
onCreatePrimitive,
onSnapToGround,
onUndo,
onRedo,
onOpenLTModal,
isLTModalOpen = false,
onBakeTo2D,
onClose,
}) => {
const { t, tc, td } = useI18n();
const activeMode = use3DStore((s) => s.activeMode);
const gizmoMode = use3DStore((s) => s.gizmoMode);
const loadedModelName = use3DStore((s) => s.loadedModelName);
const modelKind = use3DStore((s) => s.modelKind);
const activePrimitiveType = use3DStore((s) => s.activePrimitiveType);
const isBaking = use3DStore((s) => s.isBaking);
const canUndo3D = use3DStore((s) => s.canUndo3D);
const canRedo3D = use3DStore((s) => s.canRedo3D);
const isReferenceVisible = use3DStore((s) => s.isReferenceVisible);
const lighting = use3DStore((s) => s.lighting);
const [openPopover, setOpenPopover] = useState<'primitives' | 'light' | 'help' | null>(null);
const hasModel = Boolean(loadedModelName);
const dragRef = useRef<{
mode: Tool3DMode;
startX: number;
startY: number;
lastX: number;
lastY: number;
hasDragged: boolean;
} | null>(null);
const handleModeButtonPointerDown = useCallback(
(mode: Tool3DMode, e: React.PointerEvent<HTMLButtonElement>) => {
e.preventDefault();
e.stopPropagation();
if (mode === 'smart') {
store3DActions.setActiveMode('smart');
return;
}
const target = e.currentTarget;
try {
target.setPointerCapture(e.pointerId);
} catch {
// ignore
}
dragRef.current = {
mode,
startX: e.clientX,
startY: e.clientY,
lastX: e.clientX,
lastY: e.clientY,
hasDragged: false,
};
const handlePointerMove = (ev: PointerEvent) => {
if (!dragRef.current) return;
const dx = ev.clientX - dragRef.current.lastX;
const dy = ev.clientY - dragRef.current.lastY;
if (
Math.hypot(ev.clientX - dragRef.current.startX, ev.clientY - dragRef.current.startY) > 3
) {
dragRef.current.hasDragged = true;
}
dragRef.current.lastX = ev.clientX;
dragRef.current.lastY = ev.clientY;
if (dx !== 0 || dy !== 0) {
onDirectDrag(mode, dx, dy, ev.shiftKey);
}
};
const handlePointerUp = (ev: PointerEvent) => {
try {
target.releasePointerCapture(ev.pointerId);
} catch {
// ignore
}
window.removeEventListener('pointermove', handlePointerMove);
window.removeEventListener('pointerup', handlePointerUp);
window.removeEventListener('pointercancel', handlePointerUp);
if (dragRef.current) {
if (dragRef.current.hasDragged) {
onDirectDragEnd();
} else {
store3DActions.setActiveMode(mode);
if (mode === 'model-rotate' || mode === 'model-rotate-yaw' || mode === 'model-rotate-roll') {
store3DActions.setGizmoMode('rotate');
} else if (mode === 'model-move' || mode === 'model-plane-move') {
store3DActions.setGizmoMode('translate');
} else if (mode === 'model-scale') {
store3DActions.setGizmoMode('scale');
}
}
}
dragRef.current = null;
};
window.addEventListener('pointermove', handlePointerMove);
window.addEventListener('pointerup', handlePointerUp);
window.addEventListener('pointercancel', handlePointerUp);
},
[onDirectDrag, onDirectDragEnd]
);
// Minimized non-intrusive pill while user is painting with a 2D tool over the live 3D reference
if (!is3DToolActive) {
return (
<div
className="fixed top-12 left-1/2 -translate-x-1/2 z-40 bg-ui-floating border border-ui-default rounded-full px-2.5 py-1 shadow-ui-floating flex items-center gap-2 text-[11px] text-ui-primary select-none"
onPointerDown={(e) => e.stopPropagation()}
>
<span className="flex items-center gap-1.5 text-ui-secondary">
<Box className="w-3.5 h-3.5 text-ui-accent" />
<span className="max-w-[120px] truncate">
{loadedModelName ? td(loadedModelName) : t('3D Reference')}
</span>
</span>
<button
type="button"
onClick={onActivate3DTool}
title={t('Switch to 3D Tool (K) to rotate, move, scale, or adjust lighting')}
className="px-2 py-0.5 rounded-full bg-ui-accent text-ui-accent-contrast font-medium hover:opacity-90 transition-opacity cursor-pointer"
>
{t('Edit 3D (K)')}
</button>
<button
type="button"
onClick={() => store3DActions.toggleReferenceVisible()}
title={isReferenceVisible ? t('Hide 3D Reference') : t('Show 3D Reference')}
className="p-1 rounded-full text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer"
>
{isReferenceVisible ? <Eye className="w-3.5 h-3.5" /> : <EyeOff className="w-3.5 h-3.5" />}
</button>
<button
type="button"
onClick={onClose}
title={t('Close 3D Stage')}
className="p-1 rounded-full text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer"
>
<X className="w-3.5 h-3.5" />
</button>
</div>
);
}
return (
<div
data-drop-zone="3d-stage"
className="fixed top-12 left-1/2 -translate-x-1/2 z-50 bg-ui-floating border border-ui-default rounded-ui-lg px-2 py-1 shadow-ui-floating flex items-center gap-1.5 text-xs text-ui-primary select-none max-w-[calc(100vw-24px)]"
onPointerDown={(e) => e.stopPropagation()}
>
{/* 1. Import 3D Model & Primitives */}
<button
type="button"
onClick={onTriggerImport}
title={t('Import 3D Model (.glb, .gltf, .obj) — or drag & drop onto canvas')}
className={`flex items-center gap-1 px-2 py-1 rounded-ui-sm text-[11px] border border-ui-subtle transition-colors cursor-pointer max-w-[110px] truncate ${
loadedModelName
? 'bg-ui-control-active text-ui-primary font-medium'
: 'bg-ui-input text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
<Upload className="w-3 h-3 shrink-0 text-ui-accent" />
<span className="truncate">{loadedModelName ? td(loadedModelName) : t('Import 3D')}</span>
</button>
<div className="relative">
<button
type="button"
onClick={() => setOpenPopover((p) => (p === 'primitives' ? null : 'primitives'))}
title={t('Create 3D Primitive (Cube, Sphere, Capsule, Cylinder, Pyramid)')}
data-testid="toolbar-3d-primitives-btn"
className={`flex items-center gap-1 px-2 py-1 rounded-ui-sm text-[11px] border transition-colors cursor-pointer ${
openPopover === 'primitives' || modelKind === 'primitive'
? 'bg-ui-accent-subtle text-ui-accent border-ui-accent-border font-medium'
: 'bg-ui-input border-ui-subtle text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
<Shapes className="w-3 h-3 shrink-0" />
<span>{t('Primitives')}</span>
</button>
{openPopover === 'primitives' && (
<div
data-testid="toolbar-3d-primitives-popover"
className="absolute top-full mt-2 left-0 w-44 bg-ui-elevated border border-ui-default rounded-ui-md p-1.5 shadow-ui-popover z-50 space-y-0.5"
>
<div className="px-2 py-1 text-[10px] uppercase tracking-wider text-ui-muted">
{t('Create Primitive')}
</div>
{PRIMITIVE_3D_ITEMS.map((item) => {
const isSelected = modelKind === 'primitive' && activePrimitiveType === item.type;
return (
<button
key={item.type}
type="button"
onClick={() => {
onCreatePrimitive(item.type);
setOpenPopover(null);
}}
title={td(item.description)}
data-testid={`toolbar-3d-primitive-${item.type}`}
className={`w-full flex items-center gap-2 px-2 py-1.5 rounded-ui-xs text-[11px] transition-colors cursor-pointer ${
isSelected
? 'bg-ui-accent text-ui-accent-contrast font-medium'
: 'text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
<Primitive3DIcon type={item.type} className="w-3.5 h-3.5 shrink-0" />
<span>{td(item.label)}</span>
</button>
);
})}
</div>
)}
</div>
<div className="w-px h-4 bg-ui-subtle" />
{/* 2. Object Transform Modes (Click or Scrub-Drag) */}
<div className="flex items-center bg-ui-input rounded-ui-sm p-0.5 border border-ui-subtle">
{OBJECT_MODES.map((item) => (
<button
key={item.mode}
type="button"
onPointerDown={(e) => handleModeButtonPointerDown(item.mode, e)}
title={t(item.title)}
className={`px-2 py-0.5 rounded-ui-xs text-[11px] transition-colors cursor-pointer ${
activeMode === item.mode
? 'bg-ui-accent text-ui-accent-contrast font-medium'
: 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
{tc('3dToolbar', item.label)}
</button>
))}
</div>
{/* 3. Camera Modes (Click or Scrub-Drag) */}
<div className="flex items-center bg-ui-input rounded-ui-sm p-0.5 border border-ui-subtle">
<span className="px-1 text-[10px] text-ui-muted font-medium">{t('Cam')}</span>
{CAMERA_MODES.map((item) => (
<button
key={item.mode}
type="button"
onPointerDown={(e) => handleModeButtonPointerDown(item.mode, e)}
title={t(item.title)}
className={`px-1.5 py-0.5 rounded-ui-xs text-[11px] transition-colors cursor-pointer ${
activeMode === item.mode
? 'bg-ui-accent text-ui-accent-contrast font-medium'
: 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
{tc('3dCam', item.label)}
</button>
))}
</div>
<div className="w-px h-4 bg-ui-subtle" />
{/* 4. On-Canvas Gizmo Mode + Snap Floor + Undo/Redo */}
<div className="flex items-center bg-ui-input rounded-ui-sm p-0.5 border border-ui-subtle">
{GIZMO_BUTTONS.map((g) => (
<button
key={g.mode}
type="button"
onClick={() => store3DActions.setGizmoMode(g.mode)}
title={t(g.label)}
className={`p-1 rounded-ui-xs transition-colors cursor-pointer ${
gizmoMode === g.mode
? 'bg-ui-control-active text-ui-accent'
: 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
{g.icon}
</button>
))}
</div>
<button
type="button"
onClick={onSnapToGround}
disabled={!hasModel}
title={t('Snap Model to Ground Floor (Y = 0)')}
className="p-1 rounded-ui-xs text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover disabled:opacity-40 transition-colors cursor-pointer"
>
<Magnet className="w-3.5 h-3.5" />
</button>
<div className="flex items-center">
<button
type="button"
onClick={onUndo}
disabled={!canUndo3D}
title={t('Undo 3D Action (Ctrl+Z)')}
className="p-1 rounded-ui-xs text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover disabled:opacity-30 disabled:cursor-not-allowed transition-colors cursor-pointer"
>
<Undo2 className="w-3.5 h-3.5" />
</button>
<button
type="button"
onClick={onRedo}
disabled={!canRedo3D}
title={t('Redo 3D Action (Ctrl+Y)')}
className="p-1 rounded-ui-xs text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover disabled:opacity-30 disabled:cursor-not-allowed transition-colors cursor-pointer"
>
<Redo2 className="w-3.5 h-3.5" />
</button>
</div>
{/* 5. Quick Lighting Popover */}
<div className="relative">
<button
type="button"
onClick={() => setOpenPopover((p) => (p === 'light' ? null : 'light'))}
title={t('Quick Lighting & 3D Light Sphere')}
className={`p-1 rounded-ui-xs transition-colors cursor-pointer ${
openPopover === 'light'
? 'bg-ui-accent-subtle text-ui-accent'
: 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
<Sun className="w-3.5 h-3.5" />
</button>
{openPopover === 'light' && (
<div className="absolute top-full mt-2 right-0 w-64 bg-ui-elevated border border-ui-default rounded-ui-md p-2.5 shadow-ui-popover z-50">
<LightSourceWidget
embedded
compact
settings={{
azimuth: lighting.keyLight.azimuth,
elevation: lighting.keyLight.elevation,
intensity: lighting.keyLight.intensity,
ambientIntensity: lighting.ambientIntensity,
color: lighting.keyLight.color,
isBacklit: lighting.keyLight.isBacklit,
}}
onChange={(next) => {
store3DActions.updateKeyLight({
azimuth: next.azimuth,
elevation: next.elevation,
intensity: next.intensity,
color: next.color,
isBacklit: next.isBacklit,
});
store3DActions.updateLighting({ ambientIntensity: next.ambientIntensity });
}}
onReset={() => store3DActions.resetLighting()}
/>
</div>
)}
</div>
{/* 6. Behavior Rules Help Popover */}
<div className="relative">
<button
type="button"
onClick={() => setOpenPopover((p) => (p === 'help' ? null : 'help'))}
title={t('3D Tool Controls & Behavior Rules')}
className={`p-1 rounded-ui-xs transition-colors cursor-pointer ${
openPopover === 'help'
? 'bg-ui-accent-subtle text-ui-accent'
: 'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover'
}`}
>
<HelpCircle className="w-3.5 h-3.5" />
</button>
{openPopover === 'help' && (
<div className="absolute top-full mt-2 right-0 w-72 bg-ui-elevated border border-ui-default rounded-ui-md p-3 shadow-ui-popover z-50 space-y-2">
<div className="flex items-center justify-between pb-1.5 border-b border-ui-subtle">
<span className="text-[11px] font-medium text-ui-primary">{t('3D Tool Behavior Rules')}</span>
<button
type="button"
onClick={() => setOpenPopover(null)}
aria-label={t('Close')}
className="text-ui-icon-muted hover:text-ui-primary cursor-pointer"
>
<X className="w-3.5 h-3.5" />
</button>
</div>
<div className="space-y-1.5">
{TOOL_3D_BEHAVIOR_RULES.map((rule) => (
<div key={rule.gesture} className="flex items-start justify-between gap-2 text-[10px]">
<span className="font-ui-mono text-ui-primary shrink-0">{td(rule.gesture)}</span>
<span className="text-ui-muted text-right">{td(rule.action)}</span>
</div>
))}
</div>
</div>
)}
</div>
<div className="w-px h-4 bg-ui-subtle" />
{/* 7. Open Separate Line & Tone Extraction Modal with Live Preview */}
<button
type="button"
onClick={() => {
setOpenPopover(null);
onOpenLTModal();
}}
disabled={isBaking || !hasModel}
title={t('Open Line & Tone Extraction Preview Dialog')}
className={`px-2 py-1 rounded-ui-sm text-[11px] font-medium transition-colors cursor-pointer disabled:opacity-40 disabled:cursor-not-allowed ${
isLTModalOpen
? 'bg-ui-accent-subtle text-ui-accent ring-1 ring-ui-accent-border'
: 'bg-ui-control border border-ui-subtle text-ui-primary hover:bg-ui-control-hover'
}`}
>
{t('LT')}
</button>
{/* 8. Primary Bake Action (Shift+Click stamps a copy without closing!) */}
<button
type="button"
onClick={(e) => onBakeTo2D(!e.shiftKey)}
disabled={isBaking || !hasModel}
title={
!hasModel
? t('Load a 3D model first')
: t('Rasterize 3D view into a 2D Layer (Shift+Click to keep 3D stage open)')
}
className="flex items-center gap-1 px-2.5 py-1 bg-ui-accent text-ui-accent-contrast hover:opacity-90 font-medium rounded-ui-sm text-[11px] transition-opacity disabled:opacity-40 disabled:cursor-not-allowed cursor-pointer"
>
<Layers className="w-3 h-3" />
<span>{isBaking ? t('Baking...') : t('Bake')}</span>
</button>
{/* 9. Reference Visibility & Close */}
<button
type="button"
onClick={() => store3DActions.toggleReferenceVisible()}
title={isReferenceVisible ? t('Hide 3D Reference') : t('Show 3D Reference')}
className="p-1 text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-xs transition-colors cursor-pointer"
>
{isReferenceVisible ? <Eye className="w-3.5 h-3.5" /> : <EyeOff className="w-3.5 h-3.5" />}
</button>
<button
type="button"
onClick={onClose}
title={t('Close 3D Stage')}
className="p-1 text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-xs transition-colors cursor-pointer"
>
<X className="w-3.5 h-3.5" />
</button>
</div>
);
};