|
|
import { useEffect, useRef, useCallback, useState } from 'react'; |
|
|
import { Pane } from 'tweakpane'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useTweakpane = (dilationFactor, setDilationFactor, characterSize, setCharacterSize, darkMode, variantSizeImpact, setVariantSizeImpact) => { |
|
|
const paneRef = useRef(null); |
|
|
const paramsRef = useRef({ |
|
|
dilation: dilationFactor, |
|
|
size: characterSize, |
|
|
variantSizeImpact: variantSizeImpact |
|
|
}); |
|
|
const [isDebugMode, setIsDebugMode] = useState(false); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
const savedDebugState = localStorage.getItem('fontmap-debug-mode'); |
|
|
if (savedDebugState === 'true') { |
|
|
setIsDebugMode(true); |
|
|
} |
|
|
}, []); |
|
|
|
|
|
|
|
|
const createPane = useCallback(() => { |
|
|
if (paneRef.current) return; |
|
|
|
|
|
|
|
|
const pane = new Pane({ |
|
|
title: 'FontMap Debug Controls', |
|
|
container: document.body, |
|
|
expanded: true |
|
|
}); |
|
|
|
|
|
|
|
|
pane.element.style.position = 'fixed'; |
|
|
pane.element.style.top = '80px'; |
|
|
pane.element.style.right = '20px'; |
|
|
pane.element.style.zIndex = '10000'; |
|
|
pane.element.style.fontSize = '12px'; |
|
|
pane.element.style.backgroundColor = 'rgba(0, 0, 0, 0.9)'; |
|
|
pane.element.style.border = '1px solid #333'; |
|
|
pane.element.style.borderRadius = '8px'; |
|
|
|
|
|
|
|
|
pane.addBinding(paramsRef.current, 'dilation', { |
|
|
label: 'Dilation', |
|
|
min: 0, |
|
|
max: 1, |
|
|
step: 0.01, |
|
|
format: (value) => `${(value).toFixed(3)}` |
|
|
}).on('change', (ev) => { |
|
|
setDilationFactor(ev.value); |
|
|
}); |
|
|
|
|
|
|
|
|
pane.addBinding(paramsRef.current, 'size', { |
|
|
label: 'Font Size', |
|
|
min: 0.1, |
|
|
max: 2.0, |
|
|
step: 0.05, |
|
|
format: (value) => `${value.toFixed(2)}x` |
|
|
}).on('change', (ev) => { |
|
|
setCharacterSize(ev.value); |
|
|
}); |
|
|
|
|
|
|
|
|
pane.addBinding(paramsRef.current, 'variantSizeImpact', { |
|
|
label: 'Variant Size Impact' |
|
|
}).on('change', (ev) => { |
|
|
setVariantSizeImpact(ev.value); |
|
|
}); |
|
|
|
|
|
|
|
|
pane.addButton({ |
|
|
title: 'Reset' |
|
|
}).on('click', () => { |
|
|
setDilationFactor(0.035); |
|
|
setCharacterSize(1); |
|
|
setVariantSizeImpact(false); |
|
|
paramsRef.current.dilation = 0.035; |
|
|
paramsRef.current.size = 1; |
|
|
paramsRef.current.variantSizeImpact = false; |
|
|
pane.refresh(); |
|
|
}); |
|
|
|
|
|
paneRef.current = pane; |
|
|
}, [setDilationFactor, setCharacterSize, setVariantSizeImpact]); |
|
|
|
|
|
|
|
|
const destroyPane = useCallback(() => { |
|
|
if (paneRef.current) { |
|
|
paneRef.current.dispose(); |
|
|
paneRef.current = null; |
|
|
} |
|
|
}, []); |
|
|
|
|
|
|
|
|
const toggleDebugMode = useCallback(() => { |
|
|
const newDebugMode = !isDebugMode; |
|
|
setIsDebugMode(newDebugMode); |
|
|
localStorage.setItem('fontmap-debug-mode', newDebugMode.toString()); |
|
|
|
|
|
if (newDebugMode) { |
|
|
createPane(); |
|
|
console.log('🐛 Debug mode activated - Press D to toggle'); |
|
|
} else { |
|
|
destroyPane(); |
|
|
console.log('🐛 Debug mode deactivated'); |
|
|
} |
|
|
}, [isDebugMode, createPane, destroyPane]); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
if (isDebugMode) { |
|
|
createPane(); |
|
|
} else { |
|
|
destroyPane(); |
|
|
} |
|
|
}, [isDebugMode, createPane, destroyPane]); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
const handleKeyDown = (event) => { |
|
|
|
|
|
if (event.key.toLowerCase() === 'd' && !event.ctrlKey && !event.metaKey && !event.altKey) { |
|
|
|
|
|
const target = event.target; |
|
|
if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.contentEditable === 'true') { |
|
|
return; |
|
|
} |
|
|
|
|
|
event.preventDefault(); |
|
|
toggleDebugMode(); |
|
|
} |
|
|
}; |
|
|
|
|
|
document.addEventListener('keydown', handleKeyDown); |
|
|
|
|
|
return () => { |
|
|
document.removeEventListener('keydown', handleKeyDown); |
|
|
}; |
|
|
}, [toggleDebugMode]); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
if (paneRef.current) { |
|
|
paramsRef.current.dilation = dilationFactor; |
|
|
paramsRef.current.size = characterSize; |
|
|
paramsRef.current.variantSizeImpact = variantSizeImpact; |
|
|
paneRef.current.refresh(); |
|
|
} |
|
|
}, [dilationFactor, characterSize, variantSizeImpact]); |
|
|
|
|
|
return { |
|
|
isDebugMode, |
|
|
toggleDebugMode |
|
|
}; |
|
|
}; |