From b4cc1bfcc643da58d1920ec2d8a3117f433130e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leslie=20Leigh=20=28=E6=9D=8E=E7=9A=84=E5=BA=8F=29?= Date: Mon, 18 Apr 2022 10:55:55 +0800 Subject: [PATCH] Add AnimationMask.joints setter (#10780) * Add AnimationMask.joints setter * Update * Unit test --- .../animation/marionette/animation-mask.ts | 9 +++- tests/animation/animation-mask.test.ts | 53 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/animation/animation-mask.test.ts diff --git a/cocos/core/animation/marionette/animation-mask.ts b/cocos/core/animation/marionette/animation-mask.ts index 3473b75d0e..ac0cc2e5e2 100644 --- a/cocos/core/animation/marionette/animation-mask.ts +++ b/cocos/core/animation/marionette/animation-mask.ts @@ -7,13 +7,20 @@ import { CLASS_NAME_PREFIX_ANIM } from '../define'; @ccclass(`${CLASS_NAME_PREFIX_ANIM}AnimationMask`) export class AnimationMask extends Asset { @serializable - @editable private _jointMasks: JointMask[] = []; + @editable get joints (): Iterable { return this._jointMasks; } + set joints (value) { + this.clear(); + for (const joint of value) { + this.addJoint(joint.path, joint.enabled); + } + } + /** * It's undefined behaviour to add same joint path for twice or more. * @param path diff --git a/tests/animation/animation-mask.test.ts b/tests/animation/animation-mask.test.ts new file mode 100644 index 0000000000..ed890866b5 --- /dev/null +++ b/tests/animation/animation-mask.test.ts @@ -0,0 +1,53 @@ +import { AnimationMask } from '../../cocos/core/animation/marionette/animation-mask'; +import 'jest-extended'; + +test('AnimationMask', () => { + const mask = new AnimationMask(); + expect(Array.from(mask.joints)).toStrictEqual([]); + + const getSortedJointMaskInfos = () => Array.from(mask.joints) + .sort(({ path: a }, { path: b }) => a.localeCompare(b)) + .map((j) => [j.path, j.enabled]); + + mask.addJoint('a', true); + mask.addJoint('a/b', true); + mask.addJoint('a/c', false); + mask.addJoint('b', true); + expect(getSortedJointMaskInfos()).toStrictEqual([ + ['a', true], + ['a/b', true], + ['a/c', false], + ['b', true], + ]); + + // Remove a leaf + mask.removeJoint('a/b'); + expect(getSortedJointMaskInfos()).toStrictEqual([ + ['a', true], + ['a/c', false], + ['b', true], + ]); + + // Remove a non-leaf, the remove is not recursive + mask.removeJoint('a'); + expect(getSortedJointMaskInfos()).toStrictEqual([ + ['a/c', false], + ['b', true], + ]); + + // Sets all joints, this operation will clear all old joint mask infos. + mask.joints = (function*(): Generator { + yield { path: 'c', enabled: true }; + yield { path: 'd', enabled: true }; + yield { path: 'd/a', enabled: false }; + })(); + expect(getSortedJointMaskInfos()).toStrictEqual([ + ['c', true], + ['d', true], + ['d/a', false], + ]); + + // Clear. + mask.clear(); + expect(Array.from(mask.joints)).toHaveLength(0); +}); \ No newline at end of file