File size: 5,921 Bytes
9d57416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {useState} from 'react';
import {unimplementedMotion, castKinds, label} from './np-batch.ts';
import type {GenSchema, SingleConfig} from './np-batch.ts';
import './batch.css';

const display = (value: unknown): string => value === undefined || value === null ? '—' : typeof value === 'string' ? label(value) : JSON.stringify(value);
const pick = (name: string, value: string | undefined, options: string[], change: (value: string) => void, disabled: string[] = []) =>
  <select aria-label={name} value={value ?? ''} onChange={e => change(e.target.value)}>{value === undefined && <option value="">按默认</option>}{options.map(option => <option key={option} value={option} disabled={disabled.includes(option)}>{label(option)}</option>)}</select>;
// 新主体、观察者的 id:同类首字母加最小的空号(h0、h1…;观察者 A、B…),和 gen.expand 的编号一致
function freeId(taken: string[], make: (i: number) => string) {
  for (let i = 0; ; i++) if (!taken.includes(make(i))) return make(i);
}

// 屏 1 第 4 卡:看一段、改一段,可加观察者、选实体还是虚拟。改的是这段的单次配置,
// “只跑这一段”原样交给服务,由生成器 config.load 校验;去掉的主体、观察者,引用它的情形安排一并去掉
export function SegmentConfig({config, errors, schema, onRun}: {config: SingleConfig; errors: string[]; schema: GenSchema; onRun?: (config: SingleConfig) => void}) {
  const [draft, setDraft] = useState<SingleConfig>(config), [copied, setCopied] = useState('');
  const edited = JSON.stringify(draft) !== JSON.stringify(config), json = JSON.stringify(draft, null, 2);
  const cast = draft.cast, observers = draft.observers || [], ids = [...cast.map(c => c.id), ...observers.map(o => o.id)];
  const drop = (id: string) => (draft.arrangements || []).filter(a => !(a.who || []).includes(id));
  const setCast = (i: number, patch: Partial<SingleConfig['cast'][number]>) => setDraft({...draft, cast: cast.map((c, j) => i === j ? {...c, ...patch} : c)});
  const setObserver = (i: number, patch: Partial<NonNullable<SingleConfig['observers']>[number]>) => setDraft({...draft, observers: observers.map((o, j) => i === j ? {...o, ...patch} : o)});
  return <div className="segment-config">
    <p>场景 {draft.scene} · 随机种子 {draft.seed} · 时长 {draft.duration_s} s · 段风格 {display(draft.flavor)} · 干扰比例 {display(draft.decoy_rate)}{edited && <> · <b>已手改</b> <button type="button" onClick={() => setDraft(config)}>还原</button></>}</p>
    <div className="batch-table"><table><caption>主体</caption><thead><tr><th>id</th><th>种类</th><th>行为档案</th><th>外观</th><th /></tr></thead><tbody>{cast.map((actor, i) => <tr key={actor.id}><td>{actor.id}</td>
      <td>{pick(actor.id + ' 种类', actor.kind, castKinds(schema), kind => setCast(i, {kind}))}</td>
      <td>{pick(actor.id + ' 行为档案', actor.profile, Object.keys(schema.profiles), profile => setCast(i, {profile}))}</td><td>{display(actor.appearance)}</td>
      <td><button type="button" onClick={() => setDraft({...draft, cast: cast.filter((_, j) => j !== i), arrangements: drop(actor.id)})}>去掉</button></td></tr>)}</tbody></table>
      <button type="button" onClick={() => setDraft({...draft, cast: [...cast, {id: freeId(ids, i => 'h' + i), kind: 'humanoid'}]})}>+ 加主体</button></div>
    <div className="batch-table"><table><caption>观察者</caption><thead><tr><th>id</th><th>种类</th><th>移动方式</th><th>视角</th><th>机身</th><th /></tr></thead><tbody>{observers.map((observer, i) => <tr key={observer.id}><td>{observer.id}</td>
      <td>{pick(observer.id + ' 种类', observer.kind, Object.keys(schema.observer_kinds), kind => setObserver(i, {kind, body: undefined}))}</td>
      <td>{pick(observer.id + ' 移动方式', observer.motion_mode, schema.motion_modes, motion_mode => setObserver(i, {motion_mode}), unimplementedMotion(schema))}</td>
      <td>{pick(observer.id + ' 视角', observer.view?.mode, schema.view_modes, mode => setObserver(i, {view: {...observer.view, mode}}))}</td><td>{display(observer.body)}</td>
      <td><button type="button" onClick={() => setDraft({...draft, observers: observers.filter((_, j) => j !== i), arrangements: drop(observer.id)})}>去掉</button></td></tr>)}</tbody></table>
      <button type="button" onClick={() => setDraft({...draft, observers: [...observers, {id: freeId(ids, i => String.fromCharCode(65 + i)), kind: 'embodied', motion_mode: 'seek'}]})}>+ 加观察者</button></div>
    <div className="batch-table"><table><caption>情形安排</caption><thead><tr><th>类型</th><th>谁</th><th>必须/尽量</th><th>地点</th><th>时间</th></tr></thead><tbody>{(draft.arrangements || []).map((arrangement, i) => <tr key={i}><td>{label(arrangement.type)}</td><td>{arrangement.who?.join('、') || '—'}</td><td>{display(arrangement.strength)}</td><td>{display(arrangement.where)}</td><td>{display(arrangement.when)}</td></tr>)}</tbody></table>{!draft.arrangements?.length && <p>无情形安排。</p>}</div>
    {errors.length > 0 && !edited && <div className="batch-error" role="alert"><p>配置问题</p><ul>{errors.map((error, i) => <li key={i}>{error}</li>)}</ul></div>}
    <details><summary>原始配置 JSON</summary><pre>{json}</pre></details><button type="button" onClick={async () => {try {await navigator.clipboard.writeText(json); setCopied('已复制');} catch {setCopied('复制失败,请在原始配置中选中复制');}}}>复制配置 JSON</button><span role="status">{copied}</span>
    <div className="btns"><button type="button" className="btn" disabled={!onRun || errors.length > 0 && !edited} onClick={() => onRun?.(draft)}>{edited ? '按改过的只跑这一段' : '只跑这一段'}</button><span className="batch-muted">进度在右侧;生成器拒收的配置会在右侧报出原因</span></div>
  </div>;
}