diff --git a/studio/src/renderer/src/bridge/BridgeClient.ts b/studio/src/renderer/src/bridge/BridgeClient.ts index 9e7739274..131520ec5 100644 --- a/studio/src/renderer/src/bridge/BridgeClient.ts +++ b/studio/src/renderer/src/bridge/BridgeClient.ts @@ -18,6 +18,7 @@ import type { PatternEntry, PatternTile, PickInspection, + ProfileFormState, SavedStart, SavedTile, SavedWorldInfo, @@ -45,6 +46,8 @@ export interface ScenePlace { key: string variant: number size: number | null + /** Settings typed into a profile form, by name, which only change the preview. */ + overrides?: Record } export interface TileRequest { @@ -211,6 +214,13 @@ export class BridgeClient { return this.json(`suggestions?${new URLSearchParams(of)}`) } + /** A profile entry's settings as a form, with the typed values applied. */ + profileForm(viewId: string, key: string, overrides: Record, signal?: AbortSignal): Promise { + const query = sceneQuery(viewId, { key, variant: 0, size: null, overrides }) + query.delete('variant') + return this.json(`profile-form?${query}`, { signal }) + } + sceneEntries(viewId: string): Promise { return this.json(`scene-entries?${new URLSearchParams({ view: viewId })}`) } @@ -357,6 +367,9 @@ function sceneQuery(viewId: string, place: ScenePlace): URLSearchParams { if (place.size !== null) { query.set('size', String(place.size)) } + for (const [name, value] of Object.entries(place.overrides ?? {})) { + query.set(`set.${name}`, value) + } return query } diff --git a/studio/src/renderer/src/bridge/types.ts b/studio/src/renderer/src/bridge/types.ts index c1a8c6e3a..ff56e57b5 100644 --- a/studio/src/renderer/src/bridge/types.ts +++ b/studio/src/renderer/src/bridge/types.ts @@ -45,6 +45,30 @@ export interface SceneViewInfo extends ModuleViewInfo { parts: boolean /** The sizes the scenes can be built at, or null when each entry has its own. */ sizes: SceneSizes | null + /** Whether each entry has a settings form (a config profile view); absent for the module views. */ + form?: boolean +} + +/** One setting of a profile form: its value as the preview uses it, and what it is when nothing is typed. */ +export interface ProfileField { + name: string + description: string + kind: 'boolean' | 'integer' | 'decimal' | 'enum' + value: string | number | boolean + defaultValue: string | number | boolean + minimum: number | null + maximum: number | null + /** An enum's constants. */ + choices: string[] + /** Whether the value was typed into the form. */ + changed: boolean +} + +/** The settings of a profile as a form, with short lines about it. */ +export interface ProfileFormState { + title: string + fields: ProfileField[] + notes: string[] } /** The sizes a scene view builds its scenes at, as blocks across, smallest first, and the one shown first (IrisApi SceneSizes). */ diff --git a/studio/src/renderer/src/components/IdeIcon.tsx b/studio/src/renderer/src/components/IdeIcon.tsx index 3fe42edd7..83bead22a 100644 --- a/studio/src/renderer/src/components/IdeIcon.tsx +++ b/studio/src/renderer/src/components/IdeIcon.tsx @@ -36,6 +36,10 @@ export type IdeIconName = | 'undo' | 'web' | 'views/biomes' + | 'views/cave-profiles' + | 'views/fluid-profiles' + | 'views/formation-profiles' + | 'views/tree-profiles' | 'views/noise' | 'views/objects' | 'views/palettes' diff --git a/studio/src/renderer/src/icons/views/cave-profiles.svg b/studio/src/renderer/src/icons/views/cave-profiles.svg new file mode 100644 index 000000000..237f5e687 --- /dev/null +++ b/studio/src/renderer/src/icons/views/cave-profiles.svg @@ -0,0 +1,4 @@ + + + + diff --git a/studio/src/renderer/src/icons/views/cave-profiles_dark.svg b/studio/src/renderer/src/icons/views/cave-profiles_dark.svg new file mode 100644 index 000000000..65b25848d --- /dev/null +++ b/studio/src/renderer/src/icons/views/cave-profiles_dark.svg @@ -0,0 +1,4 @@ + + + + diff --git a/studio/src/renderer/src/icons/views/fluid-profiles.svg b/studio/src/renderer/src/icons/views/fluid-profiles.svg new file mode 100644 index 000000000..58443c12b --- /dev/null +++ b/studio/src/renderer/src/icons/views/fluid-profiles.svg @@ -0,0 +1,3 @@ + + + diff --git a/studio/src/renderer/src/icons/views/fluid-profiles_dark.svg b/studio/src/renderer/src/icons/views/fluid-profiles_dark.svg new file mode 100644 index 000000000..1e15e5ae5 --- /dev/null +++ b/studio/src/renderer/src/icons/views/fluid-profiles_dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/studio/src/renderer/src/icons/views/formation-profiles.svg b/studio/src/renderer/src/icons/views/formation-profiles.svg new file mode 100644 index 000000000..081586c7f --- /dev/null +++ b/studio/src/renderer/src/icons/views/formation-profiles.svg @@ -0,0 +1,4 @@ + + + + diff --git a/studio/src/renderer/src/icons/views/formation-profiles_dark.svg b/studio/src/renderer/src/icons/views/formation-profiles_dark.svg new file mode 100644 index 000000000..28ebb7862 --- /dev/null +++ b/studio/src/renderer/src/icons/views/formation-profiles_dark.svg @@ -0,0 +1,4 @@ + + + + diff --git a/studio/src/renderer/src/icons/views/tree-profiles.svg b/studio/src/renderer/src/icons/views/tree-profiles.svg new file mode 100644 index 000000000..ade20e40c --- /dev/null +++ b/studio/src/renderer/src/icons/views/tree-profiles.svg @@ -0,0 +1,4 @@ + + + + diff --git a/studio/src/renderer/src/icons/views/tree-profiles_dark.svg b/studio/src/renderer/src/icons/views/tree-profiles_dark.svg new file mode 100644 index 000000000..e1e79850a --- /dev/null +++ b/studio/src/renderer/src/icons/views/tree-profiles_dark.svg @@ -0,0 +1,4 @@ + + + + diff --git a/studio/src/renderer/src/studio/ProfileFormPanel.tsx b/studio/src/renderer/src/studio/ProfileFormPanel.tsx new file mode 100644 index 000000000..063b85f15 --- /dev/null +++ b/studio/src/renderer/src/studio/ProfileFormPanel.tsx @@ -0,0 +1,129 @@ +import { useState } from 'react' +import { IdeIcon } from '@/components/IdeIcon' +import { Button } from '@/components/ui/button' +import { Input } from '@/components/ui/input' +import { ScrollArea } from '@/components/ui/scroll-area' +import { cn } from '@/lib/utils' +import type { ProfileField, ProfileFormState } from '../bridge/types' + +interface ProfileFormPanelProps { + state: ProfileFormState | null + /** Called with the typed text, or null to go back to what the pack has. */ + onChange(name: string, value: string | null): void + onResetAll(): void + className?: string +} + +/** + * The settings of the chosen profile as a form beside its scene. What is typed here only changes the preview, so the + * pack's files stay as they are; the header offers to put every value back. + */ +export function ProfileFormPanel({ state, onChange, onResetAll, className }: ProfileFormPanelProps): React.JSX.Element { + const changes = state?.fields.filter((field) => field.changed).length ?? 0 + return ( +
+
+

{state?.title ?? 'Settings'}

+ {changes > 0 && ( + + )} +
+ +
+ {state?.notes.map((note) => ( +

+ {note} +

+ ))} + {state?.fields.map((field) => ( + + ))} +
+
+
+ ) +} + +function FieldEditor({ field, onChange }: { field: ProfileField; onChange(name: string, value: string | null): void }): React.JSX.Element { + return ( +
+
+ +
+ {field.kind === 'boolean' || field.kind === 'enum' ? : } + +
+
+
+ ) +} + +function ChoiceInput({ field, onChange }: { field: ProfileField; onChange(name: string, value: string | null): void }): React.JSX.Element { + const choices = field.kind === 'boolean' ? ['true', 'false'] : field.choices + return ( + + ) +} + +function NumberInput({ field, onChange }: { field: ProfileField; onChange(name: string, value: string | null): void }): React.JSX.Element { + const shown = String(field.value) + const [draft, setDraft] = useState(shown) + const problem = problemWith(field, draft) + const commit = (): void => { + if (draft !== shown && problem === null) { + onChange(field.name, draft.trim()) + } + } + return ( + setDraft(event.target.value)} + onBlur={commit} + onKeyDown={(event) => { + if (event.key === 'Enter') { + commit() + } else if (event.key === 'Escape') { + setDraft(shown) + } + }} + /> + ) +} + +function problemWith(field: ProfileField, draft: string): string | null { + const number = Number(draft) + if (draft.trim() === '' || !Number.isFinite(number)) { + return 'Not a number' + } + if (field.kind === 'integer' && !Number.isInteger(number)) { + return 'Not a whole number' + } + if (field.minimum !== null && number < field.minimum) { + return `At least ${field.minimum}` + } + if (field.maximum !== null && number > field.maximum) { + return `At most ${field.maximum}` + } + return null +} diff --git a/studio/src/renderer/src/studio/SceneStudio.tsx b/studio/src/renderer/src/studio/SceneStudio.tsx index 0efd569f5..1ed505b26 100644 --- a/studio/src/renderer/src/studio/SceneStudio.tsx +++ b/studio/src/renderer/src/studio/SceneStudio.tsx @@ -2,7 +2,7 @@ import { useEffect, useMemo, useRef, useState } from 'react' import { Button } from '@/components/ui/button' import { ScrollArea } from '@/components/ui/scroll-area' import type { BridgeClient } from '../bridge/BridgeClient' -import { NO_PART, type JsonPath, type SceneEntry, type SceneViewInfo } from '../bridge/types' +import { NO_PART, type JsonPath, type ProfileFormState, type SceneEntry, type SceneViewInfo } from '../bridge/types' import { parsedText } from '../editor/jsonValues' import { nodeDrawingPart, nodeWithPath } from '../editor/nodeLayout' import { SuggestionStore } from '../editor/suggestionStore' @@ -21,6 +21,7 @@ import { EntryBrowser } from './EntryBrowser' import { useEntryFiles } from './EntryFileDialogs' import { NodeInspector } from './NodeInspector' import { NodePane } from './NodePane' +import { ProfileFormPanel } from './ProfileFormPanel' import { Panel, SidebarPanel, SizeChoice, Stepper, ViewIsland } from './Sidebar' import { SceneViewport, type PickedSurface } from './SceneViewport' @@ -72,7 +73,21 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce const [pickedPart, setPickedPart] = useState(NO_PART) const highlightedPart = view.parts ? (selected?.partId ?? pickedPart) : NO_PART - const { scene, shown, sceneParts, building } = useSceneMesh(client, view, entry, variant, size, packRevision, models, images, farLeavesOnly, highlightedPart, tell) + const [typed, setTyped] = useState<{ owner: string; values: Record }>({ owner: '', values: NO_OVERRIDES }) + const typedOwner = `${view.id}:${entry?.key ?? ''}` + const overrides = typed.owner === typedOwner ? typed.values : NO_OVERRIDES + const changeSetting = (name: string, value: string | null): void => + setTyped((current) => { + const next = { ...(current.owner === typedOwner ? current.values : NO_OVERRIDES) } + if (value === null) { + delete next[name] + } else { + next[name] = value + } + return { owner: typedOwner, values: next } + }) + const profileForm = useProfileForm(client, view, entry?.key ?? null, overrides, packRevision, tell) + const { scene, shown, sceneParts, building } = useSceneMesh(client, view, entry, variant, size, overrides, packRevision, models, images, farLeavesOnly, highlightedPart, tell) const shownScene = shown?.scene ?? null const blockIndex = useMemo(() => (shownScene === null ? null : new BlockIndex(shownScene)), [shownScene]) const shownParts = partsOf(sceneParts, shownScene) @@ -143,7 +158,8 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce /> )} - + + {view.form === true && setTyped({ owner: typedOwner, values: NO_OVERRIDES })} />} {view.baked && entry !== null && } {selected !== null && editor.state !== null ? ( @@ -217,6 +233,35 @@ function partNote(parts: Int32Array, part: number): string { return blocks === 0 ? ' · part has no blocks here' : ` · part has ${NUMBER.format(blocks)}` } +const NO_OVERRIDES: Record = {} + +/** The settings form of the chosen profile with the typed values applied, for a view that has one; read again after the pack reloads. */ +function useProfileForm( + client: BridgeClient, + view: SceneViewInfo, + key: string | null, + overrides: Record, + packRevision: number, + tell: (message: string) => void +): ProfileFormState | null { + const [form, setForm] = useState(null) + const overridesKey = JSON.stringify(overrides) + const hasForm = view.form === true + useEffect(() => { + if (!hasForm || key === null) { + setForm(null) + return + } + const stop = new AbortController() + client + .profileForm(view.id, key, JSON.parse(overridesKey) as Record, stop.signal) + .then((found) => !stop.signal.aborted && setForm(found)) + .catch((failure: Error) => !stop.signal.aborted && tell(failure.message)) + return () => stop.abort() + }, [client, view.id, hasForm, key, overridesKey, packRevision, tell]) + return form +} + function useSceneEntries(client: BridgeClient, viewId: string, packRevision: number, tell: (message: string) => void): SceneEntry[] { const [entries, setEntries] = useState([]) useEffect(() => { @@ -259,6 +304,7 @@ function useSceneMesh( entry: SceneEntry | null, variant: number, size: number | null, + overrides: Record, packRevision: number, models: BlockModels, images: BridgeAssets, @@ -274,13 +320,14 @@ function useSceneMesh( const key = entry?.key ?? null const viewId = view.id const hasParts = view.parts + const overridesKey = JSON.stringify(overrides) useEffect(() => { if (key === null) { return } const stop = new AbortController() - const place = { key, variant, size } + const place = { key, variant, size, overrides: JSON.parse(overridesKey) as Record } setBuilding(true) client .scene(viewId, place, stop.signal) @@ -304,7 +351,7 @@ function useSceneMesh( setBuilding(false) }) return () => stop.abort() - }, [client, viewId, hasParts, key, variant, size, packRevision, tell]) + }, [client, viewId, hasParts, key, variant, size, overridesKey, packRevision, tell]) const faint = useMemo(() => { const parts = partsOf(sceneParts, scene) diff --git a/studio/src/renderer/src/studio/ViewList.tsx b/studio/src/renderer/src/studio/ViewList.tsx index 8cb8e6c6c..3572dd390 100644 --- a/studio/src/renderer/src/studio/ViewList.tsx +++ b/studio/src/renderer/src/studio/ViewList.tsx @@ -12,6 +12,10 @@ const VIEW_ICONS: Record = { biomes: 'views/biomes', regions: 'views/regions', rivers: 'views/rivers', + caveProfiles: 'views/cave-profiles', + treeProfiles: 'views/tree-profiles', + formationProfiles: 'views/formation-profiles', + fluidProfiles: 'views/fluid-profiles', world3d: 'views/world-3d', savedWorld: 'views/saved-world', 'iris:terrain/palettes': 'views/palettes',