import { useRef, useState, type ReactNode } from 'react' import { useAssetStyles } from '../assetStyles' import { lineFates } from '../lines' import { useStore } from '../store' import { BUBBLE_SHAPES, BUBBLE_TAILS, textless } from '../geometry' import { BubblesField } from './BubblesField' import { MarksBar } from './MarksBar' import { firstLook, inked, KINDS, LOOKS, painted, STRENGTH } from '../regionKinds' import type { BubbleShape, BubbleTail, Region, RegionKind, RegionLook, SectionId, Tool } from '../types' import { Button, ColorField, Icon, IconButton, Section, Segmented, Select, Slider, Toggle } from './controls' type FieldName = 'text' | 'caption' | 'sfx' interface SliderSpec { key: string label: string hint: string min: number max: number step: number } // A label is short enough for one line; the hint says the same in full, on hover. const ALL_TEXT: SliderSpec[] = [ { key: 'soften', label: 'Soften edges, px', hint: 'Blur the letter edges, in pixels', min: 0, max: 3, step: 0.1 }, { key: 'grain', label: 'Uneven ink', hint: 'Ink that is thinner in places, like a worn stamp', min: 0, max: 1, step: 0.05 }, { key: 'arc', label: 'Bend into an arc', hint: 'Curve the lines up or down', min: -1, max: 1, step: 0.05 }, { key: 'rotation', label: 'Tilt, °', hint: 'Tilt all text, degrees', min: -45, max: 45, step: 0.5 }, { key: 'opacity', label: 'Opacity', hint: 'How solid all text is', min: 0.1, max: 1, step: 0.05 }, { key: 'gradient_angle', label: 'Gradient angle, °', hint: 'Which way every gradient runs: 0 top to bottom, 90 left to right', min: -180, max: 180, step: 5 }, { key: 'drip', label: 'Drips', hint: 'Drips run down from the letters: none to many and long', min: 0, max: 1, step: 0.05 }, ] const BUBBLE_GLOW: SliderSpec = { key: 'caption_bubble_glow', label: 'Bubble glow', hint: "A glow of the outline's color round the caption's bubble: none to strong", min: 0, max: 1, step: 0.05, } const TAIL_ANGLE: SliderSpec = { key: 'caption_tail_angle', label: 'Tail direction, °', hint: 'Where the tail points, degrees: 0 right, 90 down, 180 left, 270 up', min: 0, max: 360, step: 5, } const BUBBLES: SliderSpec[] = [ { key: 'bubble_size', label: 'Largest font, %', hint: 'Largest font in a bubble, % of image height', min: 1, max: 30, step: 0.5 }, { key: 'bubble_min_size', label: 'Smallest font, %', hint: 'Smallest font still readable, % of image height; longer text goes elsewhere', min: 0.5, max: 10, step: 0.1, }, { key: 'margin', label: 'Gap to the edge', hint: "Space between the text and the bubble's edge", min: 0, max: 0.3, step: 0.01 }, { key: 'white_threshold', label: 'Darkest bubble fill', hint: 'The darkest fill still taken for a bubble (0-255)', min: 40, max: 255, step: 1 }, { key: 'bubble_stroke_width', label: 'Outline width', hint: 'Outline around text on a bubble, as a share of the font size. 0 = none. Also used in your own bubbles and in a caption put in a bubble.', min: 0, max: 0.3, step: 0.01, }, { key: 'bubble_glow', label: 'Glow of drawn bubbles', hint: 'A glow round the bubbles you draw yourself (areas with a bubble fill): none to strong', min: 0, max: 1, step: 0.05 }, ] const CAPTION: SliderSpec[] = [ { key: 'free_size', label: 'Font size, %', hint: 'Caption font size, % of image height', min: 1, max: 30, step: 0.5 }, { key: 'stroke_width', label: 'Outline width', hint: 'Caption outline width', min: 0, max: 0.5, step: 0.01 }, { key: 'shadow', label: 'Shadow', hint: 'Caption shadow strength', min: 0, max: 1, step: 0.05 }, ] const EFFECTS: SliderSpec[] = [ { key: 'sfx_size', label: 'Font size, %', hint: 'Effect font size, % of image height', min: 1, max: 30, step: 0.5 }, { key: 'sfx_tilt', label: 'Largest tilt, °', hint: 'Effects tilt at random up to this many degrees', min: 0, max: 45, step: 1 }, { key: 'sfx_opacity', label: 'Opacity', hint: 'How solid the effects are', min: 0.1, max: 1, step: 0.05 }, { key: 'sfx_glow', label: 'Glow', hint: 'A glow round the letters of an effect: none to strong', min: 0, max: 1, step: 0.05 }, ] const CUSTOM_MOOD: SliderSpec[] = [ { key: 'jitter', label: 'Letters wobble', hint: 'Letters move off the line', min: 0, max: 1, step: 0.05 }, { key: 'size_jitter', label: 'Letter sizes vary', hint: 'Letters differ in size', min: 0, max: 1, step: 0.05 }, { key: 'rough', label: 'Ragged edges', hint: 'Letter edges are torn', min: 0, max: 1, step: 0.05 }, { key: 'line_tilt', label: 'Lines tilt, °', hint: 'Each line tilts a little, degrees', min: 0, max: 15, step: 0.5 }, { key: 'line_shift', label: 'Lines shift', hint: 'Lines move sideways', min: 0, max: 1, step: 0.05 }, { key: 'line_spacing', label: 'Line spacing', hint: 'Space between lines', min: 0.7, max: 1.6, step: 0.02 }, ] const FILLS: { value: Region['fill']; label: string }[] = [ { value: 'none', label: 'On the art' }, { value: 'light', label: 'Light bubble' }, { value: 'dark', label: 'Dark bubble' }, { value: 'black', label: 'Black' }, { value: 'gray', label: 'Gray message' }, { value: 'blue', label: 'Blue message' }, { value: 'soft', label: 'Soft patch' }, ] const FILL_LABELS = FILLS.map((entry) => entry.label) /** What the area's text sits on, from a list: there are too many to lay out in a row. */ function FillSelect({ value, onChange }: { value: Region['fill']; onChange: (fill: Region['fill']) => void }) { return ( onChange(KINDS.find((entry) => entry.label === label)?.value ?? value)} /> ) } /** How the kind looks, where it has looks to choose from. */ function LookRow({ kind, value, onChange }: { kind: RegionKind; value: RegionLook | undefined; onChange: (look: RegionLook) => void }) { const looks = LOOKS[kind] if (!looks) return null return compact value={value ?? looks[0].value} options={looks} onChange={onChange} /> } /** The shape of a bubble template, from a list. */ function ShapeSelect({ value, onChange }: { value: BubbleShape; onChange: (shape: BubbleShape) => void }) { const label = (shape: string) => shape[0].toUpperCase() + shape.slice(1) return ( setStamp({ tail: tail as BubbleTail })} /> reshape(region.id, { tail: tail as BubbleTail })} /> )} {region.kind === 'text' && changeRegion(region.id, { fill })} />} {(region.kind === 'text' || region.kind === 'sfx') && region.shapes.some((shape) => shape.type === 'stroke') && ( changeRegion(region.id, { follow })} label="Write along the brush stroke" /> )} {(region.kind === 'text' || region.kind === 'sfx') && region.follow === true && ( changeRegion(region.id, { flip })} label="Turn the text round" /> )} {(region.kind === 'text' || region.kind === 'sfx' || region.kind === 'mark') && ( <> changeRegion(region.id, { scale: value / 100 }, false)} onCommit={() => void render()} /> {region.follow !== true && ( changeRegion(region.id, { rotation: value }, false)} onCommit={() => void render()} /> )} {region.follow !== true && region.rotation !== undefined && ( )} )} ), )} ) } /** A small heading inside a section. */ function Group({ title, children }: { title: string; children: ReactNode }) { return (
{title}
{children}
) } function Sliders({ specs }: { specs: SliderSpec[] }) { const settings = useStore((state) => state.settings) const setSetting = useStore((state) => state.setSetting) const render = useStore((state) => state.render) return ( <> {specs.map((spec) => ( setSetting(spec.key, value, false)} onCommit={() => void render()} /> ))} ) } function Check({ name, label }: { name: string; label: string }) { const value = useStore((state) => Boolean(state.settings[name])) const setSetting = useStore((state) => state.setSetting) return setSetting(name, next)} label={label} /> } const GRADIENT_HINT = 'The color the letters fade to, top to bottom (bubbles and caption share it). auto: one color, no gradient' function Color({ name, label, hint }: { name: string; label: string; hint?: string }) { const value = useStore((state) => String(state.settings[name] ?? 'auto')) const setSetting = useStore((state) => state.setSetting) return ( setSetting(name, next, false)} onCommit={(next) => setSetting(name, next, true)} /> ) } /** The sliders behind the mood "custom". There is one set of them: every text set to custom follows it. */ function CustomMood() { return ( ) } /** What a folded section says of its text: the first line that has something in it, cut short. */ function firstLine(text: string) { const line = text.split('\n').find((entry) => entry.trim()) ?? '' const short = line.trim() if (!short) return 'empty' return short.length > 24 ? `${short.slice(0, 23)}…` : short } export function SidePanel() { const options = useStore((state) => state.options) const settings = useStore((state) => state.settings) const fields = { text: useStore((state) => state.text), caption: useStore((state) => state.caption), sfx: useStore((state) => state.sfx), } const auto = useStore((state) => state.auto) const embedded = useStore((state) => state.embedded) const comfy = useStore((state) => state.comfy) const open = useStore((state) => state.open) const areaCount = useStore((state) => state.slots[state.source]?.regions.length ?? 0) const slot = useStore((state) => state.slots[state.source]) const { setField, setSetting, setAuto, setOpen, moveEffects, resetLook, render } = useStore.getState() const areas = useRef>({ text: null, caption: null, sfx: null }) const [more, setMore] = useState>({ text: false, caption: false, sfx: false }) const [keepFonts, setKeepFonts] = useState(true) // The fonts and the shapes of the marks, for the lists and the buttons to draw with. const fontOf = useAssetStyles(options) if (!options) return null const list =(key: string, label: string, choices: string[], fonts = false) => (