feat(scene-editor): add persistent dockable tool workspaces
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import {decodeDockTree,dockIds,dockPosition,pruneDockTree,type DockTree,type DockRect,type DockMove} from './dockTree.js';
|
||||
|
||||
/** The primary view is permanent. Tools can leave the split tree without losing their identity. */
|
||||
export interface EditorDockState {tree:DockTree;floating:Record<string,DockRect>}
|
||||
export function decodeEditorDock(raw:unknown,primary:string,tools:readonly string[]):EditorDockState|null {
|
||||
if(!raw||typeof raw!=='object')return null;
|
||||
const value=raw as Partial<EditorDockState>,allowed=[primary,...tools];
|
||||
let tree=pruneDockTree(decodeDockTree(value.tree),allowed);
|
||||
if(!dockIds(tree).includes(primary))tree=tree?{axis:'x',ratio:70,first:{id:primary},second:tree}:{id:primary};
|
||||
const floating:Record<string,DockRect>={};
|
||||
for(const id of tools){const r=value.floating?.[id];if(!r||!['left','top','width','height'].every(k=>Number.isFinite(r[k as keyof DockRect]))||r.width<=0||r.height<=0)continue;
|
||||
floating[id]={left:Math.max(-16384,Math.min(16384,r.left)),top:Math.max(-16384,Math.min(16384,r.top)),width:Math.min(16384,r.width),height:Math.min(16384,r.height)};
|
||||
}
|
||||
return {tree:tree!,floating};
|
||||
}
|
||||
|
||||
/** Inserts an absent/floating tool, or moves an existing tool. Never swaps the primary view. */
|
||||
export function dockEditorTool(state:EditorDockState,primary:string,move:DockMove|null,id:string,rect:DockRect):EditorDockState {
|
||||
if(id===primary)return state;
|
||||
const remaining=pruneDockTree(state.tree,dockIds(state.tree).filter(v=>v!==id))??{id:primary};
|
||||
const floating={...state.floating,[id]:rect};
|
||||
if(!move||move.position==='center'||(move.scope!=='workspace'&&!dockIds(remaining).includes(move.overId)))return {tree:remaining,floating};
|
||||
const before=move.position==='left'||move.position==='top',axis=move.position==='left'||move.position==='right'?'x':'y';
|
||||
const wrap=(node:DockTree,ratio:number):DockTree=>({axis,ratio:before?ratio:100-ratio,first:before?{id}:node,second:before?node:{id}});
|
||||
const insert=(node:DockTree):DockTree=>'id' in node?(node.id===move.overId?wrap(node,50):node):{...node,first:insert(node.first),second:insert(node.second)};
|
||||
return {tree:move.scope==='workspace'?wrap(remaining,axis==='x'?25:28):insert(remaining),floating};
|
||||
}
|
||||
|
||||
/** Outer edge has priority; broad magnets fill the gap between a tool and the viewport. */
|
||||
export function editorDockTarget(bounds:DockRect,rects:Record<string,DockRect>,primary:string,id:string,x:number,y:number):DockMove|null {
|
||||
if(!dockPosition(bounds,x,y))return null;
|
||||
const edges:[DockMove['position'],number][]=[['left',x-bounds.left],['right',bounds.left+bounds.width-x],['top',y-bounds.top],['bottom',bounds.top+bounds.height-y]];
|
||||
const nearest=edges.slice().sort((a,b)=>a[1]-b[1])[0];
|
||||
if(nearest[1]<=24)return {activeId:id,overId:primary,position:nearest[0],scope:'workspace'};
|
||||
for(const [target,r] of Object.entries(rects)){
|
||||
if(target===id||target===primary||!dockPosition(r,x,y))continue;
|
||||
const distances:[DockMove['position'],number][]=[['left',(x-r.left)/r.width],['right',(r.left+r.width-x)/r.width],['top',(y-r.top)/r.height],['bottom',(r.top+r.height-y)/r.height]];
|
||||
return {activeId:id,overId:target,position:distances.sort((a,b)=>a[1]-b[1])[0][0],scope:'panel'};
|
||||
}
|
||||
const magnets=edges.map(([position,distance])=>[position,distance/Math.min(position==='left'||position==='right'?160:96,(position==='left'||position==='right'?bounds.width:bounds.height)*.16)] as const).sort((a,b)=>a[1]-b[1]);
|
||||
return magnets[0][1]<=1?{activeId:id,overId:primary,position:magnets[0][0],scope:'workspace'}:null;
|
||||
}
|
||||
|
||||
/** Resize visible splits while retaining the positions of temporarily closed tools. */
|
||||
export function mergeEditorDockRatios(full:DockTree,visible:DockTree):DockTree {
|
||||
const ids=dockIds(visible),ratios=new Map<string,number>();
|
||||
const key=(node:DockTree)=>dockIds(node).sort().join('\0');
|
||||
const collect=(node:DockTree)=>{if('id' in node)return;ratios.set(key(node),node.ratio);collect(node.first);collect(node.second);};collect(visible);
|
||||
const merge=(node:DockTree):DockTree=>{
|
||||
if('id' in node)return node;
|
||||
const a=pruneDockTree(node.first,ids),b=pruneDockTree(node.second,ids),shown=pruneDockTree(node,ids);
|
||||
return {...node,ratio:a&&b&&shown?(ratios.get(key(shown))??node.ratio):node.ratio,first:merge(node.first),second:merge(node.second)};
|
||||
};return merge(full);
|
||||
}
|
||||
@@ -29,12 +29,16 @@ function clamp(value: number, min: number, max: number) {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
|
||||
export function createGlassMaterialVariables(settings: GlassMaterialSettings): Record<string, string> {
|
||||
export function createGlassMaterialVariables(settings: GlassMaterialSettings, context: "floating" | "scene" = "floating"): Record<string, string> {
|
||||
const tintHex = normalizeHex(settings.tintHex);
|
||||
const red = Number.parseInt(tintHex.slice(1, 3), 16);
|
||||
const green = Number.parseInt(tintHex.slice(3, 5), 16);
|
||||
const blue = Number.parseInt(tintHex.slice(5, 7), 16);
|
||||
return {
|
||||
...(context === "scene" ? {
|
||||
"--nodedc-scene-glass-opacity": String(clamp(settings.tintOpacity, 0, 100) / 100),
|
||||
"--nodedc-scene-glass-blur": `${clamp(settings.blur, 0, 120)}px`,
|
||||
} : {}),
|
||||
"--nodedc-modal-glass-tint-rgb": `${red} ${green} ${blue}`,
|
||||
"--nodedc-modal-glass-tint-opacity": String(clamp(settings.tintOpacity, 0, 100) / 100),
|
||||
"--nodedc-blur-modal": `${clamp(settings.blur, 0, 120)}px`,
|
||||
|
||||
@@ -12,3 +12,4 @@ export {dockMinimum, constrainDockTree, type DockMinimum, type DockMinimums} fro
|
||||
export {preferDockSizes} from "./dockTree.js";
|
||||
|
||||
export * from "./menuAim.js";
|
||||
export * from "./editorDock.js";
|
||||
|
||||
Reference in New Issue
Block a user