mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Pose graph copy paste operation (#15132)
This commit is contained in:
committed by
GitHub
parent
f84baf24c5
commit
da8d1979d0
@@ -2,10 +2,12 @@ import { PoseGraphNode } from "../../../cocos/animation/marionette/pose-graph/fo
|
||||
import {
|
||||
getPoseGraphNodeEditorMetadata, PoseGraphCreateNodeContext, PoseGraphNodeAppearanceOptions,
|
||||
} from "../../../cocos/animation/marionette/pose-graph/foundation/authoring/node-authoring";
|
||||
import { js } from "../../../cocos/core/utils";
|
||||
import { PoseGraph, poseGraphOp } from "../../exports/new-gen-anim";
|
||||
import { instantiate } from "../../../cocos/serialization";
|
||||
import { PoseGraphOutputNode } from "../../../cocos/animation/marionette/pose-graph/graph-output-node";
|
||||
import { PoseNode } from "../../../cocos/animation/marionette/pose-graph/pose-node";
|
||||
import { PureValueNode } from "../../../cocos/animation/marionette/pose-graph/pure-value-node";
|
||||
import { poseGraphOp } from '../../../cocos/animation/marionette/pose-graph/op';
|
||||
import { assertIsTrue, editorExtrasTag } from "../../../exports/base";
|
||||
|
||||
type Constructor<T = unknown> = new (...args: any[]) => T;
|
||||
|
||||
@@ -84,3 +86,152 @@ export function getInputDefaultDisplayName(inputKey: poseGraphOp.InputKey) {
|
||||
return `${inputKey[0]}[${inputKey[1]}]`;
|
||||
}
|
||||
}
|
||||
|
||||
function clonePoseGraphNode(node: PoseGraphNode) {
|
||||
return instantiate(node);
|
||||
}
|
||||
|
||||
interface PoseGraphOutputNodeCopyInfo {
|
||||
editorExtras: PoseGraphOutputNode[typeof editorExtrasTag];
|
||||
}
|
||||
|
||||
function copyPoseGraphOutputNode(node: PoseGraphOutputNode): PoseGraphOutputNodeCopyInfo {
|
||||
return {
|
||||
editorExtras: instantiate(node[editorExtrasTag]),
|
||||
};
|
||||
}
|
||||
|
||||
function pastPoseGraphOutputNode(node: PoseGraphOutputNode, copyInfo: PoseGraphOutputNodeCopyInfo) {
|
||||
node[editorExtrasTag] = copyInfo.editorExtras;
|
||||
}
|
||||
|
||||
interface PoseGraphNodesCopyInfo {
|
||||
nodes: (PoseGraphNode | PoseGraphOutputNodeCopyInfo)[];
|
||||
|
||||
bindings: Array<{
|
||||
consumer: number;
|
||||
inputKey: poseGraphOp.InputKey;
|
||||
producer: number;
|
||||
outputKey: poseGraphOp.OutputKey;
|
||||
}>;
|
||||
}
|
||||
|
||||
function cloneInputKey(inputKey: poseGraphOp.InputKey) {
|
||||
assertIsTrue(Array.isArray(inputKey) && inputKey.every((v) => typeof v === 'number' || typeof v === 'string'));
|
||||
return inputKey.slice() as unknown as poseGraphOp.InputKey;
|
||||
}
|
||||
|
||||
export interface CopyPoseGraphNodesOptions {
|
||||
copyOutputNodeEditorExtras?: boolean;
|
||||
}
|
||||
|
||||
export function copyPoseGraphNodes(poseGraph: PoseGraph, nodes: PoseGraphNode[], options: CopyPoseGraphNodesOptions = {
|
||||
copyOutputNodeEditorExtras: true,
|
||||
}): PoseGraphNodesCopyInfo {
|
||||
const nodesDeduplicated = [...new Set(nodes)];
|
||||
|
||||
// Copy nodes.
|
||||
const nodeCopyInfos = nodesDeduplicated.map((node) => {
|
||||
if (node === poseGraph.outputNode) {
|
||||
return options.copyOutputNodeEditorExtras ? copyPoseGraphOutputNode(poseGraph.outputNode) : {} as PoseGraphOutputNodeCopyInfo;
|
||||
} else {
|
||||
return clonePoseGraphNode(node);
|
||||
}
|
||||
});
|
||||
|
||||
// Copy bindings.
|
||||
const bindingCopyInfos: PoseGraphNodesCopyInfo['bindings'] = [];
|
||||
nodesDeduplicated.forEach((node, consumerNodeIndex) => {
|
||||
for (const inputKey of poseGraphOp.getInputKeys(node)) {
|
||||
const binding = poseGraphOp.getInputBinding(poseGraph, node, inputKey);
|
||||
if (!binding) {
|
||||
continue;
|
||||
}
|
||||
const producerNode = binding.producer;
|
||||
const producerNodeIndex = nodesDeduplicated.indexOf(producerNode);
|
||||
if (producerNodeIndex < 0) {
|
||||
continue;
|
||||
}
|
||||
bindingCopyInfos.push({
|
||||
consumer: consumerNodeIndex,
|
||||
inputKey: cloneInputKey(inputKey),
|
||||
producer: producerNodeIndex,
|
||||
outputKey: binding.outputIndex,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
nodes: nodeCopyInfos,
|
||||
bindings: bindingCopyInfos,
|
||||
};
|
||||
}
|
||||
|
||||
export interface pastePoseGraphNodesResult {
|
||||
addedNodes: PoseGraphNode[];
|
||||
}
|
||||
|
||||
export interface PastePoseGraphNodesOptions {
|
||||
outputNodeBindingRedirect?: {
|
||||
consumerNode: PoseNode;
|
||||
inputKey: poseGraphOp.InputKey;
|
||||
};
|
||||
}
|
||||
|
||||
export function pastePoseGraphNodes(
|
||||
poseGraph: PoseGraph, copyInfo: PoseGraphNodesCopyInfo,
|
||||
options: PastePoseGraphNodesOptions = { },
|
||||
) {
|
||||
const { nodes: nodeCopyInfos, bindings: bindingCopyInfos } = copyInfo;
|
||||
|
||||
const addedNodes: PoseGraphNode[] = [];
|
||||
|
||||
// Past nodes.
|
||||
for (const nodeCopyInfo of nodeCopyInfos) {
|
||||
if (nodeCopyInfo instanceof PoseGraphNode) {
|
||||
poseGraph.addNode(nodeCopyInfo);
|
||||
addedNodes.push(nodeCopyInfo);
|
||||
} else {
|
||||
pastPoseGraphOutputNode(poseGraph.outputNode, nodeCopyInfo);
|
||||
}
|
||||
}
|
||||
|
||||
// Paste bindings.
|
||||
for (const { consumer, inputKey, producer, outputKey } of bindingCopyInfos) {
|
||||
assertIsTrue(consumer >= 0 && consumer < nodeCopyInfos.length);
|
||||
assertIsTrue(producer >= 0 && producer < nodeCopyInfos.length);
|
||||
const consumerCopyInfo = nodeCopyInfos[consumer];
|
||||
const producerNode = nodeCopyInfos[producer];
|
||||
assertIsTrue(producerNode instanceof PoseGraphNode);
|
||||
if (!(consumerCopyInfo instanceof PoseGraphNode) && options.outputNodeBindingRedirect) {
|
||||
poseGraphOp.connectNode(
|
||||
poseGraph,
|
||||
options.outputNodeBindingRedirect.consumerNode,
|
||||
options.outputNodeBindingRedirect.inputKey,
|
||||
producerNode,
|
||||
outputKey,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
const consumerNode = consumerCopyInfo instanceof PoseGraphNode
|
||||
? consumerCopyInfo
|
||||
: poseGraph.outputNode;
|
||||
poseGraphOp.connectNode(
|
||||
poseGraph,
|
||||
consumerNode,
|
||||
inputKey,
|
||||
producerNode,
|
||||
outputKey,
|
||||
);
|
||||
}
|
||||
|
||||
// We're doing a cut.
|
||||
nodeCopyInfos.length = 0;
|
||||
bindingCopyInfos.length = 0;
|
||||
|
||||
return {
|
||||
addedNodes,
|
||||
};
|
||||
}
|
||||
|
||||
export * from './pose-graph/drag';
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
export { getPoseGraphAssetDragHandlersMap, createPoseNodeOnAssetDrag } from './registry';
|
||||
export type { PoseGraphAssetDragHandlersInfo } from './registry';
|
||||
@@ -0,0 +1,82 @@
|
||||
import { PoseGraphNode } from "../../../../../cocos/animation/marionette/pose-graph/foundation/pose-graph-node";
|
||||
import { assertIsTrue, Asset, Constructor } from "../../../../../exports/base";
|
||||
|
||||
/**
|
||||
* In pose graph, describes the drag handling info for an asset type.
|
||||
*/
|
||||
export type PoseGraphAssetDragHandlersInfo = {
|
||||
/**
|
||||
* All handlers handling dragging on this asset type. The key is handler id.
|
||||
*/
|
||||
handlers: Record<string, {
|
||||
/**
|
||||
* The handler's display name.
|
||||
*/
|
||||
displayName: string;
|
||||
}>
|
||||
};
|
||||
|
||||
/**
|
||||
* Query all asset drag handlers.
|
||||
*/
|
||||
export function* getPoseGraphAssetDragHandlersMap(): Generator<[Constructor<Asset>, PoseGraphAssetDragHandlersInfo]> {
|
||||
for (const [assetType, register] of assetDragHandlerRegistry.entries()) {
|
||||
const info: PoseGraphAssetDragHandlersInfo = { handlers: {} };
|
||||
for (const [handlerId, handler] of Object.entries(register.handlers)) {
|
||||
info.handlers[handlerId] = {
|
||||
displayName: handler.displayName,
|
||||
};
|
||||
}
|
||||
yield [assetType, info];
|
||||
}
|
||||
}
|
||||
|
||||
export function createPoseNodeOnAssetDrag(asset: Asset, handlerId: string): PoseGraphNode | undefined {
|
||||
const assetType = asset.constructor as new () => Asset;
|
||||
|
||||
const assetDragRegister = assetDragHandlerRegistry.get(assetType);
|
||||
if (!assetDragRegister) {
|
||||
console.warn(`There has any no handlers for asset type ${assetType}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const handler = assetDragRegister.handlers[handlerId];
|
||||
if (!handler) {
|
||||
console.warn(`Asset type ${assetType} has no handler ${handlerId}, existing handlers are ${Object.keys(assetDragRegister.handlers).join(',')}`);
|
||||
return;
|
||||
}
|
||||
|
||||
return handler.handle(asset);
|
||||
}
|
||||
|
||||
const assetDragHandlerRegistry: Map<new () => Asset, AssetDragHandlerRegister<Asset>> = new Map();
|
||||
|
||||
export interface AssetDragHandler<TAssetType extends Asset> {
|
||||
displayName: string;
|
||||
|
||||
handle(asset: TAssetType): PoseGraphNode | undefined;
|
||||
}
|
||||
|
||||
interface AssetDragHandlerRegister<TAssetType extends Asset> {
|
||||
handlers: Record<string, AssetDragHandler<TAssetType>>;
|
||||
}
|
||||
|
||||
let handlerIdGenerator = 1;
|
||||
|
||||
export function registerCreatePoseNodeOnAssetDragHandler<TAssetType extends Asset>(
|
||||
assetType: new () => TAssetType,
|
||||
handler: AssetDragHandler<TAssetType>,
|
||||
) {
|
||||
let assetDragHandlerRegister = assetDragHandlerRegistry.get(assetType);
|
||||
if (!assetDragHandlerRegister) {
|
||||
assetDragHandlerRegister = {
|
||||
handlers: {},
|
||||
};
|
||||
assetDragHandlerRegistry.set(assetType, assetDragHandlerRegister);
|
||||
}
|
||||
|
||||
const handlerId = handlerIdGenerator++;
|
||||
assertIsTrue(!(handlerId in assetDragHandlerRegister.handlers));
|
||||
|
||||
assetDragHandlerRegister.handlers[handlerId] = handler;
|
||||
}
|
||||
@@ -6,9 +6,13 @@ import {
|
||||
isAnimationTransition,
|
||||
EmptyState,
|
||||
SubStateMachine,
|
||||
PoseState,
|
||||
} from "../../../cocos/animation/marionette/animation-graph";
|
||||
import { cloneAnimationGraphEditorExtrasFrom } from "../../../cocos/animation/marionette/animation-graph-editor-extras-clone-helper";
|
||||
import { MotionState } from "../../../cocos/animation/marionette/state-machine/motion-state";
|
||||
import { assertIsTrue } from "../../../cocos/core/data/utils/asserts";
|
||||
import { editorExtrasTag } from "../../../exports/base";
|
||||
import { copyPoseGraphNodes, pastePoseGraphNodes } from "../../exports/new-gen-anim";
|
||||
|
||||
function copyTransitionConditions(lhs: Transition, rhs: Transition) {
|
||||
lhs.conditions = rhs.conditions.map((condition) => condition.clone());
|
||||
@@ -40,7 +44,7 @@ function copyTransition<T extends Transition>(lhs: T, rhs: T) {
|
||||
* The method `clone` has the signature: `(host: EditorExtendableObject) => unknown`.
|
||||
* Otherwise, if no `clone` method provide, the new editor extras would be set to undefined.
|
||||
*/
|
||||
export function cloneState<TState extends MotionState | EmptyState | SubStateMachine>(
|
||||
export function cloneState<TState extends MotionState | EmptyState | SubStateMachine | PoseState>(
|
||||
stateMachine: StateMachine,
|
||||
state: TState,
|
||||
includeTransitions: boolean,
|
||||
@@ -62,11 +66,11 @@ export function cloneState<TState extends MotionState | EmptyState | SubStateMac
|
||||
*/
|
||||
export function cloneState(
|
||||
stateMachine: StateMachine,
|
||||
state: MotionState | EmptyState | SubStateMachine,
|
||||
state: MotionState | EmptyState | SubStateMachine | PoseState,
|
||||
targetStateMachine: StateMachine,
|
||||
): SubStateMachine;
|
||||
|
||||
export function cloneState(stateMachine: StateMachine, state: MotionState | EmptyState | SubStateMachine, includeTransitions: boolean | StateMachine) {
|
||||
export function cloneState(stateMachine: StateMachine, state: MotionState | EmptyState | SubStateMachine | PoseState, includeTransitions: boolean | StateMachine) {
|
||||
const newStateOwner = typeof includeTransitions === 'boolean' ? stateMachine : includeTransitions;
|
||||
let newState: State;
|
||||
if (state instanceof MotionState) {
|
||||
@@ -75,6 +79,11 @@ export function cloneState(stateMachine: StateMachine, state: MotionState | Empt
|
||||
} else if (state instanceof EmptyState) {
|
||||
const newEmptyState = newState = newStateOwner.addEmpty();
|
||||
state.copyTo(newEmptyState);
|
||||
} else if (state instanceof PoseState) {
|
||||
const newPoseState = newState = newStateOwner.addPoseState();
|
||||
newPoseState[editorExtrasTag] = cloneAnimationGraphEditorExtrasFrom(state);
|
||||
const copyInfo = copyPoseGraphNodes(state.graph, [...state.graph.nodes()]);
|
||||
pastePoseGraphNodes(newPoseState.graph, copyInfo);
|
||||
} else /* if (state instanceof SubStateMachine) */ {
|
||||
const newSubStateMachine = newState = newStateOwner.addSubStateMachine();
|
||||
state.copyTo(newSubStateMachine);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Layer, PoseState, StateMachine } from "../../../../cocos/animation/marionette/animation-graph";
|
||||
import { PoseNodeStateMachine } from '../../../../cocos/animation/marionette/pose-graph/pose-nodes/state-machine';
|
||||
import { PoseGraph, PoseNode } from "../../../exports/new-gen-anim";
|
||||
|
||||
export function* visitPoseNodeInLayer(layer: Layer): Generator<PoseNode> {
|
||||
yield* visitPoseNodesInStateMachine(layer.stateMachine);
|
||||
for (const [stashId, stash] of layer.stashes()) {
|
||||
yield* visitPoseNodeInPoseGraph(stash.graph);
|
||||
}
|
||||
}
|
||||
|
||||
export function* visitPoseNodeInPoseGraph(poseGraph: PoseGraph): Generator<PoseNode> {
|
||||
for (const node of poseGraph.nodes()) {
|
||||
if (node instanceof PoseNode) {
|
||||
yield node;
|
||||
}
|
||||
if (node instanceof PoseNodeStateMachine) {
|
||||
yield* visitPoseNodesInStateMachine(node.stateMachine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function* visitPoseNodesInStateMachine(stateMachine: StateMachine) {
|
||||
for (const state of stateMachine.states()) {
|
||||
if (state instanceof PoseState) {
|
||||
yield* visitPoseNodeInPoseGraph(state.graph);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user