Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adapting collision mask to new sprites #5500

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ const CollisionMasksEditor = ({
animation
</Trans>
}
hideControlsForSprite={(sprite: gdSprite) =>
spriteConfiguration.adaptCollisionMaskAutomatically() ||
sprite.isFullImageCollisionMask()
}
/>
</Column>
</Line>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ type Props = {|
resourcesLoader: typeof ResourcesLoader,
resourceManagementProps: ResourceManagementProps,
onReplaceByDirection: (newDirection: gdDirection) => void,
onSpriteAdded: (sprite: gdSprite) => void,
onSpriteUpdated?: () => void,
onFirstSpriteUpdated?: () => void,
onChangeName: (newAnimationName: string) => void, // Used by piskel to set the name, if there is no name
Expand All @@ -186,6 +187,7 @@ const SpritesList = ({
resourcesLoader,
resourceManagementProps,
onReplaceByDirection,
onSpriteAdded,
onSpriteUpdated,
onFirstSpriteUpdated,
onChangeName,
Expand Down Expand Up @@ -312,6 +314,7 @@ const SpritesList = ({
if (allDirectionSpritesHaveSameCollisionMasks) {
copySpritePolygons(direction.getSprite(0), sprite);
}
onSpriteAdded(sprite); // Call the callback before `addSprite`, as `addSprite` will store a copy of it.
direction.addSprite(sprite);
sprite.delete();
});
Expand All @@ -336,6 +339,7 @@ const SpritesList = ({
resourceManagementProps,
forceUpdate,
onSpriteUpdated,
onSpriteAdded,
onFirstSpriteUpdated,
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Props = {|

setSameForAllAnimationsLabel: React.Node,
setSameForAllSpritesLabel: React.Node,

hideControlsForSprite?: (sprite: gdSprite) => boolean,
|};

/**
Expand All @@ -52,6 +54,7 @@ const SpriteSelector = ({
setSameForAllSprites,
setSameForAllAnimationsLabel,
setSameForAllSpritesLabel,
hideControlsForSprite,
}: Props) => {
const { animation, direction, sprite } = getCurrentElements(
spriteConfiguration,
Expand All @@ -60,12 +63,11 @@ const SpriteSelector = ({
spriteIndex
);

const hideControls =
const shouldHideControls =
!direction ||
!direction.getSpritesCount() ||
spriteConfiguration.adaptCollisionMaskAutomatically() ||
!sprite ||
sprite.isFullImageCollisionMask();
(hideControlsForSprite && hideControlsForSprite(sprite));

return (
<React.Fragment>
Expand Down Expand Up @@ -120,7 +122,7 @@ const SpriteSelector = ({
</SelectField>
)}
</ResponsiveLineStackLayout>
{!hideControls && (
{!shouldHideControls && (
<>
<Toggle
label={setSameForAllAnimationsLabel}
Expand Down
28 changes: 27 additions & 1 deletion newIDE/app/src/ObjectEditor/Editors/SpriteEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,27 @@ export default function SpriteEditor({
}
}
}

forceUpdate();
},
[spriteConfiguration, project, forceUpdate]
);

const onApplyFirstSpriteCollisionMaskToSprite = React.useCallback(
(sprite: gdSprite) => {
if (spriteConfiguration.getAnimationsCount() === 0) return;
const firstAnimation = spriteConfiguration.getAnimation(0);
if (firstAnimation.getDirectionsCount() === 0) return;
const firstDirection = firstAnimation.getDirection(0);
if (firstDirection.getSpritesCount() === 0) return;
const firstSprite = firstDirection.getSprite(0);
sprite.setFullImageCollisionMask(firstSprite.isFullImageCollisionMask());
sprite.setCustomCollisionMask(firstSprite.getCustomCollisionMask());

forceUpdate();
},
[spriteConfiguration, forceUpdate]
);

const moveAnimation = React.useCallback(
(targetIndex: number) => {
const draggedIndex = draggedAnimationIndex.current;
Expand Down Expand Up @@ -538,6 +553,17 @@ export default function SpriteEditor({
}
: undefined
}
onSpriteAdded={(sprite: gdSprite) => {
// If a sprite is added, we want to ensure it gets the automatic
// collision mask of the object, if the option is enabled.
if (
spriteConfiguration.adaptCollisionMaskAutomatically()
) {
onApplyFirstSpriteCollisionMaskToSprite(
sprite
);
}
}}
/>
);
}
Expand Down