2019-05-29 00:40:29 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { Object3D } from 'three';
|
2019-05-29 23:04:06 +08:00
|
|
|
import { Area, Quest } from '../domain';
|
2019-05-29 00:40:29 +08:00
|
|
|
import { Renderer } from '../rendering/Renderer';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
quest?: Quest;
|
|
|
|
area?: Area;
|
|
|
|
model?: Object3D;
|
|
|
|
}
|
|
|
|
|
2019-05-29 23:04:06 +08:00
|
|
|
export class RendererComponent extends React.Component<Props> {
|
|
|
|
private renderer = new Renderer();
|
2019-05-29 00:40:29 +08:00
|
|
|
|
|
|
|
render() {
|
2019-05-29 20:46:29 +08:00
|
|
|
return <div style={{ overflow: 'hidden' }} ref={this.modifyDom} />;
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-05-29 20:46:29 +08:00
|
|
|
window.addEventListener('resize', this.onResize);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2019-05-29 20:46:29 +08:00
|
|
|
window.removeEventListener('resize', this.onResize);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps({ quest, area, model }: Props) {
|
|
|
|
if (model) {
|
2019-05-29 20:46:29 +08:00
|
|
|
this.renderer.setModel(model);
|
2019-05-29 00:40:29 +08:00
|
|
|
} else {
|
2019-05-29 20:46:29 +08:00
|
|
|
this.renderer.setQuestAndArea(quest, area);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:46:29 +08:00
|
|
|
private modifyDom = (div: HTMLDivElement) => {
|
|
|
|
this.renderer.setSize(div.clientWidth, div.clientHeight);
|
|
|
|
div.appendChild(this.renderer.domElement);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 20:46:29 +08:00
|
|
|
private onResize = () => {
|
|
|
|
const wrapperDiv = this.renderer.domElement.parentNode as HTMLDivElement;
|
|
|
|
this.renderer.setSize(wrapperDiv.clientWidth, wrapperDiv.clientHeight);
|
2019-05-29 00:40:29 +08:00
|
|
|
}
|
|
|
|
}
|