Skip to content

Commit

Permalink
refactor update manager for easier usage
Browse files Browse the repository at this point in the history
  • Loading branch information
daign committed May 18, 2023
1 parent e6d3998 commit fc8b695
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 46 deletions.
7 changes: 7 additions & 0 deletions lib/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ export class Application extends Group {
public fitToContent( margin?: number ): void {
this.drawingLayer.fitToContent( margin );
}

/**
* Redraw and render the application.
*/
public redraw(): void {
this.updateManager.redraw();
}
}
2 changes: 1 addition & 1 deletion lib/application/controlLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ControlLayer extends Group {
this.baseClass = 'control-layer';

// Subscribe to the update manager to know when to redraw the controls.
this.application.updateManager.redrawControlsSignal.setObserver( (): void => {
this.application.updateManager.setRedrawControlsFunction( (): void => {
this.redrawControls();
} )
}
Expand Down
4 changes: 3 additions & 1 deletion lib/application/interactiveViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ export class InteractiveViewport extends Viewport {
}

this.updateViewport();
this.application.redraw();
};

// Define action to deactivate element.
handle.clicked = (): void => {
this.application.selectionManager.setSelection( null, null );
this.application.updateManager.redrawSignal.emit();
this.application.redraw();
};

// Define zoom action.
Expand All @@ -156,6 +157,7 @@ export class InteractiveViewport extends Viewport {
this.viewCenter.sub( mousePosition ).multiplyScalar( 1 / factor ).add( mousePosition );

this.updateViewport();
this.application.redraw();
};
}
}
Expand Down
65 changes: 44 additions & 21 deletions lib/application/updateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,55 @@ import { EventEmitter, UnicastEventEmitter } from '@daign/observable';
* Class that manages the steps to update and redraw the graphic.
*/
export class UpdateManager {
// Incoming signal to run the update and redraw cycle.
public redrawSignal: UnicastEventEmitter = new UnicastEventEmitter();
// Event that other components can use to get informed about a redraw.
private redrawEvent: EventEmitter = new EventEmitter();

// Components that want to update when the graphic was changed subscribe to this event.
public graphicUpdateEvent: EventEmitter = new EventEmitter();
// The event that lets the control layer know when to redraw the controls.
private redrawControlsSignal: UnicastEventEmitter = new UnicastEventEmitter();

// The control layer subscribes to this event to know when to redraw the controls.
public redrawControlsSignal: UnicastEventEmitter = new UnicastEventEmitter();

// Subscribe your render function to this event.
public renderSignal: UnicastEventEmitter = new UnicastEventEmitter();
// The event that the render function subscribes to.
private renderSignal: UnicastEventEmitter = new UnicastEventEmitter();

/**
* Constructor.
*/
public constructor() {
// Run the update and redraw cycle on incoming signal.
this.redrawSignal.setObserver( (): void => {
// Notify components that want to update when the graphic was changed.
this.graphicUpdateEvent.emit();

// Redraw the controls.
this.redrawControlsSignal.emit();

// Render the graphic.
this.renderSignal.emit();
} );
public constructor() {}

/**
* Set a callback that gets informed when a redraw event was started.
* @param callback - The callback that will be called when a redraw event was started.
*/
public subscribeToRedrawEvent( callback: () => void ): void {
this.redrawEvent.subscribeToChanges( callback );
}

/**
* Set the callback that redraws the control layer.
* @param callback - The callback that redraws the control layer.
*/
public setRedrawControlsFunction( callback: () => void ): void {
this.redrawControlsSignal.setObserver( callback );
}

/**
* Set the callback that renders the graphic.
* @param callback - The callback that renders the graphic.
*/
public setRenderFunction( callback: () => void ): void {
this.renderSignal.setObserver( callback );
}

/**
* Redraw and render the application.
*/
public redraw(): void {
// Notify components that want to be informed about a redraw event.
this.redrawEvent.emit();

// Redraw the controls.
this.redrawControlsSignal.emit();

// Render the graphic.
this.renderSignal.emit();
}
}
2 changes: 0 additions & 2 deletions lib/application/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,5 @@ export class Viewport extends Group {
this.decenteringTransform.translation.copy( decentering );
this.scaleTransform.scaling.copy( scaling );
this.translateTransform.translation.copy( translation );

this.application.updateManager.redrawSignal.emit();
}
}
2 changes: 1 addition & 1 deletion lib/control-elements/controlPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ export class ControlPoint extends Group {

this.calculateOffset();

this.application.updateManager.redrawSignal.emit();
this.application.redraw();
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@daign/2d-graphics",
"version": "1.1.3",
"version": "1.1.4",
"description": "Two dimensional graphics library that implements the daign-2d-pipeline.",
"keywords": [
"graphics",
Expand Down
15 changes: 15 additions & 0 deletions test/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ describe( 'Application', (): void => {
expect( spyFitToContent.calledWith( 2 ) ).to.be.true;
} );
} );

