-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
28 deletions.
There are no files selected for viewing
116 changes: 94 additions & 22 deletions
116
bouncy_frontend/src/lib/components/editor/PoseEditForm.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,126 @@ | ||
<script> | ||
import { GRAY_COLORING } from '$lib/constants'; | ||
import { t } from '$lib/i18n'; | ||
import { | ||
PoseWrapper, | ||
Skeleton, | ||
SkeletonField, | ||
} from '$lib/instructor/bouncy_instructor'; | ||
import Svg from '../avatar/Svg.svelte'; | ||
import SvgAvatar from '../avatar/SvgAvatar.svelte'; | ||
/** @type {PoseWrapper} */ | ||
export let pose; | ||
/** @type {PoseWrapper | undefined} */ | ||
let pose; | ||
/** @type {Skeleton | undefined} */ | ||
let skeleton; | ||
/** @type {WeightedPoseLimb[]} */ | ||
let weightedLimbs = []; | ||
/** @param {PoseWrapper} newPose */ | ||
export function loadPose(newPose) { | ||
// using an indirect setting, rather than a property, to better control when | ||
// the updates happen | ||
pose = newPose; | ||
onPoseUpdated(pose); | ||
} | ||
/** @param {PoseWrapper} newPose */ | ||
function onPoseUpdated(newPose) { | ||
skeleton = newPose.skeleton(); | ||
weightedLimbs = extractWeightedLimbs(newPose); | ||
} | ||
$: skeleton = pose.skeleton(); | ||
$: weightedLimbs = extractWeightedLimbs(pose); | ||
$: markedLimbIndices = weightedLimbs.map((limb) => limb.index); | ||
/** | ||
* @param {PoseWrapper} pose | ||
* @returns {WeightedPoseLimb[]} | ||
*/ | ||
function extractWeightedLimbs(pose) { | ||
return allSkeletonFields() | ||
.map((limb) => ({ | ||
name: SkeletonField[limb], | ||
index: limb, | ||
weight: pose.getWeight(limb), | ||
})) | ||
.filter((limb) => limb.weight > 0.0); | ||
} | ||
/** @param {number} weightedLimbIndex */ | ||
function updateAvatar(weightedLimbIndex) { | ||
let limb = weightedLimbs[weightedLimbIndex]; | ||
if (pose && limb) { | ||
pose.setWeight(limb.index, limb.weight); | ||
onPoseUpdated(pose); | ||
} else { | ||
console.log('Pose or limb not available'); | ||
} | ||
} | ||
function allSkeletonFields() { | ||
// using reduce as a map + filter to avoid the extra array | ||
return Object.keys(SkeletonField).reduce( | ||
/** @type {(acc: WeightedPoseLimb[], field: string) => WeightedPoseLimb[]} */ | ||
/** @type {(acc: number[], field: string) => number[]} */ | ||
(acc, field) => { | ||
// enum has static values for indices and names, we want to filter only the numbers | ||
let limb = SkeletonField[field]; | ||
if (typeof limb === 'number') { | ||
let weight = pose.getWeight(limb); | ||
if (weight > 0.0) { | ||
acc.push({ name: SkeletonField[limb], index: limb, weight }); | ||
} | ||
acc.push(limb); | ||
} | ||
return acc; | ||
}, | ||
/** @type {WeightedPoseLimb[]}*/ | ||
/** @type {number[]}*/ | ||
[] | ||
); | ||
} | ||
</script> | ||
|
||
<div class="avatar"> | ||
<Svg width={300} height={300} orderByZ> | ||
{#if skeleton} | ||
<SvgAvatar | ||
{skeleton} | ||
width={300} | ||
height={300} | ||
style={GRAY_COLORING} | ||
{markedLimbIndices} | ||
></SvgAvatar> | ||
{/if} | ||
</Svg> | ||
<div class="container"> | ||
<div class="avatar"> | ||
<Svg width={300} height={300} orderByZ> | ||
{#if skeleton} | ||
<SvgAvatar | ||
{skeleton} | ||
width={300} | ||
height={300} | ||
style={GRAY_COLORING} | ||
{markedLimbIndices} | ||
></SvgAvatar> | ||
{/if} | ||
</Svg> | ||
</div> | ||
|
||
<div class="weights"> | ||
{#each weightedLimbs as limb, index} | ||
<div class="body-part"> | ||
<label for={limb.name}>{$t(limb.name)}</label> | ||
<input | ||
name={limb.name} | ||
type="number" | ||
class="weight-input" | ||
bind:value={limb.weight} | ||
on:input={() => updateAvatar(index)} | ||
placeholder="Weight" | ||
/> | ||
</div> | ||
{/each} | ||
</div> | ||
</div> | ||
|
||
<style> | ||
.container { | ||
display: grid; | ||
grid-template-columns: 2fr 1fr; | ||
} | ||
.body-part { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
width: 90%; | ||
margin: auto; | ||
} | ||
.weight-input { | ||
width: 100%; | ||
text-align: center; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters