This commit is contained in:
loucass003 committed 2025-03-20 10:44:40 +01:00
1 parent b97277b996
commit 5a70a1925a
1 file changed
+66 -55
+66 -55
View File
@@ -11,23 +11,23 @@ import { useEffect, useMemo, useState } from 'react';
type LabelBase = {
value: number;
label: string;
} & ({ unit: 'cm' } | { unit: 'percent'; ratio: number })
} & ({ unit: 'cm' } | { unit: 'percent'; ratio: number });
export type BoneLabel = LabelBase & {
type: 'bone';
bone: SkeletonBone;
}
};
export type GroupLabel = LabelBase & {
type: 'group';
bones: GroupPartLabel[];
}
};
export type GroupPartLabel = LabelBase & {
type: 'group-part';
bone: SkeletonBone;
group: string;
}
};
export type Label = BoneLabel | GroupLabel | GroupPartLabel;
@@ -45,20 +45,13 @@ const BONE_MAPPING: Map<string, SkeletonBone[]> = new Map([
['skeleton_bone-arm_group', [SkeletonBone.UPPER_ARM, SkeletonBone.LOWER_ARM]],
]);
export type UpdateBoneParams =
{ newValue: number }
& (
| { type: 'bone', bone: SkeletonBone }
| { type: 'group', group: string }
| { type: 'group-part', group: string, bone: SkeletonBone }
);
export type UpdateBoneParams = { newValue: number } & (
| { type: 'bone'; bone: SkeletonBone }
| { type: 'group'; group: string }
| { type: 'group-part'; group: string; bone: SkeletonBone }
);
export function useManualProportions({
type
}: {
type: 'linear' | 'ratio',
}): {
export function useManualProportions({ type }: { type: 'linear' | 'ratio' }): {
bodyPartsGrouped: Label[];
changeBoneValue: (params: UpdateBoneParams) => void;
} {
@@ -69,45 +62,59 @@ export function useManualProportions({
const bodyPartsGrouped: Label[] = useMemo(() => {
if (!config) return [];
if (type === 'linear') {
return config.skeletonParts.map(({ bone, value }) => ({
type: 'bone',
unit: 'cm',
bone,
label: 'skeleton_bone-' + SkeletonBone[bone],
value,
} satisfies BoneLabel))
return config.skeletonParts.map(
({ bone, value }) =>
({
type: 'bone',
unit: 'cm',
bone,
label: 'skeleton_bone-' + SkeletonBone[bone],
value,
}) satisfies BoneLabel
);
}
return [
...BONE_MAPPING.keys().map((groupName) => {
const groupBones = BONE_MAPPING.get(groupName);
if (!groupBones) throw 'invalid state - this value should always exits'
const total = config.skeletonParts.filter(({ bone }) => groupBones.includes(bone)).reduce((acc, cur) => cur.value + acc, 0);
if (!groupBones) throw 'invalid state - this value should always exits';
const total = config.skeletonParts
.filter(({ bone }) => groupBones.includes(bone))
.reduce((acc, cur) => cur.value + acc, 0);
return {
type: 'group',
bones: config.skeletonParts.filter(({ bone }) => groupBones.includes(bone)).map(({ bone, value }) => ({
type: 'group-part',
group: groupName,
unit: 'percent',
bone,
label: 'skeleton_bone-' + SkeletonBone[bone],
value: value,
ratio: value / total,
})),
bones: config.skeletonParts
.filter(({ bone }) => groupBones.includes(bone))
.map(({ bone, value }) => ({
type: 'group-part',
group: groupName,
unit: 'percent',
bone,
label: 'skeleton_bone-' + SkeletonBone[bone],
value: value,
ratio: value / total,
})),
unit: 'cm',
label: groupName,
value: total,
} satisfies GroupLabel
} satisfies GroupLabel;
}),
...config.skeletonParts.filter(({ bone }) => !BONE_MAPPING.values().find((bones) => bones.includes(bone))).map(({ bone, value }) => ({
type: 'bone',
unit: 'cm',
bone,
label: 'skeleton_bone-' + SkeletonBone[bone],
value,
} satisfies BoneLabel))
...config.skeletonParts
.filter(
({ bone }) => !BONE_MAPPING.values().find((bones) => bones.includes(bone))
)
.map(
({ bone, value }) =>
({
type: 'bone',
unit: 'cm',
bone,
label: 'skeleton_bone-' + SkeletonBone[bone],
value,
}) satisfies BoneLabel
),
];
}, [config, type])
}, [config, type]);
useRPCPacket(RpcMessage.SkeletonConfigResponse, (data: SkeletonConfigResponseT) => {
setConfig(data);
@@ -123,13 +130,13 @@ export function useManualProportions({
if (!config) return;
if (params.type === 'group') {
const group = BONE_MAPPING.get(params.group);
if (!group) throw 'invalid state - group should exist'
if (!group) throw 'invalid state - group should exist';
const oldGroupTotal = config.skeletonParts
.filter(({ bone }) => group.includes(bone))
.reduce((acc, cur) => cur.value + acc, 0);
for (const part of group) {
const currentValue = config.skeletonParts.find(({ bone }) => bone === part);
if (!currentValue) throw 'invalid state - the bone should exists'
if (!currentValue) throw 'invalid state - the bone should exists';
const currentRatio = currentValue.value / oldGroupTotal;
sendRPCPacket(
RpcMessage.ChangeSkeletonConfigRequest,
@@ -140,15 +147,16 @@ export function useManualProportions({
if (params.type === 'group-part') {
const group = BONE_MAPPING.get(params.group);
if (!group) throw 'invalid state - group should exist'
if (!group) throw 'invalid state - group should exist';
const part = config.skeletonParts.find(({ bone }) => bone === params.bone);
if (!part) throw 'invalid state - the part should exists'
if (!part) throw 'invalid state - the part should exists';
const oldGroupTotal = config.skeletonParts
.filter(({ bone }) => group.includes(bone))
.reduce((acc, cur) => cur.value + acc, 0);
let newValue = part.value + oldGroupTotal * params.newValue // the new ratio is computed from the group size and not the bone
if (newValue <= 0) // Prevent ratios from getting below zero
newValue = 0
let newValue = part.value + oldGroupTotal * params.newValue; // the new ratio is computed from the group size and not the bone
if (newValue <= 0)
// Prevent ratios from getting below zero
newValue = 0;
sendRPCPacket(
RpcMessage.ChangeSkeletonConfigRequest,
@@ -160,12 +168,15 @@ export function useManualProportions({
const diffValue = Math.abs(newValue - part.value);
const signDiff = Math.sign(newValue - part.value);
for (const part of group) {
if (part === params.bone) continue
if (part === params.bone) continue;
const currentValue = config.skeletonParts.find(({ bone }) => bone === part);
if (!currentValue) throw 'invalid state - the bone should exists'
if (!currentValue) throw 'invalid state - the bone should exists';
sendRPCPacket(
RpcMessage.ChangeSkeletonConfigRequest,
new ChangeSkeletonConfigRequestT(part, currentValue.value - (diffValue / (group.length - 1)) * signDiff)
new ChangeSkeletonConfigRequestT(
part,
currentValue.value - (diffValue / (group.length - 1)) * signDiff
)
);
}
}
@@ -177,7 +188,7 @@ export function useManualProportions({
);
}
sendRPCPacket(RpcMessage.SkeletonConfigRequest, new SkeletonConfigRequestT());
}
},
};
}