File size: 27,235 Bytes
38f2c13 e196096 38f2c13 e196096 38f2c13 e196096 38f2c13 e196096 38f2c13 e196096 38f2c13 e196096 38f2c13 | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | import React, { useState, useEffect, useRef, lazy, Suspense } from "react";
import { createRoot } from "react-dom/client";
import { mountViewer, configure } from "./viewers";
import "./style.css";
const SkinEditor = lazy(() =>
import("./editor/SkinEditor").then((m) => ({ default: m.SkinEditor })),
);
const done = (x) => !!(x.best || x.rejection);
const modelOf = (x) => x.model ?? x.folder;
const rejectionLabel = (x) =>
x === "edited_unsatisfied" ? "edited 不满意" : "结果都不满意";
const annotationTypes = {
all: "全部已标注",
perfect: "有完美标注",
best: "有最佳标注(含完美)",
ordinary: "仅最佳标注(无完美)",
result_unsatisfied: "结果都不满意",
edited_unsatisfied: "edited 不满意",
};
const matchesAnnotation = (item, type) =>
type === "all" ||
(type === "perfect" && !!item.perfect) ||
(type === "best" && !!item.best) ||
(type === "ordinary" && !!item.best && !item.perfect) ||
item.rejection === type;
const label = (r) => (r.version === "original" ? "原版" : r.version);
async function api(path, body) {
const res = await fetch(
path,
body === undefined
? {}
: {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Annotation-Client": "ddj-review",
},
body: JSON.stringify(body),
},
);
const data = await res.json();
if (!res.ok) throw Error(data.error || "请求失败");
return data;
}
function Preview({ result }) {
const ref = useRef(null);
useEffect(() => mountViewer(ref.current, result), [result.url]);
return (
<div className="skin-stage" ref={ref}>
<canvas />
<span className="preview-status">正在加载 3D…</span>
</div>
);
}
function App() {
const [items, setItems] = useState([]),
[key, setKey] = useState(null),
[filter, setFilter] = useState("pending"),
[annotationType, setAnnotationType] = useState("all"),
[folder, setFolder] = useState(""),
[search, setSearch] = useState("");
const [skipped, setSkipped] = useState(new Set()),
[history, setHistory] = useState([]),
[busy, setBusy] = useState(false),
[notice, setNotice] = useState(null);
const [camera, setCamera] = useState("angle"),
[outer, setOuter] = useState(true),
[uv, setUv] = useState(false),
[image, setImage] = useState(null),
[editor, setEditor] = useState(null);
const busyRef = useRef(false),
dialog = useRef(null),
editorDialog = useRef(null);
const item = items.find((x) => x.key === key);
const matches = (
x,
f = filter,
fo = folder,
q = search,
type = annotationType,
) =>
(!fo || modelOf(x) === fo) &&
x.id.toLowerCase().includes(q.trim().toLowerCase()) &&
(f === "all" ||
(f === "pending" && x.results.length > 0 && !done(x)) ||
(f === "done" && done(x) && matchesAnnotation(x, type)) ||
(f === "rejected" && !!x.rejection) ||
(f === "no_results" && !x.results.length));
const rows = items.filter((x) => matches(x));
const jumpRows = item && !rows.includes(item) ? [item, ...rows] : rows;
const toast = (message, error = false) => setNotice({ message, error });
const show = (k, remember = true) => {
if (busyRef.current) return;
if (remember && key && key !== k) setHistory((h) => [...h, key]);
setKey(k);
};
const first = (f = filter, fo = folder, q = search, skip = skipped) =>
items.find((x) => matches(x, f, fo, q) && !skip.has(x.key))?.key || null;
const next = (list = items, skip = skipped) => {
const pool = list.filter(
(x) => matches(x) && !skip.has(x.key) && x.key !== key,
);
const index = list.findIndex((x) => x.key === key);
show((pool.find((x) => list.indexOf(x) > index) || pool[0])?.key || null);
};
async function load(refresh = false) {
if (busyRef.current) return;
busyRef.current = true;
setBusy(true);
try {
const data = await api(
refresh ? "/api/refresh" : "/api/state",
refresh ? {} : undefined,
);
setItems(data.items);
setKey(
data.items.find((x) => x.key === key && matches(x))?.key ||
data.items.find((x) => matches(x) && !skipped.has(x.key))?.key ||
null,
);
if (refresh) toast("目录已刷新");
} catch (e) {
toast(e.message, true);
} finally {
busyRef.current = false;
setBusy(false);
}
}
async function save(kind, result, png) {
if (busyRef.current || !item) return;
busyRef.current = true;
setBusy(true);
try {
const data = await api("/api/" + kind, {
key: item.key,
revision: item.revision,
result: result?.name,
reason: typeof result === "string" ? result : undefined,
png,
});
const updated = items.map((x) => (x.key === key ? data.item : x));
setItems(updated);
const skip = new Set(skipped);
skip.delete(key);
setSkipped(skip);
busyRef.current = false;
setEditor(null);
if (kind !== "clear") next(updated, skip);
toast(kind === "clear" ? "已撤销标注" : "已保存 · " + item.id);
} catch (e) {
toast(e.message, true);
} finally {
busyRef.current = false;
setBusy(false);
}
}
function skip() {
if (busyRef.current || !item) return;
const s = new Set(skipped);
s.add(key);
setSkipped(s);
next(items, s);
}
function previous() {
if (!busyRef.current && history.length) {
setKey(history.at(-1));
setHistory(history.slice(0, -1));
}
}
useEffect(() => {
load();
}, []);
useEffect(() => configure(camera, outer, uv), [camera, outer, uv, key]);
useEffect(() => {
if (!notice) return;
const t = setTimeout(() => setNotice(null), notice.error ? 8000 : 3500);
return () => clearTimeout(t);
}, [notice]);
useEffect(() => {
if (image) dialog.current?.showModal();
}, [image]);
useEffect(() => {
if (editor) editorDialog.current?.showModal();
}, [editor]);
useEffect(() => {
const handler = (e) => {
if (
busy ||
image ||
editor ||
e.ctrlKey ||
e.metaKey ||
e.altKey ||
e.target.closest("input,select,textarea,[contenteditable]")
)
return;
if (e.key.toLowerCase() === "s") {
e.preventDefault();
skip();
} else if (e.key === "ArrowLeft") {
e.preventDefault();
previous();
} else if (e.key === "ArrowRight") {
e.preventDefault();
next();
} else if (/^[1-9]$/.test(e.key)) {
const r = item?.results[+e.key - 1];
if (r) {
e.preventDefault();
save("choose", r);
}
}
};
document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
});
const progressItems = items.filter((x) => !folder || modelOf(x) === folder);
const progressDone = progressItems.filter(done).length;
const progressTotal =
progressDone +
progressItems.filter((x) => x.results.length && !done(x)).length;
const counts = {
done: progressItems.filter(done).length,
pending: progressItems.filter((x) => x.results.length && !done(x)).length,
rejected: progressItems.filter((x) => x.rejection).length,
all: progressItems.length,
no_results: progressItems.filter((x) => !x.results.length).length,
};
const changeFilter = (f) => {
if (busyRef.current) return;
setFilter(f);
show(first(f));
};
return (
<>
<header className="topbar">
<div className="brand">
<span className="brand-icon">D</span>
<div>
<h1>DDJ 最佳结果标注</h1>
<p>对照原图,选出最好的皮肤</p>
</div>
</div>
<div className="progress">
<div>
<span>标注进度</span>
<strong id="progressText">
{progressDone} / {progressTotal}
</strong>
</div>
<div className="progress-track">
<i
style={{
width: `${(progressDone / (progressTotal || 1)) * 100}%`,
}}
/>
</div>
</div>
<button
id="refresh"
className="button secondary"
disabled={busy}
onClick={() => load(true)}
>
刷新目录
</button>
</header>
<section className="filters">
<div className="segments">
{Object.entries({
pending: "待标注",
done: "已标注",
rejected: "不满意",
all: "全部",
no_results: "无结果",
}).map(([f, l]) => (
<button
key={f}
data-filter={f}
disabled={busy}
className={filter === f ? "active" : ""}
onClick={() => changeFilter(f)}
>
{l}{" "}
<b
id={`${{ all: "total", no_results: "noResult" }[f] || f}Count`}
>
{counts[f]}
</b>
</button>
))}
</div>
<select
id="folder"
aria-label="模型目录"
disabled={busy}
value={folder}
onChange={(e) => {
setFolder(e.target.value);
show(first(filter, e.target.value));
}}
>
<option value="">所有模型</option>
{[...new Set(items.map(modelOf))].map((f) => (
<option key={f}>{f}</option>
))}
</select>
<input
id="search"
type="search"
aria-label="搜索编号"
placeholder="搜索编号…"
disabled={busy}
value={search}
onChange={(e) => {
setSearch(e.target.value);
show(first(filter, folder, e.target.value));
}}
/>
</section>
{filter === "done" && (
<section className="annotation-filters" aria-label="已标注类型筛选">
<label htmlFor="annotationType">标注类型</label>
<select
id="annotationType"
disabled={busy}
value={annotationType}
onChange={(e) => {
const type = e.target.value;
setAnnotationType(type);
show(
items.find(
(x) =>
matches(x, "done", folder, search, type) &&
!skipped.has(x.key),
)?.key || null,
);
}}
>
{Object.entries(annotationTypes).map(([type, title]) => (
<option key={type} value={type}>
{title} ·{" "}
{
items.filter((x) => matches(x, "done", folder, search, type))
.length
}
</option>
))}
</select>
<span>可与数据目录、编号搜索组合筛选</span>
</section>
)}
<main>
{item ? (
<>
<section className="item-heading">
<div>
<span className="eyebrow">{item.folder}</span>
<h2 id="itemId">{item.id}</h2>
<p id="itemInfo">
{item.results.length} 个结果版本 ·{" "}
{item.annotation_conflict
? "标注冲突,请重新选择"
: item.invalid_best || item.invalid_rejection
? "标注已失效,请重新选择"
: item.perfect
? "已选最佳(完美)"
: item.best
? "已保存最佳结果"
: item.rejection
? rejectionLabel(item.rejection)
: "请选择最佳结果、标记不满意,或暂时忽略"}
</p>
</div>
<div className="jump">
<label htmlFor="jump">跳转条目</label>
<select
id="jump"
disabled={busy}
value={key}
onChange={(e) => {
const s = new Set(skipped);
s.delete(e.target.value);
setSkipped(s);
show(e.target.value);
}}
>
{jumpRows.map((x) => (
<option key={x.key} value={x.key}>
{x.perfect ? "★ " : done(x) ? "✓ " : ""}
{x.id}
</option>
))}
</select>
</div>
</section>
<div className="workspace" id="workspace">
<aside className="source-panel panel">
<div className="reference-images">
{[
["source", "原图", item.source_url],
["edited", "中间图", item.edited_url],
].map(([type, title, url]) => (
<section className="reference-image" key={type}>
<div className="panel-title">
<h3>
{type} · {title}
</h3>
<span>
{type === "source" && item.source_error
? "err 样例"
: ""}
</span>
</div>
<button
hidden={!url}
id={
type === "source" ? "enlargeSource" : "enlargeEdited"
}
className="source-image-button"
onClick={() =>
setImage({ url, title: item.id + "_" + type })
}
>
<img
id={type + "Image"}
src={url || undefined}
alt={title}
/>
</button>
{type === "edited" && (
<div
id="missingEdited"
className="missing-edited"
hidden={!!url}
>
暂无 edited 图片
</div>
)}
</section>
))}
</div>
<div className="source-caption">
原图与中间图同时对照 · 点击任意图片放大
</div>
<div className="reject-actions">
<button
id="rejectResults"
className="button reject-button"
disabled={
busy ||
!item.results.length ||
item.rejection === "result_unsatisfied"
}
onClick={() => save("reject", "result_unsatisfied")}
>
结果都不满意
</button>
<button
id="rejectEdited"
className="button reject-button"
disabled={
busy ||
!item.edited_url ||
item.rejection === "edited_unsatisfied"
}
onClick={() => save("reject", "edited_unsatisfied")}
>
edited 不满意
</button>
</div>
<div
id="savedBox"
className="saved-box"
hidden={
!done(item) &&
!item.invalid_best &&
!item.invalid_rejection &&
!item.annotation_conflict
}
>
<span>当前标注{item.perfect ? " · 完美" : ""}</span>
<strong id="savedName">
{item.rejection
? rejectionLabel(item.rejection)
: item.best || "原标注需要重新选择"}
</strong>
{item.perfect_url && (
<button
className="text-button"
onClick={() =>
setImage({
url: item.perfect_url,
title: item.id + "_perfect.png",
pixelated: true,
})
}
>
查看修复后的皮肤
</button>
)}
<button
id="clearBest"
className="text-button"
disabled={busy}
onClick={() => save("clear")}
>
撤销此标注
</button>
</div>
<p className="save-note">
保存后自动进入下一项。暂时忽略不会写入文件。
</p>
</aside>
<section className="comparison">
<div className="comparison-toolbar">
<div className="camera-buttons">
<span>同步视角</span>
{Object.entries({
angle: "斜前",
front: "正面",
back: "背面",
head: "头部",
}).map(([c, l]) => (
<button
key={c}
data-camera={c}
className={camera === c ? "active" : ""}
onClick={() => setCamera(c)}
>
{l}
</button>
))}
</div>
<div className="view-options">
<label>
<input
id="outerLayer"
type="checkbox"
checked={outer}
onChange={(e) => setOuter(e.target.checked)}
/>
显示外层
</label>
<button
id="uvMode"
className="button tiny"
aria-pressed={uv}
onClick={() => setUv(!uv)}
>
{uv ? "显示 3D" : "只看 UV"}
</button>
</div>
</div>
<div
id="results"
className={"result-grid" + (uv ? " uv-only" : "")}
>
{item.results.map((r, i) => (
<article
key={item.key + r.name}
data-result={r.name}
className={
"result-card" +
(item.best === r.name ? " selected" : "")
}
>
<div className="result-header">
<span className="version-name">{label(r)}</span>
<span
className={
item.best === r.name
? "chosen-badge"
: "version-number"
}
>
{item.best === r.name
? item.perfect
? "最佳 · 完美"
: "已选最佳"
: i + 1}
</span>
</div>
<Preview result={r} />
<div className="uv-area">
<button
className="uv-button"
onClick={() =>
setImage({
url: r.url,
title: r.name,
pixelated: true,
})
}
>
<img src={r.url} alt={label(r) + " 的 UV 贴图"} />
</button>
<div className="uv-caption">
<strong>UV 贴图</strong>
<span>点击放大</span>
</div>
</div>
<div className="result-actions">
<div className="result-file">{r.name}</div>
<button
className="choose-button"
disabled={
busy || (item.best === r.name && !item.perfect)
}
onClick={() => save("choose", r)}
>
{item.best === r.name && !item.perfect
? "当前最佳"
: "选为最佳"}
</button>
<button
className="perfect-button"
disabled={
busy ||
(item.perfect === r.name && !item.perfect_url)
}
onClick={() => save("perfect", r)}
>
选为最佳(完美)
</button>
<button
className="repair-button"
disabled={busy}
onClick={() =>
setEditor({
result: r,
url:
item.perfect === r.name && item.perfect_url
? item.perfect_url
: r.url,
})
}
>
修复并选为最佳(完美)
</button>
</div>
</article>
))}
{!item.results.length && (
<div className="no-result-card">
这个 source 暂无 result 图片
</div>
)}
</div>
<p className="preview-hint">
拖动任一模型可同步旋转,滚轮缩放;点击 UV 贴图可放大。
</p>
</section>
</div>
</>
) : (
<section id="empty" className="empty panel">
<h2>当前筛选下没有更多条目</h2>
<p>可以切换筛选,或重新查看暂时忽略的条目。</p>
<button
id="resume"
className="button primary"
disabled={busy}
onClick={() => {
setSkipped(new Set());
setFilter("pending");
setFolder("");
setSearch("");
show(first("pending", "", "", new Set()));
}}
>
继续查看待标注
</button>
</section>
)}
</main>
<footer className="footer">
<div className="navigation">
<button
id="previous"
className="button secondary"
disabled={busy || !history.length}
onClick={previous}
>
← 上一个
</button>
<button
id="skip"
className="button secondary"
disabled={busy || !item}
onClick={skip}
>
暂时忽略 →
</button>
</div>
<span className="keyboard-hint">1–9 选择版本 S 忽略 ← 回看</span>
<span>本次已忽略 {skipped.size} 项 · 忽略不保存</span>
</footer>
{notice && (
<div
id="toast"
role="status"
className={"toast" + (notice.error ? " error" : "")}
>
{notice.message}
</div>
)}
{image && (
<dialog
id="imageDialog"
ref={dialog}
onCancel={() => setImage(null)}
onClick={(e) => {
if (e.target === e.currentTarget) setImage(null);
}}
>
<div className="dialog-top">
<span>{image.title}</span>
<button
id="closeDialog"
className="button secondary"
onClick={() => setImage(null)}
>
关闭 ×
</button>
</div>
<div className="dialog-image">
<img
id="dialogImage"
src={image.url}
alt="放大预览"
className={image.pixelated ? "pixelated" : ""}
/>
</div>
</dialog>
)}
{editor && (
<dialog
className="editor-dialog"
ref={editorDialog}
onCancel={(e) => {
e.preventDefault();
if (!busy) setEditor(null);
}}
>
<div className="dialog-top">
<span>修复 {editor.result.name} · 左键绘制,右键旋转</span>
<button
className="button secondary"
disabled={busy}
onClick={() => setEditor(null)}
>
取消 ×
</button>
</div>
<div className="editor-content">
<aside className="editor-reference" aria-label="edited 参考图">
<h3>edited · 参考图</h3>
{item?.edited_url ? (
<img
src={item.edited_url}
alt={`${item.id} 的 edited 参考图`}
/>
) : (
<p>此条目暂无 edited 图片</p>
)}
</aside>
<div className="editor-canvas-panel">
<Suspense fallback={<p>正在加载皮肤编辑器…</p>}>
<SkinEditor
textureUrl={editor.url}
busy={busy}
onError={(m) => toast(m, true)}
onSubmit={(png) => save("repair", editor.result, png)}
/>
</Suspense>
</div>
</div>
{notice?.error && <p role="alert">{notice.message}</p>}
</dialog>
)}
</>
);
}
createRoot(document.getElementById("root")).render(<App />);
|