Merge pull request #429 from pascalorg/feat/paint-panel-polish

Paint panel polish: sticky controls, selection outlines, category auto-select
This commit is contained in:
Wassim SAMAD
2026-06-18 12:53:28 -04:00
committed by GitHub
3 changed files with 160 additions and 167 deletions
@@ -8,7 +8,7 @@ import {
useScene, useScene,
} from '@pascal-app/core' } from '@pascal-app/core'
import { useViewer } from '@pascal-app/viewer' import { useViewer } from '@pascal-app/viewer'
import { Eraser, RotateCcw } from 'lucide-react' import { Eraser, Plus, RotateCcw } from 'lucide-react'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { import {
buildResetSurfaceMaterialUpdates, buildResetSurfaceMaterialUpdates,
@@ -16,15 +16,16 @@ import {
} from './../../../lib/material-paint' } from './../../../lib/material-paint'
import useEditor from './../../../store/use-editor' import useEditor from './../../../store/use-editor'
import { Button } from '../primitives/button' import { Button } from '../primitives/button'
import { Tooltip, TooltipContent, TooltipTrigger } from '../primitives/tooltip'
import { MaterialPicker } from './material-picker' import { MaterialPicker } from './material-picker'
import { PanelSection } from './panel-section'
import { SceneMaterialList } from './scene-material-list' import { SceneMaterialList } from './scene-material-list'
/** /**
* Material picker for paint mode. Embedders render this wherever paint controls * Material picker for paint mode. Embedders render this wherever paint controls
* belong (the community editor places it in the Build sidebar while paint mode * belong (the community editor places it in the Build sidebar while paint mode
* is active). It owns the paint-target/material wiring so the host only needs * is active). It fills its container's height and lays out as three bands: a
* to mount it; it fills its container's width. * fixed control/category header, a single scrolling catalog grid, and a fixed
* scene-material footer (always visible, with a `+` to add a custom material).
*/ */
export function MaterialPaintPanel() { export function MaterialPaintPanel() {
const activePaintMaterial = useEditor((state) => state.activePaintMaterial) const activePaintMaterial = useEditor((state) => state.activePaintMaterial)
@@ -55,9 +56,34 @@ export function MaterialPaintPanel() {
useScene.getState().updateNodes(buildResetSurfaceMaterialUpdates(nodes, selectedNode)) useScene.getState().updateNodes(buildResetSurfaceMaterialUpdates(nodes, selectedNode))
} }
// Create a blank custom scene material, select it as the brush (`scene:` ref so
// edits propagate), and open its inline editor. Available from any category.
const createCustomMaterial = () => {
const id = generateSceneMaterialId()
const count = Object.keys(useScene.getState().materials).length
useScene.getState().addSceneMaterial({
id,
name: `Material ${count + 1}`,
material: {
preset: 'custom',
properties: {
color: '#ffffff',
roughness: 0.5,
metalness: 0,
opacity: 1,
transparent: false,
side: 'front',
},
},
})
setActivePaintMaterial({ materialPreset: toSceneMaterialRef(id), sourceTarget: activePaintTarget })
setAutoEditMaterialId(id)
}
return ( return (
<div className="w-full space-y-2"> <div className="flex h-full min-h-0 w-full flex-col">
<div className="flex items-center gap-2"> {/* Fixed: eraser / reset. */}
<div className="flex shrink-0 items-center gap-2 pb-2">
<Button <Button
aria-pressed={paintEraser} aria-pressed={paintEraser}
className="flex-1" className="flex-1"
@@ -79,32 +105,48 @@ export function MaterialPaintPanel() {
Reset all Reset all
</Button> </Button>
</div> </div>
{/* Scrolls: category tabs (fixed inside) + catalog grid (the scroll). */}
<div className="min-h-0 flex-1">
<MaterialPicker <MaterialPicker
onChange={(material) => {
// Custom-create: pre-create a scene material and select it as the
// brush via a `scene:` ref so painting stores the ref and edits to
// it propagate everywhere. The user edits it inline in the scene-
// material list below (auto-opened) — no separate right-side pane.
const id = generateSceneMaterialId()
const count = Object.keys(useScene.getState().materials).length
useScene.getState().addSceneMaterial({ id, name: `Material ${count + 1}`, material })
setActivePaintMaterial({
materialPreset: toSceneMaterialRef(id),
sourceTarget: activePaintTarget,
})
setAutoEditMaterialId(id)
}}
onSelectMaterialPreset={(materialPreset) => { onSelectMaterialPreset={(materialPreset) => {
setActivePaintMaterial({ materialPreset, sourceTarget: activePaintTarget }) setActivePaintMaterial({ materialPreset, sourceTarget: activePaintTarget })
}} }}
selectedMaterialPreset={activePaintMaterial?.materialPreset} selectedMaterialPreset={activePaintMaterial?.materialPreset}
value={activePaintMaterial?.material}
/> />
</div>
{/* Fixed footer: scene materials, always visible, with a `+` to add one. */}
<div className="mt-2 shrink-0 space-y-1.5 border-border/60 border-t pt-2">
<div className="flex items-center justify-between">
<span className="font-medium text-muted-foreground text-xs uppercase tracking-[0.12em]">
Scene materials
</span>
<Tooltip>
<TooltipTrigger asChild>
<Button
aria-label="Add material"
onClick={createCustomMaterial}
size="icon-sm"
type="button"
variant="outline"
>
<Plus />
</Button>
</TooltipTrigger>
<TooltipContent>Add material</TooltipContent>
</Tooltip>
</div>
<div className="subtle-scrollbar max-h-56 overflow-y-auto">
{materialCount > 0 ? ( {materialCount > 0 ? (
<PanelSection title="Scene materials">
<SceneMaterialList autoEditId={autoEditMaterialId} /> <SceneMaterialList autoEditId={autoEditMaterialId} />
</PanelSection> ) : (
) : null} <p className="px-0.5 py-1 text-muted-foreground text-xs">
No custom materials yet add one with +.
</p>
)}
</div>
</div>
</div> </div>
) )
} }
@@ -5,7 +5,6 @@ import {
getLibraryMaterialIdFromRef, getLibraryMaterialIdFromRef,
getMaterialsForCategory, getMaterialsForCategory,
MATERIAL_CATEGORIES, MATERIAL_CATEGORIES,
type MaterialSchema,
type MaterialTarget, type MaterialTarget,
toLibraryMaterialRef, toLibraryMaterialRef,
} from '@pascal-app/core' } from '@pascal-app/core'
@@ -13,9 +12,7 @@ import { useEffect, useState } from 'react'
import { triggerSFX } from '../../../lib/sfx-bus' import { triggerSFX } from '../../../lib/sfx-bus'
type MaterialPickerProps = { type MaterialPickerProps = {
value?: MaterialSchema
selectedMaterialPreset?: string selectedMaterialPreset?: string
onChange?: (material: MaterialSchema) => void
onSelectMaterialPreset?: (materialPreset: string) => void onSelectMaterialPreset?: (materialPreset: string) => void
disabled?: boolean disabled?: boolean
nodeType?: MaterialTarget nodeType?: MaterialTarget
@@ -26,14 +23,16 @@ function getCategoryLabel(category: (typeof MATERIAL_CATEGORIES)[number]) {
return category.charAt(0).toUpperCase() + category.slice(1) return category.charAt(0).toUpperCase() + category.slice(1)
} }
/**
* Catalog material picker: a fixed row of category tabs over a scrollable grid
* of swatches. Custom-material creation lives in the scene-material section
* (the host's `+` action), not here, so it's available from any category.
*/
export function MaterialPicker({ export function MaterialPicker({
value,
selectedMaterialPreset, selectedMaterialPreset,
onChange,
onSelectMaterialPreset, onSelectMaterialPreset,
disabled = false, disabled = false,
}: MaterialPickerProps) { }: MaterialPickerProps) {
const [showCustom, setShowCustom] = useState<boolean>(!!value?.properties)
const [selectedCategory, setSelectedCategory] = useState<(typeof MATERIAL_CATEGORIES)[number]>( const [selectedCategory, setSelectedCategory] = useState<(typeof MATERIAL_CATEGORIES)[number]>(
MATERIAL_CATEGORIES[0], MATERIAL_CATEGORIES[0],
) )
@@ -42,61 +41,25 @@ export function MaterialPicker({
) )
const catalogItems = getMaterialsForCategory(selectedCategory) const catalogItems = getMaterialsForCategory(selectedCategory)
// Keep the visible category in sync with the externally-selected catalog
// material (a `scene:` ref matches no catalog entry, so the tab stays put).
useEffect(() => { useEffect(() => {
setShowCustom(!!value?.properties && !selectedMaterialPreset) const catalogId = getLibraryMaterialIdFromRef(selectedMaterialPreset) ?? undefined
}, [selectedMaterialPreset, value?.properties]) const entry = getCatalogMaterialById(catalogId)
if (entry?.category) setSelectedCategory(entry.category)
useEffect(() => { }, [selectedMaterialPreset])
if (!selectedMaterialPreset && value?.properties) {
setSelectedCategory('colors')
return
}
const catalogId =
getLibraryMaterialIdFromRef(selectedMaterialPreset) ?? value?.id ?? undefined
const selectedCatalogEntry = getCatalogMaterialById(catalogId)
if (selectedCatalogEntry?.category) {
setSelectedCategory(selectedCatalogEntry.category)
}
}, [selectedMaterialPreset, value?.id])
const selectedCatalogId =
selectedMaterialPreset ?? (value?.id ? toLibraryMaterialRef(value.id) : undefined)
const selectedCatalogMaterialId = getLibraryMaterialIdFromRef(selectedCatalogId) ?? undefined
const selectedCatalogEntry = getCatalogMaterialById(selectedCatalogMaterialId)
const handleCatalogSelect = (materialId: string) => { const handleCatalogSelect = (materialId: string) => {
if (disabled) return if (disabled) return
setShowCustom(false)
onSelectMaterialPreset?.(toLibraryMaterialRef(materialId)) onSelectMaterialPreset?.(toLibraryMaterialRef(materialId))
} }
// Seed a new custom material from the current/forked colour and hand it to
// the host (MaterialPaintPanel), which pre-creates a scene material the user
// edits inline in the build pane — no separate right-side editor pane.
const handleCustomOpen = () => {
if (disabled) return
const forkColor = selectedMaterialPreset
? (selectedCatalogEntry?.previewColor ?? '#ffffff')
: '#ffffff'
onChange?.({
preset: 'custom',
properties: {
color: value?.properties?.color || forkColor,
roughness: value?.properties?.roughness ?? 0.5,
metalness: value?.properties?.metalness ?? 0,
opacity: value?.properties?.opacity ?? 1,
transparent: value?.properties?.transparent ?? false,
side: value?.properties?.side ?? 'front',
},
})
}
return ( return (
<div className={`min-w-0 space-y-3 ${disabled ? 'pointer-events-none opacity-50' : ''}`}> <div
{(catalogItems.length > 0 || onChange) && ( className={`flex h-full min-h-0 flex-col gap-2 ${disabled ? 'pointer-events-none opacity-50' : ''}`}
<div className="min-w-0 space-y-1"> >
<div className="flex flex-wrap gap-1 pb-1"> {/* Fixed category tabs — outside the scroll region. */}
<div className="flex shrink-0 flex-wrap gap-1">
{availableCategories.map((category) => ( {availableCategories.map((category) => (
<button <button
className={`rounded-full px-3 py-1 font-medium text-xs transition-colors ${ className={`rounded-full px-3 py-1 font-medium text-xs transition-colors ${
@@ -107,9 +70,10 @@ export function MaterialPicker({
key={category} key={category}
onClick={() => { onClick={() => {
setSelectedCategory(category) setSelectedCategory(category)
if (showCustom) { // Auto-select the first material in the category so the brush is
setShowCustom(false) // immediately ready (and the swatch shows as selected).
} const first = getMaterialsForCategory(category)[0]
if (first) handleCatalogSelect(first.id)
}} }}
type="button" type="button"
> >
@@ -117,16 +81,17 @@ export function MaterialPicker({
</button> </button>
))} ))}
</div> </div>
{/* The only scrolling region. */}
<div <div
className="grid gap-2 pb-1" className="subtle-scrollbar grid min-h-0 flex-1 auto-rows-min gap-2 overflow-y-auto pb-1"
style={{ gridTemplateColumns: 'repeat(auto-fill, minmax(72px, 1fr))' }} style={{ gridTemplateColumns: 'repeat(auto-fill, minmax(72px, 1fr))' }}
> >
{catalogItems.map((item) => { {catalogItems.map((item) => {
const isSelected = selectedCatalogId === toLibraryMaterialRef(item.id) const isSelected = selectedMaterialPreset === toLibraryMaterialRef(item.id)
return ( return (
<button <button
className={`group relative flex flex-col gap-1.5 rounded-xl p-1.5 transition-colors hover:cursor-pointer hover:bg-sidebar-accent ${ className={`group relative flex flex-col gap-1.5 rounded-xl p-1.5 transition-colors hover:cursor-pointer hover:bg-sidebar-accent ${
isSelected ? 'bg-sidebar-accent ring-2 ring-primary-foreground' : '' isSelected ? 'bg-sidebar-accent ring-1 ring-primary ring-inset' : ''
}`} }`}
key={item.id} key={item.id}
onClick={() => { onClick={() => {
@@ -156,29 +121,7 @@ export function MaterialPicker({
</button> </button>
) )
})} })}
{selectedCategory === 'colors' && onChange ? (
<button
className={`group relative flex flex-col gap-1.5 rounded-xl p-1.5 transition-colors hover:cursor-pointer hover:bg-sidebar-accent ${
showCustom ? 'bg-sidebar-accent ring-2 ring-primary-foreground' : ''
}`}
onClick={() => {
triggerSFX('sfx:menu-click')
handleCustomOpen()
}}
onMouseEnter={() => triggerSFX('sfx:menu-hover')}
type="button"
>
<div className="flex aspect-square w-full items-center justify-center rounded-lg bg-muted text-lg text-muted-foreground group-hover:text-foreground">
+
</div> </div>
<span className="truncate px-0.5 text-left font-medium text-[11px] text-muted-foreground group-hover:text-foreground">
Custom
</span>
</button>
) : null}
</div>
</div>
)}
</div> </div>
) )
} }
@@ -32,6 +32,7 @@ export function SceneMaterialList({ autoEditId }: { autoEditId?: SceneMaterialId
const updateSceneMaterial = useScene((state) => state.updateSceneMaterial) const updateSceneMaterial = useScene((state) => state.updateSceneMaterial)
const removeSceneMaterial = useScene((state) => state.removeSceneMaterial) const removeSceneMaterial = useScene((state) => state.removeSceneMaterial)
const activePaintTarget = useEditor((state) => state.activePaintTarget) const activePaintTarget = useEditor((state) => state.activePaintTarget)
const activePaintRef = useEditor((state) => state.activePaintMaterial?.materialPreset)
const setActivePaintMaterial = useEditor((state) => state.setActivePaintMaterial) const setActivePaintMaterial = useEditor((state) => state.setActivePaintMaterial)
const materialEntries = useMemo( const materialEntries = useMemo(
@@ -71,6 +72,7 @@ export function SceneMaterialList({ autoEditId }: { autoEditId?: SceneMaterialId
activePaintTarget={activePaintTarget} activePaintTarget={activePaintTarget}
autoEdit={autoEditId === id} autoEdit={autoEditId === id}
id={id} id={id}
isActive={activePaintRef === toSceneMaterialRef(id)}
key={id} key={id}
removeSceneMaterial={removeSceneMaterial} removeSceneMaterial={removeSceneMaterial}
sceneMaterial={sceneMaterial} sceneMaterial={sceneMaterial}
@@ -89,6 +91,7 @@ function SceneMaterialRow({
usageCount, usageCount,
activePaintTarget, activePaintTarget,
autoEdit, autoEdit,
isActive,
addSceneMaterial, addSceneMaterial,
updateSceneMaterial, updateSceneMaterial,
removeSceneMaterial, removeSceneMaterial,
@@ -99,6 +102,7 @@ function SceneMaterialRow({
usageCount: number usageCount: number
activePaintTarget: ReturnType<typeof useEditor.getState>['activePaintTarget'] activePaintTarget: ReturnType<typeof useEditor.getState>['activePaintTarget']
autoEdit: boolean autoEdit: boolean
isActive: boolean
addSceneMaterial: ReturnType<typeof useScene.getState>['addSceneMaterial'] addSceneMaterial: ReturnType<typeof useScene.getState>['addSceneMaterial']
updateSceneMaterial: ReturnType<typeof useScene.getState>['updateSceneMaterial'] updateSceneMaterial: ReturnType<typeof useScene.getState>['updateSceneMaterial']
removeSceneMaterial: ReturnType<typeof useScene.getState>['removeSceneMaterial'] removeSceneMaterial: ReturnType<typeof useScene.getState>['removeSceneMaterial']
@@ -133,7 +137,11 @@ function SceneMaterialRow({
} }
return ( return (
<div className="rounded-md border border-border/60 bg-background/40 p-2"> <div
className={`rounded-md border border-border/60 bg-background/40 p-2 ${
isActive ? 'ring-1 ring-primary ring-inset' : ''
}`}
>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<span <span
className="h-8 w-8 shrink-0 rounded-md border border-border/70" className="h-8 w-8 shrink-0 rounded-md border border-border/70"