Factor out getImmutableIndices().

pull/230/head
Atul Varma 2021-09-28 11:50:51 -04:00
rodzic 09304f00b6
commit 9d91a08761
1 zmienionych plików z 34 dodań i 23 usunięć

Wyświetl plik

@ -16,7 +16,6 @@ import {
AttachmentPointType, AttachmentPointType,
ATTACHMENT_POINT_TYPES, ATTACHMENT_POINT_TYPES,
iterAttachmentPoints, iterAttachmentPoints,
PointWithNormal,
} from "../../specs"; } from "../../specs";
import { Random } from "../../random"; import { Random } from "../../random";
import { capitalize, range } from "../../util"; import { capitalize, range } from "../../util";
@ -266,27 +265,13 @@ export const CREATURE_DESIGN_DEFAULTS: CreatureDesign = {
}, },
}; };
const AttachmentIndicesWidget: React.FC<{ function getImmutableIndices(
label: string; attachmentsOfType: AttachedCreatureSymbol[],
points: PointWithNormal[]; attachment: AttachedCreatureSymbol
attachmentsOfType: AttachedCreatureSymbol[]; ): Set<number> {
attachment: AttachedCreatureSymbol;
onChange: (attachment: AttachedCreatureSymbol) => void;
}> = ({ attachment, ...props }) => {
const allIndices = range(props.points.length);
const immutableIndices = new Set<number>(); const immutableIndices = new Set<number>();
const toggleIndex = (i: number) => {
const indices = attachment.indices.slice();
const idx = indices.indexOf(i);
if (idx === -1) {
indices.push(i);
} else {
indices.splice(idx, 1);
}
props.onChange({ ...attachment, indices });
};
for (let a of props.attachmentsOfType) { for (let a of attachmentsOfType) {
if (a !== attachment) { if (a !== attachment) {
for (let idx of a.indices) { for (let idx of a.indices) {
// This index is taken up by another attachment. // This index is taken up by another attachment.
@ -300,9 +285,31 @@ const AttachmentIndicesWidget: React.FC<{
immutableIndices.add(attachment.indices[0]); immutableIndices.add(attachment.indices[0]);
} }
return immutableIndices;
}
const AttachmentIndicesWidget: React.FC<{
label: string;
numIndices: number;
immutableIndices: Set<number>;
attachment: AttachedCreatureSymbol;
onChange: (attachment: AttachedCreatureSymbol) => void;
}> = ({ attachment, onChange, label, numIndices, immutableIndices }) => {
const allIndices = range(numIndices);
const toggleIndex = (i: number) => {
const indices = attachment.indices.slice();
const idx = indices.indexOf(i);
if (idx === -1) {
indices.push(i);
} else {
indices.splice(idx, 1);
}
onChange({ ...attachment, indices });
};
return ( return (
<> <>
<div>{props.label}</div> <div>{label}</div>
<div> <div>
{allIndices.map((i) => { {allIndices.map((i) => {
return ( return (
@ -390,6 +397,10 @@ function AttachmentEditor<T extends CreatureSymbol>({
</div> </div>
{creatureAttachments.map((attach, i) => { {creatureAttachments.map((attach, i) => {
const atIdPrefix = `${idPrefix}_${type}_${i}_`; const atIdPrefix = `${idPrefix}_${type}_${i}_`;
const immutableIndices = getImmutableIndices(
creatureAttachments,
attach
);
return ( return (
<div <div
@ -401,8 +412,8 @@ function AttachmentEditor<T extends CreatureSymbol>({
> >
<AttachmentIndicesWidget <AttachmentIndicesWidget
label={`${typeCap} attachment point indices:`} label={`${typeCap} attachment point indices:`}
points={points} numIndices={points.length}
attachmentsOfType={creatureAttachments} immutableIndices={immutableIndices}
attachment={attach} attachment={attach}
onChange={updateAttachment.bind(null, attach)} onChange={updateAttachment.bind(null, attach)}
/> />