describe( 'redraw', (): void => {
it( 'should call redraw on update manager', (): void => {
// Arrange
const context = new TestContext();
const application = new Application( context );
const redrawSpy = spy( application.updateManager, 'redraw' );

// Act
application.redraw();

// Assert
expect( redrawSpy.calledOnce ).to.be.true;
} );
} );
} );
2 changes: 1 addition & 1 deletion test/application/controlLayer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe( 'ControlLayer', (): void => {
const redrawControlsSpy = spy( controlLayer as any, 'redrawControls' );

// Act
application.updateManager.redrawControlsSignal.emit();
application.updateManager.redraw();

// Assert
expect( redrawControlsSpy.calledOnce ).to.be.true;
Expand Down
4 changes: 2 additions & 2 deletions test/application/interarctiveViewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe( 'InteractiveViewport', (): void => {
const dragEvent = new MockEvent().setClientPoint( 1, 10 );
const endEvent = new MockEvent().setClientPoint( 2, 10 );

const redrawSpy = spy( application.updateManager.redrawSignal, 'emit' );
const redrawSpy = spy( application.updateManager, 'redraw' );

// Act
domNode.sendEvent( 'mousedown', startEvent );
Expand Down Expand Up @@ -390,7 +390,7 @@ describe( 'InteractiveViewport', (): void => {
const controlObject = new TestObject();
application.selectionManager.setSelection( controlObject, null );

const redrawSpy = spy( application.updateManager.redrawSignal, 'emit' );
const redrawSpy = spy( application.updateManager, 'redraw' );

const clickEvent = new MockEvent();
clickEvent.setOffsetPoint( 100, 100 );
Expand Down
34 changes: 19 additions & 15 deletions test/application/updateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,60 @@ import { Application, UpdateManager } from '../../lib';
import { TestContext } from '../testContext';

describe( 'UpdateManager', (): void => {
describe( 'constructor', (): void => {
it( 'should emit a graphic update event when the redraw signal is received', (): void => {
describe( 'subscribeToRedrawEvent', (): void => {
it( 'should register a callback that gets informed about redraw events', (): void => {
// Arrange
const context = new TestContext();
const application = new Application( context, true );
const view = new View();
view.mountNode( application );

const updateManager = new UpdateManager();
const graphicUpdateSpy = spy( updateManager.graphicUpdateEvent, 'emit' );

// Act
updateManager.redrawSignal.emit();
const redrawSpy = spy();
updateManager.subscribeToRedrawEvent( redrawSpy );
updateManager.redraw();

// Assert
expect( graphicUpdateSpy.calledOnce ).to.be.true;
expect( redrawSpy.calledOnce ).to.be.true;
} );
} );

it( 'should emit a redraw controls signal when the redraw signal is received', (): void => {
describe( 'setRedrawControlsFunction', (): void => {
it( 'should register a callback that gets informed when to redraw the controls', (): void => {
// Arrange
const context = new TestContext();
const application = new Application( context, true );
const view = new View();
view.mountNode( application );

const updateManager = new UpdateManager();
const redrawControlsSpy = spy( updateManager.redrawControlsSignal, 'emit' );

// Act
updateManager.redrawSignal.emit();
const redrawControlsSpy = spy();
updateManager.setRedrawControlsFunction( redrawControlsSpy );
updateManager.redraw();

// Assert
expect( redrawControlsSpy.calledOnce ).to.be.true;
} );
} );

it( 'should emit a render signal when the redraw signal is received', (): void => {
describe( 'setRenderFunction', (): void => {
it( 'should register a callback that gets informed when to render the graphic', (): void => {
// Arrange
const context = new TestContext();
const application = new Application( context, true );
const view = new View();
view.mountNode( application );

const updateManager = new UpdateManager();
const renderSignalSpy = spy( updateManager.renderSignal, 'emit' );

// Act
updateManager.redrawSignal.emit();
const renderSpy = spy();
updateManager.setRenderFunction( renderSpy );
updateManager.redraw();

// Assert
expect( renderSignalSpy.calledOnce ).to.be.true;
expect( renderSpy.calledOnce ).to.be.true;
} );
} );
} );
2 changes: 1 addition & 1 deletion test/control-elements/controlPoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe( 'ControlPoint', (): void => {
expect( controlPoint.center.equals( points[ 0 ] ) ).to.be.true;
controlPoint.snap();

const redrawSpy = spy( application.updateManager.redrawSignal, 'emit' );
const redrawSpy = spy( application.updateManager, 'redraw' );

// Act
controlPoint.drag( new Vector2( 2, 4 ) );
Expand Down

0 comments on commit fc8b695

Please sign in to comment.