2019-05-29 00:40:29 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { Object3D } from 'three';
|
2019-05-29 20:46:29 +08:00
|
|
|
import { entitySelected } from '../actions';
|
2019-05-29 00:40:29 +08:00
|
|
|
import { Renderer } from '../rendering/Renderer';
|
|
|
|
import { Area, Quest, VisibleQuestEntity } from '../domain';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
quest?: Quest;
|
|
|
|
area?: Area;
|
|
|
|
model?: Object3D;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Area3DComponent extends React.Component<Props> {
|
2019-05-29 20:46:29 +08:00
|
|
|
private renderer: Renderer;
|
2019-05-29 00:40:29 +08:00
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
2019-05-29 20:46:29 +08:00
|
|
|
// renderer has to be assigned here so that it happens after onSelect is assigned.
|
|
|
|
this.renderer = new Renderer({
|
|
|
|
onSelect: this.onSelect
|
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 onSelect = (entity?: VisibleQuestEntity) => {
|
|
|
|
entitySelected(entity);
|
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
|
|
|
}
|
|
|
|
}
|