Skip to content

Commit

Permalink
fix(model3d): Save defaults returned from metadata (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored May 27, 2021
1 parent 688aa03 commit d027f10
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
28 changes: 22 additions & 6 deletions src/lib/viewers/box3d/model3d/Model3DViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class Model3DViewer extends Box3DViewer {
forward: null,
};

/** @property {Object} - Stores the defaults for the model settings */
defaults = {
projection: CAMERA_PROJECTION_PERSPECTIVE,
renderMode: RENDER_MODE_LIT,
showGrid: true,
};

/** @property {Object[]} - List of Box3D instances added to the scene */
instances = [];

Expand Down Expand Up @@ -226,6 +233,13 @@ class Model3DViewer extends Box3DViewer {
this.showGrid = DEFAULT_RENDER_GRID;
}

// Save the defaults so handleReset will change the values appropriately
this.defaults = {
projection: this.projection,
renderMode: this.renderMode,
showGrid: this.showGrid,
};

if (this.axes.up !== DEFAULT_AXIS_UP || this.axes.forward !== DEFAULT_AXIS_FORWARD) {
this.handleRotationAxisSet(this.axes.up, this.axes.forward, false);
}
Expand Down Expand Up @@ -347,24 +361,26 @@ class Model3DViewer extends Box3DViewer {
* @inheritdoc
*/
handleReset() {
const { projection, renderMode, showGrid } = this.defaults;

super.handleReset();

this.setAnimationState(false);
this.handleSetCameraProjection(CAMERA_PROJECTION_PERSPECTIVE);
this.handleSetRenderMode(RENDER_MODE_LIT);
this.handleShowGrid(true);
this.handleSetCameraProjection(projection);
this.handleSetRenderMode(renderMode);
this.handleShowGrid(showGrid);
this.handleShowSkeletons(false);
this.handleShowWireframes(false);

if (this.controls) {
if (this.getViewerOption('useReactControls')) {
this.renderUI();
} else {
this.controls.handleSetRenderMode(RENDER_MODE_LIT);
this.controls.setCurrentProjectionMode(CAMERA_PROJECTION_PERSPECTIVE);
this.controls.handleSetRenderMode(renderMode);
this.controls.setCurrentProjectionMode(projection);
this.controls.handleSetSkeletonsVisible(false);
this.controls.handleSetWireframesVisible(false);
this.controls.handleSetGridVisible(this.showGrid);
this.controls.handleSetGridVisible(showGrid);
}
}

Expand Down
43 changes: 32 additions & 11 deletions src/lib/viewers/box3d/model3d/__tests__/Model3DViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import Model3DControls from '../Model3DControls';
import Model3DRenderer from '../Model3DRenderer';
import Model3DViewer from '../Model3DViewer';
import {
CAMERA_PROJECTION_ORTHOGRAPHIC,
CAMERA_PROJECTION_PERSPECTIVE,
EVENT_CANVAS_CLICK,
EVENT_ROTATE_ON_AXIS,
EVENT_SELECT_ANIMATION_CLIP,
EVENT_SET_CAMERA_PROJECTION,
EVENT_SET_GRID_VISIBLE,
EVENT_SET_RENDER_MODE,
EVENT_SET_SKELETONS_VISIBLE,
EVENT_SET_WIREFRAMES_VISIBLE,
EVENT_SET_GRID_VISIBLE,
EVENT_TOGGLE_ANIMATION,
EVENT_TOGGLE_HELPERS,
RENDER_MODE_NORMALS,
RENDER_MODE_UNLIT,
} from '../model3DConstants';

const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -798,12 +802,29 @@ describe('lib/viewers/box3d/model3d/Model3DViewer', () => {
});

describe('handleReset()', () => {
beforeEach(() => {
model3d.defaults = {
projection: CAMERA_PROJECTION_ORTHOGRAPHIC,
renderMode: RENDER_MODE_NORMALS,
showGrid: false,
};
});

test('should reset control settings', () => {
sandbox.mock(model3d.controls).expects('handleSetRenderMode');
sandbox.mock(model3d.controls).expects('setCurrentProjectionMode');
sandbox
.mock(model3d.controls)
.expects('handleSetRenderMode')
.withArgs(RENDER_MODE_NORMALS);
sandbox
.mock(model3d.controls)
.expects('setCurrentProjectionMode')
.withArgs(CAMERA_PROJECTION_ORTHOGRAPHIC);
sandbox.mock(model3d.controls).expects('handleSetSkeletonsVisible');
sandbox.mock(model3d.controls).expects('handleSetWireframesVisible');
sandbox.mock(model3d.controls).expects('handleSetGridVisible');
sandbox
.mock(model3d.controls)
.expects('handleSetGridVisible')
.withArgs(false);
const renderMock = sandbox.mock(model3d.renderer);
renderMock.expects('stopAnimation').once();
model3d.handleReset();
Expand Down Expand Up @@ -833,18 +854,18 @@ describe('lib/viewers/box3d/model3d/Model3DViewer', () => {

test('should reset controls state and call renderUI', () => {
model3d.isAnimationPlaying = true;
model3d.projection = 'Orthographic';
model3d.renderMode = 'Normals';
model3d.showGrid = false;
model3d.projection = CAMERA_PROJECTION_PERSPECTIVE;
model3d.renderMode = RENDER_MODE_UNLIT;
model3d.showGrid = true;
model3d.showSkeletons = true;
model3d.showWireframes = true;

model3d.handleReset();

expect(model3d.isAnimationPlaying).toBe(false);
expect(model3d.projection).toBe('Perspective');
expect(model3d.renderMode).toBe('Lit');
expect(model3d.showGrid).toBe(true);
expect(model3d.projection).toBe(CAMERA_PROJECTION_ORTHOGRAPHIC);
expect(model3d.renderMode).toBe(RENDER_MODE_NORMALS);
expect(model3d.showGrid).toBe(false);
expect(model3d.showSkeletons).toBe(false);
expect(model3d.showWireframes).toBe(false);
expect(model3d.renderUI).toBeCalled();
Expand Down Expand Up @@ -986,7 +1007,7 @@ describe('lib/viewers/box3d/model3d/Model3DViewer', () => {

expect(getProps(model3d)).toMatchObject({
animationClips: [],
cameraProjection: 'Perspective',
cameraProjection: CAMERA_PROJECTION_PERSPECTIVE,
currentAnimationClipId: '123',
isPlaying: false,
isVrShown: false,
Expand Down

0 comments on commit d027f10

Please sign in to comment.