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 anchor behavior when objects has custom origin #6970

Merged
merged 4 commits into from
Sep 18, 2024
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
22 changes: 18 additions & 4 deletions Extensions/AnchorBehavior/anchorruntimebehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,15 @@ namespace gdjs {
this._rightEdgeAnchor !== HorizontalAnchor.None &&
this._leftEdgeAnchor !== HorizontalAnchor.None
) {
this.owner.setWidth(right - left);
this.owner.setX(left);
const width = right - left;
this.owner.setX(
this.owner.getX() === this.owner.getDrawableX()
? left
: left +
((this.owner.getX() - this.owner.getDrawableX()) * width) /
this.owner.getWidth()
);
this.owner.setWidth(width);
} else {
if (this._leftEdgeAnchor !== HorizontalAnchor.None) {
this.owner.setX(
Expand All @@ -292,8 +299,15 @@ namespace gdjs {
this._bottomEdgeAnchor !== VerticalAnchor.None &&
this._topEdgeAnchor !== VerticalAnchor.None
) {
this.owner.setHeight(bottom - top);
this.owner.setY(top);
const height = bottom - top;
this.owner.setY(
this.owner.getY() === this.owner.getDrawableY()
? top
: top +
((this.owner.getY() - this.owner.getDrawableY()) * height) /
this.owner.getHeight()
);
this.owner.setHeight(height);
} else {
if (this._topEdgeAnchor !== VerticalAnchor.None) {
this.owner.setY(
Expand Down
65 changes: 65 additions & 0 deletions Extensions/AnchorBehavior/tests/anchorruntimebehavior.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,50 @@ describe('gdjs.AnchorRuntimeBehavior', function () {
return object;
}

const createSpriteWithOriginAtCenter = (behaviorProperties) => {
const object = new gdjs.TestSpriteRuntimeObject(runtimeScene, {
name: 'obj1',
type: '',
behaviors: [
{
name: anchorBehaviorName,
type: 'AnchorBehavior::AnchorBehavior',
// @ts-ignore - properties are not typed
rightEdgeAnchor: 0,
leftEdgeAnchor: 0,
topEdgeAnchor: 0,
bottomEdgeAnchor: 0,
relativeToOriginalWindowSize: false,
useLegacyBottomAndRightAnchors: false,
...behaviorProperties,
},
],
effects: [],
animations: [
{
name: 'animation',
directions: [
{
sprites: [
{
originPoint: { x: 50, y: 50 },
centerPoint: { x: 50, y: 50 },
points: [],
hasCustomCollisionMask: false,
customCollisionMask: [],
},
],
},
],
},
],
});
object.setUnscaledWidthAndHeight(100, 100);
object.setCustomWidthAndHeight(10, 10);
runtimeScene.addObject(object);
return object;
};

describe('(anchor horizontal edge)', function () {
['rightEdgeAnchor', 'leftEdgeAnchor'].forEach((objectEdge) => {
it(`anchors the ${objectEdge} edge of object to window left (fixed)`, function () {
Expand Down Expand Up @@ -200,5 +244,26 @@ describe('gdjs.AnchorRuntimeBehavior', function () {
expect(object.getY()).to.equal(1000);
expect(object.getWidth()).to.equal(10);
});

it('can fill the screen with an object (with custom origin)', function () {
setGameResolutionSizeAndStep(1000, 500);

const object = createSpriteWithOriginAtCenter({
leftEdgeAnchor: 1,
topEdgeAnchor: 1,
rightEdgeAnchor: 2,
bottomEdgeAnchor: 2,
});
object.setCustomWidthAndHeight(1000, 500);
object.setPosition(500, 250);
runtimeScene.renderAndStep(1000 / 60);

setGameResolutionSizeAndStep(2000, 3000);

expect(object.getX()).to.equal(1000);
expect(object.getY()).to.equal(1500);
expect(object.getWidth()).to.equal(2000);
expect(object.getHeight()).to.equal(3000);
});
});
});
8 changes: 8 additions & 0 deletions GDJS/tests/tests/Extensions/testspriteruntimeobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
return this._customHeight;
}

setWidth(width) {
this._customWidth = width;
}

setHeight(height) {
this._customHeight = height;
}

getCenterX() {
if (this._customCenterX === null) return super.getCenterX();
return this._customCenterX;
Expand Down
Loading