Skip to content

Commit

Permalink
fix: [#2854] Actor pre/post draw events fixed (#2860)
Browse files Browse the repository at this point in the history
Closes #2854
  • Loading branch information
eonarheim authored Dec 31, 2023
1 parent a3ba631 commit 58b570a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixed Actor so it receives `predraw`/`postdraw` events per the advertised strongly typed events
- Fixed infinite loop :bomb: when certain degenerate polygons were attempted to be triangulated!
- Fixed incorrect type on `ex.Tilemap.getTileByPoint()`
- Fixed TS type on `GraphicsComponent` and allow `.material` to be null to unset, current workaround is using `.material = null as any`
Expand Down
3 changes: 3 additions & 0 deletions src/engine/Graphics/GraphicsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ParallaxComponent } from './ParallaxComponent';
import { CoordPlane } from '../Math/coord-plane';
import { BodyComponent } from '../Collision/BodyComponent';
import { FontCache } from './FontCache';
import { PostDrawEvent, PreDrawEvent } from '..';

export class GraphicsSystem extends System<TransformComponent | GraphicsComponent> {
public readonly types = ['ex.transform', 'ex.graphics'] as const;
Expand Down Expand Up @@ -126,6 +127,7 @@ export class GraphicsSystem extends System<TransformComponent | GraphicsComponen
if (graphics.onPreDraw) {
graphics.onPreDraw(this._graphicsContext, delta);
}
entity.events.emit('predraw', new PreDrawEvent(this._graphicsContext, delta, entity));

// TODO remove this hack on the particle redo
const particleOpacity = (entity instanceof Particle) ? entity.opacity : 1;
Expand All @@ -138,6 +140,7 @@ export class GraphicsSystem extends System<TransformComponent | GraphicsComponen
if (graphics.onPostDraw) {
graphics.onPostDraw(this._graphicsContext, delta);
}
entity.events.emit('postdraw', new PostDrawEvent(this._graphicsContext, delta, entity));

this._graphicsContext.restore();

Expand Down
22 changes: 22 additions & 0 deletions src/spec/ActorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,28 @@ describe('A game actor', () => {
expect(animation.tick).toHaveBeenCalledWith(100, 2);
});

it('will receive predraw/postdraw', () => {
const predraw = jasmine.createSpy('predraw');
const postdraw = jasmine.createSpy('postdraw');

const actor = new ex.Actor({
name: 'events',
pos: ex.vec(100, 100),
width: 100,
height: 100,
color: ex.Color.Red
});

actor.events.on('predraw', predraw);
actor.events.on('postdraw', postdraw);

scene.add(actor);
scene.draw(engine.graphicsContext, 100);

expect(predraw).toHaveBeenCalledOnceWith(new ex.PreDrawEvent(engine.graphicsContext, 100, actor));
expect(postdraw).toHaveBeenCalledOnceWith(new ex.PostDrawEvent(engine.graphicsContext, 100, actor));
});

it('can detect containment off of child actors', () => {
const parent = new ex.Actor({ x: 600, y: 100, width: 100, height: 100 });
const child = new ex.Actor({ x: 0, y: 0, width: 100, height: 100 });
Expand Down

0 comments on commit 58b570a

Please sign in to comment.